Skip to content
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

fix(drizzle): reimplement Drizzle adapter #10479

Merged
merged 28 commits into from Apr 13, 2024
Merged

Conversation

realmikesolo
Copy link
Contributor

@realmikesolo realmikesolo commented Apr 4, 2024

Reasoning

The previous Drizzle adapter for NextAuth was implemented incorrectly. I have rewritten it from the ground up to fix the issues and align it with Drizzle's best practices. The adapter passed all tests. Documentation was updated according to new functionality.

🧢 Checklist

  • Documentation
  • Tests
  • Ready to be merged

Copy link

vercel bot commented Apr 4, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
auth-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 13, 2024 1:14pm
1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
next-auth-docs ⬜️ Ignored (Inspect) Visit Preview Apr 13, 2024 1:14pm

Copy link

vercel bot commented Apr 4, 2024

@realmikesolo is attempting to deploy a commit to the authjs Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions bot added adapters Changes related to the core code concerning database adapters drizzle @auth/drizzle-adapter labels Apr 4, 2024
@realmikesolo realmikesolo marked this pull request as draft April 4, 2024 10:53
Copy link
Member

@ndom91 ndom91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey so in general I appreciate the effort!

But changing the name of the tables is a bugger breaking change.. we'd have to bump the major version and ideally provide some sort of migration / codemod to automate as much as possible.

@realmikesolo
Copy link
Contributor Author

Hey so in general I appreciate the effort!

But changing the name of the tables is a bugger breaking change.. we'd have to bump the major version and ideally provide some sort of migration / codemod to automate as much as possible.

Got it. I will revert to the previous table names

packages/adapter-drizzle/src/lib/mysql.ts Outdated Show resolved Hide resolved
packages/adapter-drizzle/src/lib/mysql.ts Outdated Show resolved Hide resolved
"mysql2": "^3.2.0",
"postgres": "^3.3.4",
"tsx": "^4.7.0"
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix \n

Copy link

socket-security bot commented Apr 5, 2024

👍 Dependency issues cleared. Learn more about Socket for GitHub ↗︎

This PR previously contained dependency changes with security issues that have been resolved, removed, or ignored.

View full report↗︎

@AlexBlokh
Copy link

@ndom91 we're ready for review and to be merged!

@realmikesolo realmikesolo marked this pull request as ready for review April 5, 2024 16:22
@ThangHuuVu
Copy link
Member

wow, thanks a lot for the contribution @realmikesolo @AlexBlokh , we'll look into it! cc @ndom91

@@ -19,7 +19,8 @@
"exports": {
".": {
"types": "./index.d.ts",
"import": "./index.js"
"import": "./index.js",
"require": "./index.js"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can drop this require conditional exports, as we export exclusively for use with ESM and have no intention of supporting CommonJS consumers. cc @balazsorban44

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, all Auth.js packages are ESM-only

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The require line was added to ensure that the default tables imported from the adapter-drizzle package can be used with the drizzle-kit commands, such as generate and push. These commands are used for applying changes to the database. Without it, users will encounter a no exports main defined error when attempting to use drizzle-kit commands.

packages/adapter-drizzle/src/index.ts Outdated Show resolved Hide resolved
packages/adapter-drizzle/src/index.ts Outdated Show resolved Hide resolved
We would like to have feature parity of Auth.js when hosted either on something like Vercel or in Docker. Moving the docker config to the same example app we host on Vercel and deploying it via Docker will make this easier to verify
@themixednuts
Copy link

Thank you for this PR, I was literally just looking into this myself to do the same thing and wondered why there was hard-coded tables without any schema passthrough. Awesome work!

@ndom91
Copy link
Member

ndom91 commented Apr 12, 2024

@realmikesolo could you take a look at the pnpm-lock merge conflict 🙏

@AlexBlokh
Copy link

seems like we're good

@ndom91
Copy link
Member

ndom91 commented Apr 13, 2024

Merging shortly 🙏

@ndom91 ndom91 merged commit a816f15 into nextauthjs:main Apr 13, 2024
10 of 11 checks passed
@ndom91 ndom91 mentioned this pull request Apr 13, 2024
3 tasks
Comment on lines +103 to +104
async createUser(data: Omit<AdapterUser, "id">) {
const id = randomUUID()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we change this so we can leverage drizzle's .defaultFn() here and let users have control of the id generation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you have for the sessions table so don't see why users table should be any different

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @juliusmarminge. As MySQL doesn't have returning clause, generated id was used to find the row after insertion. But you are right, we can find inserted row by email for user and by sessionToken for session and remove redundant id generation in the code. Thank you for these suggestions!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@realmikesolo will you remove these redundant ID's in your follow up PR as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep @ndom91

Comment on lines +269 to +277
* id: text("id").primaryKey().$defaultFn(() => randomUUID())
* sessionToken: text("sessionToken").notNull().unique(),
* userId: text("userId")
* .notNull()
* .references(() => users.id, { onDelete: "cascade" }),
* expires: integer("expires", { mode: "timestamp_ms" }).notNull(),
* })
* }, (table) => ({
* userIdIdx: index('Session_userId_index').on(table.userId)
* }))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is also a breaking change for the underlying database - why was this made?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with you. It was a mistake to add indexes for foreign keys. I will remove them in new PR

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@realmikesolo thanks for stepping up to remove those, feel free to tag me in the PR so we can get it merged quickly 🙏

@lucasfeliciano
Copy link

lucasfeliciano commented Apr 20, 2024

Amazing! Great direction this is going! :)

Would be possible to make the column names also customizable?
Currently the type is literal.

image

Thinking the same thing about the field type.
For fields that are not coupled with the implementation might be nice to be able to specify the type.

@ndom91
Copy link
Member

ndom91 commented Apr 21, 2024

@realmikesolo thanks for commenting and offering to fix a few of these issues, theres some discussion on this adapter / these issues in the discord as well if you'd like to join:

Also we merged the new docs while this PR was being worked on, the docs are now at /docs/pages/getting-started/adapters/drizzle.mdx 🙏

@realmikesolo
Copy link
Contributor Author

Thank you @ndom91!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adapters Changes related to the core code concerning database adapters drizzle @auth/drizzle-adapter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants