-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(adapters): Drizzle adapter #7165
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Ignored Deployment
|
@anthonyshew is attempting to deploy a commit to the authjs Team on Vercel. A member of the Team first needs to authorize it. |
🚨 Potential security issues detected. Learn more about Socket for GitHub ↗︎ To accept the risk, merge this PR and you will not be notified again. Next stepsWhat is a potential typo squat?Package name is similar to other popular packages and may not be the package you want. Use care when consuming similarly named packages and ensure that you did not intend to consume a different package. Malicious packages often publish using similar names as existing popular packages. What's wrong with native code?Contains native code which could be a vector to obscure malicious code, and generally decrease the likelihood of reproducible or reliable installs. Ensure that native code bindings are expected. Consumers may consider pure JS and functionally similar alternatives to avoid the challenges and risks associated with native code bindings. What is an install script?Install scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts. Packages should not be running non-essential scripts during install and there are often solutions to problems people solve with install scripts that can be run at publish time instead. What is protestware and troll packages?This package is a joke, parody, or includes undocumented or hidden behavior unrelated to its primary function. Consider that consuming this package my come along with functionality unrelated to its primary purpose. What is telemetry?This package contains telemetry which tracks you. Most telemetry comes with settings to disable it. Consider disabling telemetry if you do not want to be tracked. Take a deeper look at the dependencyTake a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at support [AT] socket [DOT] dev. Remove the packageIf you happen to install a dependency that Socket reports as Known Malware you should immediately remove it and select a different dependency. For other alert types, you may may wish to investigate alternative packages or consider if there are other ways to mitigate the specific risk posed by the dependency. Mark a package as acceptable riskTo ignore an alert, reply with a comment starting with
|
Add fixtures to workaround millisecond precision.
Lint and format.
Okay, folks, I think(?) we've got it! I've written up tests and manually tested all three SQL flavors with Docker. One thing of note: I would recommend marking this adapter as experimental.
|
token_type: varchar("token_type", { length: 255 }), | ||
scope: varchar("scope", { length: 255 }), | ||
id_token: varchar("id_token", { length: 255 }), | ||
session_state: varchar("session_state", { length: 255 }), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied this code in my project because I wanted to start using Planetscale + Next-Auth + Drizzel. So thank you!.
Is this mysql adapter the one we should use if we want to use Planetscale? If yes, in my case I had to use for id_token
and session_state
a text
field not a varchar
because I got an error when using Google Auth.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I didn't see your previous comment here #7165 (comment)
Ignore this. I guess I'll use my custom adapter for planet scale for now. Thanks for the work!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind sharing your adapter? I'm also using Planetscale so this bugs me as well ... 😕
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @andresgutgon! It would be nice, maybe you have a public repo in which we can read your twist of the adapter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied this code in my project because I wanted to start using Planetscale + Next-Auth + Drizzel. So thank you!.
Is this mysql adapter the one we should use if we want to use Planetscale? If yes, in my case I had to use for
id_token
andsession_state
atext
field not avarchar
because I got an error when using Google Auth.
Sharing is caring
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't even know what's that lol. So I guess no xD. What you see in the gist is what I did. Nothing else related with Drizzle + NextAuth + Planetscale
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andresgutgon prepared queries with your build are not erroring out locally. Pushing to Versel now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain here quickly what's a prepared query and what's the benefit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A prepared statement is kinda what the name implies. Instead of generate each sql query binary on request which is the normal procedure usually when using ORMs, Drizzle can make prepared statements, where the SQL binary is made on build, and when requested by code it it can use it directly which improves performance a lot because it can reuse the binary compilation. It can also take in params if doing joins and what not, which still is much faster. See here for more https://orm.drizzle.team/docs/performance#
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so Prepared statement
When you run query to the database there're several things that happens:
- all the configurations of the query builder got concatenated to the SQL string
- that string and params are sent to the database driver
- driver compiles SQL query to the binary SQL executable format and sends it to the database
- With prepared statements you do SQL concatenation once on the Drizzle ORM side and then database driver is able to reuse precompiled binary SQL instead of parsing query all the time. It has extreme performance benefits on large SQL queries.
https://orm.drizzle.team/docs/performance
can also watch the first half of this video
https://www.youtube.com/watch?v=O9zxsSl9zgE&t=430s
.returning() | ||
.then((res) => res[0]) | ||
}, | ||
linkAccount: async (rawAccount) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you get the following error with Github provider:
"message": "Cannot read properties of undefined (reading 'name')",
"stack": "TypeError: Cannot read properties of undefined (reading 'name')\n
It's because GitHub adds an extra property refresh_token_expires_in
:
{
provider: 'github',
type: 'oauth',
providerAccountId: '',
access_token: '',
expires_at: 1690073663,
refresh_token: '',
refresh_token_expires_in: 15897600, // <-----
token_type: 'bearer',
scope: '',
userId: '53c777ed-353a-438b-a57e-wqweqwe'
}
I think maybe it would be best (for compatibility) to add this column to the provider? Or I guess maybe point that out in the docs so people can add that column to the schema?
Also, it may be better to use bigInt (with mode number
) instead of integer for the expires column(s) for flexibility. https://orm.drizzle.team/docs/column-types/pg#bigint
Is there a reason to why this adapter is not using prepared statements? Hence prepared statements has a much quicker build time that standard typings, and that these auth request would be called millions of millions of time, prepared statements should be really beneficial? Update 30/7: After creating my own adapter and schema, I don’t find it possible to create a adapter that utilize prepared statements out-of-the-box, unless this adapter takes in a list of optional arguments that contains the prepared statements with the queries. |
Yes please, I shared mine here but not with prepare statements. I'm curious |
Currently flying home from vacation, but I'll make a test-next repo tonight and post it here, so all of it can be seen 🫡 |
As promised https://github.com/mschieller/drizzle-nextauth-planetscale be aware that typing was a bit tricky so the adapter is not typesafe yet |
Hey, thanks for that :) would it also work for database session strategy? |
Pretty confident it does yes, haven't tried though. Please give feedback if/when you have tried 😀 |
Unfortunately getting some errors... btw I'm using Google adapter
|
I got the same errors my self, forgot to redo a change I had made as a test prior. Check the new update I have made to the auth schema :-) |
This comment was marked as off-topic.
This comment was marked as off-topic.
🎉 Hi folks, this is now released under Docs: https://authjs.dev/reference/adapter/drizzle Note, it should work with both NextAuth.js and Auth.js libs. Thanks for everyone chipping in and your patience! Let us know! |
☕️ Reasoning
Introducing a
drizzle-orm
adapter!With
drizzle
's increasing popularity, it's no surprise that there's a discussion for an adapter. This PR adds it.🧢 Checklist
📌 Resources
Notes
instanceof
checks to allow for multiple drizzle-orm instances to be used drizzle-team/drizzle-orm#283runBasicTests
utility to be able to provide some custom fixtures to workaround this. [mysql] Stop truncating timestamp milliseconds drizzle-team/drizzle-orm#668