@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 +13 -0
- package/LICENSE.md +20 -0
- package/dist/index.d.mts +47 -0
- package/dist/index.mjs +285 -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 +7 -0
- package/vitest.config.ts +3 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
|
|
2
|
+
> @better-auth/drizzle-adapter@1.5.0-beta.9 build /home/runner/work/better-auth/better-auth/packages/drizzle-adapter
|
|
3
|
+
> tsdown
|
|
4
|
+
|
|
5
|
+
[34mℹ[39m tsdown [2mv0.19.0[22m powered by rolldown [2mv1.0.0-beta.59[22m
|
|
6
|
+
[34mℹ[39m config file: [4m/home/runner/work/better-auth/better-auth/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 [2mdist/[22m[1mindex.mjs[22m [2m13.87 kB[22m [2m│ gzip: 2.88 kB[22m
|
|
11
|
+
[34mℹ[39m [2mdist/[22m[32m[1mindex.d.mts[22m[39m [2m 1.46 kB[22m [2m│ gzip: 0.65 kB[22m
|
|
12
|
+
[34mℹ[39m 2 files, total: 15.32 kB
|
|
13
|
+
[32m✔[39m Build complete in [32m8555ms[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,47 @@
|
|
|
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 };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
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$1) => ({ getFieldName, options }) => {
|
|
10
|
+
function getSchema(model) {
|
|
11
|
+
const schema = config.schema || db$1._.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$1.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$1.select({ id: sql`LAST_INSERT_ID()` }).from(schemaModel).orderBy(desc(schemaModel.id)).limit(1))[0].id;
|
|
34
|
+
return (await db$1.select().from(schemaModel).where(eq(schemaModel.id, tId)).limit(1).execute())[0];
|
|
35
|
+
} else if (data.id) return (await db$1.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$1.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$1.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$1.query || !db$1.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$1, 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$1}${pluralSuffix}`] = isUnique ? true : { limit };
|
|
150
|
+
if (!isUnique) pluralJoinResults.push(`${model$1}${pluralSuffix}`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
const res$1 = await db$1.query[model].findFirst({
|
|
154
|
+
where: clause[0],
|
|
155
|
+
with: includes
|
|
156
|
+
});
|
|
157
|
+
if (res$1) for (const pluralJoinResult of pluralJoinResults) {
|
|
158
|
+
const singularKey = !config.usePlural ? pluralJoinResult.slice(0, -1) : pluralJoinResult;
|
|
159
|
+
res$1[singularKey] = res$1[pluralJoinResult];
|
|
160
|
+
if (pluralJoinResult !== singularKey) delete res$1[pluralJoinResult];
|
|
161
|
+
}
|
|
162
|
+
return res$1;
|
|
163
|
+
}
|
|
164
|
+
const res = await db$1.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$1.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$1, joinAttr] of joinEntries) {
|
|
182
|
+
const isUnique = joinAttr.relation === "one-to-one";
|
|
183
|
+
const limit$1 = joinAttr.limit ?? options.advanced?.database?.defaultFindManyLimit ?? 100;
|
|
184
|
+
const pluralSuffix = isUnique || config.usePlural ? "" : "s";
|
|
185
|
+
includes[`${model$1}${pluralSuffix}`] = isUnique ? true : { limit: limit$1 };
|
|
186
|
+
if (!isUnique) pluralJoinResults.push(`${model$1}${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$1.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$1.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$1.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$1.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$1.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$1.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$1.delete(schemaModel).where(...clause);
|
|
244
|
+
let count$1 = 0;
|
|
245
|
+
if (res && "rowCount" in res) count$1 = res.rowCount;
|
|
246
|
+
else if (Array.isArray(res)) count$1 = res.length;
|
|
247
|
+
else if (res && ("affectedRows" in res || "rowsAffected" in res || "changes" in res)) count$1 = res.affectedRows ?? res.rowsAffected ?? res.changes;
|
|
248
|
+
if (typeof count$1 !== "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$1;
|
|
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 };
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@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/better-auth/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
|
+
"@better-auth/core": "1.5.0-beta.9"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@better-auth/utils": "^0.3.0",
|
|
28
|
+
"drizzle-orm": "^0.30.0",
|
|
29
|
+
"tsdown": "^0.19.0",
|
|
30
|
+
"typescript": "^5.9.3",
|
|
31
|
+
"@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