@better-auth/kysely-adapter 1.6.9 → 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 +43 -3
  2. package/package.json +5 -5
package/dist/index.mjs CHANGED
@@ -124,7 +124,7 @@ function insensitiveNe(columnRef, value) {
124
124
  //#region src/kysely-adapter.ts
125
125
  const kyselyAdapter = (db, config) => {
126
126
  let lazyOptions = null;
127
- const createCustomAdapter = (db) => {
127
+ const createCustomAdapter = (db, inTransaction = false) => {
128
128
  return ({ getFieldName, schema, getDefaultFieldName, getDefaultModelName, getFieldAttributes, getModelName }) => {
129
129
  const selectAllJoins = (join) => {
130
130
  const allSelects = [];
@@ -425,6 +425,43 @@ const kyselyAdapter = (db, config) => {
425
425
  const res = (await query.executeTakeFirst()).numDeletedRows;
426
426
  return res > Number.MAX_SAFE_INTEGER ? Number.MAX_SAFE_INTEGER : Number(res);
427
427
  },
428
+ async consumeOne({ model, where }) {
429
+ const { and, or } = convertWhereClause(model, where);
430
+ const applyWhere = (query) => {
431
+ if (and) query = query.where((eb) => eb.and(and.map((expr) => expr(eb))));
432
+ if (or) query = query.where((eb) => eb.or(or.map((expr) => expr(eb))));
433
+ return query;
434
+ };
435
+ const idField = getFieldName({
436
+ model,
437
+ field: "id"
438
+ });
439
+ const deleteSelectedRow = async (db, row) => {
440
+ const targetId = row[idField] ?? row.id;
441
+ if (targetId === void 0 || targetId === null) return null;
442
+ const query = db.deleteFrom(model).where(`${model}.${idField}`, "=", targetId);
443
+ if (config?.type === "mysql") {
444
+ const result = await query.executeTakeFirst();
445
+ return Number(result.numDeletedRows) > 0 ? row : null;
446
+ }
447
+ if (config?.type === "mssql") return await query.outputAll("deleted").executeTakeFirst() ?? null;
448
+ return await query.returningAll().executeTakeFirst() ?? null;
449
+ };
450
+ const deleteWithReturning = async (query) => {
451
+ if (config?.type === "mssql") return await query.outputAll("deleted").executeTakeFirst() ?? null;
452
+ return await query.returningAll().executeTakeFirst() ?? null;
453
+ };
454
+ if (config?.type === "mysql") {
455
+ const claimFromTransaction = async (trx) => {
456
+ const row = await applyWhere(trx.selectFrom(model).selectAll().forUpdate()).limit(1).executeTakeFirst();
457
+ if (!row) return null;
458
+ return deleteSelectedRow(trx, row);
459
+ };
460
+ return inTransaction ? claimFromTransaction(db) : db.transaction().execute(claimFromTransaction);
461
+ }
462
+ const targetIds = applyWhere(db.selectFrom(model).select(`${model}.${idField}`)).limit(1);
463
+ return deleteWithReturning(db.deleteFrom(model).where(`${model}.${idField}`, "in", targetIds));
464
+ },
428
465
  options: config
429
466
  };
430
467
  };
@@ -443,8 +480,11 @@ const kyselyAdapter = (db, config) => {
443
480
  supportsUUIDs: config?.type === "postgres" ? true : false,
444
481
  transaction: config?.transaction ? (cb) => db.transaction().execute((trx) => {
445
482
  return cb(createAdapterFactory({
446
- config: adapterOptions.config,
447
- adapter: createCustomAdapter(trx)
483
+ config: {
484
+ ...adapterOptions.config,
485
+ transaction: false
486
+ },
487
+ adapter: createCustomAdapter(trx, true)
448
488
  })(lazyOptions));
449
489
  }) : false
450
490
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@better-auth/kysely-adapter",
3
- "version": "1.6.9",
3
+ "version": "1.6.11",
4
4
  "description": "Kysely adapter for Better Auth",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -41,8 +41,8 @@
41
41
  },
42
42
  "peerDependencies": {
43
43
  "@better-auth/utils": "0.4.0",
44
- "kysely": "^0.28.14",
45
- "@better-auth/core": "^1.6.9"
44
+ "kysely": "^0.28.17",
45
+ "@better-auth/core": "^1.6.11"
46
46
  },
47
47
  "peerDependenciesMeta": {
48
48
  "kysely": {
@@ -52,10 +52,10 @@
52
52
  "devDependencies": {
53
53
  "@cloudflare/workers-types": "^4.20250121.0",
54
54
  "@better-auth/utils": "0.4.0",
55
- "kysely": "^0.28.14",
55
+ "kysely": "^0.28.17",
56
56
  "tsdown": "0.21.1",
57
57
  "typescript": "^5.9.3",
58
- "@better-auth/core": "1.6.9"
58
+ "@better-auth/core": "1.6.11"
59
59
  },
60
60
  "scripts": {
61
61
  "build": "tsdown",