next-auth
next-auth copied to clipboard
adapter: add kysely adapter
Description 📓
Hello,
Kysely is a new minimal ORM that I have been playing around with it and really like it.
Was just wondering, would you like to have an adapter for it? It hasn’t released a 1.0 yet though so maybe it is too early?
Happy to start working on this but wanted to check in first and see what you think.
How to reproduce ☕️
Right now you have to right your own custom adapter as documented here
Contributing 🙌🏽
Yes, I am willing to help implement this feature in a PR
We welcome any adapter! Feel free to work on it :wink: And let us know if you are stuck.
It looks like this issue did not receive any activity for 60 days. It will be closed in 7 days if no further activity occurs. If you think your issue is still relevant, commenting will keep it open. Thanks!
@juliusmarminge I started working on a Kysely adapter and then found this post. Were you working on one too or holding off?
@juliusmarminge I started working on a Kysely adapter and then found this post. Were you working on one too or holding off?
I started a while ago but didn't come very far due to time.
I have a working (but untested) version that implements everything. https://gist.github.com/lawrencecchen/67c55529c1a832a787bc3758b2d1f8b6
Would love to to this repo once I figure out how to add tests.
I'm currently using prisma for the database schema. Currently, the adapter's table names aren't configurable, and I allow a higher order function to generate ids.
Any thoughts?
Hey @lawrencecchen, nice work.
I have a branch with a pretty similar implementation + tests and docs: https://github.com/nextauthjs/next-auth/compare/main...mwojtul:next-auth:kysely-adapter
Almost ready to PR, just need to do some cleanup and add finishing touches. If you see anything missing I'd be happy to add it.
@mwojtul yours looks great, will definitely switch to it! Some questions:
-
is it necessary to use pgcrypto? afaik, gen_random_uuid() is built in since postgres 14, and next-auth doesn't do passwords/hashing https://github.com/nextauthjs/next-auth/compare/main...mwojtul:next-auth:kysely-adapter#diff-6a28b4be4cb27449fadf3d8e9ced38b8ac3bf853965550c072a732a6dae2898eR114
-
do you think we could allow table names to be configurable (eg snake case)? this will affect the migrations and table typings as well
@lawrencecchen thanks!
- is it necessary to use pgcrypto? afaik, gen_random_uuid() is built in since postgres 14, and next-auth doesn't do passwords/hashing
It does seem like it might be built in, so I'll go ahead and remove.
- do you think we could allow table names to be configurable (eg snake case)? this will affect the migrations and table typings as well
Would Kysely's CamelCasePlugin be able to handle this, or do we still need something more configurable?
In my idea I had type validation for the tables too, so they had to extend the minimal fields defined in https://next-auth.js.org/adapters/models/.
Would Kysely's CamelCasePlugin be able to handle this, or do we still need something more configurable?
I think this is a good idea. One option is to define every table/column in the database using snake case. We shouldn't assume that the kysely instance that the user passes in has the CamelCasePlugin()
, but I think withPlugin()
does the trick.
In my idea I had type validation for the tables too, so they had to extend the minimal fields defined in https://next-auth.js.org/adapters/models/.
We also have to override the snake case fields in the account table. Something like this should work, but I haven't tested it yet:
import { CamelCasePlugin } from "kysely";
const snakeCaseSet = new Set([
"refresh_token",
"access_token",
"expires_at",
"token_type",
"id_token",
"session_state",
"oauth_token_secret",
"oauth_token",
]);
export class NextAuthCamelCasePlugin extends CamelCasePlugin {
protected override snakeCase(str: string): string {
if (snakeCaseSet.has(str)) {
return str;
}
return super.snakeCase(str);
}
protected override camelCase(str: string): string {
if (snakeCaseSet.has(str)) {
return str;
}
return super.camelCase(str);
}
}
In my idea I had type validation for the tables too, so they had to extend the minimal fields defined in https://next-auth.js.org/adapters/models/.
Good idea. I've added the same type validation you were working on, an AuthedKysely
wrapper around the Kysely constructor.
We also have to override the snake case fields in the account table. Something like this should work, but I haven't tested it yet:
Is overriding the right move? Doing so might make the behavior for the end user a little weird: almost all fields will be camelCased except for these overrides. Another option could be doing it in the adapter methods themselves. That way NextAuth would still be happy and everything would be camelCased when the user interacts with the Kysely instance.
Edit: looking at how the Prisma and TypeORM adapters handle this (they don't avoid their client remapping these fields) makes me think we may not actually need to do anything. CamelCasePlugin
would just work out of the box as it seems NextAuth doesn't rely on the column names.
Hyped for this :D
Is it still in review?
any updates on this?
Is anyone working on this? I built one for myself and I'd happy to contribute.
Is anyone working on this? I built one for myself and I'd happy to contribute.
#5464 has working implementation + documentation. Just needs a review from maintainer
@balazsorban44 please could you review this?
This package should now also work with Kysely for mysql and postgres. It uses the sql template tag. Maybe this is helpful until there is an official package.
what is the status? What is the current best solution to use next-auth with kysely?
what is the status? What is the current best solution to use next-auth with kysely?
Copy the adapter in the open PR and use in your project. I've been using it in production for a while and its working flawlessly
Copy the adapter in the open PR and use in your project. I've been using it in production for a while and its working flawlessly
are you using prisma-kysely
by any chance @juliusmarminge?
I am getting some type errors and was wondering if you could share your prisma schema with me? Would be hugely appreciated :)
I am getting some type errors and was wondering if you could share your prisma schema with me? Would be hugely appreciated :)
https://github.com/juliusmarminge/acme-corp/tree/main/packages/db
Copy the adapter in the open PR and use in your project. I've been using it in production for a while and its working flawlessly
are you using
prisma-kysely
by any chance @juliusmarminge?I am getting some type errors and was wondering if you could share your prisma schema with me? Would be hugely appreciated :)
Hey @pingustar, did you manage to get it working? I'm also struggling to use it with prisma-kysely
Copy the adapter in the open PR and use in your project. I've been using it in production for a while and its working flawlessly
are you using
prisma-kysely
by any chance @juliusmarminge? I am getting some type errors and was wondering if you could share your prisma schema with me? Would be hugely appreciated :)Hey @pingustar, did you manage to get it working? I'm also struggling to use it with prisma-kysely
@zivtamary not yet, ended up using prisma - let me know if you figure it out :)
Copy the adapter in the open PR and use in your project. I've been using it in production for a while and its working flawlessly
are you using
prisma-kysely
by any chance @juliusmarminge? I am getting some type errors and was wondering if you could share your prisma schema with me? Would be hugely appreciated :)Hey @pingustar, did you manage to get it working? I'm also struggling to use it with prisma-kysely
@zivtamary not yet, ended up using prisma - let me know if you figure it out :)
I fixed some typing errors and the rest ignored with // @ts-expect-error The adapter works flawlessly though, so I don't mind :)
Copy the adapter in the open PR and use in your project. I've been using it in production for a while and its working flawlessly
are you using
prisma-kysely
by any chance @juliusmarminge? I am getting some type errors and was wondering if you could share your prisma schema with me? Would be hugely appreciated :)Hey @pingustar, did you manage to get it working? I'm also struggling to use it with prisma-kysely
@zivtamary not yet, ended up using prisma - let me know if you figure it out :)
I fixed some typing errors and the rest ignored with // @ts-expect-error The adapter works flawlessly though, so I don't mind :)
I saw that they have added an official adapter. I'll give this a shot over the weekend
https://authjs.dev/reference/adapter/kysely