Using Kysely with better auth
The docs seem to indicate that better auth ships with Kysely, but I can't figure out how to use it.
I can install and set up Kysely from scratch probably, but it seems like Kysely should somehow already be usable and configured. How would I use it? I need to query for my organizations.
6 Replies
you should check kysley docs on how to use kysley. It's just Better Auth internally by default uses kysley to query your db. It's not exposed through us on how to use it.
thanks, I'll set it up manually or maybe just go for node-postgres
The docs dosen't include any info on how to use Kyseley.
It seems like I should generate a sql file
npx @better-auth/cli generate
But what is this file called, where is it and how do I tie it to Kysley?
I cna create a new Kysley instance like this it seems
database: new Kysely({ dialect }),
but I'd still somehow need to pass the types/schema to it, which I can generate but not find.Other Relational Databases | Better Auth
Integrate Better Auth with other relational databases.
The docs wont for better-auth won't show any info on how to use kysley but they do tell you if you want more info visit the kysley docs. you would have to learn how to use kysley on the kysley docs. kysley docs can be found here
As bekacru said previously keysly is how better auth can query you database meaning that you can write you own kysley dialect and use it with better auth and it will work. Means its amazing for making your own adapter or using a community made adapter for kysley can work with better-auth.
The link I sent should contain some example community adapters
using kyseley seems OK enough, I managed to set it up and get it working with the DB in a project that dosen't use better auth
My issue is getting the generated types from better auth. The docs seem to not mention anywhere where the better auth types are generated, I need to pass these to keysley.
Or am I supposed to generate the DB types using keysley without better auth?
The docs show me how to use adapters for prisma, I could change to prisma since it is at least clear how to use it. but I prefer Kyseley as I've used it before and like it.
Docs say
"The generate command creates the schema required by Better Auth. If you're using a database adapter like Prisma or Drizzle, this command will generate the right schema for your ORM. If you're using the built-in Kysely adapter, it will generate an SQL file you can run directly on your database."
But I can't find this file, only a migrations file
I tried something like this with a kysley.js
And calling my DB like this in auth.js
but using this method better auth fails to connect to my DB, if I do the connection string directly it works
After a delete of node modules this started working. Can't get types for Pool from pg to work, but at least it works 🙂
If you do have kysley questions that are related better auth bekacru will probably know more about the actual implementation