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

feat(drizzle-adapter): custom database's tables naming strategy #8344

Closed
wants to merge 2 commits into from
Closed

Conversation

skyf0l
Copy link
Contributor

@skyf0l skyf0l commented Aug 17, 2023

NOTE:

  • It's a good idea to open an issue first to discuss potential changes.
  • Please make sure that you are NOT opening a PR to fix a potential security vulnerability. Instead, please follow the Security guidelines to disclose the issue to us confidentially.

☕️ Reasoning

I'm migrating my database from Prisma to Drizzle, but I got a problem using drizzle adapter cause table names are different (camelCase vs PascalCase). So, I added an option to force the naming strategy 🚀

🧢 Checklist

  • Documentation
  • Tests
  • Ready to be merged

🎫 Affected issues

Please scout and link issues that might be solved by this PR.

Fixes: INSERT_ISSUE_LINK_HERE

📌 Resources

@skyf0l skyf0l requested a review from ndom91 as a code owner August 17, 2023 12:09
@vercel
Copy link

vercel bot commented Aug 17, 2023

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 Jan 12, 2024 10:13am
1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
next-auth-docs ⬜️ Ignored (Inspect) Visit Preview Jan 12, 2024 10:13am

@vercel
Copy link

vercel bot commented Aug 17, 2023

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

A member of the Team first needs to authorize it.

@skyf0l
Copy link
Contributor Author

skyf0l commented Sep 4, 2023

Up?

@GLips
Copy link

GLips commented Sep 6, 2023

I'm in a similar boat, migrating from Prisma to Drizzle and about to copy my own DrizzleAdapter out to make these modifications.

@nhedger
Copy link

nhedger commented Oct 15, 2023

Hey @skyf0l, I'm wondering whether giving the option to use either one of PascalCase, camelCase or snake_case would be a better idea. I'd be willing to help implement this. What is your take on this?

@skyf0l
Copy link
Contributor Author

skyf0l commented Oct 15, 2023

Hey @skyf0l, I'm wondering whether giving the option to use either one of PascalCase, camelCase or snake_case would be a better idea. I'd be willing to help implement this. What is your take on this?

Hey @nhedger, first it was to keep consistency with the Prima adapter, then to avoid renaming tables in my production database just because of an ORM switch in my main application.

As the Drizzle adapter is new, I think it's a good idea to start keeping consistency between them all.

Finally, even though some database table names are case insensitive, not all are (e.g. PlanetScale).

In any case, the main goal is consistency :)

(ATM, we use a fork of the Drizzle adapter to avoid renaming our tables.)

@nhedger
Copy link

nhedger commented Oct 15, 2023

I understand that; my reasoning was that some users may prefer to use a different case. For example, all my tables use snake_case, so for the sake of consistency, I'd like these tables to use snake_case too, hence my proposition to make it configurable.

If you personally don't need this, I'd fully understand if you did not want to invest time in it and can submit a PR of my own.

@skyf0l
Copy link
Contributor Author

skyf0l commented Oct 15, 2023

Oh, I read it sideways, yes indeed your suggestion is definitely the best!
I should have 5 minutes in the week to update this quickly!

@nhedger
Copy link

nhedger commented Oct 15, 2023

Glad to hear; let me know if you need any assistance.

@felixnorden
Copy link

+1 on what you both have said @nhedger @skyf0l , we have a project where the tables are in camelCase and so are the fields in JS-land, but in SQL they are snake_case, so starting to work towards allowing the user to configure the naming scheme (as the docs state other adapters do) would be grand.
Let me know if you need any help as well!

@skyf0l
Copy link
Contributor Author

skyf0l commented Jan 9, 2024

I've synchronized my fork with the upstream 🚀

Copy link

@ormarek ormarek left a comment

Choose a reason for hiding this comment

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

Im not in actual power here to accept or reject those changes you've made, but wanted to make a small suggestion

You could create a small naming table like:

const names = {
  snake_case: {
    userId: "user_id",
    ...
  },
  camelCase: {
    userId: "userId",
    ...
  },
  pascalCase: {
    userId: "UserId",
    ...
  }
}

and then instead of doing tedious trenary everywhere (which is also typo prone) you could use names[namingStrategy].<name> e.g. names[namingStrategy].userId

@ndom91
Copy link
Member

ndom91 commented Jan 12, 2024

In general this looks good to me, tests are still passing too!

@balazsorban44 @ThangHuuVu we had something similar with another db adapter in the past, but I can't remember the details. I think technically this is good to go, yall okay with this generally?

@ndom91
Copy link
Member

ndom91 commented Jan 12, 2024

Btw see this PR that adds support for custom tables and seems to ahve a lot of overlap: #8561

@nahtnam
Copy link

nahtnam commented Jan 21, 2024

I have no authority here but just want to chime in that I prefer the other PR over this one. I want to do snake_case but pluralize and I dont think this PR would support that. I assume future developers might want totally different names or a prefix for the table names. It would be better instead to just allow us to pass the schemas as options

@skyf0l skyf0l closed this by deleting the head repository Apr 26, 2024
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.

8 participants