@better-auth/drizzle-adapter 1.7.0-beta.4 → 1.7.0-beta.6
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.
- package/dist/index.mjs +80 -11
- package/package.json +8 -5
package/dist/index.mjs
CHANGED
|
@@ -38,6 +38,36 @@ function insensitiveNe(column, value) {
|
|
|
38
38
|
}
|
|
39
39
|
//#endregion
|
|
40
40
|
//#region src/drizzle-adapter.ts
|
|
41
|
+
/**
|
|
42
|
+
* Derive the number of affected rows from a Drizzle write result.
|
|
43
|
+
*
|
|
44
|
+
* Drizzle's drivers report affected rows under different shapes: postgres-js
|
|
45
|
+
* exposes `rowCount`, mysql2 reports `affectedRows`/`rowsAffected` (sometimes as
|
|
46
|
+
* the first element of a result-header array), and better-sqlite3 uses
|
|
47
|
+
* `changes`. This normalizes those so write methods that depend on affected
|
|
48
|
+
* rows honor the adapter contract instead of leaking the raw driver result.
|
|
49
|
+
*/
|
|
50
|
+
function getAffectedRowCount(result, operation, context) {
|
|
51
|
+
let count = 0;
|
|
52
|
+
if (result && typeof result === "object" && "rowCount" in result) count = result.rowCount;
|
|
53
|
+
else if (Array.isArray(result)) count = result.length > 0 && hasDriverRowCount(result[0]) ? readDriverRowCount(result[0]) : result.length;
|
|
54
|
+
else if (hasDriverRowCount(result)) count = readDriverRowCount(result);
|
|
55
|
+
if (typeof count !== "number" || !Number.isFinite(count)) {
|
|
56
|
+
logger.error(`[Drizzle Adapter] The result of the ${operation} operation is not a finite number. This is likely a bug in the adapter. Please report this issue to the Better Auth team.`, {
|
|
57
|
+
result,
|
|
58
|
+
...context
|
|
59
|
+
});
|
|
60
|
+
throw new BetterAuthError(`Drizzle adapter ${operation} returned an invalid affected row count`);
|
|
61
|
+
}
|
|
62
|
+
return count;
|
|
63
|
+
}
|
|
64
|
+
function hasDriverRowCount(result) {
|
|
65
|
+
return !!result && typeof result === "object" && ("affectedRows" in result || "rowsAffected" in result || "changes" in result);
|
|
66
|
+
}
|
|
67
|
+
function readDriverRowCount(result) {
|
|
68
|
+
const r = result;
|
|
69
|
+
return r.affectedRows ?? r.rowsAffected ?? r.changes;
|
|
70
|
+
}
|
|
41
71
|
const drizzleAdapter = (db, config) => {
|
|
42
72
|
let lazyOptions = null;
|
|
43
73
|
let mysqlNoIdWarned = false;
|
|
@@ -431,7 +461,10 @@ const drizzleAdapter = (db, config) => {
|
|
|
431
461
|
async updateMany({ model, where, update: values }) {
|
|
432
462
|
const schemaModel = getSchema(model);
|
|
433
463
|
const clause = convertWhereClause(where, model);
|
|
434
|
-
return await db.update(schemaModel).set(values).where(...clause)
|
|
464
|
+
return getAffectedRowCount(await db.update(schemaModel).set(values).where(...clause), "updateMany", {
|
|
465
|
+
model,
|
|
466
|
+
where
|
|
467
|
+
});
|
|
435
468
|
},
|
|
436
469
|
async delete({ model, where }) {
|
|
437
470
|
const schemaModel = getSchema(model);
|
|
@@ -441,17 +474,10 @@ const drizzleAdapter = (db, config) => {
|
|
|
441
474
|
async deleteMany({ model, where }) {
|
|
442
475
|
const schemaModel = getSchema(model);
|
|
443
476
|
const clause = convertWhereClause(where, model);
|
|
444
|
-
|
|
445
|
-
let count = 0;
|
|
446
|
-
if (res && "rowCount" in res) count = res.rowCount;
|
|
447
|
-
else if (Array.isArray(res)) count = res.length;
|
|
448
|
-
else if (res && ("affectedRows" in res || "rowsAffected" in res || "changes" in res)) count = res.affectedRows ?? res.rowsAffected ?? res.changes;
|
|
449
|
-
if (typeof count !== "number") logger.error("[Drizzle Adapter] The result of the deleteMany operation is not a number. This is likely a bug in the adapter. Please report this issue to the Better Auth team.", {
|
|
450
|
-
res,
|
|
477
|
+
return getAffectedRowCount(await db.delete(schemaModel).where(...clause), "deleteMany", {
|
|
451
478
|
model,
|
|
452
479
|
where
|
|
453
480
|
});
|
|
454
|
-
return count;
|
|
455
481
|
},
|
|
456
482
|
async consumeOne({ model, where }) {
|
|
457
483
|
const schemaModel = getSchema(model);
|
|
@@ -467,8 +493,10 @@ const drizzleAdapter = (db, config) => {
|
|
|
467
493
|
if (!target) return null;
|
|
468
494
|
const targetId = target[idField] ?? target.id;
|
|
469
495
|
if (targetId === void 0 || targetId === null || !idColumn) return null;
|
|
470
|
-
|
|
471
|
-
|
|
496
|
+
return getAffectedRowCount(await tx.delete(schemaModel).where(eq(idColumn, targetId)).execute(), "consumeOne", {
|
|
497
|
+
model,
|
|
498
|
+
where
|
|
499
|
+
}) > 0 ? target : null;
|
|
472
500
|
};
|
|
473
501
|
return inTransaction ? claimFromTransaction(db) : db.transaction(claimFromTransaction);
|
|
474
502
|
}
|
|
@@ -476,6 +504,47 @@ const drizzleAdapter = (db, config) => {
|
|
|
476
504
|
const targetIds = db.select({ id: idColumn }).from(schemaModel).where(...clause).limit(1);
|
|
477
505
|
return (await db.delete(schemaModel).where(inArray(idColumn, targetIds)).returning())[0] ?? null;
|
|
478
506
|
},
|
|
507
|
+
async incrementOne({ model, where, increment, set }) {
|
|
508
|
+
const schemaModel = getSchema(model);
|
|
509
|
+
const clause = convertWhereClause(where, model);
|
|
510
|
+
const idField = getFieldName({
|
|
511
|
+
model,
|
|
512
|
+
field: "id"
|
|
513
|
+
});
|
|
514
|
+
const idColumn = schemaModel[idField];
|
|
515
|
+
const assignments = {};
|
|
516
|
+
for (const [field, delta] of Object.entries(increment)) {
|
|
517
|
+
const columnName = getFieldName({
|
|
518
|
+
model,
|
|
519
|
+
field
|
|
520
|
+
});
|
|
521
|
+
const column = schemaModel[columnName];
|
|
522
|
+
if (!column) throw new BetterAuthError(`The field "${field}" does not exist in the schema for the model "${model}". Please update your schema.`);
|
|
523
|
+
assignments[columnName] = sql`${column} + ${sql.param(delta)}`;
|
|
524
|
+
}
|
|
525
|
+
if (set) for (const [field, value] of Object.entries(set)) {
|
|
526
|
+
const columnName = getFieldName({
|
|
527
|
+
model,
|
|
528
|
+
field
|
|
529
|
+
});
|
|
530
|
+
if (!schemaModel[columnName]) throw new BetterAuthError(`The field "${field}" does not exist in the schema for the model "${model}". Please update your schema.`);
|
|
531
|
+
assignments[columnName] = value;
|
|
532
|
+
}
|
|
533
|
+
if (config.provider === "mysql") {
|
|
534
|
+
const mutateInTransaction = async (tx) => {
|
|
535
|
+
const target = (await tx.select().from(schemaModel).where(...clause).for("update").limit(1))[0];
|
|
536
|
+
if (!target) return null;
|
|
537
|
+
const targetId = target[idField] ?? target.id;
|
|
538
|
+
if (targetId === void 0 || targetId === null || !idColumn) return null;
|
|
539
|
+
await tx.update(schemaModel).set(assignments).where(eq(idColumn, targetId)).execute();
|
|
540
|
+
return (await tx.select().from(schemaModel).where(eq(idColumn, targetId)).limit(1).execute())[0] ?? null;
|
|
541
|
+
};
|
|
542
|
+
return inTransaction ? mutateInTransaction(db) : db.transaction(mutateInTransaction);
|
|
543
|
+
}
|
|
544
|
+
if (!idColumn) return null;
|
|
545
|
+
const targetIds = db.select({ id: idColumn }).from(schemaModel).where(...clause).limit(1);
|
|
546
|
+
return (await db.update(schemaModel).set(assignments).where(inArray(idColumn, targetIds)).returning())[0] ?? null;
|
|
547
|
+
},
|
|
479
548
|
options: config
|
|
480
549
|
};
|
|
481
550
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@better-auth/drizzle-adapter",
|
|
3
|
-
"version": "1.7.0-beta.
|
|
3
|
+
"version": "1.7.0-beta.6",
|
|
4
|
+
"bugs": {
|
|
5
|
+
"url": "https://github.com/better-auth/better-auth/issues"
|
|
6
|
+
},
|
|
4
7
|
"description": "Drizzle adapter for Better Auth",
|
|
5
8
|
"type": "module",
|
|
6
9
|
"license": "MIT",
|
|
@@ -35,9 +38,9 @@
|
|
|
35
38
|
}
|
|
36
39
|
},
|
|
37
40
|
"peerDependencies": {
|
|
38
|
-
"@better-auth/utils": "0.4.
|
|
41
|
+
"@better-auth/utils": "0.4.2",
|
|
39
42
|
"drizzle-orm": "^0.45.2",
|
|
40
|
-
"@better-auth/core": "^1.7.0-beta.
|
|
43
|
+
"@better-auth/core": "^1.7.0-beta.6"
|
|
41
44
|
},
|
|
42
45
|
"peerDependenciesMeta": {
|
|
43
46
|
"drizzle-orm": {
|
|
@@ -45,11 +48,11 @@
|
|
|
45
48
|
}
|
|
46
49
|
},
|
|
47
50
|
"devDependencies": {
|
|
48
|
-
"@better-auth/utils": "0.4.
|
|
51
|
+
"@better-auth/utils": "0.4.2",
|
|
49
52
|
"drizzle-orm": "^0.45.2",
|
|
50
53
|
"tsdown": "0.21.1",
|
|
51
54
|
"typescript": "^5.9.3",
|
|
52
|
-
"@better-auth/core": "1.7.0-beta.
|
|
55
|
+
"@better-auth/core": "1.7.0-beta.6"
|
|
53
56
|
},
|
|
54
57
|
"scripts": {
|
|
55
58
|
"build": "tsdown",
|