@better-auth/prisma-adapter 1.6.10 → 1.6.11

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 +52 -5
  2. package/package.json +3 -3
package/dist/index.mjs CHANGED
@@ -1,9 +1,12 @@
1
1
  import { createAdapterFactory } from "@better-auth/core/db/adapter";
2
2
  import { BetterAuthError } from "@better-auth/core/error";
3
3
  //#region src/prisma-adapter.ts
4
+ function isPrismaNotFoundError(e) {
5
+ return e?.code === "P2025" || e?.meta?.cause === "Record to delete does not exist.";
6
+ }
4
7
  const prismaAdapter = (prisma, config) => {
5
8
  let lazyOptions = null;
6
- const createCustomAdapter = (prisma) => ({ getFieldName, getModelName, getFieldAttributes, getDefaultModelName, schema }) => {
9
+ const createCustomAdapter = (prisma, inTransaction = false) => ({ getFieldName, getModelName, getFieldAttributes, getDefaultModelName, schema }) => {
7
10
  const db = prisma;
8
11
  const convertSelect = (select, model, join) => {
9
12
  if (!select && !join) return void 0;
@@ -295,8 +298,7 @@ const prismaAdapter = (prisma, config) => {
295
298
  try {
296
299
  await db[model].delete({ where: whereClause });
297
300
  } catch (e) {
298
- if (e?.meta?.cause === "Record to delete does not exist.") return;
299
- if (e?.code === "P2025") return;
301
+ if (isPrismaNotFoundError(e)) return;
300
302
  console.log(e);
301
303
  }
302
304
  },
@@ -309,6 +311,48 @@ const prismaAdapter = (prisma, config) => {
309
311
  const result = await db[model].deleteMany({ where: whereClause });
310
312
  return result ? result.count : 0;
311
313
  },
314
+ async consumeOne({ model, where }) {
315
+ if (!db[model]) throw new BetterAuthError(`Model ${model} does not exist in the database. If you haven't generated the Prisma client, you need to run 'npx prisma generate'`);
316
+ if (where?.some((w) => w.field === "id")) {
317
+ const whereClause = convertWhereClause({
318
+ model,
319
+ where,
320
+ action: "delete"
321
+ });
322
+ try {
323
+ return await db[model].delete({ where: whereClause }) ?? null;
324
+ } catch (e) {
325
+ if (isPrismaNotFoundError(e)) return null;
326
+ throw e;
327
+ }
328
+ }
329
+ const findWhere = convertWhereClause({
330
+ model,
331
+ where,
332
+ action: "findOne"
333
+ });
334
+ const claimFromTransaction = async (tx) => {
335
+ const target = await tx[model].findFirst({ where: findWhere });
336
+ if (!target) return null;
337
+ try {
338
+ return (await tx[model].deleteMany({ where: convertWhereClause({
339
+ model,
340
+ where: [...where ?? [], {
341
+ field: "id",
342
+ value: target.id,
343
+ operator: "eq",
344
+ connector: "AND",
345
+ mode: "sensitive"
346
+ }],
347
+ action: "deleteMany"
348
+ }) }))?.count > 0 ? target : null;
349
+ } catch (e) {
350
+ if (isPrismaNotFoundError(e)) return null;
351
+ throw e;
352
+ }
353
+ };
354
+ return inTransaction || typeof db.$transaction !== "function" ? claimFromTransaction(db) : db.$transaction(claimFromTransaction);
355
+ },
312
356
  options: config
313
357
  };
314
358
  };
@@ -323,8 +367,11 @@ const prismaAdapter = (prisma, config) => {
323
367
  supportsArrays: config.provider === "postgresql" || config.provider === "mongodb" ? true : false,
324
368
  transaction: config.transaction ?? false ? (cb) => prisma.$transaction((tx) => {
325
369
  return cb(createAdapterFactory({
326
- config: adapterOptions.config,
327
- adapter: createCustomAdapter(tx)
370
+ config: {
371
+ ...adapterOptions.config,
372
+ transaction: false
373
+ },
374
+ adapter: createCustomAdapter(tx, true)
328
375
  })(lazyOptions));
329
376
  }) : false
330
377
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@better-auth/prisma-adapter",
3
- "version": "1.6.10",
3
+ "version": "1.6.11",
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.0",
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.6.10"
41
+ "@better-auth/core": "^1.6.11"
42
42
  },
43
43
  "peerDependenciesMeta": {
44
44
  "@prisma/client": {
@@ -52,7 +52,7 @@
52
52
  "@better-auth/utils": "0.4.0",
53
53
  "tsdown": "0.21.1",
54
54
  "typescript": "^5.9.3",
55
- "@better-auth/core": "1.6.10"
55
+ "@better-auth/core": "1.6.11"
56
56
  },
57
57
  "scripts": {
58
58
  "build": "tsdown",