@better-auth/prisma-adapter 1.7.0-beta.1 → 1.7.0-beta.10

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 +112 -10
  2. package/package.json +5 -5
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";
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;
@@ -258,10 +261,15 @@ const prismaAdapter = (prisma, config) => {
258
261
  where,
259
262
  action: "update"
260
263
  });
261
- return await db[model].update({
262
- where: whereClause,
263
- data: update
264
- });
264
+ try {
265
+ return await db[model].update({
266
+ where: whereClause,
267
+ data: update
268
+ });
269
+ } catch (e) {
270
+ if (isPrismaNotFoundError(e)) return null;
271
+ throw e;
272
+ }
265
273
  },
266
274
  async updateMany({ model, where, update }) {
267
275
  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'`);
@@ -295,9 +303,8 @@ const prismaAdapter = (prisma, config) => {
295
303
  try {
296
304
  await db[model].delete({ where: whereClause });
297
305
  } catch (e) {
298
- if (e?.meta?.cause === "Record to delete does not exist.") return;
299
- if (e?.code === "P2025") return;
300
- console.log(e);
306
+ if (isPrismaNotFoundError(e)) return;
307
+ throw e;
301
308
  }
302
309
  },
303
310
  async deleteMany({ model, where }) {
@@ -309,6 +316,98 @@ const prismaAdapter = (prisma, config) => {
309
316
  const result = await db[model].deleteMany({ where: whereClause });
310
317
  return result ? result.count : 0;
311
318
  },
319
+ async consumeOne({ model, where }) {
320
+ 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'`);
321
+ if (where?.some((w) => w.field === "id")) {
322
+ const whereClause = convertWhereClause({
323
+ model,
324
+ where,
325
+ action: "delete"
326
+ });
327
+ try {
328
+ return await db[model].delete({ where: whereClause }) ?? null;
329
+ } catch (e) {
330
+ if (isPrismaNotFoundError(e)) return null;
331
+ throw e;
332
+ }
333
+ }
334
+ const findWhere = convertWhereClause({
335
+ model,
336
+ where,
337
+ action: "findOne"
338
+ });
339
+ const claimFromTransaction = async (tx) => {
340
+ const target = await tx[model].findFirst({ where: findWhere });
341
+ if (!target) return null;
342
+ try {
343
+ return (await tx[model].deleteMany({ where: convertWhereClause({
344
+ model,
345
+ where: [...where ?? [], {
346
+ field: "id",
347
+ value: target.id,
348
+ operator: "eq",
349
+ connector: "AND",
350
+ mode: "sensitive"
351
+ }],
352
+ action: "deleteMany"
353
+ }) }))?.count > 0 ? target : null;
354
+ } catch (e) {
355
+ if (isPrismaNotFoundError(e)) return null;
356
+ throw e;
357
+ }
358
+ };
359
+ return inTransaction || typeof db.$transaction !== "function" ? claimFromTransaction(db) : db.$transaction(claimFromTransaction);
360
+ },
361
+ async incrementOne({ model, where, increment, set }) {
362
+ 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'`);
363
+ const data = { ...set ?? {} };
364
+ for (const [field, delta] of Object.entries(increment)) data[field] = { increment: delta };
365
+ if (where?.some((w) => w.field === "id")) {
366
+ const whereClause = convertWhereClause({
367
+ model,
368
+ where,
369
+ action: "update"
370
+ });
371
+ try {
372
+ return await db[model].update({
373
+ where: whereClause,
374
+ data
375
+ }) ?? null;
376
+ } catch (e) {
377
+ if (isPrismaNotFoundError(e)) return null;
378
+ throw e;
379
+ }
380
+ }
381
+ const findWhere = convertWhereClause({
382
+ model,
383
+ where,
384
+ action: "findOne"
385
+ });
386
+ const mutateInTransaction = async (tx) => {
387
+ const target = await tx[model].findFirst({ where: findWhere });
388
+ if (!target) return null;
389
+ try {
390
+ return await tx[model].update({
391
+ where: convertWhereClause({
392
+ model,
393
+ where: [...where, {
394
+ field: "id",
395
+ value: target.id,
396
+ operator: "eq",
397
+ connector: "AND",
398
+ mode: "sensitive"
399
+ }],
400
+ action: "update"
401
+ }),
402
+ data
403
+ }) ?? null;
404
+ } catch (e) {
405
+ if (isPrismaNotFoundError(e)) return null;
406
+ throw e;
407
+ }
408
+ };
409
+ return inTransaction || typeof db.$transaction !== "function" ? mutateInTransaction(db) : db.$transaction(mutateInTransaction);
410
+ },
312
411
  options: config
313
412
  };
314
413
  };
@@ -323,8 +422,11 @@ const prismaAdapter = (prisma, config) => {
323
422
  supportsArrays: config.provider === "postgresql" || config.provider === "mongodb" ? true : false,
324
423
  transaction: config.transaction ?? false ? (cb) => prisma.$transaction((tx) => {
325
424
  return cb(createAdapterFactory({
326
- config: adapterOptions.config,
327
- adapter: createCustomAdapter(tx)
425
+ config: {
426
+ ...adapterOptions.config,
427
+ transaction: false
428
+ },
429
+ adapter: createCustomAdapter(tx, true)
328
430
  })(lazyOptions));
329
431
  }) : false
330
432
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@better-auth/prisma-adapter",
3
- "version": "1.7.0-beta.1",
3
+ "version": "1.7.0-beta.10",
4
4
  "description": "Prisma adapter for Better Auth",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -35,10 +35,10 @@
35
35
  }
36
36
  },
37
37
  "peerDependencies": {
38
- "@better-auth/utils": "0.4.0",
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.0-beta.1"
41
+ "@better-auth/core": "^1.7.0-beta.10"
42
42
  },
43
43
  "peerDependenciesMeta": {
44
44
  "@prisma/client": {
@@ -49,10 +49,10 @@
49
49
  }
50
50
  },
51
51
  "devDependencies": {
52
- "@better-auth/utils": "0.4.0",
52
+ "@better-auth/utils": "0.4.2",
53
53
  "tsdown": "0.21.1",
54
54
  "typescript": "^5.9.3",
55
- "@better-auth/core": "1.7.0-beta.1"
55
+ "@better-auth/core": "1.7.0-beta.10"
56
56
  },
57
57
  "scripts": {
58
58
  "build": "tsdown",