@better-auth/drizzle-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 +29 -3
  2. package/package.json +3 -3
package/dist/index.mjs CHANGED
@@ -40,7 +40,7 @@ function insensitiveNe(column, value) {
40
40
  //#region src/drizzle-adapter.ts
41
41
  const drizzleAdapter = (db, config) => {
42
42
  let lazyOptions = null;
43
- const createCustomAdapter = (db) => ({ getFieldName, getDefaultFieldName, options }) => {
43
+ const createCustomAdapter = (db, inTransaction = false) => ({ getFieldName, getDefaultFieldName, options }) => {
44
44
  function getSchema(model) {
45
45
  const schema = config.schema || db._.fullSchema;
46
46
  if (!schema) throw new BetterAuthError("Drizzle adapter failed to initialize. Schema not found. Please provide a schema object in the adapter options object.");
@@ -419,6 +419,29 @@ const drizzleAdapter = (db, config) => {
419
419
  });
420
420
  return count;
421
421
  },
422
+ async consumeOne({ model, where }) {
423
+ const schemaModel = getSchema(model);
424
+ const clause = convertWhereClause(where, model);
425
+ const idField = getFieldName({
426
+ model,
427
+ field: "id"
428
+ });
429
+ const idColumn = schemaModel[idField];
430
+ if (config.provider === "mysql") {
431
+ const claimFromTransaction = async (tx) => {
432
+ const target = (await tx.select().from(schemaModel).where(...clause).for("update").limit(1))[0];
433
+ if (!target) return null;
434
+ const targetId = target[idField] ?? target.id;
435
+ if (targetId === void 0 || targetId === null || !idColumn) return null;
436
+ const delRes = await tx.delete(schemaModel).where(eq(idColumn, targetId)).execute();
437
+ return ((delRes && (delRes.rowsAffected ?? delRes.affectedRows ?? delRes.changes)) ?? 0) > 0 ? target : null;
438
+ };
439
+ return inTransaction ? claimFromTransaction(db) : db.transaction(claimFromTransaction);
440
+ }
441
+ if (!idColumn) return null;
442
+ const targetIds = db.select({ id: idColumn }).from(schemaModel).where(...clause).limit(1);
443
+ return (await db.delete(schemaModel).where(inArray(idColumn, targetIds)).returning())[0] ?? null;
444
+ },
422
445
  options: config
423
446
  };
424
447
  };
@@ -441,8 +464,11 @@ const drizzleAdapter = (db, config) => {
441
464
  },
442
465
  transaction: config.transaction ?? false ? (cb) => db.transaction((tx) => {
443
466
  return cb(createAdapterFactory({
444
- config: adapterOptions.config,
445
- adapter: createCustomAdapter(tx)
467
+ config: {
468
+ ...adapterOptions.config,
469
+ transaction: false
470
+ },
471
+ adapter: createCustomAdapter(tx, true)
446
472
  })(lazyOptions));
447
473
  }) : false
448
474
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@better-auth/drizzle-adapter",
3
- "version": "1.6.10",
3
+ "version": "1.6.11",
4
4
  "description": "Drizzle adapter for Better Auth",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -37,7 +37,7 @@
37
37
  "peerDependencies": {
38
38
  "@better-auth/utils": "0.4.0",
39
39
  "drizzle-orm": "^0.45.2",
40
- "@better-auth/core": "^1.6.10"
40
+ "@better-auth/core": "^1.6.11"
41
41
  },
42
42
  "peerDependenciesMeta": {
43
43
  "drizzle-orm": {
@@ -49,7 +49,7 @@
49
49
  "drizzle-orm": "^0.45.2",
50
50
  "tsdown": "0.21.1",
51
51
  "typescript": "^5.9.3",
52
- "@better-auth/core": "1.6.10"
52
+ "@better-auth/core": "1.6.11"
53
53
  },
54
54
  "scripts": {
55
55
  "build": "tsdown",