Skip to content

Commit

Permalink
feat: add options object where you can specify a schema
Browse files Browse the repository at this point in the history
feat: configure a schema for the tables

add docs
  • Loading branch information
jlvdh committed Aug 9, 2024
1 parent 064ee18 commit 8e13850
Show file tree
Hide file tree
Showing 3 changed files with 223 additions and 146 deletions.
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

0 comments on commit 8e13850

Please sign in to comment.