@better-auth/drizzle-adapter 1.7.2 → 1.7.4

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,5 +1,6 @@
1
- import { a as insensitiveNe, c as getOneToOneRelationKey, i as insensitiveInArray, n as insensitiveEq, o as insensitiveNotInArray, r as insensitiveIlike, s as buildRelationKeysByModel } from "./query-builders-CBLMSM7v.mjs";
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, 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 { Column, and, asc, count, desc, eq, gt, gte, inArray, is, isNotNull, isNull, like, lt, lte, ne, notInArray, or, sql } from "drizzle-orm";
@@ -567,7 +568,9 @@ const drizzleAdapter = (db, config) => {
567
568
  const adapter = createAdapterFactory(adapterOptions);
568
569
  return (options) => {
569
570
  lazyOptions = options;
570
- return adapter(options);
571
+ const instance = adapter(options);
572
+ if (checksSchema(options)) registerSchemaCheck(instance, createSchemaCheck(async () => findDrizzleSchemaProblems(config.schema ?? db._?.fullSchema ?? {}, options, config.usePlural), "drizzle"));
573
+ return instance;
571
574
  };
572
575
  };
573
576
  //#endregion
@@ -1,5 +1,6 @@
1
- import { a as insensitiveNe, c as getOneToOneRelationKey, i as insensitiveInArray, n as insensitiveEq, o as insensitiveNotInArray, s as buildRelationKeysByModel, t as escapedLike } from "../query-builders-CBLMSM7v.mjs";
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";
@@ -273,7 +274,7 @@ const drizzleAdapter = (db, config) => {
273
274
  }
274
275
  function checkMissingFields(schema, model, values) {
275
276
  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 @better-auth/cli@latest generate".`);
277
+ 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
278
  }
278
279
  return {
279
280
  async create({ model, data: values }) {
@@ -287,7 +288,7 @@ const drizzleAdapter = (db, config) => {
287
288
  if (join) {
288
289
  const queryModel = getQueryModel(model);
289
290
  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 @better-auth/cli@latest generate".`);
291
+ 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
292
  logger.info("Falling back to regular query");
292
293
  } else {
293
294
  let includes;
@@ -344,7 +345,7 @@ const drizzleAdapter = (db, config) => {
344
345
  if (join) {
345
346
  const queryModel = getQueryModel(model);
346
347
  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 @better-auth/cli@latest generate".`);
348
+ 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
349
  logger.info("Falling back to regular query");
349
350
  } else {
350
351
  let includes;
@@ -506,7 +507,7 @@ const drizzleAdapter = (db, config) => {
506
507
  return (await db.update(schemaModel).set(assignments).where(inArray(idColumn, targetIds)).returning())[0] ?? null;
507
508
  },
508
509
  async createSchema(props) {
509
- const { generateDrizzleSchema } = await import("../generate-drizzle-schema-huQqmolx.mjs");
510
+ const { generateDrizzleSchema } = await import("../generate-drizzle-schema-iWvrXnu0.mjs");
510
511
  return await generateDrizzleSchema({
511
512
  adapterConfig: config,
512
513
  options,
@@ -544,7 +545,16 @@ const drizzleAdapter = (db, config) => {
544
545
  const adapter = createAdapterFactory(adapterOptions);
545
546
  return (options) => {
546
547
  lazyOptions = options;
547
- return adapter(options);
548
+ const instance = adapter(options);
549
+ if (checksSchema(options)) registerSchemaCheck(instance, createSchemaCheck(async () => {
550
+ const relations = db._?.relations ?? {};
551
+ return findDrizzleSchemaProblems({
552
+ ...db._?.fullSchema,
553
+ ...Object.fromEntries(Object.entries(relations).map(([name, relation]) => [name, relation.table])),
554
+ ...config.schema
555
+ }, options, config.usePlural);
556
+ }, "drizzle"));
557
+ return instance;
548
558
  };
549
559
  };
550
560
  //#endregion
@@ -1,4 +1,5 @@
1
- import { ilike, sql } from "drizzle-orm";
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
- export { insensitiveNe as a, getOneToOneRelationKey as c, insensitiveInArray as i, insensitiveEq as n, insensitiveNotInArray as o, insensitiveIlike as r, buildRelationKeysByModel as s, escapedLike as t };
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.2",
3
+ "version": "1.7.4",
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.2"
48
+ "@better-auth/core": "^1.7.4"
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.2"
60
+ "@better-auth/core": "1.7.4"
61
61
  },
62
62
  "scripts": {
63
63
  "build": "tsdown",
@@ -1,8 +1,8 @@
1
1
  import { initGetFieldName, initGetModelName } from "@better-auth/core/db/adapter";
2
+ import { getDatabaseFieldIndexName, getDatabaseIndexStringLength, resolveDatabaseSchemaIndexes } from "@better-auth/core/db/internal";
2
3
  import { existsSync } from "node:fs";
3
4
  import path from "node:path";
4
5
  import { getAuthTables } from "@better-auth/core/db";
5
- import { getDatabaseFieldIndexName, getDatabaseIndexStringLength, resolveDatabaseSchemaIndexes } from "@better-auth/core/db/internal";
6
6
  //#region src/relations-v2/generate-drizzle-schema.ts
7
7
  function convertToSnakeCase(str, camelCase) {
8
8
  if (camelCase) return str;