BA
Better Auth•5d ago
Vimes

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
bekacru
bekacru•5d ago
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.
Vimes
VimesOP•5d ago
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.
lonelyplanet
lonelyplanet•4d ago
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
Vimes
VimesOP•4d ago
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
import type { DB } from "kysely-codegen";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
import { Pool } from "pg";
import { Kysely, PostgresDialect } from "kysely";

const dialect = new PostgresDialect({
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-call
pool: new Pool({
user: "REDACTED",
password: process.env.PG_PASSWORD,
host: process.env.PG_URL,
port: 5432,
database: "REDACTED",
}),
});


export const db = new Kysely<DB>({
dialect,
});
import type { DB } from "kysely-codegen";
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
import { Pool } from "pg";
import { Kysely, PostgresDialect } from "kysely";

const dialect = new PostgresDialect({
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,@typescript-eslint/no-unsafe-call
pool: new Pool({
user: "REDACTED",
password: process.env.PG_PASSWORD,
host: process.env.PG_URL,
port: 5432,
database: "REDACTED",
}),
});


export const db = new Kysely<DB>({
dialect,
});
And calling my DB like this in auth.js
import { betterAuth } from "better-auth";
import { Pool } from "pg";
import { nextCookies } from "better-auth/next-js";
import { admin, organization } from "better-auth/plugins";
import { adminClient } from "better-auth/client/plugins";
import { db } from "./kysley";

export const auth = betterAuth({

database: {
db: db,
type: "postgres"
},

// database: db,
emailAndPassword: {
enabled: true,
},
emailVerification: {
sendOnSignUp: true,
autoSignInAfterVerification: true,
},

plugins: [organization(), admin(), adminClient(), nextCookies()],
});
import { betterAuth } from "better-auth";
import { Pool } from "pg";
import { nextCookies } from "better-auth/next-js";
import { admin, organization } from "better-auth/plugins";
import { adminClient } from "better-auth/client/plugins";
import { db } from "./kysley";

export const auth = betterAuth({

database: {
db: db,
type: "postgres"
},

// database: db,
emailAndPassword: {
enabled: true,
},
emailVerification: {
sendOnSignUp: true,
autoSignInAfterVerification: true,
},

plugins: [organization(), admin(), adminClient(), nextCookies()],
});
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 🙂
lonelyplanet
lonelyplanet•4d ago
If you do have kysley questions that are related better auth bekacru will probably know more about the actual implementation

Did you find this page helpful?