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: add options object where you can specify a schema #11344

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/pages/getting-started/adapters/kysely.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ export async function down(db: Kysely<any>): Promise<void> {

For more information about creating and running migrations with Kysely, refer to the [Kysely migrations documentation](https://kysely.dev/docs/migrations).

If you'd prefer organising your tables under a separate schema you can initialise the Kysely adapter as follows:

```
KyselyAdapter(db, { schemaName: "auth" })
```

### Naming conventions

If mixed snake_case and camelCase column names is an issue for you and/or your underlying database system, we recommend using Kysely's `CamelCasePlugin` ([see the documentation here](https://kysely-org.github.io/kysely-apidoc/classes/CamelCasePlugin.html)) feature to change the field names. This won't affect NextAuth.js, but will allow you to have consistent casing when using Kysely.
22 changes: 19 additions & 3 deletions packages/adapter-kysely/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ export interface Database {
VerificationToken: VerificationToken
}

interface Options {
schemaName?: string
}

export const format = {
from<T>(object?: Record<string, any>): T {
const newObject: Record<string, unknown> = {}
Expand All @@ -51,14 +55,26 @@ export const format = {
},
}

export function KyselyAdapter(db: Kysely<Database>): Adapter {
const { adapter } = db.getExecutor()
const { supportsReturning } = adapter
export function KyselyAdapter(
kyselyDb: Kysely<Database>,
options?: Options
): Adapter {
const { adapter } = kyselyDb.getExecutor()
const isSqlite = adapter instanceof SqliteAdapter
if (options?.schemaName && isSqlite) {
console.warn(
'SQLite does not support schemas. Ignoring "schemaName" option.'
)
}
const { supportsReturning } = adapter
/** If the database is SQLite, turn dates into an ISO string */
const to = isSqlite ? format.to : <T>(x: T) => x as T
/** If the database is SQLite, turn ISO strings into dates */
const from = isSqlite ? format.from : <T>(x: T) => x as T
const db =
options?.schemaName && !isSqlite
? kyselyDb.withSchema(options.schemaName)
: kyselyDb
return {
async createUser(data) {
const user = { ...data, id: crypto.randomUUID() }
Expand Down
Loading
Loading