next-auth icon indicating copy to clipboard operation
next-auth copied to clipboard

adapter: add kysely adapter

Open juliusmarminge opened this issue 2 years ago • 4 comments

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

juliusmarminge avatar Jul 13 '22 12:07 juliusmarminge

We welcome any adapter! Feel free to work on it :wink: And let us know if you are stuck.

balazsorban44 avatar Jul 13 '22 12:07 balazsorban44

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!

stale[bot] avatar Sep 16 '22 00:09 stale[bot]

@juliusmarminge I started working on a Kysely adapter and then found this post. Were you working on one too or holding off?

mwojtul avatar Sep 21 '22 08:09 mwojtul

@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.

juliusmarminge avatar Sep 21 '22 08:09 juliusmarminge

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.

lawrencecchen avatar Sep 26 '22 10:09 lawrencecchen

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?

lawrencecchen avatar Sep 26 '22 10:09 lawrencecchen

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 avatar Sep 26 '22 19:09 mwojtul

@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 avatar Sep 26 '22 21:09 lawrencecchen

@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?

mwojtul avatar Sep 27 '22 01:09 mwojtul

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/.

juliusmarminge avatar Sep 27 '22 09:09 juliusmarminge

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);
  }
}

lawrencecchen avatar Sep 28 '22 01:09 lawrencecchen

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.

mwojtul avatar Sep 28 '22 09:09 mwojtul

Hyped for this :D

kharann avatar Nov 10 '22 01:11 kharann

Is it still in review?

Pelps12 avatar Dec 12 '22 06:12 Pelps12

any updates on this?

mattclosson avatar Feb 15 '23 18:02 mattclosson

Is anyone working on this? I built one for myself and I'd happy to contribute.

martinsione avatar Mar 27 '23 12:03 martinsione

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

juliusmarminge avatar Mar 27 '23 14:03 juliusmarminge

@balazsorban44 please could you review this?

kaimacmaster avatar Apr 22 '23 02:04 kaimacmaster

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.

roelandxyz avatar May 14 '23 19:05 roelandxyz

what is the status? What is the current best solution to use next-auth with kysely?

pingustar avatar Jun 29 '23 14:06 pingustar

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

juliusmarminge avatar Jun 29 '23 14:06 juliusmarminge

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 :)

pingustar avatar Jul 03 '23 19:07 pingustar

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

juliusmarminge avatar Jul 03 '23 19:07 juliusmarminge

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 avatar Aug 02 '23 11:08 zivtamary

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 :)

pingustar avatar Aug 08 '23 07:08 pingustar

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 :)

zivtamary avatar Aug 08 '23 13:08 zivtamary

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

pingustar avatar Aug 11 '23 17:08 pingustar