@hammadj/better-auth-drizzle-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/.turbo/turbo-build.log +41 -0
- package/LICENSE.md +20 -0
- package/dist/index.d.mts +48 -0
- package/dist/index.mjs +286 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +39 -0
- package/src/drizzle-adapter.test.ts +17 -0
- package/src/drizzle-adapter.ts +606 -0
- package/src/index.ts +1 -0
- package/tsconfig.json +9 -0
- package/tsdown.config.ts +8 -0
- package/vitest.config.ts +3 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
|
|
2
|
+
> @hammadj/better-auth-drizzle-adapter@1.5.0-beta.9 build /Users/hammadjutt/Dev/better-auth-fork/packages/drizzle-adapter
|
|
3
|
+
> tsdown
|
|
4
|
+
|
|
5
|
+
[34mβΉ[39m tsdown [2mv0.20.1[22m powered by rolldown [2mv1.0.0-rc.1[22m
|
|
6
|
+
[34mβΉ[39m config file: [4m/Users/hammadjutt/Dev/better-auth-fork/packages/drizzle-adapter/tsdown.config.ts[24m
|
|
7
|
+
[34mβΉ[39m entry: [34msrc/index.ts[39m
|
|
8
|
+
[34mβΉ[39m tsconfig: [34mtsconfig.json[39m
|
|
9
|
+
[34mβΉ[39m Build start
|
|
10
|
+
[34mβΉ[39m Cleaning 2 files
|
|
11
|
+
[34mβΉ[39m [2mdist/[22m[1mindex.mjs[22m [2m13.81 kB[22m [2mβ gzip: 2.87 kB[22m
|
|
12
|
+
[34mβΉ[39m [2mdist/[22mindex.mjs.map [2m31.14 kB[22m [2mβ gzip: 6.19 kB[22m
|
|
13
|
+
[34mβΉ[39m [2mdist/[22m[32m[1mindex.d.mts[22m[39m [2m 1.50 kB[22m [2mβ gzip: 0.67 kB[22m
|
|
14
|
+
[34mβΉ[39m 3 files, total: 46.45 kB
|
|
15
|
+
src/drizzle-adapter.ts (9:37) [33m[UNRESOLVED_IMPORT] Warning:[0m Could not resolve '@better-auth/core/db/adapter' in src/drizzle-adapter.ts
|
|
16
|
+
[38;5;246mβ[0m[38;5;246mβ[0m[38;5;246m[[0m src/drizzle-adapter.ts:9:38 [38;5;246m][0m
|
|
17
|
+
[38;5;246mβ[0m
|
|
18
|
+
[38;5;246m9 β[0m [38;5;249mi[0m[38;5;249mm[0m[38;5;249mp[0m[38;5;249mo[0m[38;5;249mr[0m[38;5;249mt[0m[38;5;249m [0m[38;5;249m{[0m[38;5;249m [0m[38;5;249mc[0m[38;5;249mr[0m[38;5;249me[0m[38;5;249ma[0m[38;5;249mt[0m[38;5;249me[0m[38;5;249mA[0m[38;5;249md[0m[38;5;249ma[0m[38;5;249mp[0m[38;5;249mt[0m[38;5;249me[0m[38;5;249mr[0m[38;5;249mF[0m[38;5;249ma[0m[38;5;249mc[0m[38;5;249mt[0m[38;5;249mo[0m[38;5;249mr[0m[38;5;249my[0m[38;5;249m [0m[38;5;249m}[0m[38;5;249m [0m[38;5;249mf[0m[38;5;249mr[0m[38;5;249mo[0m[38;5;249mm[0m[38;5;249m [0m"@better-auth/core/db/adapter"[38;5;249m;[0m
|
|
19
|
+
[38;5;240m β[0m ββββββββββββββββ¬ββββββββββββββ
|
|
20
|
+
[38;5;240m β[0m β°ββββββββββββββββ Module not found, treating it as an external dependency
|
|
21
|
+
[38;5;246mββββ―[0m
|
|
22
|
+
|
|
23
|
+
src/drizzle-adapter.ts (10:23) [33m[UNRESOLVED_IMPORT] Warning:[0m Could not resolve '@better-auth/core/env' in src/drizzle-adapter.ts
|
|
24
|
+
[38;5;246mβ[0m[38;5;246mβ[0m[38;5;246m[[0m src/drizzle-adapter.ts:10:24 [38;5;246m][0m
|
|
25
|
+
[38;5;246mβ[0m
|
|
26
|
+
[38;5;246m10 β[0m [38;5;249mi[0m[38;5;249mm[0m[38;5;249mp[0m[38;5;249mo[0m[38;5;249mr[0m[38;5;249mt[0m[38;5;249m [0m[38;5;249m{[0m[38;5;249m [0m[38;5;249ml[0m[38;5;249mo[0m[38;5;249mg[0m[38;5;249mg[0m[38;5;249me[0m[38;5;249mr[0m[38;5;249m [0m[38;5;249m}[0m[38;5;249m [0m[38;5;249mf[0m[38;5;249mr[0m[38;5;249mo[0m[38;5;249mm[0m[38;5;249m [0m"@better-auth/core/env"[38;5;249m;[0m
|
|
27
|
+
[38;5;240m β[0m ββββββββββββ¬βββββββββββ
|
|
28
|
+
[38;5;240m β[0m β°βββββββββββββ Module not found, treating it as an external dependency
|
|
29
|
+
[38;5;246mβββββ―[0m
|
|
30
|
+
|
|
31
|
+
src/drizzle-adapter.ts (11:32) [33m[UNRESOLVED_IMPORT] Warning:[0m Could not resolve '@better-auth/core/error' in src/drizzle-adapter.ts
|
|
32
|
+
[38;5;246mβ[0m[38;5;246mβ[0m[38;5;246m[[0m src/drizzle-adapter.ts:11:33 [38;5;246m][0m
|
|
33
|
+
[38;5;246mβ[0m
|
|
34
|
+
[38;5;246m11 β[0m [38;5;249mi[0m[38;5;249mm[0m[38;5;249mp[0m[38;5;249mo[0m[38;5;249mr[0m[38;5;249mt[0m[38;5;249m [0m[38;5;249m{[0m[38;5;249m [0m[38;5;249mB[0m[38;5;249me[0m[38;5;249mt[0m[38;5;249mt[0m[38;5;249me[0m[38;5;249mr[0m[38;5;249mA[0m[38;5;249mu[0m[38;5;249mt[0m[38;5;249mh[0m[38;5;249mE[0m[38;5;249mr[0m[38;5;249mr[0m[38;5;249mo[0m[38;5;249mr[0m[38;5;249m [0m[38;5;249m}[0m[38;5;249m [0m[38;5;249mf[0m[38;5;249mr[0m[38;5;249mo[0m[38;5;249mm[0m[38;5;249m [0m"@better-auth/core/error"[38;5;249m;[0m
|
|
35
|
+
[38;5;240m β[0m βββββββββββββ¬ββββββββββββ
|
|
36
|
+
[38;5;240m β[0m β°ββββββββββββββ Module not found, treating it as an external dependency
|
|
37
|
+
[38;5;246mβββββ―[0m
|
|
38
|
+
|
|
39
|
+
[33m[PLUGIN_TIMINGS] Warning:[0m Your build spent significant time in plugin `rolldown-plugin-dts:generate`. See https://rolldown.rs/options/checks#plugintimings for more details.
|
|
40
|
+
|
|
41
|
+
[32mβ[39m Build complete in [32m5397ms[39m
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
Copyright (c) 2024 - present, Bereket Engida
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
5
|
+
this software and associated documentation files (the βSoftwareβ), to deal in
|
|
6
|
+
the Software without restriction, including without limitation the rights to
|
|
7
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
8
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
9
|
+
subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED βAS ISβ, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
16
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
17
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
18
|
+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
19
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
20
|
+
DEALINGS IN THE SOFTWARE.
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { DBAdapter, DBAdapterDebugLogOption } from "@better-auth/core/db/adapter";
|
|
2
|
+
import { BetterAuthOptions } from "@better-auth/core";
|
|
3
|
+
|
|
4
|
+
//#region src/drizzle-adapter.d.ts
|
|
5
|
+
interface DB {
|
|
6
|
+
[key: string]: any;
|
|
7
|
+
}
|
|
8
|
+
interface DrizzleAdapterConfig {
|
|
9
|
+
/**
|
|
10
|
+
* The schema object that defines the tables and fields
|
|
11
|
+
*/
|
|
12
|
+
schema?: Record<string, any> | undefined;
|
|
13
|
+
/**
|
|
14
|
+
* The database provider
|
|
15
|
+
*/
|
|
16
|
+
provider: "pg" | "mysql" | "sqlite";
|
|
17
|
+
/**
|
|
18
|
+
* If the table names in the schema are plural
|
|
19
|
+
* set this to true. For example, if the schema
|
|
20
|
+
* has an object with a key "users" instead of "user"
|
|
21
|
+
*/
|
|
22
|
+
usePlural?: boolean | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Enable debug logs for the adapter
|
|
25
|
+
*
|
|
26
|
+
* @default false
|
|
27
|
+
*/
|
|
28
|
+
debugLogs?: DBAdapterDebugLogOption | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* By default snake case is used for table and field names
|
|
31
|
+
* when the CLI is used to generate the schema. If you want
|
|
32
|
+
* to use camel case, set this to true.
|
|
33
|
+
* @default false
|
|
34
|
+
*/
|
|
35
|
+
camelCase?: boolean | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Whether to execute multiple operations in a transaction.
|
|
38
|
+
*
|
|
39
|
+
* If the database doesn't support transactions,
|
|
40
|
+
* set this to `false` and operations will be executed sequentially.
|
|
41
|
+
* @default false
|
|
42
|
+
*/
|
|
43
|
+
transaction?: boolean | undefined;
|
|
44
|
+
}
|
|
45
|
+
declare const drizzleAdapter: (db: DB, config: DrizzleAdapterConfig) => (options: BetterAuthOptions) => DBAdapter<BetterAuthOptions>;
|
|
46
|
+
//#endregion
|
|
47
|
+
export { DB, DrizzleAdapterConfig, drizzleAdapter };
|
|
48
|
+
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
import { createAdapterFactory } from "@better-auth/core/db/adapter";
|
|
2
|
+
import { logger } from "@better-auth/core/env";
|
|
3
|
+
import { BetterAuthError } from "@better-auth/core/error";
|
|
4
|
+
import { and, asc, count, desc, eq, gt, gte, inArray, like, lt, lte, ne, notInArray, or, sql } from "drizzle-orm";
|
|
5
|
+
|
|
6
|
+
//#region src/drizzle-adapter.ts
|
|
7
|
+
const drizzleAdapter = (db, config) => {
|
|
8
|
+
let lazyOptions = null;
|
|
9
|
+
const createCustomAdapter = (db) => ({ getFieldName, options }) => {
|
|
10
|
+
function getSchema(model) {
|
|
11
|
+
const schema = config.schema || db._.fullSchema;
|
|
12
|
+
if (!schema) throw new BetterAuthError("Drizzle adapter failed to initialize. Schema not found. Please provide a schema object in the adapter options object.");
|
|
13
|
+
const schemaModel = schema[model];
|
|
14
|
+
if (!schemaModel) throw new BetterAuthError(`[# Drizzle Adapter]: The model "${model}" was not found in the schema object. Please pass the schema directly to the adapter options.`);
|
|
15
|
+
return schemaModel;
|
|
16
|
+
}
|
|
17
|
+
const withReturning = async (model, builder, data, where) => {
|
|
18
|
+
if (config.provider !== "mysql") return (await builder.returning())[0];
|
|
19
|
+
await builder.execute();
|
|
20
|
+
const schemaModel = getSchema(model);
|
|
21
|
+
const builderVal = builder.config?.values;
|
|
22
|
+
if (where?.length) {
|
|
23
|
+
const clause = convertWhereClause(where.map((w) => {
|
|
24
|
+
if (data[w.field] !== void 0) return {
|
|
25
|
+
...w,
|
|
26
|
+
value: data[w.field]
|
|
27
|
+
};
|
|
28
|
+
return w;
|
|
29
|
+
}), model);
|
|
30
|
+
return (await db.select().from(schemaModel).where(...clause))[0];
|
|
31
|
+
} else if (builderVal && builderVal[0]?.id?.value) {
|
|
32
|
+
let tId = builderVal[0]?.id?.value;
|
|
33
|
+
if (!tId) tId = (await db.select({ id: sql`LAST_INSERT_ID()` }).from(schemaModel).orderBy(desc(schemaModel.id)).limit(1))[0].id;
|
|
34
|
+
return (await db.select().from(schemaModel).where(eq(schemaModel.id, tId)).limit(1).execute())[0];
|
|
35
|
+
} else if (data.id) return (await db.select().from(schemaModel).where(eq(schemaModel.id, data.id)).limit(1).execute())[0];
|
|
36
|
+
else {
|
|
37
|
+
if (!("id" in schemaModel)) throw new BetterAuthError(`The model "${model}" does not have an "id" field. Please use the "id" field as your primary key.`);
|
|
38
|
+
return (await db.select().from(schemaModel).orderBy(desc(schemaModel.id)).limit(1).execute())[0];
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
function convertWhereClause(where, model) {
|
|
42
|
+
const schemaModel = getSchema(model);
|
|
43
|
+
if (!where) return [];
|
|
44
|
+
if (where.length === 1) {
|
|
45
|
+
const w = where[0];
|
|
46
|
+
if (!w) return [];
|
|
47
|
+
const field = getFieldName({
|
|
48
|
+
model,
|
|
49
|
+
field: w.field
|
|
50
|
+
});
|
|
51
|
+
if (!schemaModel[field]) throw new BetterAuthError(`The field "${w.field}" does not exist in the schema for the model "${model}". Please update your schema.`);
|
|
52
|
+
if (w.operator === "in") {
|
|
53
|
+
if (!Array.isArray(w.value)) throw new BetterAuthError(`The value for the field "${w.field}" must be an array when using the "in" operator.`);
|
|
54
|
+
return [inArray(schemaModel[field], w.value)];
|
|
55
|
+
}
|
|
56
|
+
if (w.operator === "not_in") {
|
|
57
|
+
if (!Array.isArray(w.value)) throw new BetterAuthError(`The value for the field "${w.field}" must be an array when using the "not_in" operator.`);
|
|
58
|
+
return [notInArray(schemaModel[field], w.value)];
|
|
59
|
+
}
|
|
60
|
+
if (w.operator === "contains") return [like(schemaModel[field], `%${w.value}%`)];
|
|
61
|
+
if (w.operator === "starts_with") return [like(schemaModel[field], `${w.value}%`)];
|
|
62
|
+
if (w.operator === "ends_with") return [like(schemaModel[field], `%${w.value}`)];
|
|
63
|
+
if (w.operator === "lt") return [lt(schemaModel[field], w.value)];
|
|
64
|
+
if (w.operator === "lte") return [lte(schemaModel[field], w.value)];
|
|
65
|
+
if (w.operator === "ne") return [ne(schemaModel[field], w.value)];
|
|
66
|
+
if (w.operator === "gt") return [gt(schemaModel[field], w.value)];
|
|
67
|
+
if (w.operator === "gte") return [gte(schemaModel[field], w.value)];
|
|
68
|
+
return [eq(schemaModel[field], w.value)];
|
|
69
|
+
}
|
|
70
|
+
const andGroup = where.filter((w) => w.connector === "AND" || !w.connector);
|
|
71
|
+
const orGroup = where.filter((w) => w.connector === "OR");
|
|
72
|
+
const andClause = and(...andGroup.map((w) => {
|
|
73
|
+
const field = getFieldName({
|
|
74
|
+
model,
|
|
75
|
+
field: w.field
|
|
76
|
+
});
|
|
77
|
+
if (w.operator === "in") {
|
|
78
|
+
if (!Array.isArray(w.value)) throw new BetterAuthError(`The value for the field "${w.field}" must be an array when using the "in" operator.`);
|
|
79
|
+
return inArray(schemaModel[field], w.value);
|
|
80
|
+
}
|
|
81
|
+
if (w.operator === "not_in") {
|
|
82
|
+
if (!Array.isArray(w.value)) throw new BetterAuthError(`The value for the field "${w.field}" must be an array when using the "not_in" operator.`);
|
|
83
|
+
return notInArray(schemaModel[field], w.value);
|
|
84
|
+
}
|
|
85
|
+
if (w.operator === "contains") return like(schemaModel[field], `%${w.value}%`);
|
|
86
|
+
if (w.operator === "starts_with") return like(schemaModel[field], `${w.value}%`);
|
|
87
|
+
if (w.operator === "ends_with") return like(schemaModel[field], `%${w.value}`);
|
|
88
|
+
if (w.operator === "lt") return lt(schemaModel[field], w.value);
|
|
89
|
+
if (w.operator === "lte") return lte(schemaModel[field], w.value);
|
|
90
|
+
if (w.operator === "gt") return gt(schemaModel[field], w.value);
|
|
91
|
+
if (w.operator === "gte") return gte(schemaModel[field], w.value);
|
|
92
|
+
if (w.operator === "ne") return ne(schemaModel[field], w.value);
|
|
93
|
+
return eq(schemaModel[field], w.value);
|
|
94
|
+
}));
|
|
95
|
+
const orClause = or(...orGroup.map((w) => {
|
|
96
|
+
const field = getFieldName({
|
|
97
|
+
model,
|
|
98
|
+
field: w.field
|
|
99
|
+
});
|
|
100
|
+
if (w.operator === "in") {
|
|
101
|
+
if (!Array.isArray(w.value)) throw new BetterAuthError(`The value for the field "${w.field}" must be an array when using the "in" operator.`);
|
|
102
|
+
return inArray(schemaModel[field], w.value);
|
|
103
|
+
}
|
|
104
|
+
if (w.operator === "not_in") {
|
|
105
|
+
if (!Array.isArray(w.value)) throw new BetterAuthError(`The value for the field "${w.field}" must be an array when using the "not_in" operator.`);
|
|
106
|
+
return notInArray(schemaModel[field], w.value);
|
|
107
|
+
}
|
|
108
|
+
if (w.operator === "contains") return like(schemaModel[field], `%${w.value}%`);
|
|
109
|
+
if (w.operator === "starts_with") return like(schemaModel[field], `${w.value}%`);
|
|
110
|
+
if (w.operator === "ends_with") return like(schemaModel[field], `%${w.value}`);
|
|
111
|
+
if (w.operator === "lt") return lt(schemaModel[field], w.value);
|
|
112
|
+
if (w.operator === "lte") return lte(schemaModel[field], w.value);
|
|
113
|
+
if (w.operator === "gt") return gt(schemaModel[field], w.value);
|
|
114
|
+
if (w.operator === "gte") return gte(schemaModel[field], w.value);
|
|
115
|
+
if (w.operator === "ne") return ne(schemaModel[field], w.value);
|
|
116
|
+
return eq(schemaModel[field], w.value);
|
|
117
|
+
}));
|
|
118
|
+
const clause = [];
|
|
119
|
+
if (andGroup.length) clause.push(andClause);
|
|
120
|
+
if (orGroup.length) clause.push(orClause);
|
|
121
|
+
return clause;
|
|
122
|
+
}
|
|
123
|
+
function checkMissingFields(schema, model, values) {
|
|
124
|
+
if (!schema) throw new BetterAuthError("Drizzle adapter failed to initialize. Drizzle Schema not found. Please provide a schema object in the adapter options object.");
|
|
125
|
+
for (const key in values) if (!schema[key]) throw new BetterAuthError(`The field "${key}" does not exist in the "${model}" Drizzle schema. Please update your drizzle schema or re-generate using "npx @better-auth/cli@latest generate".`);
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
async create({ model, data: values }) {
|
|
129
|
+
const schemaModel = getSchema(model);
|
|
130
|
+
checkMissingFields(schemaModel, model, values);
|
|
131
|
+
return await withReturning(model, db.insert(schemaModel).values(values), values);
|
|
132
|
+
},
|
|
133
|
+
async findOne({ model, where, join }) {
|
|
134
|
+
const schemaModel = getSchema(model);
|
|
135
|
+
const clause = convertWhereClause(where, model);
|
|
136
|
+
if (options.experimental?.joins) if (!db.query || !db.query[model]) {
|
|
137
|
+
logger.error(`[# Drizzle Adapter]: The model "${model}" was not found in the query object. Please update your Drizzle schema to include relations or re-generate using "npx @better-auth/cli@latest generate".`);
|
|
138
|
+
logger.info("Falling back to regular query");
|
|
139
|
+
} else {
|
|
140
|
+
let includes;
|
|
141
|
+
const pluralJoinResults = [];
|
|
142
|
+
if (join) {
|
|
143
|
+
includes = {};
|
|
144
|
+
const joinEntries = Object.entries(join);
|
|
145
|
+
for (const [model, joinAttr] of joinEntries) {
|
|
146
|
+
const limit = joinAttr.limit ?? options.advanced?.database?.defaultFindManyLimit ?? 100;
|
|
147
|
+
const isUnique = joinAttr.relation === "one-to-one";
|
|
148
|
+
const pluralSuffix = isUnique || config.usePlural ? "" : "s";
|
|
149
|
+
includes[`${model}${pluralSuffix}`] = isUnique ? true : { limit };
|
|
150
|
+
if (!isUnique) pluralJoinResults.push(`${model}${pluralSuffix}`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
const res = await db.query[model].findFirst({
|
|
154
|
+
where: clause[0],
|
|
155
|
+
with: includes
|
|
156
|
+
});
|
|
157
|
+
if (res) for (const pluralJoinResult of pluralJoinResults) {
|
|
158
|
+
const singularKey = !config.usePlural ? pluralJoinResult.slice(0, -1) : pluralJoinResult;
|
|
159
|
+
res[singularKey] = res[pluralJoinResult];
|
|
160
|
+
if (pluralJoinResult !== singularKey) delete res[pluralJoinResult];
|
|
161
|
+
}
|
|
162
|
+
return res;
|
|
163
|
+
}
|
|
164
|
+
const res = await db.select().from(schemaModel).where(...clause);
|
|
165
|
+
if (!res.length) return null;
|
|
166
|
+
return res[0];
|
|
167
|
+
},
|
|
168
|
+
async findMany({ model, where, sortBy, limit, offset, join }) {
|
|
169
|
+
const schemaModel = getSchema(model);
|
|
170
|
+
const clause = where ? convertWhereClause(where, model) : [];
|
|
171
|
+
const sortFn = sortBy?.direction === "desc" ? desc : asc;
|
|
172
|
+
if (options.experimental?.joins) if (!db.query[model]) {
|
|
173
|
+
logger.error(`[# Drizzle Adapter]: The model "${model}" was not found in the query object. Please update your Drizzle schema to include relations or re-generate using "npx @better-auth/cli@latest generate".`);
|
|
174
|
+
logger.info("Falling back to regular query");
|
|
175
|
+
} else {
|
|
176
|
+
let includes;
|
|
177
|
+
const pluralJoinResults = [];
|
|
178
|
+
if (join) {
|
|
179
|
+
includes = {};
|
|
180
|
+
const joinEntries = Object.entries(join);
|
|
181
|
+
for (const [model, joinAttr] of joinEntries) {
|
|
182
|
+
const isUnique = joinAttr.relation === "one-to-one";
|
|
183
|
+
const limit = joinAttr.limit ?? options.advanced?.database?.defaultFindManyLimit ?? 100;
|
|
184
|
+
const pluralSuffix = isUnique || config.usePlural ? "" : "s";
|
|
185
|
+
includes[`${model}${pluralSuffix}`] = isUnique ? true : { limit };
|
|
186
|
+
if (!isUnique) pluralJoinResults.push(`${model}${pluralSuffix}`);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
let orderBy = void 0;
|
|
190
|
+
if (sortBy?.field) orderBy = [sortFn(schemaModel[getFieldName({
|
|
191
|
+
model,
|
|
192
|
+
field: sortBy?.field
|
|
193
|
+
})])];
|
|
194
|
+
const res = await db.query[model].findMany({
|
|
195
|
+
where: clause[0],
|
|
196
|
+
with: includes,
|
|
197
|
+
limit: limit ?? 100,
|
|
198
|
+
offset: offset ?? 0,
|
|
199
|
+
orderBy
|
|
200
|
+
});
|
|
201
|
+
if (res) for (const item of res) for (const pluralJoinResult of pluralJoinResults) {
|
|
202
|
+
const singularKey = !config.usePlural ? pluralJoinResult.slice(0, -1) : pluralJoinResult;
|
|
203
|
+
if (singularKey === pluralJoinResult) continue;
|
|
204
|
+
item[singularKey] = item[pluralJoinResult];
|
|
205
|
+
delete item[pluralJoinResult];
|
|
206
|
+
}
|
|
207
|
+
return res;
|
|
208
|
+
}
|
|
209
|
+
let builder = db.select().from(schemaModel);
|
|
210
|
+
const effectiveLimit = limit;
|
|
211
|
+
const effectiveOffset = offset;
|
|
212
|
+
if (typeof effectiveLimit !== "undefined") builder = builder.limit(effectiveLimit);
|
|
213
|
+
if (typeof effectiveOffset !== "undefined") builder = builder.offset(effectiveOffset);
|
|
214
|
+
if (sortBy?.field) builder = builder.orderBy(sortFn(schemaModel[getFieldName({
|
|
215
|
+
model,
|
|
216
|
+
field: sortBy?.field
|
|
217
|
+
})]));
|
|
218
|
+
return await builder.where(...clause);
|
|
219
|
+
},
|
|
220
|
+
async count({ model, where }) {
|
|
221
|
+
const schemaModel = getSchema(model);
|
|
222
|
+
const clause = where ? convertWhereClause(where, model) : [];
|
|
223
|
+
return (await db.select({ count: count() }).from(schemaModel).where(...clause))[0].count;
|
|
224
|
+
},
|
|
225
|
+
async update({ model, where, update: values }) {
|
|
226
|
+
const schemaModel = getSchema(model);
|
|
227
|
+
const clause = convertWhereClause(where, model);
|
|
228
|
+
return await withReturning(model, db.update(schemaModel).set(values).where(...clause), values, where);
|
|
229
|
+
},
|
|
230
|
+
async updateMany({ model, where, update: values }) {
|
|
231
|
+
const schemaModel = getSchema(model);
|
|
232
|
+
const clause = convertWhereClause(where, model);
|
|
233
|
+
return await db.update(schemaModel).set(values).where(...clause);
|
|
234
|
+
},
|
|
235
|
+
async delete({ model, where }) {
|
|
236
|
+
const schemaModel = getSchema(model);
|
|
237
|
+
const clause = convertWhereClause(where, model);
|
|
238
|
+
return await db.delete(schemaModel).where(...clause);
|
|
239
|
+
},
|
|
240
|
+
async deleteMany({ model, where }) {
|
|
241
|
+
const schemaModel = getSchema(model);
|
|
242
|
+
const clause = convertWhereClause(where, model);
|
|
243
|
+
const res = await db.delete(schemaModel).where(...clause);
|
|
244
|
+
let count = 0;
|
|
245
|
+
if (res && "rowCount" in res) count = res.rowCount;
|
|
246
|
+
else if (Array.isArray(res)) count = res.length;
|
|
247
|
+
else if (res && ("affectedRows" in res || "rowsAffected" in res || "changes" in res)) count = res.affectedRows ?? res.rowsAffected ?? res.changes;
|
|
248
|
+
if (typeof count !== "number") logger.error("[Drizzle Adapter] The result of the deleteMany operation is not a number. This is likely a bug in the adapter. Please report this issue to the Better Auth team.", {
|
|
249
|
+
res,
|
|
250
|
+
model,
|
|
251
|
+
where
|
|
252
|
+
});
|
|
253
|
+
return count;
|
|
254
|
+
},
|
|
255
|
+
options: config
|
|
256
|
+
};
|
|
257
|
+
};
|
|
258
|
+
let adapterOptions = null;
|
|
259
|
+
adapterOptions = {
|
|
260
|
+
config: {
|
|
261
|
+
adapterId: "drizzle",
|
|
262
|
+
adapterName: "Drizzle Adapter",
|
|
263
|
+
usePlural: config.usePlural ?? false,
|
|
264
|
+
debugLogs: config.debugLogs ?? false,
|
|
265
|
+
supportsUUIDs: config.provider === "pg" ? true : false,
|
|
266
|
+
supportsJSON: config.provider === "pg" ? true : false,
|
|
267
|
+
supportsArrays: config.provider === "pg" ? true : false,
|
|
268
|
+
transaction: config.transaction ?? false ? (cb) => db.transaction((tx) => {
|
|
269
|
+
return cb(createAdapterFactory({
|
|
270
|
+
config: adapterOptions.config,
|
|
271
|
+
adapter: createCustomAdapter(tx)
|
|
272
|
+
})(lazyOptions));
|
|
273
|
+
}) : false
|
|
274
|
+
},
|
|
275
|
+
adapter: createCustomAdapter(db)
|
|
276
|
+
};
|
|
277
|
+
const adapter = createAdapterFactory(adapterOptions);
|
|
278
|
+
return (options) => {
|
|
279
|
+
lazyOptions = options;
|
|
280
|
+
return adapter(options);
|
|
281
|
+
};
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
//#endregion
|
|
285
|
+
export { drizzleAdapter };
|
|
286
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/drizzle-adapter.ts"],"sourcesContent":["import type { BetterAuthOptions } from \"@better-auth/core\";\nimport type {\n\tAdapterFactoryCustomizeAdapterCreator,\n\tAdapterFactoryOptions,\n\tDBAdapter,\n\tDBAdapterDebugLogOption,\n\tWhere,\n} from \"@better-auth/core/db/adapter\";\nimport { createAdapterFactory } from \"@better-auth/core/db/adapter\";\nimport { logger } from \"@better-auth/core/env\";\nimport { BetterAuthError } from \"@better-auth/core/error\";\nimport type { SQL } from \"drizzle-orm\";\nimport {\n\tand,\n\tasc,\n\tcount,\n\tdesc,\n\teq,\n\tgt,\n\tgte,\n\tinArray,\n\tlike,\n\tlt,\n\tlte,\n\tne,\n\tnotInArray,\n\tor,\n\tsql,\n} from \"drizzle-orm\";\n\nexport interface DB {\n\t[key: string]: any;\n}\n\nexport interface DrizzleAdapterConfig {\n\t/**\n\t * The schema object that defines the tables and fields\n\t */\n\tschema?: Record<string, any> | undefined;\n\t/**\n\t * The database provider\n\t */\n\tprovider: \"pg\" | \"mysql\" | \"sqlite\";\n\t/**\n\t * If the table names in the schema are plural\n\t * set this to true. For example, if the schema\n\t * has an object with a key \"users\" instead of \"user\"\n\t */\n\tusePlural?: boolean | undefined;\n\t/**\n\t * Enable debug logs for the adapter\n\t *\n\t * @default false\n\t */\n\tdebugLogs?: DBAdapterDebugLogOption | undefined;\n\t/**\n\t * By default snake case is used for table and field names\n\t * when the CLI is used to generate the schema. If you want\n\t * to use camel case, set this to true.\n\t * @default false\n\t */\n\tcamelCase?: boolean | undefined;\n\t/**\n\t * Whether to execute multiple operations in a transaction.\n\t *\n\t * If the database doesn't support transactions,\n\t * set this to `false` and operations will be executed sequentially.\n\t * @default false\n\t */\n\ttransaction?: boolean | undefined;\n}\n\nexport const drizzleAdapter = (db: DB, config: DrizzleAdapterConfig) => {\n\tlet lazyOptions: BetterAuthOptions | null = null;\n\tconst createCustomAdapter =\n\t\t(db: DB): AdapterFactoryCustomizeAdapterCreator =>\n\t\t({ getFieldName, options }) => {\n\t\t\tfunction getSchema(model: string) {\n\t\t\t\tconst schema = config.schema || db._.fullSchema;\n\t\t\t\tif (!schema) {\n\t\t\t\t\tthrow new BetterAuthError(\n\t\t\t\t\t\t\"Drizzle adapter failed to initialize. Schema not found. Please provide a schema object in the adapter options object.\",\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tconst schemaModel = schema[model];\n\t\t\t\tif (!schemaModel) {\n\t\t\t\t\tthrow new BetterAuthError(\n\t\t\t\t\t\t`[# Drizzle Adapter]: The model \"${model}\" was not found in the schema object. Please pass the schema directly to the adapter options.`,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\treturn schemaModel;\n\t\t\t}\n\t\t\tconst withReturning = async (\n\t\t\t\tmodel: string,\n\t\t\t\tbuilder: any,\n\t\t\t\tdata: Record<string, any>,\n\t\t\t\twhere?: Where[] | undefined,\n\t\t\t) => {\n\t\t\t\tif (config.provider !== \"mysql\") {\n\t\t\t\t\tconst c = await builder.returning();\n\t\t\t\t\treturn c[0];\n\t\t\t\t}\n\t\t\t\tawait builder.execute();\n\t\t\t\tconst schemaModel = getSchema(model);\n\t\t\t\tconst builderVal = builder.config?.values;\n\t\t\t\tif (where?.length) {\n\t\t\t\t\t// If we're updating a field that's in the where clause, use the new value\n\t\t\t\t\tconst updatedWhere = where.map((w) => {\n\t\t\t\t\t\t// If this field was updated, use the new value for lookup\n\t\t\t\t\t\tif (data[w.field] !== undefined) {\n\t\t\t\t\t\t\treturn { ...w, value: data[w.field] };\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn w;\n\t\t\t\t\t});\n\n\t\t\t\t\tconst clause = convertWhereClause(updatedWhere, model);\n\t\t\t\t\tconst res = await db\n\t\t\t\t\t\t.select()\n\t\t\t\t\t\t.from(schemaModel)\n\t\t\t\t\t\t.where(...clause);\n\t\t\t\t\treturn res[0];\n\t\t\t\t} else if (builderVal && builderVal[0]?.id?.value) {\n\t\t\t\t\tlet tId = builderVal[0]?.id?.value;\n\t\t\t\t\tif (!tId) {\n\t\t\t\t\t\t//get last inserted id\n\t\t\t\t\t\tconst lastInsertId = await db\n\t\t\t\t\t\t\t.select({ id: sql`LAST_INSERT_ID()` })\n\t\t\t\t\t\t\t.from(schemaModel)\n\t\t\t\t\t\t\t.orderBy(desc(schemaModel.id))\n\t\t\t\t\t\t\t.limit(1);\n\t\t\t\t\t\ttId = lastInsertId[0].id;\n\t\t\t\t\t}\n\t\t\t\t\tconst res = await db\n\t\t\t\t\t\t.select()\n\t\t\t\t\t\t.from(schemaModel)\n\t\t\t\t\t\t.where(eq(schemaModel.id, tId))\n\t\t\t\t\t\t.limit(1)\n\t\t\t\t\t\t.execute();\n\t\t\t\t\treturn res[0];\n\t\t\t\t} else if (data.id) {\n\t\t\t\t\tconst res = await db\n\t\t\t\t\t\t.select()\n\t\t\t\t\t\t.from(schemaModel)\n\t\t\t\t\t\t.where(eq(schemaModel.id, data.id))\n\t\t\t\t\t\t.limit(1)\n\t\t\t\t\t\t.execute();\n\t\t\t\t\treturn res[0];\n\t\t\t\t} else {\n\t\t\t\t\t// If the user doesn't have `id` as a field, then this will fail.\n\t\t\t\t\t// We expect that they defined `id` in all of their models.\n\t\t\t\t\tif (!(\"id\" in schemaModel)) {\n\t\t\t\t\t\tthrow new BetterAuthError(\n\t\t\t\t\t\t\t`The model \"${model}\" does not have an \"id\" field. Please use the \"id\" field as your primary key.`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\tconst res = await db\n\t\t\t\t\t\t.select()\n\t\t\t\t\t\t.from(schemaModel)\n\t\t\t\t\t\t.orderBy(desc(schemaModel.id))\n\t\t\t\t\t\t.limit(1)\n\t\t\t\t\t\t.execute();\n\t\t\t\t\treturn res[0];\n\t\t\t\t}\n\t\t\t};\n\t\t\tfunction convertWhereClause(where: Where[], model: string) {\n\t\t\t\tconst schemaModel = getSchema(model);\n\t\t\t\tif (!where) return [];\n\t\t\t\tif (where.length === 1) {\n\t\t\t\t\tconst w = where[0];\n\t\t\t\t\tif (!w) {\n\t\t\t\t\t\treturn [];\n\t\t\t\t\t}\n\t\t\t\t\tconst field = getFieldName({ model, field: w.field });\n\t\t\t\t\tif (!schemaModel[field]) {\n\t\t\t\t\t\tthrow new BetterAuthError(\n\t\t\t\t\t\t\t`The field \"${w.field}\" does not exist in the schema for the model \"${model}\". Please update your schema.`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\tif (w.operator === \"in\") {\n\t\t\t\t\t\tif (!Array.isArray(w.value)) {\n\t\t\t\t\t\t\tthrow new BetterAuthError(\n\t\t\t\t\t\t\t\t`The value for the field \"${w.field}\" must be an array when using the \"in\" operator.`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn [inArray(schemaModel[field], w.value)];\n\t\t\t\t\t}\n\n\t\t\t\t\tif (w.operator === \"not_in\") {\n\t\t\t\t\t\tif (!Array.isArray(w.value)) {\n\t\t\t\t\t\t\tthrow new BetterAuthError(\n\t\t\t\t\t\t\t\t`The value for the field \"${w.field}\" must be an array when using the \"not_in\" operator.`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn [notInArray(schemaModel[field], w.value)];\n\t\t\t\t\t}\n\n\t\t\t\t\tif (w.operator === \"contains\") {\n\t\t\t\t\t\treturn [like(schemaModel[field], `%${w.value}%`)];\n\t\t\t\t\t}\n\n\t\t\t\t\tif (w.operator === \"starts_with\") {\n\t\t\t\t\t\treturn [like(schemaModel[field], `${w.value}%`)];\n\t\t\t\t\t}\n\n\t\t\t\t\tif (w.operator === \"ends_with\") {\n\t\t\t\t\t\treturn [like(schemaModel[field], `%${w.value}`)];\n\t\t\t\t\t}\n\n\t\t\t\t\tif (w.operator === \"lt\") {\n\t\t\t\t\t\treturn [lt(schemaModel[field], w.value)];\n\t\t\t\t\t}\n\n\t\t\t\t\tif (w.operator === \"lte\") {\n\t\t\t\t\t\treturn [lte(schemaModel[field], w.value)];\n\t\t\t\t\t}\n\n\t\t\t\t\tif (w.operator === \"ne\") {\n\t\t\t\t\t\treturn [ne(schemaModel[field], w.value)];\n\t\t\t\t\t}\n\n\t\t\t\t\tif (w.operator === \"gt\") {\n\t\t\t\t\t\treturn [gt(schemaModel[field], w.value)];\n\t\t\t\t\t}\n\n\t\t\t\t\tif (w.operator === \"gte\") {\n\t\t\t\t\t\treturn [gte(schemaModel[field], w.value)];\n\t\t\t\t\t}\n\n\t\t\t\t\treturn [eq(schemaModel[field], w.value)];\n\t\t\t\t}\n\t\t\t\tconst andGroup = where.filter(\n\t\t\t\t\t(w) => w.connector === \"AND\" || !w.connector,\n\t\t\t\t);\n\t\t\t\tconst orGroup = where.filter((w) => w.connector === \"OR\");\n\n\t\t\t\tconst andClause = and(\n\t\t\t\t\t...andGroup.map((w) => {\n\t\t\t\t\t\tconst field = getFieldName({ model, field: w.field });\n\t\t\t\t\t\tif (w.operator === \"in\") {\n\t\t\t\t\t\t\tif (!Array.isArray(w.value)) {\n\t\t\t\t\t\t\t\tthrow new BetterAuthError(\n\t\t\t\t\t\t\t\t\t`The value for the field \"${w.field}\" must be an array when using the \"in\" operator.`,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn inArray(schemaModel[field], w.value);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (w.operator === \"not_in\") {\n\t\t\t\t\t\t\tif (!Array.isArray(w.value)) {\n\t\t\t\t\t\t\t\tthrow new BetterAuthError(\n\t\t\t\t\t\t\t\t\t`The value for the field \"${w.field}\" must be an array when using the \"not_in\" operator.`,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn notInArray(schemaModel[field], w.value);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (w.operator === \"contains\") {\n\t\t\t\t\t\t\treturn like(schemaModel[field], `%${w.value}%`);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (w.operator === \"starts_with\") {\n\t\t\t\t\t\t\treturn like(schemaModel[field], `${w.value}%`);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (w.operator === \"ends_with\") {\n\t\t\t\t\t\t\treturn like(schemaModel[field], `%${w.value}`);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (w.operator === \"lt\") {\n\t\t\t\t\t\t\treturn lt(schemaModel[field], w.value);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (w.operator === \"lte\") {\n\t\t\t\t\t\t\treturn lte(schemaModel[field], w.value);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (w.operator === \"gt\") {\n\t\t\t\t\t\t\treturn gt(schemaModel[field], w.value);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (w.operator === \"gte\") {\n\t\t\t\t\t\t\treturn gte(schemaModel[field], w.value);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (w.operator === \"ne\") {\n\t\t\t\t\t\t\treturn ne(schemaModel[field], w.value);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn eq(schemaModel[field], w.value);\n\t\t\t\t\t}),\n\t\t\t\t);\n\t\t\t\tconst orClause = or(\n\t\t\t\t\t...orGroup.map((w) => {\n\t\t\t\t\t\tconst field = getFieldName({ model, field: w.field });\n\t\t\t\t\t\tif (w.operator === \"in\") {\n\t\t\t\t\t\t\tif (!Array.isArray(w.value)) {\n\t\t\t\t\t\t\t\tthrow new BetterAuthError(\n\t\t\t\t\t\t\t\t\t`The value for the field \"${w.field}\" must be an array when using the \"in\" operator.`,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn inArray(schemaModel[field], w.value);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (w.operator === \"not_in\") {\n\t\t\t\t\t\t\tif (!Array.isArray(w.value)) {\n\t\t\t\t\t\t\t\tthrow new BetterAuthError(\n\t\t\t\t\t\t\t\t\t`The value for the field \"${w.field}\" must be an array when using the \"not_in\" operator.`,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn notInArray(schemaModel[field], w.value);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (w.operator === \"contains\") {\n\t\t\t\t\t\t\treturn like(schemaModel[field], `%${w.value}%`);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (w.operator === \"starts_with\") {\n\t\t\t\t\t\t\treturn like(schemaModel[field], `${w.value}%`);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (w.operator === \"ends_with\") {\n\t\t\t\t\t\t\treturn like(schemaModel[field], `%${w.value}`);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (w.operator === \"lt\") {\n\t\t\t\t\t\t\treturn lt(schemaModel[field], w.value);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (w.operator === \"lte\") {\n\t\t\t\t\t\t\treturn lte(schemaModel[field], w.value);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (w.operator === \"gt\") {\n\t\t\t\t\t\t\treturn gt(schemaModel[field], w.value);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (w.operator === \"gte\") {\n\t\t\t\t\t\t\treturn gte(schemaModel[field], w.value);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (w.operator === \"ne\") {\n\t\t\t\t\t\t\treturn ne(schemaModel[field], w.value);\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn eq(schemaModel[field], w.value);\n\t\t\t\t\t}),\n\t\t\t\t);\n\n\t\t\t\tconst clause: SQL<unknown>[] = [];\n\n\t\t\t\tif (andGroup.length) clause.push(andClause!);\n\t\t\t\tif (orGroup.length) clause.push(orClause!);\n\t\t\t\treturn clause;\n\t\t\t}\n\t\t\tfunction checkMissingFields(\n\t\t\t\tschema: Record<string, any>,\n\t\t\t\tmodel: string,\n\t\t\t\tvalues: Record<string, any>,\n\t\t\t) {\n\t\t\t\tif (!schema) {\n\t\t\t\t\tthrow new BetterAuthError(\n\t\t\t\t\t\t\"Drizzle adapter failed to initialize. Drizzle Schema not found. Please provide a schema object in the adapter options object.\",\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tfor (const key in values) {\n\t\t\t\t\tif (!schema[key]) {\n\t\t\t\t\t\tthrow new BetterAuthError(\n\t\t\t\t\t\t\t`The field \"${key}\" does not exist in the \"${model}\" Drizzle schema. Please update your drizzle schema or re-generate using \"npx @better-auth/cli@latest generate\".`,\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\tasync create({ model, data: values }) {\n\t\t\t\t\tconst schemaModel = getSchema(model);\n\t\t\t\t\tcheckMissingFields(schemaModel, model, values);\n\t\t\t\t\tconst builder = db.insert(schemaModel).values(values);\n\t\t\t\t\tconst returned = await withReturning(model, builder, values);\n\t\t\t\t\treturn returned;\n\t\t\t\t},\n\t\t\t\tasync findOne({ model, where, join }) {\n\t\t\t\t\tconst schemaModel = getSchema(model);\n\t\t\t\t\tconst clause = convertWhereClause(where, model);\n\n\t\t\t\t\tif (options.experimental?.joins) {\n\t\t\t\t\t\tif (!db.query || !db.query[model]) {\n\t\t\t\t\t\t\tlogger.error(\n\t\t\t\t\t\t\t\t`[# Drizzle Adapter]: The model \"${model}\" was not found in the query object. Please update your Drizzle schema to include relations or re-generate using \"npx @better-auth/cli@latest generate\".`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tlogger.info(\"Falling back to regular query\");\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tlet includes:\n\t\t\t\t\t\t\t\t| Record<string, { limit: number } | boolean>\n\t\t\t\t\t\t\t\t| undefined;\n\n\t\t\t\t\t\t\tconst pluralJoinResults: string[] = [];\n\t\t\t\t\t\t\tif (join) {\n\t\t\t\t\t\t\t\tincludes = {};\n\t\t\t\t\t\t\t\tconst joinEntries = Object.entries(join);\n\t\t\t\t\t\t\t\tfor (const [model, joinAttr] of joinEntries) {\n\t\t\t\t\t\t\t\t\tconst limit =\n\t\t\t\t\t\t\t\t\t\tjoinAttr.limit ??\n\t\t\t\t\t\t\t\t\t\toptions.advanced?.database?.defaultFindManyLimit ??\n\t\t\t\t\t\t\t\t\t\t100;\n\t\t\t\t\t\t\t\t\tconst isUnique = joinAttr.relation === \"one-to-one\";\n\t\t\t\t\t\t\t\t\tconst pluralSuffix = isUnique || config.usePlural ? \"\" : \"s\";\n\t\t\t\t\t\t\t\t\tincludes[`${model}${pluralSuffix}`] = isUnique\n\t\t\t\t\t\t\t\t\t\t? true\n\t\t\t\t\t\t\t\t\t\t: { limit };\n\t\t\t\t\t\t\t\t\tif (!isUnique) {\n\t\t\t\t\t\t\t\t\t\tpluralJoinResults.push(`${model}${pluralSuffix}`);\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tconst query = db.query[model].findFirst({\n\t\t\t\t\t\t\t\twhere: clause[0],\n\t\t\t\t\t\t\t\twith: includes,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\tconst res = await query;\n\n\t\t\t\t\t\t\tif (res) {\n\t\t\t\t\t\t\t\tfor (const pluralJoinResult of pluralJoinResults) {\n\t\t\t\t\t\t\t\t\tconst singularKey = !config.usePlural\n\t\t\t\t\t\t\t\t\t\t? pluralJoinResult.slice(0, -1)\n\t\t\t\t\t\t\t\t\t\t: pluralJoinResult;\n\t\t\t\t\t\t\t\t\tres[singularKey] = res[pluralJoinResult];\n\t\t\t\t\t\t\t\t\tif (pluralJoinResult !== singularKey) {\n\t\t\t\t\t\t\t\t\t\tdelete res[pluralJoinResult];\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn res;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tconst query = db\n\t\t\t\t\t\t.select()\n\t\t\t\t\t\t.from(schemaModel)\n\t\t\t\t\t\t.where(...clause);\n\n\t\t\t\t\tconst res = await query;\n\n\t\t\t\t\tif (!res.length) return null;\n\t\t\t\t\treturn res[0];\n\t\t\t\t},\n\t\t\t\tasync findMany({ model, where, sortBy, limit, offset, join }) {\n\t\t\t\t\tconst schemaModel = getSchema(model);\n\t\t\t\t\tconst clause = where ? convertWhereClause(where, model) : [];\n\t\t\t\t\tconst sortFn = sortBy?.direction === \"desc\" ? desc : asc;\n\n\t\t\t\t\tif (options.experimental?.joins) {\n\t\t\t\t\t\tif (!db.query[model]) {\n\t\t\t\t\t\t\tlogger.error(\n\t\t\t\t\t\t\t\t`[# Drizzle Adapter]: The model \"${model}\" was not found in the query object. Please update your Drizzle schema to include relations or re-generate using \"npx @better-auth/cli@latest generate\".`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tlogger.info(\"Falling back to regular query\");\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tlet includes:\n\t\t\t\t\t\t\t\t| Record<string, { limit: number } | boolean>\n\t\t\t\t\t\t\t\t| undefined;\n\n\t\t\t\t\t\t\tconst pluralJoinResults: string[] = [];\n\t\t\t\t\t\t\tif (join) {\n\t\t\t\t\t\t\t\tincludes = {};\n\t\t\t\t\t\t\t\tconst joinEntries = Object.entries(join);\n\t\t\t\t\t\t\t\tfor (const [model, joinAttr] of joinEntries) {\n\t\t\t\t\t\t\t\t\tconst isUnique = joinAttr.relation === \"one-to-one\";\n\t\t\t\t\t\t\t\t\tconst limit =\n\t\t\t\t\t\t\t\t\t\tjoinAttr.limit ??\n\t\t\t\t\t\t\t\t\t\toptions.advanced?.database?.defaultFindManyLimit ??\n\t\t\t\t\t\t\t\t\t\t100;\n\t\t\t\t\t\t\t\t\tconst pluralSuffix = isUnique || config.usePlural ? \"\" : \"s\";\n\t\t\t\t\t\t\t\t\tincludes[`${model}${pluralSuffix}`] = isUnique\n\t\t\t\t\t\t\t\t\t\t? true\n\t\t\t\t\t\t\t\t\t\t: { limit };\n\t\t\t\t\t\t\t\t\tif (!isUnique)\n\t\t\t\t\t\t\t\t\t\tpluralJoinResults.push(`${model}${pluralSuffix}`);\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tlet orderBy: SQL<unknown>[] | undefined = undefined;\n\t\t\t\t\t\t\tif (sortBy?.field) {\n\t\t\t\t\t\t\t\torderBy = [\n\t\t\t\t\t\t\t\t\tsortFn(\n\t\t\t\t\t\t\t\t\t\tschemaModel[getFieldName({ model, field: sortBy?.field })],\n\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t];\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tconst query = db.query[model].findMany({\n\t\t\t\t\t\t\t\twhere: clause[0],\n\t\t\t\t\t\t\t\twith: includes,\n\t\t\t\t\t\t\t\tlimit: limit ?? 100,\n\t\t\t\t\t\t\t\toffset: offset ?? 0,\n\t\t\t\t\t\t\t\torderBy,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\tconst res = await query;\n\t\t\t\t\t\t\tif (res) {\n\t\t\t\t\t\t\t\tfor (const item of res) {\n\t\t\t\t\t\t\t\t\tfor (const pluralJoinResult of pluralJoinResults) {\n\t\t\t\t\t\t\t\t\t\tconst singularKey = !config.usePlural\n\t\t\t\t\t\t\t\t\t\t\t? pluralJoinResult.slice(0, -1)\n\t\t\t\t\t\t\t\t\t\t\t: pluralJoinResult;\n\t\t\t\t\t\t\t\t\t\tif (singularKey === pluralJoinResult) continue;\n\t\t\t\t\t\t\t\t\t\titem[singularKey] = item[pluralJoinResult];\n\t\t\t\t\t\t\t\t\t\tdelete item[pluralJoinResult];\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\treturn res;\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tlet builder = db.select().from(schemaModel);\n\n\t\t\t\t\tconst effectiveLimit = limit;\n\t\t\t\t\tconst effectiveOffset = offset;\n\n\t\t\t\t\tif (typeof effectiveLimit !== \"undefined\") {\n\t\t\t\t\t\tbuilder = builder.limit(effectiveLimit);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (typeof effectiveOffset !== \"undefined\") {\n\t\t\t\t\t\tbuilder = builder.offset(effectiveOffset);\n\t\t\t\t\t}\n\n\t\t\t\t\tif (sortBy?.field) {\n\t\t\t\t\t\tbuilder = builder.orderBy(\n\t\t\t\t\t\t\tsortFn(\n\t\t\t\t\t\t\t\tschemaModel[getFieldName({ model, field: sortBy?.field })],\n\t\t\t\t\t\t\t),\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\n\t\t\t\t\tconst res = await builder.where(...clause);\n\t\t\t\t\treturn res;\n\t\t\t\t},\n\t\t\t\tasync count({ model, where }) {\n\t\t\t\t\tconst schemaModel = getSchema(model);\n\t\t\t\t\tconst clause = where ? convertWhereClause(where, model) : [];\n\t\t\t\t\tconst res = await db\n\t\t\t\t\t\t.select({ count: count() })\n\t\t\t\t\t\t.from(schemaModel)\n\t\t\t\t\t\t.where(...clause);\n\t\t\t\t\treturn res[0].count;\n\t\t\t\t},\n\t\t\t\tasync update({ model, where, update: values }) {\n\t\t\t\t\tconst schemaModel = getSchema(model);\n\t\t\t\t\tconst clause = convertWhereClause(where, model);\n\t\t\t\t\tconst builder = db\n\t\t\t\t\t\t.update(schemaModel)\n\t\t\t\t\t\t.set(values)\n\t\t\t\t\t\t.where(...clause);\n\t\t\t\t\treturn await withReturning(model, builder, values as any, where);\n\t\t\t\t},\n\t\t\t\tasync updateMany({ model, where, update: values }) {\n\t\t\t\t\tconst schemaModel = getSchema(model);\n\t\t\t\t\tconst clause = convertWhereClause(where, model);\n\t\t\t\t\tconst builder = db\n\t\t\t\t\t\t.update(schemaModel)\n\t\t\t\t\t\t.set(values)\n\t\t\t\t\t\t.where(...clause);\n\t\t\t\t\treturn await builder;\n\t\t\t\t},\n\t\t\t\tasync delete({ model, where }) {\n\t\t\t\t\tconst schemaModel = getSchema(model);\n\t\t\t\t\tconst clause = convertWhereClause(where, model);\n\t\t\t\t\tconst builder = db.delete(schemaModel).where(...clause);\n\t\t\t\t\treturn await builder;\n\t\t\t\t},\n\t\t\t\tasync deleteMany({ model, where }) {\n\t\t\t\t\tconst schemaModel = getSchema(model);\n\t\t\t\t\tconst clause = convertWhereClause(where, model);\n\t\t\t\t\tconst builder = db.delete(schemaModel).where(...clause);\n\t\t\t\t\tconst res = await builder;\n\t\t\t\t\tlet count = 0;\n\t\t\t\t\tif (res && \"rowCount\" in res) count = res.rowCount;\n\t\t\t\t\telse if (Array.isArray(res)) count = res.length;\n\t\t\t\t\telse if (\n\t\t\t\t\t\tres &&\n\t\t\t\t\t\t(\"affectedRows\" in res || \"rowsAffected\" in res || \"changes\" in res)\n\t\t\t\t\t)\n\t\t\t\t\t\tcount = res.affectedRows ?? res.rowsAffected ?? res.changes;\n\t\t\t\t\tif (typeof count !== \"number\") {\n\t\t\t\t\t\tlogger.error(\n\t\t\t\t\t\t\t\"[Drizzle Adapter] The result of the deleteMany operation is not a number. This is likely a bug in the adapter. Please report this issue to the Better Auth team.\",\n\t\t\t\t\t\t\t{ res, model, where },\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\treturn count;\n\t\t\t\t},\n\t\t\t\toptions: config,\n\t\t\t};\n\t\t};\n\tlet adapterOptions: AdapterFactoryOptions | null = null;\n\tadapterOptions = {\n\t\tconfig: {\n\t\t\tadapterId: \"drizzle\",\n\t\t\tadapterName: \"Drizzle Adapter\",\n\t\t\tusePlural: config.usePlural ?? false,\n\t\t\tdebugLogs: config.debugLogs ?? false,\n\t\t\tsupportsUUIDs: config.provider === \"pg\" ? true : false,\n\t\t\tsupportsJSON:\n\t\t\t\tconfig.provider === \"pg\" // even though mysql also supports it, mysql requires to pass stringified json anyway.\n\t\t\t\t\t? true\n\t\t\t\t\t: false,\n\t\t\tsupportsArrays: config.provider === \"pg\" ? true : false,\n\t\t\ttransaction:\n\t\t\t\t(config.transaction ?? false)\n\t\t\t\t\t? (cb) =>\n\t\t\t\t\t\t\tdb.transaction((tx: DB) => {\n\t\t\t\t\t\t\t\tconst adapter = createAdapterFactory({\n\t\t\t\t\t\t\t\t\tconfig: adapterOptions!.config,\n\t\t\t\t\t\t\t\t\tadapter: createCustomAdapter(tx),\n\t\t\t\t\t\t\t\t})(lazyOptions!);\n\t\t\t\t\t\t\t\treturn cb(adapter);\n\t\t\t\t\t\t\t})\n\t\t\t\t\t: false,\n\t\t},\n\t\tadapter: createCustomAdapter(db),\n\t};\n\tconst adapter = createAdapterFactory(adapterOptions);\n\treturn (options: BetterAuthOptions): DBAdapter<BetterAuthOptions> => {\n\t\tlazyOptions = options;\n\t\treturn adapter(options);\n\t};\n};\n"],"mappings":";;;;;;AAwEA,MAAa,kBAAkB,IAAQ,WAAiC;CACvE,IAAI,cAAwC;CAC5C,MAAM,uBACJ,QACA,EAAE,cAAc,cAAc;EAC9B,SAAS,UAAU,OAAe;GACjC,MAAM,SAAS,OAAO,UAAU,GAAG,EAAE;AACrC,OAAI,CAAC,OACJ,OAAM,IAAI,gBACT,wHACA;GAEF,MAAM,cAAc,OAAO;AAC3B,OAAI,CAAC,YACJ,OAAM,IAAI,gBACT,mCAAmC,MAAM,+FACzC;AAEF,UAAO;;EAER,MAAM,gBAAgB,OACrB,OACA,SACA,MACA,UACI;AACJ,OAAI,OAAO,aAAa,QAEvB,SADU,MAAM,QAAQ,WAAW,EAC1B;AAEV,SAAM,QAAQ,SAAS;GACvB,MAAM,cAAc,UAAU,MAAM;GACpC,MAAM,aAAa,QAAQ,QAAQ;AACnC,OAAI,OAAO,QAAQ;IAUlB,MAAM,SAAS,mBARM,MAAM,KAAK,MAAM;AAErC,SAAI,KAAK,EAAE,WAAW,OACrB,QAAO;MAAE,GAAG;MAAG,OAAO,KAAK,EAAE;MAAQ;AAEtC,YAAO;MACN,EAE8C,MAAM;AAKtD,YAJY,MAAM,GAChB,QAAQ,CACR,KAAK,YAAY,CACjB,MAAM,GAAG,OAAO,EACP;cACD,cAAc,WAAW,IAAI,IAAI,OAAO;IAClD,IAAI,MAAM,WAAW,IAAI,IAAI;AAC7B,QAAI,CAAC,IAOJ,QALqB,MAAM,GACzB,O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AO,kBAAkB,OAAO,aAAa,KAEhE,SAAQ,IAAI,gBAAgB,IAAI,gBAAgB,IAAI;AACrD,QAAI,OAAO,UAAU,SACpB,QAAO,MACN,oKACA;KAAE;KAAK;KAAO;KAAO,CACrB;AAEF,WAAO;;GAER,SAAS;GACT;;CAEH,IAAI,iBAA+C;AACnD,kBAAiB;EAChB,QAAQ;GACP,WAAW;GACX,aAAa;GACb,WAAW,OAAO,aAAa;GAC/B,WAAW,OAAO,aAAa;GAC/B,eAAe,OAAO,aAAa,OAAO,OAAO;GACjD,cACC,OAAO,aAAa,OACjB,OACA;GACJ,gBAAgB,OAAO,aAAa,OAAO,OAAO;GAClD,aACE,OAAO,eAAe,SACnB,OACD,GAAG,aAAa,OAAW;AAK1B,WAAO,GAJS,qBAAqB;KACpC,QAAQ,eAAgB;KACxB,SAAS,oBAAoB,GAAG;KAChC,CAAC,CAAC,YAAa,CACE;KACjB,GACF;GACJ;EACD,SAAS,oBAAoB,GAAG;EAChC;CACD,MAAM,UAAU,qBAAqB,eAAe;AACpD,SAAQ,YAA6D;AACpE,gBAAc;AACd,SAAO,QAAQ,QAAQ"}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hammadj/better-auth-drizzle-adapter",
|
|
3
|
+
"version": "1.5.0-beta.9",
|
|
4
|
+
"description": "Drizzle adapter for Better Auth",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/META-DREAMER/better-auth.git",
|
|
9
|
+
"directory": "packages/drizzle-adapter"
|
|
10
|
+
},
|
|
11
|
+
"main": "./dist/index.mjs",
|
|
12
|
+
"module": "./dist/index.mjs",
|
|
13
|
+
"types": "./dist/index.d.mts",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"dev-source": "./src/index.ts",
|
|
17
|
+
"types": "./dist/index.d.mts",
|
|
18
|
+
"default": "./dist/index.mjs"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"peerDependencies": {
|
|
22
|
+
"@better-auth/utils": "^0.3.0",
|
|
23
|
+
"drizzle-orm": "^0.30.0",
|
|
24
|
+
"@hammadj/better-auth-core": "1.5.0-beta.9"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@better-auth/utils": "^0.3.1",
|
|
28
|
+
"drizzle-orm": "^0.30.0",
|
|
29
|
+
"tsdown": "^0.20.1",
|
|
30
|
+
"typescript": "^5.9.3",
|
|
31
|
+
"@hammadj/better-auth-core": "1.5.0-beta.9"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsdown",
|
|
35
|
+
"dev": "tsdown --watch",
|
|
36
|
+
"test": "vitest",
|
|
37
|
+
"typecheck": "tsc --noEmit"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { drizzleAdapter } from "./drizzle-adapter";
|
|
3
|
+
|
|
4
|
+
describe("drizzle-adapter", () => {
|
|
5
|
+
it("should create drizzle adapter", () => {
|
|
6
|
+
const db = {
|
|
7
|
+
_: {
|
|
8
|
+
fullSchema: {},
|
|
9
|
+
},
|
|
10
|
+
} as any;
|
|
11
|
+
const config = {
|
|
12
|
+
provider: "sqlite" as const,
|
|
13
|
+
};
|
|
14
|
+
const adapter = drizzleAdapter(db, config);
|
|
15
|
+
expect(adapter).toBeDefined();
|
|
16
|
+
});
|
|
17
|
+
});
|
|
@@ -0,0 +1,606 @@
|
|
|
1
|
+
import type { BetterAuthOptions } from "@better-auth/core";
|
|
2
|
+
import type {
|
|
3
|
+
AdapterFactoryCustomizeAdapterCreator,
|
|
4
|
+
AdapterFactoryOptions,
|
|
5
|
+
DBAdapter,
|
|
6
|
+
DBAdapterDebugLogOption,
|
|
7
|
+
Where,
|
|
8
|
+
} from "@better-auth/core/db/adapter";
|
|
9
|
+
import { createAdapterFactory } from "@better-auth/core/db/adapter";
|
|
10
|
+
import { logger } from "@better-auth/core/env";
|
|
11
|
+
import { BetterAuthError } from "@better-auth/core/error";
|
|
12
|
+
import type { SQL } from "drizzle-orm";
|
|
13
|
+
import {
|
|
14
|
+
and,
|
|
15
|
+
asc,
|
|
16
|
+
count,
|
|
17
|
+
desc,
|
|
18
|
+
eq,
|
|
19
|
+
gt,
|
|
20
|
+
gte,
|
|
21
|
+
inArray,
|
|
22
|
+
like,
|
|
23
|
+
lt,
|
|
24
|
+
lte,
|
|
25
|
+
ne,
|
|
26
|
+
notInArray,
|
|
27
|
+
or,
|
|
28
|
+
sql,
|
|
29
|
+
} from "drizzle-orm";
|
|
30
|
+
|
|
31
|
+
export interface DB {
|
|
32
|
+
[key: string]: any;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface DrizzleAdapterConfig {
|
|
36
|
+
/**
|
|
37
|
+
* The schema object that defines the tables and fields
|
|
38
|
+
*/
|
|
39
|
+
schema?: Record<string, any> | undefined;
|
|
40
|
+
/**
|
|
41
|
+
* The database provider
|
|
42
|
+
*/
|
|
43
|
+
provider: "pg" | "mysql" | "sqlite";
|
|
44
|
+
/**
|
|
45
|
+
* If the table names in the schema are plural
|
|
46
|
+
* set this to true. For example, if the schema
|
|
47
|
+
* has an object with a key "users" instead of "user"
|
|
48
|
+
*/
|
|
49
|
+
usePlural?: boolean | undefined;
|
|
50
|
+
/**
|
|
51
|
+
* Enable debug logs for the adapter
|
|
52
|
+
*
|
|
53
|
+
* @default false
|
|
54
|
+
*/
|
|
55
|
+
debugLogs?: DBAdapterDebugLogOption | undefined;
|
|
56
|
+
/**
|
|
57
|
+
* By default snake case is used for table and field names
|
|
58
|
+
* when the CLI is used to generate the schema. If you want
|
|
59
|
+
* to use camel case, set this to true.
|
|
60
|
+
* @default false
|
|
61
|
+
*/
|
|
62
|
+
camelCase?: boolean | undefined;
|
|
63
|
+
/**
|
|
64
|
+
* Whether to execute multiple operations in a transaction.
|
|
65
|
+
*
|
|
66
|
+
* If the database doesn't support transactions,
|
|
67
|
+
* set this to `false` and operations will be executed sequentially.
|
|
68
|
+
* @default false
|
|
69
|
+
*/
|
|
70
|
+
transaction?: boolean | undefined;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export const drizzleAdapter = (db: DB, config: DrizzleAdapterConfig) => {
|
|
74
|
+
let lazyOptions: BetterAuthOptions | null = null;
|
|
75
|
+
const createCustomAdapter =
|
|
76
|
+
(db: DB): AdapterFactoryCustomizeAdapterCreator =>
|
|
77
|
+
({ getFieldName, options }) => {
|
|
78
|
+
function getSchema(model: string) {
|
|
79
|
+
const schema = config.schema || db._.fullSchema;
|
|
80
|
+
if (!schema) {
|
|
81
|
+
throw new BetterAuthError(
|
|
82
|
+
"Drizzle adapter failed to initialize. Schema not found. Please provide a schema object in the adapter options object.",
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
const schemaModel = schema[model];
|
|
86
|
+
if (!schemaModel) {
|
|
87
|
+
throw new BetterAuthError(
|
|
88
|
+
`[# Drizzle Adapter]: The model "${model}" was not found in the schema object. Please pass the schema directly to the adapter options.`,
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
return schemaModel;
|
|
92
|
+
}
|
|
93
|
+
const withReturning = async (
|
|
94
|
+
model: string,
|
|
95
|
+
builder: any,
|
|
96
|
+
data: Record<string, any>,
|
|
97
|
+
where?: Where[] | undefined,
|
|
98
|
+
) => {
|
|
99
|
+
if (config.provider !== "mysql") {
|
|
100
|
+
const c = await builder.returning();
|
|
101
|
+
return c[0];
|
|
102
|
+
}
|
|
103
|
+
await builder.execute();
|
|
104
|
+
const schemaModel = getSchema(model);
|
|
105
|
+
const builderVal = builder.config?.values;
|
|
106
|
+
if (where?.length) {
|
|
107
|
+
// If we're updating a field that's in the where clause, use the new value
|
|
108
|
+
const updatedWhere = where.map((w) => {
|
|
109
|
+
// If this field was updated, use the new value for lookup
|
|
110
|
+
if (data[w.field] !== undefined) {
|
|
111
|
+
return { ...w, value: data[w.field] };
|
|
112
|
+
}
|
|
113
|
+
return w;
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
const clause = convertWhereClause(updatedWhere, model);
|
|
117
|
+
const res = await db
|
|
118
|
+
.select()
|
|
119
|
+
.from(schemaModel)
|
|
120
|
+
.where(...clause);
|
|
121
|
+
return res[0];
|
|
122
|
+
} else if (builderVal && builderVal[0]?.id?.value) {
|
|
123
|
+
let tId = builderVal[0]?.id?.value;
|
|
124
|
+
if (!tId) {
|
|
125
|
+
//get last inserted id
|
|
126
|
+
const lastInsertId = await db
|
|
127
|
+
.select({ id: sql`LAST_INSERT_ID()` })
|
|
128
|
+
.from(schemaModel)
|
|
129
|
+
.orderBy(desc(schemaModel.id))
|
|
130
|
+
.limit(1);
|
|
131
|
+
tId = lastInsertId[0].id;
|
|
132
|
+
}
|
|
133
|
+
const res = await db
|
|
134
|
+
.select()
|
|
135
|
+
.from(schemaModel)
|
|
136
|
+
.where(eq(schemaModel.id, tId))
|
|
137
|
+
.limit(1)
|
|
138
|
+
.execute();
|
|
139
|
+
return res[0];
|
|
140
|
+
} else if (data.id) {
|
|
141
|
+
const res = await db
|
|
142
|
+
.select()
|
|
143
|
+
.from(schemaModel)
|
|
144
|
+
.where(eq(schemaModel.id, data.id))
|
|
145
|
+
.limit(1)
|
|
146
|
+
.execute();
|
|
147
|
+
return res[0];
|
|
148
|
+
} else {
|
|
149
|
+
// If the user doesn't have `id` as a field, then this will fail.
|
|
150
|
+
// We expect that they defined `id` in all of their models.
|
|
151
|
+
if (!("id" in schemaModel)) {
|
|
152
|
+
throw new BetterAuthError(
|
|
153
|
+
`The model "${model}" does not have an "id" field. Please use the "id" field as your primary key.`,
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
const res = await db
|
|
157
|
+
.select()
|
|
158
|
+
.from(schemaModel)
|
|
159
|
+
.orderBy(desc(schemaModel.id))
|
|
160
|
+
.limit(1)
|
|
161
|
+
.execute();
|
|
162
|
+
return res[0];
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
function convertWhereClause(where: Where[], model: string) {
|
|
166
|
+
const schemaModel = getSchema(model);
|
|
167
|
+
if (!where) return [];
|
|
168
|
+
if (where.length === 1) {
|
|
169
|
+
const w = where[0];
|
|
170
|
+
if (!w) {
|
|
171
|
+
return [];
|
|
172
|
+
}
|
|
173
|
+
const field = getFieldName({ model, field: w.field });
|
|
174
|
+
if (!schemaModel[field]) {
|
|
175
|
+
throw new BetterAuthError(
|
|
176
|
+
`The field "${w.field}" does not exist in the schema for the model "${model}". Please update your schema.`,
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
if (w.operator === "in") {
|
|
180
|
+
if (!Array.isArray(w.value)) {
|
|
181
|
+
throw new BetterAuthError(
|
|
182
|
+
`The value for the field "${w.field}" must be an array when using the "in" operator.`,
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
return [inArray(schemaModel[field], w.value)];
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (w.operator === "not_in") {
|
|
189
|
+
if (!Array.isArray(w.value)) {
|
|
190
|
+
throw new BetterAuthError(
|
|
191
|
+
`The value for the field "${w.field}" must be an array when using the "not_in" operator.`,
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
return [notInArray(schemaModel[field], w.value)];
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (w.operator === "contains") {
|
|
198
|
+
return [like(schemaModel[field], `%${w.value}%`)];
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (w.operator === "starts_with") {
|
|
202
|
+
return [like(schemaModel[field], `${w.value}%`)];
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (w.operator === "ends_with") {
|
|
206
|
+
return [like(schemaModel[field], `%${w.value}`)];
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (w.operator === "lt") {
|
|
210
|
+
return [lt(schemaModel[field], w.value)];
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (w.operator === "lte") {
|
|
214
|
+
return [lte(schemaModel[field], w.value)];
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (w.operator === "ne") {
|
|
218
|
+
return [ne(schemaModel[field], w.value)];
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (w.operator === "gt") {
|
|
222
|
+
return [gt(schemaModel[field], w.value)];
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (w.operator === "gte") {
|
|
226
|
+
return [gte(schemaModel[field], w.value)];
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return [eq(schemaModel[field], w.value)];
|
|
230
|
+
}
|
|
231
|
+
const andGroup = where.filter(
|
|
232
|
+
(w) => w.connector === "AND" || !w.connector,
|
|
233
|
+
);
|
|
234
|
+
const orGroup = where.filter((w) => w.connector === "OR");
|
|
235
|
+
|
|
236
|
+
const andClause = and(
|
|
237
|
+
...andGroup.map((w) => {
|
|
238
|
+
const field = getFieldName({ model, field: w.field });
|
|
239
|
+
if (w.operator === "in") {
|
|
240
|
+
if (!Array.isArray(w.value)) {
|
|
241
|
+
throw new BetterAuthError(
|
|
242
|
+
`The value for the field "${w.field}" must be an array when using the "in" operator.`,
|
|
243
|
+
);
|
|
244
|
+
}
|
|
245
|
+
return inArray(schemaModel[field], w.value);
|
|
246
|
+
}
|
|
247
|
+
if (w.operator === "not_in") {
|
|
248
|
+
if (!Array.isArray(w.value)) {
|
|
249
|
+
throw new BetterAuthError(
|
|
250
|
+
`The value for the field "${w.field}" must be an array when using the "not_in" operator.`,
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
return notInArray(schemaModel[field], w.value);
|
|
254
|
+
}
|
|
255
|
+
if (w.operator === "contains") {
|
|
256
|
+
return like(schemaModel[field], `%${w.value}%`);
|
|
257
|
+
}
|
|
258
|
+
if (w.operator === "starts_with") {
|
|
259
|
+
return like(schemaModel[field], `${w.value}%`);
|
|
260
|
+
}
|
|
261
|
+
if (w.operator === "ends_with") {
|
|
262
|
+
return like(schemaModel[field], `%${w.value}`);
|
|
263
|
+
}
|
|
264
|
+
if (w.operator === "lt") {
|
|
265
|
+
return lt(schemaModel[field], w.value);
|
|
266
|
+
}
|
|
267
|
+
if (w.operator === "lte") {
|
|
268
|
+
return lte(schemaModel[field], w.value);
|
|
269
|
+
}
|
|
270
|
+
if (w.operator === "gt") {
|
|
271
|
+
return gt(schemaModel[field], w.value);
|
|
272
|
+
}
|
|
273
|
+
if (w.operator === "gte") {
|
|
274
|
+
return gte(schemaModel[field], w.value);
|
|
275
|
+
}
|
|
276
|
+
if (w.operator === "ne") {
|
|
277
|
+
return ne(schemaModel[field], w.value);
|
|
278
|
+
}
|
|
279
|
+
return eq(schemaModel[field], w.value);
|
|
280
|
+
}),
|
|
281
|
+
);
|
|
282
|
+
const orClause = or(
|
|
283
|
+
...orGroup.map((w) => {
|
|
284
|
+
const field = getFieldName({ model, field: w.field });
|
|
285
|
+
if (w.operator === "in") {
|
|
286
|
+
if (!Array.isArray(w.value)) {
|
|
287
|
+
throw new BetterAuthError(
|
|
288
|
+
`The value for the field "${w.field}" must be an array when using the "in" operator.`,
|
|
289
|
+
);
|
|
290
|
+
}
|
|
291
|
+
return inArray(schemaModel[field], w.value);
|
|
292
|
+
}
|
|
293
|
+
if (w.operator === "not_in") {
|
|
294
|
+
if (!Array.isArray(w.value)) {
|
|
295
|
+
throw new BetterAuthError(
|
|
296
|
+
`The value for the field "${w.field}" must be an array when using the "not_in" operator.`,
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
return notInArray(schemaModel[field], w.value);
|
|
300
|
+
}
|
|
301
|
+
if (w.operator === "contains") {
|
|
302
|
+
return like(schemaModel[field], `%${w.value}%`);
|
|
303
|
+
}
|
|
304
|
+
if (w.operator === "starts_with") {
|
|
305
|
+
return like(schemaModel[field], `${w.value}%`);
|
|
306
|
+
}
|
|
307
|
+
if (w.operator === "ends_with") {
|
|
308
|
+
return like(schemaModel[field], `%${w.value}`);
|
|
309
|
+
}
|
|
310
|
+
if (w.operator === "lt") {
|
|
311
|
+
return lt(schemaModel[field], w.value);
|
|
312
|
+
}
|
|
313
|
+
if (w.operator === "lte") {
|
|
314
|
+
return lte(schemaModel[field], w.value);
|
|
315
|
+
}
|
|
316
|
+
if (w.operator === "gt") {
|
|
317
|
+
return gt(schemaModel[field], w.value);
|
|
318
|
+
}
|
|
319
|
+
if (w.operator === "gte") {
|
|
320
|
+
return gte(schemaModel[field], w.value);
|
|
321
|
+
}
|
|
322
|
+
if (w.operator === "ne") {
|
|
323
|
+
return ne(schemaModel[field], w.value);
|
|
324
|
+
}
|
|
325
|
+
return eq(schemaModel[field], w.value);
|
|
326
|
+
}),
|
|
327
|
+
);
|
|
328
|
+
|
|
329
|
+
const clause: SQL<unknown>[] = [];
|
|
330
|
+
|
|
331
|
+
if (andGroup.length) clause.push(andClause!);
|
|
332
|
+
if (orGroup.length) clause.push(orClause!);
|
|
333
|
+
return clause;
|
|
334
|
+
}
|
|
335
|
+
function checkMissingFields(
|
|
336
|
+
schema: Record<string, any>,
|
|
337
|
+
model: string,
|
|
338
|
+
values: Record<string, any>,
|
|
339
|
+
) {
|
|
340
|
+
if (!schema) {
|
|
341
|
+
throw new BetterAuthError(
|
|
342
|
+
"Drizzle adapter failed to initialize. Drizzle Schema not found. Please provide a schema object in the adapter options object.",
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
for (const key in values) {
|
|
346
|
+
if (!schema[key]) {
|
|
347
|
+
throw new BetterAuthError(
|
|
348
|
+
`The field "${key}" does not exist in the "${model}" Drizzle schema. Please update your drizzle schema or re-generate using "npx @better-auth/cli@latest generate".`,
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
return {
|
|
355
|
+
async create({ model, data: values }) {
|
|
356
|
+
const schemaModel = getSchema(model);
|
|
357
|
+
checkMissingFields(schemaModel, model, values);
|
|
358
|
+
const builder = db.insert(schemaModel).values(values);
|
|
359
|
+
const returned = await withReturning(model, builder, values);
|
|
360
|
+
return returned;
|
|
361
|
+
},
|
|
362
|
+
async findOne({ model, where, join }) {
|
|
363
|
+
const schemaModel = getSchema(model);
|
|
364
|
+
const clause = convertWhereClause(where, model);
|
|
365
|
+
|
|
366
|
+
if (options.experimental?.joins) {
|
|
367
|
+
if (!db.query || !db.query[model]) {
|
|
368
|
+
logger.error(
|
|
369
|
+
`[# Drizzle Adapter]: The model "${model}" was not found in the query object. Please update your Drizzle schema to include relations or re-generate using "npx @better-auth/cli@latest generate".`,
|
|
370
|
+
);
|
|
371
|
+
logger.info("Falling back to regular query");
|
|
372
|
+
} else {
|
|
373
|
+
let includes:
|
|
374
|
+
| Record<string, { limit: number } | boolean>
|
|
375
|
+
| undefined;
|
|
376
|
+
|
|
377
|
+
const pluralJoinResults: string[] = [];
|
|
378
|
+
if (join) {
|
|
379
|
+
includes = {};
|
|
380
|
+
const joinEntries = Object.entries(join);
|
|
381
|
+
for (const [model, joinAttr] of joinEntries) {
|
|
382
|
+
const limit =
|
|
383
|
+
joinAttr.limit ??
|
|
384
|
+
options.advanced?.database?.defaultFindManyLimit ??
|
|
385
|
+
100;
|
|
386
|
+
const isUnique = joinAttr.relation === "one-to-one";
|
|
387
|
+
const pluralSuffix = isUnique || config.usePlural ? "" : "s";
|
|
388
|
+
includes[`${model}${pluralSuffix}`] = isUnique
|
|
389
|
+
? true
|
|
390
|
+
: { limit };
|
|
391
|
+
if (!isUnique) {
|
|
392
|
+
pluralJoinResults.push(`${model}${pluralSuffix}`);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
const query = db.query[model].findFirst({
|
|
397
|
+
where: clause[0],
|
|
398
|
+
with: includes,
|
|
399
|
+
});
|
|
400
|
+
const res = await query;
|
|
401
|
+
|
|
402
|
+
if (res) {
|
|
403
|
+
for (const pluralJoinResult of pluralJoinResults) {
|
|
404
|
+
const singularKey = !config.usePlural
|
|
405
|
+
? pluralJoinResult.slice(0, -1)
|
|
406
|
+
: pluralJoinResult;
|
|
407
|
+
res[singularKey] = res[pluralJoinResult];
|
|
408
|
+
if (pluralJoinResult !== singularKey) {
|
|
409
|
+
delete res[pluralJoinResult];
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
return res;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const query = db
|
|
418
|
+
.select()
|
|
419
|
+
.from(schemaModel)
|
|
420
|
+
.where(...clause);
|
|
421
|
+
|
|
422
|
+
const res = await query;
|
|
423
|
+
|
|
424
|
+
if (!res.length) return null;
|
|
425
|
+
return res[0];
|
|
426
|
+
},
|
|
427
|
+
async findMany({ model, where, sortBy, limit, offset, join }) {
|
|
428
|
+
const schemaModel = getSchema(model);
|
|
429
|
+
const clause = where ? convertWhereClause(where, model) : [];
|
|
430
|
+
const sortFn = sortBy?.direction === "desc" ? desc : asc;
|
|
431
|
+
|
|
432
|
+
if (options.experimental?.joins) {
|
|
433
|
+
if (!db.query[model]) {
|
|
434
|
+
logger.error(
|
|
435
|
+
`[# Drizzle Adapter]: The model "${model}" was not found in the query object. Please update your Drizzle schema to include relations or re-generate using "npx @better-auth/cli@latest generate".`,
|
|
436
|
+
);
|
|
437
|
+
logger.info("Falling back to regular query");
|
|
438
|
+
} else {
|
|
439
|
+
let includes:
|
|
440
|
+
| Record<string, { limit: number } | boolean>
|
|
441
|
+
| undefined;
|
|
442
|
+
|
|
443
|
+
const pluralJoinResults: string[] = [];
|
|
444
|
+
if (join) {
|
|
445
|
+
includes = {};
|
|
446
|
+
const joinEntries = Object.entries(join);
|
|
447
|
+
for (const [model, joinAttr] of joinEntries) {
|
|
448
|
+
const isUnique = joinAttr.relation === "one-to-one";
|
|
449
|
+
const limit =
|
|
450
|
+
joinAttr.limit ??
|
|
451
|
+
options.advanced?.database?.defaultFindManyLimit ??
|
|
452
|
+
100;
|
|
453
|
+
const pluralSuffix = isUnique || config.usePlural ? "" : "s";
|
|
454
|
+
includes[`${model}${pluralSuffix}`] = isUnique
|
|
455
|
+
? true
|
|
456
|
+
: { limit };
|
|
457
|
+
if (!isUnique)
|
|
458
|
+
pluralJoinResults.push(`${model}${pluralSuffix}`);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
let orderBy: SQL<unknown>[] | undefined = undefined;
|
|
462
|
+
if (sortBy?.field) {
|
|
463
|
+
orderBy = [
|
|
464
|
+
sortFn(
|
|
465
|
+
schemaModel[getFieldName({ model, field: sortBy?.field })],
|
|
466
|
+
),
|
|
467
|
+
];
|
|
468
|
+
}
|
|
469
|
+
const query = db.query[model].findMany({
|
|
470
|
+
where: clause[0],
|
|
471
|
+
with: includes,
|
|
472
|
+
limit: limit ?? 100,
|
|
473
|
+
offset: offset ?? 0,
|
|
474
|
+
orderBy,
|
|
475
|
+
});
|
|
476
|
+
const res = await query;
|
|
477
|
+
if (res) {
|
|
478
|
+
for (const item of res) {
|
|
479
|
+
for (const pluralJoinResult of pluralJoinResults) {
|
|
480
|
+
const singularKey = !config.usePlural
|
|
481
|
+
? pluralJoinResult.slice(0, -1)
|
|
482
|
+
: pluralJoinResult;
|
|
483
|
+
if (singularKey === pluralJoinResult) continue;
|
|
484
|
+
item[singularKey] = item[pluralJoinResult];
|
|
485
|
+
delete item[pluralJoinResult];
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
return res;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
let builder = db.select().from(schemaModel);
|
|
494
|
+
|
|
495
|
+
const effectiveLimit = limit;
|
|
496
|
+
const effectiveOffset = offset;
|
|
497
|
+
|
|
498
|
+
if (typeof effectiveLimit !== "undefined") {
|
|
499
|
+
builder = builder.limit(effectiveLimit);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
if (typeof effectiveOffset !== "undefined") {
|
|
503
|
+
builder = builder.offset(effectiveOffset);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
if (sortBy?.field) {
|
|
507
|
+
builder = builder.orderBy(
|
|
508
|
+
sortFn(
|
|
509
|
+
schemaModel[getFieldName({ model, field: sortBy?.field })],
|
|
510
|
+
),
|
|
511
|
+
);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
const res = await builder.where(...clause);
|
|
515
|
+
return res;
|
|
516
|
+
},
|
|
517
|
+
async count({ model, where }) {
|
|
518
|
+
const schemaModel = getSchema(model);
|
|
519
|
+
const clause = where ? convertWhereClause(where, model) : [];
|
|
520
|
+
const res = await db
|
|
521
|
+
.select({ count: count() })
|
|
522
|
+
.from(schemaModel)
|
|
523
|
+
.where(...clause);
|
|
524
|
+
return res[0].count;
|
|
525
|
+
},
|
|
526
|
+
async update({ model, where, update: values }) {
|
|
527
|
+
const schemaModel = getSchema(model);
|
|
528
|
+
const clause = convertWhereClause(where, model);
|
|
529
|
+
const builder = db
|
|
530
|
+
.update(schemaModel)
|
|
531
|
+
.set(values)
|
|
532
|
+
.where(...clause);
|
|
533
|
+
return await withReturning(model, builder, values as any, where);
|
|
534
|
+
},
|
|
535
|
+
async updateMany({ model, where, update: values }) {
|
|
536
|
+
const schemaModel = getSchema(model);
|
|
537
|
+
const clause = convertWhereClause(where, model);
|
|
538
|
+
const builder = db
|
|
539
|
+
.update(schemaModel)
|
|
540
|
+
.set(values)
|
|
541
|
+
.where(...clause);
|
|
542
|
+
return await builder;
|
|
543
|
+
},
|
|
544
|
+
async delete({ model, where }) {
|
|
545
|
+
const schemaModel = getSchema(model);
|
|
546
|
+
const clause = convertWhereClause(where, model);
|
|
547
|
+
const builder = db.delete(schemaModel).where(...clause);
|
|
548
|
+
return await builder;
|
|
549
|
+
},
|
|
550
|
+
async deleteMany({ model, where }) {
|
|
551
|
+
const schemaModel = getSchema(model);
|
|
552
|
+
const clause = convertWhereClause(where, model);
|
|
553
|
+
const builder = db.delete(schemaModel).where(...clause);
|
|
554
|
+
const res = await builder;
|
|
555
|
+
let count = 0;
|
|
556
|
+
if (res && "rowCount" in res) count = res.rowCount;
|
|
557
|
+
else if (Array.isArray(res)) count = res.length;
|
|
558
|
+
else if (
|
|
559
|
+
res &&
|
|
560
|
+
("affectedRows" in res || "rowsAffected" in res || "changes" in res)
|
|
561
|
+
)
|
|
562
|
+
count = res.affectedRows ?? res.rowsAffected ?? res.changes;
|
|
563
|
+
if (typeof count !== "number") {
|
|
564
|
+
logger.error(
|
|
565
|
+
"[Drizzle Adapter] The result of the deleteMany operation is not a number. This is likely a bug in the adapter. Please report this issue to the Better Auth team.",
|
|
566
|
+
{ res, model, where },
|
|
567
|
+
);
|
|
568
|
+
}
|
|
569
|
+
return count;
|
|
570
|
+
},
|
|
571
|
+
options: config,
|
|
572
|
+
};
|
|
573
|
+
};
|
|
574
|
+
let adapterOptions: AdapterFactoryOptions | null = null;
|
|
575
|
+
adapterOptions = {
|
|
576
|
+
config: {
|
|
577
|
+
adapterId: "drizzle",
|
|
578
|
+
adapterName: "Drizzle Adapter",
|
|
579
|
+
usePlural: config.usePlural ?? false,
|
|
580
|
+
debugLogs: config.debugLogs ?? false,
|
|
581
|
+
supportsUUIDs: config.provider === "pg" ? true : false,
|
|
582
|
+
supportsJSON:
|
|
583
|
+
config.provider === "pg" // even though mysql also supports it, mysql requires to pass stringified json anyway.
|
|
584
|
+
? true
|
|
585
|
+
: false,
|
|
586
|
+
supportsArrays: config.provider === "pg" ? true : false,
|
|
587
|
+
transaction:
|
|
588
|
+
(config.transaction ?? false)
|
|
589
|
+
? (cb) =>
|
|
590
|
+
db.transaction((tx: DB) => {
|
|
591
|
+
const adapter = createAdapterFactory({
|
|
592
|
+
config: adapterOptions!.config,
|
|
593
|
+
adapter: createCustomAdapter(tx),
|
|
594
|
+
})(lazyOptions!);
|
|
595
|
+
return cb(adapter);
|
|
596
|
+
})
|
|
597
|
+
: false,
|
|
598
|
+
},
|
|
599
|
+
adapter: createCustomAdapter(db),
|
|
600
|
+
};
|
|
601
|
+
const adapter = createAdapterFactory(adapterOptions);
|
|
602
|
+
return (options: BetterAuthOptions): DBAdapter<BetterAuthOptions> => {
|
|
603
|
+
lazyOptions = options;
|
|
604
|
+
return adapter(options);
|
|
605
|
+
};
|
|
606
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./drizzle-adapter";
|
package/tsconfig.json
ADDED
package/tsdown.config.ts
ADDED
package/vitest.config.ts
ADDED