@hammadj/better-auth-kysely-adapter 1.5.0-beta.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/dialect.ts ADDED
@@ -0,0 +1,148 @@
1
+ import type { BetterAuthOptions } from "@better-auth/core";
2
+ import type { Dialect } from "kysely";
3
+ import {
4
+ Kysely,
5
+ MssqlDialect,
6
+ MysqlDialect,
7
+ PostgresDialect,
8
+ SqliteDialect,
9
+ } from "kysely";
10
+ import type { KyselyDatabaseType } from "./types";
11
+
12
+ export function getKyselyDatabaseType(
13
+ db: BetterAuthOptions["database"],
14
+ ): KyselyDatabaseType | null {
15
+ if (!db) {
16
+ return null;
17
+ }
18
+ if ("dialect" in db) {
19
+ return getKyselyDatabaseType(db.dialect as Dialect);
20
+ }
21
+ if ("createDriver" in db) {
22
+ if (db instanceof SqliteDialect) {
23
+ return "sqlite";
24
+ }
25
+ if (db instanceof MysqlDialect) {
26
+ return "mysql";
27
+ }
28
+ if (db instanceof PostgresDialect) {
29
+ return "postgres";
30
+ }
31
+ if (db instanceof MssqlDialect) {
32
+ return "mssql";
33
+ }
34
+ }
35
+ if ("aggregate" in db) {
36
+ return "sqlite";
37
+ }
38
+
39
+ if ("getConnection" in db) {
40
+ return "mysql";
41
+ }
42
+ if ("connect" in db) {
43
+ return "postgres";
44
+ }
45
+ if ("fileControl" in db) {
46
+ return "sqlite";
47
+ }
48
+ if ("open" in db && "close" in db && "prepare" in db) {
49
+ return "sqlite";
50
+ }
51
+ return null;
52
+ }
53
+
54
+ export const createKyselyAdapter = async (config: BetterAuthOptions) => {
55
+ const db = config.database;
56
+
57
+ if (!db) {
58
+ return {
59
+ kysely: null,
60
+ databaseType: null,
61
+ transaction: undefined,
62
+ };
63
+ }
64
+
65
+ if ("db" in db) {
66
+ return {
67
+ kysely: db.db,
68
+ databaseType: db.type,
69
+ transaction: db.transaction,
70
+ };
71
+ }
72
+
73
+ if ("dialect" in db) {
74
+ return {
75
+ kysely: new Kysely<any>({ dialect: db.dialect }),
76
+ databaseType: db.type,
77
+ transaction: db.transaction,
78
+ };
79
+ }
80
+
81
+ let dialect: Dialect | undefined = undefined;
82
+
83
+ const databaseType = getKyselyDatabaseType(db);
84
+
85
+ if ("createDriver" in db) {
86
+ dialect = db;
87
+ }
88
+
89
+ if ("aggregate" in db && !("createSession" in db)) {
90
+ dialect = new SqliteDialect({
91
+ database: db,
92
+ });
93
+ }
94
+
95
+ if ("getConnection" in db) {
96
+ // @ts-expect-error - mysql2/promise
97
+ dialect = new MysqlDialect(db);
98
+ }
99
+
100
+ if ("connect" in db) {
101
+ dialect = new PostgresDialect({
102
+ pool: db,
103
+ });
104
+ }
105
+
106
+ if ("fileControl" in db) {
107
+ const { BunSqliteDialect } = await import("./bun-sqlite-dialect");
108
+ dialect = new BunSqliteDialect({
109
+ database: db,
110
+ });
111
+ }
112
+
113
+ if ("createSession" in db) {
114
+ let DatabaseSync: typeof import("node:sqlite").DatabaseSync | undefined =
115
+ undefined;
116
+ try {
117
+ const nodeSqlite: string = "node:sqlite";
118
+ // Ignore both Vite and Webpack for dynamic import as they both try to pre-bundle 'node:sqlite' which might fail
119
+ // It's okay because we are in a try-catch block
120
+ ({ DatabaseSync } = await import(
121
+ /* @vite-ignore */
122
+ /* webpackIgnore: true */
123
+ nodeSqlite
124
+ ));
125
+ } catch (error: unknown) {
126
+ if (
127
+ error !== null &&
128
+ typeof error === "object" &&
129
+ "code" in error &&
130
+ error.code !== "ERR_UNKNOWN_BUILTIN_MODULE"
131
+ ) {
132
+ throw error;
133
+ }
134
+ }
135
+ if (DatabaseSync && db instanceof DatabaseSync) {
136
+ const { NodeSqliteDialect } = await import("./node-sqlite-dialect");
137
+ dialect = new NodeSqliteDialect({
138
+ database: db,
139
+ });
140
+ }
141
+ }
142
+
143
+ return {
144
+ kysely: dialect ? new Kysely<any>({ dialect }) : null,
145
+ databaseType,
146
+ transaction: undefined,
147
+ };
148
+ };
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ export * from "./dialect";
2
+ export * from "./kysely-adapter";
3
+ export * from "./types";
4
+ // Don't export node:sqlite by default, as it is not production ready.
5
+ // export * from "./node-sqlite";
@@ -0,0 +1,24 @@
1
+ import { Kysely, SqliteDialect } from "kysely";
2
+ import { describe, expect, it } from "vitest";
3
+ import { kyselyAdapter } from "./kysely-adapter";
4
+
5
+ describe("kysely-adapter", () => {
6
+ it("should create kysely adapter", () => {
7
+ const db = new Kysely({
8
+ dialect: new SqliteDialect({
9
+ database: {
10
+ close: () => {},
11
+ prepare: () =>
12
+ ({
13
+ all: () => [],
14
+ run: () => {},
15
+ get: () => {},
16
+ iterate: () => [],
17
+ }) as any,
18
+ } as any,
19
+ }),
20
+ });
21
+ const adapter = kyselyAdapter(db);
22
+ expect(adapter).toBeDefined();
23
+ });
24
+ });