@better-auth/drizzle-adapter 1.7.3 → 1.7.5
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/dist/index.mjs
CHANGED
|
@@ -1,38 +1,9 @@
|
|
|
1
|
-
import { a as
|
|
1
|
+
import { a as insensitiveInArray, c as buildRelationKeysByModel, i as insensitiveIlike, l as getOneToOneRelationKey, o as insensitiveNe, r as insensitiveEq, s as insensitiveNotInArray, t as findDrizzleSchemaProblems } from "./schema-check-_Aj1MGau.mjs";
|
|
2
2
|
import { createAdapterFactory } from "@better-auth/core/db/adapter";
|
|
3
|
-
import { checksSchema, createSchemaCheck,
|
|
3
|
+
import { checksSchema, createSchemaCheck, registerSchemaCheck } from "@better-auth/core/db/internal";
|
|
4
4
|
import { logger } from "@better-auth/core/env";
|
|
5
5
|
import { BetterAuthError } from "@better-auth/core/error";
|
|
6
|
-
import { Column,
|
|
7
|
-
//#region src/schema-check.ts
|
|
8
|
-
/**
|
|
9
|
-
* Reads a Drizzle schema object the way the adapter addresses it: each table
|
|
10
|
-
* by the key it is exported under, each column by its property name. Values
|
|
11
|
-
* that are not tables, such as relations, are skipped.
|
|
12
|
-
*/
|
|
13
|
-
function introspectDrizzleSchema(schema) {
|
|
14
|
-
const tables = [];
|
|
15
|
-
for (const [name, table] of Object.entries(schema)) {
|
|
16
|
-
if (!is(table, Table)) continue;
|
|
17
|
-
const columns = Object.entries(getTableColumns(table)).map(([key, column]) => ({
|
|
18
|
-
name: key,
|
|
19
|
-
nullable: !column.notNull,
|
|
20
|
-
hasDefault: column.hasDefault || column.generated !== void 0 || column.generatedIdentity !== void 0
|
|
21
|
-
}));
|
|
22
|
-
tables.push({
|
|
23
|
-
name,
|
|
24
|
-
columns
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
return tables;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Compares a Drizzle schema object with the tables this configuration writes.
|
|
31
|
-
*/
|
|
32
|
-
function findDrizzleSchemaProblems(schema, options, usePlural) {
|
|
33
|
-
return diffSchema(getExpectedSchema(options, { usePlural }), introspectDrizzleSchema(schema));
|
|
34
|
-
}
|
|
35
|
-
//#endregion
|
|
6
|
+
import { Column, and, asc, count, desc, eq, gt, gte, inArray, is, isNotNull, isNull, like, lt, lte, ne, notInArray, or, sql } from "drizzle-orm";
|
|
36
7
|
//#region src/drizzle-adapter.ts
|
|
37
8
|
/**
|
|
38
9
|
* Derive the number of affected rows from a Drizzle write result.
|
|
@@ -78,7 +49,8 @@ function hasDriverRowCount(result) {
|
|
|
78
49
|
const drizzleAdapter = (db, config) => {
|
|
79
50
|
let lazyOptions = null;
|
|
80
51
|
let mysqlNoIdWarned = false;
|
|
81
|
-
|
|
52
|
+
let relationKeysByModel;
|
|
53
|
+
const getRelationKeysByModel = () => relationKeysByModel ??= buildRelationKeysByModel(db._?.schema);
|
|
82
54
|
const createCustomAdapter = (db, inTransaction = false) => ({ getFieldName, getDefaultFieldName, getDefaultModelName, options, schema: baSchema }) => {
|
|
83
55
|
if (config.provider === "mysql" && options.advanced?.database?.generateId === false && !mysqlNoIdWarned) {
|
|
84
56
|
mysqlNoIdWarned = true;
|
|
@@ -350,7 +322,7 @@ const drizzleAdapter = (db, config) => {
|
|
|
350
322
|
} else {
|
|
351
323
|
let includes;
|
|
352
324
|
const renamedJoinResults = [];
|
|
353
|
-
const relationKeys =
|
|
325
|
+
const relationKeys = getRelationKeysByModel().get(queryModel);
|
|
354
326
|
includes = {};
|
|
355
327
|
const joinEntries = Object.entries(join);
|
|
356
328
|
for (const [joinModel, joinAttr] of joinEntries) {
|
|
@@ -406,7 +378,7 @@ const drizzleAdapter = (db, config) => {
|
|
|
406
378
|
} else {
|
|
407
379
|
let includes;
|
|
408
380
|
const renamedJoinResults = [];
|
|
409
|
-
const relationKeys =
|
|
381
|
+
const relationKeys = getRelationKeysByModel().get(queryModel);
|
|
410
382
|
includes = {};
|
|
411
383
|
const joinEntries = Object.entries(join);
|
|
412
384
|
for (const [joinModel, joinAttr] of joinEntries) {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { a as
|
|
1
|
+
import { a as insensitiveInArray, c as buildRelationKeysByModel, l as getOneToOneRelationKey, n as escapedLike, o as insensitiveNe, r as insensitiveEq, s as insensitiveNotInArray, t as findDrizzleSchemaProblems } from "../schema-check-_Aj1MGau.mjs";
|
|
2
2
|
import { createAdapterFactory } from "@better-auth/core/db/adapter";
|
|
3
|
+
import { checksSchema, createSchemaCheck, registerSchemaCheck } from "@better-auth/core/db/internal";
|
|
3
4
|
import { logger } from "@better-auth/core/env";
|
|
4
5
|
import { BetterAuthError } from "@better-auth/core/error";
|
|
5
6
|
import { and, asc, count, desc, eq, gt, gte, inArray, isNotNull, isNull, lt, lte, ne, notInArray, or, sql } from "drizzle-orm";
|
|
@@ -72,7 +73,8 @@ function applyWhereOperator(column, w, fieldLabel, provider) {
|
|
|
72
73
|
const drizzleAdapter = (db, config) => {
|
|
73
74
|
let lazyOptions = null;
|
|
74
75
|
let mysqlNoIdWarned = false;
|
|
75
|
-
|
|
76
|
+
let relationKeysByModel;
|
|
77
|
+
const getRelationKeysByModel = () => relationKeysByModel ??= buildRelationKeysByModel(db._?.relations);
|
|
76
78
|
const createCustomAdapter = (db, inTransaction = false) => ({ getFieldName, getDefaultModelName, options, schema: baSchema }) => {
|
|
77
79
|
if (config.provider === "mysql" && options.advanced?.database?.generateId === false && !mysqlNoIdWarned) {
|
|
78
80
|
mysqlNoIdWarned = true;
|
|
@@ -273,7 +275,7 @@ const drizzleAdapter = (db, config) => {
|
|
|
273
275
|
}
|
|
274
276
|
function checkMissingFields(schema, model, values) {
|
|
275
277
|
if (!schema) throw new BetterAuthError("Drizzle adapter failed to initialize. Drizzle Schema not found. Please provide a schema object in the adapter options object.");
|
|
276
|
-
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
|
|
278
|
+
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 auth@latest generate".`);
|
|
277
279
|
}
|
|
278
280
|
return {
|
|
279
281
|
async create({ model, data: values }) {
|
|
@@ -287,12 +289,12 @@ const drizzleAdapter = (db, config) => {
|
|
|
287
289
|
if (join) {
|
|
288
290
|
const queryModel = getQueryModel(model);
|
|
289
291
|
if (!db.query || !queryModel) {
|
|
290
|
-
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
|
|
292
|
+
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 auth@latest generate".`);
|
|
291
293
|
logger.info("Falling back to regular query");
|
|
292
294
|
} else {
|
|
293
295
|
let includes;
|
|
294
296
|
const renamedJoinResults = [];
|
|
295
|
-
const relationKeys =
|
|
297
|
+
const relationKeys = getRelationKeysByModel().get(queryModel);
|
|
296
298
|
includes = {};
|
|
297
299
|
const joinEntries = Object.entries(join);
|
|
298
300
|
for (const [joinModel, joinAttr] of joinEntries) {
|
|
@@ -344,12 +346,12 @@ const drizzleAdapter = (db, config) => {
|
|
|
344
346
|
if (join) {
|
|
345
347
|
const queryModel = getQueryModel(model);
|
|
346
348
|
if (!db.query || !queryModel) {
|
|
347
|
-
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
|
|
349
|
+
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 auth@latest generate".`);
|
|
348
350
|
logger.info("Falling back to regular query");
|
|
349
351
|
} else {
|
|
350
352
|
let includes;
|
|
351
353
|
const renamedJoinResults = [];
|
|
352
|
-
const relationKeys =
|
|
354
|
+
const relationKeys = getRelationKeysByModel().get(queryModel);
|
|
353
355
|
includes = {};
|
|
354
356
|
const joinEntries = Object.entries(join);
|
|
355
357
|
for (const [joinModel, joinAttr] of joinEntries) {
|
|
@@ -544,7 +546,16 @@ const drizzleAdapter = (db, config) => {
|
|
|
544
546
|
const adapter = createAdapterFactory(adapterOptions);
|
|
545
547
|
return (options) => {
|
|
546
548
|
lazyOptions = options;
|
|
547
|
-
|
|
549
|
+
const instance = adapter(options);
|
|
550
|
+
if (checksSchema(options)) registerSchemaCheck(instance, createSchemaCheck(async () => {
|
|
551
|
+
const relations = db._?.relations ?? {};
|
|
552
|
+
return findDrizzleSchemaProblems({
|
|
553
|
+
...db._?.fullSchema,
|
|
554
|
+
...Object.fromEntries(Object.entries(relations).map(([name, relation]) => [name, relation.table])),
|
|
555
|
+
...config.schema
|
|
556
|
+
}, options, config.usePlural);
|
|
557
|
+
}, "drizzle"));
|
|
558
|
+
return instance;
|
|
548
559
|
};
|
|
549
560
|
};
|
|
550
561
|
//#endregion
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { diffSchema, getExpectedSchema } from "@better-auth/core/db/internal";
|
|
2
|
+
import { Table, getTableColumns, ilike, is, sql } from "drizzle-orm";
|
|
2
3
|
//#region src/join-relation-key.ts
|
|
3
4
|
/**
|
|
4
5
|
* Reads the relation registry used to build Drizzle relational queries.
|
|
@@ -75,4 +76,33 @@ function insensitiveNe(column, value) {
|
|
|
75
76
|
return sql`LOWER(${column}) <> LOWER(${value})`;
|
|
76
77
|
}
|
|
77
78
|
//#endregion
|
|
78
|
-
|
|
79
|
+
//#region src/schema-check.ts
|
|
80
|
+
/**
|
|
81
|
+
* Reads a Drizzle schema object the way the adapter addresses it: each table
|
|
82
|
+
* by the key it is exported under, each column by its property name. Values
|
|
83
|
+
* that are not tables, such as relations, are skipped.
|
|
84
|
+
*/
|
|
85
|
+
function introspectDrizzleSchema(schema) {
|
|
86
|
+
const tables = [];
|
|
87
|
+
for (const [name, table] of Object.entries(schema)) {
|
|
88
|
+
if (!is(table, Table)) continue;
|
|
89
|
+
const columns = Object.entries(getTableColumns(table)).map(([key, column]) => ({
|
|
90
|
+
name: key,
|
|
91
|
+
nullable: !column.notNull,
|
|
92
|
+
hasDefault: column.hasDefault || column.generated !== void 0 || column.generatedIdentity !== void 0
|
|
93
|
+
}));
|
|
94
|
+
tables.push({
|
|
95
|
+
name,
|
|
96
|
+
columns
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
return tables;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Compares a Drizzle schema object with the tables this configuration writes.
|
|
103
|
+
*/
|
|
104
|
+
function findDrizzleSchemaProblems(schema, options, usePlural) {
|
|
105
|
+
return diffSchema(getExpectedSchema(options, { usePlural }), introspectDrizzleSchema(schema));
|
|
106
|
+
}
|
|
107
|
+
//#endregion
|
|
108
|
+
export { insensitiveInArray as a, buildRelationKeysByModel as c, insensitiveIlike as i, getOneToOneRelationKey as l, escapedLike as n, insensitiveNe as o, insensitiveEq as r, insensitiveNotInArray as s, findDrizzleSchemaProblems as t };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@better-auth/drizzle-adapter",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.5",
|
|
4
4
|
"bugs": {
|
|
5
5
|
"url": "https://github.com/better-auth/better-auth/issues"
|
|
6
6
|
},
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"peerDependencies": {
|
|
46
46
|
"@better-auth/utils": "0.4.2",
|
|
47
47
|
"drizzle-orm": "^0.45.2 || >=1.0.0-rc.1 <2.0.0",
|
|
48
|
-
"@better-auth/core": "^1.7.
|
|
48
|
+
"@better-auth/core": "^1.7.5"
|
|
49
49
|
},
|
|
50
50
|
"peerDependenciesMeta": {
|
|
51
51
|
"drizzle-orm": {
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"drizzle-orm": "^0.45.2",
|
|
58
58
|
"tsdown": "0.21.10",
|
|
59
59
|
"typescript": "^6.0.3",
|
|
60
|
-
"@better-auth/core": "1.7.
|
|
60
|
+
"@better-auth/core": "1.7.5"
|
|
61
61
|
},
|
|
62
62
|
"scripts": {
|
|
63
63
|
"build": "tsdown",
|