@better-auth/prisma-adapter 1.7.2 → 1.7.3

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.
Files changed (2) hide show
  1. package/dist/index.mjs +44 -1
  2. package/package.json +3 -3
package/dist/index.mjs CHANGED
@@ -1,5 +1,45 @@
1
1
  import { createAdapterFactory } from "@better-auth/core/db/adapter";
2
+ import { checksSchema, createSchemaCheck, diffSchema, getExpectedSchema, registerSchemaCheck } from "@better-auth/core/db/internal";
2
3
  import { BetterAuthError } from "@better-auth/core/error";
4
+ //#region src/schema-check.ts
5
+ /**
6
+ * The data model a generated client carries, or nothing for a client that
7
+ * predates it or stands in for one.
8
+ */
9
+ function readPrismaDataModel(client) {
10
+ const dataModel = client._runtimeDataModel;
11
+ return typeof dataModel === "object" && dataModel !== null && "models" in dataModel ? dataModel : void 0;
12
+ }
13
+ /**
14
+ * Prisma exposes the model `Account` as `prisma.account`.
15
+ */
16
+ function clientProperty(model) {
17
+ return model.charAt(0).toLowerCase() + model.slice(1);
18
+ }
19
+ /**
20
+ * Reads a generated client's data model the way the adapter addresses it:
21
+ * each model by its client property, each field by name. Relation fields are
22
+ * not columns and are skipped. A field whose model does not say whether it
23
+ * is required is taken as one an insert may omit, so a compact model reports
24
+ * missing tables and columns but never a required column.
25
+ */
26
+ function introspectPrismaDataModel(dataModel) {
27
+ return Object.entries(dataModel.models).map(([model, { fields }]) => ({
28
+ name: clientProperty(model),
29
+ columns: fields.filter((field) => field.kind !== "object").map((field) => ({
30
+ name: field.name,
31
+ nullable: field.isRequired !== true,
32
+ hasDefault: field.hasDefaultValue === true || field.isUpdatedAt === true
33
+ }))
34
+ }));
35
+ }
36
+ /**
37
+ * Compares a generated client's data model with the tables this configuration writes.
38
+ */
39
+ function findPrismaSchemaProblems(dataModel, options, usePlural) {
40
+ return diffSchema(getExpectedSchema(options, { usePlural }), introspectPrismaDataModel(dataModel));
41
+ }
42
+ //#endregion
3
43
  //#region src/prisma-adapter.ts
4
44
  function isPrismaNotFoundError(e) {
5
45
  return e?.code === "P2025";
@@ -435,7 +475,10 @@ const prismaAdapter = (prisma, config) => {
435
475
  const adapter = createAdapterFactory(adapterOptions);
436
476
  return (options) => {
437
477
  lazyOptions = options;
438
- return adapter(options);
478
+ const instance = adapter(options);
479
+ const dataModel = readPrismaDataModel(prisma);
480
+ if (dataModel && checksSchema(options)) registerSchemaCheck(instance, createSchemaCheck(async () => findPrismaSchemaProblems(dataModel, options, config.usePlural), "prisma"));
481
+ return instance;
439
482
  };
440
483
  };
441
484
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@better-auth/prisma-adapter",
3
- "version": "1.7.2",
3
+ "version": "1.7.3",
4
4
  "description": "Prisma adapter for Better Auth",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -38,7 +38,7 @@
38
38
  "@better-auth/utils": "0.4.2",
39
39
  "@prisma/client": "^5.0.0 || ^6.0.0 || ^7.0.0",
40
40
  "prisma": "^5.0.0 || ^6.0.0 || ^7.0.0",
41
- "@better-auth/core": "^1.7.2"
41
+ "@better-auth/core": "^1.7.3"
42
42
  },
43
43
  "peerDependenciesMeta": {
44
44
  "@prisma/client": {
@@ -52,7 +52,7 @@
52
52
  "@better-auth/utils": "0.4.2",
53
53
  "tsdown": "0.21.10",
54
54
  "typescript": "^6.0.3",
55
- "@better-auth/core": "1.7.2"
55
+ "@better-auth/core": "1.7.3"
56
56
  },
57
57
  "scripts": {
58
58
  "build": "tsdown",