@better-auth/drizzle-adapter 1.7.0-rc.0 → 1.7.0-rc.1

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 +21 -10
  2. package/package.json +3 -3
package/dist/index.mjs CHANGED
@@ -7,15 +7,19 @@ import { and, asc, count, desc, eq, gt, gte, inArray, isNotNull, isNull, like, l
7
7
  /**
8
8
  * Derive the number of affected rows from a Drizzle write result.
9
9
  *
10
- * Drizzle's drivers report affected rows under different shapes: postgres-js
11
- * exposes `rowCount`, mysql2 reports `affectedRows`/`rowsAffected` (sometimes as
12
- * the first element of a result-header array), and better-sqlite3 uses
13
- * `changes`. This normalizes those so write methods that depend on affected
14
- * rows honor the adapter contract instead of leaking the raw driver result.
10
+ * Drizzle returns the raw per-driver result for a non-returning write, so the
11
+ * count lives under a different field per driver: node-postgres / neon expose
12
+ * `rowCount`, postgres-js / bun-sql carry `count` on an Array subclass, mysql2
13
+ * reports `affectedRows` (in a result-header array), planetscale and other
14
+ * serverless drivers use `rowsAffected`, better-sqlite3 uses `changes`, and
15
+ * Cloudflare D1 nests the count under `meta.changes`. This normalizes them so
16
+ * write methods that depend on affected rows honor the adapter contract instead
17
+ * of leaking the raw driver result.
15
18
  */
16
19
  function getAffectedRowCount(result, operation, context) {
17
20
  let count = 0;
18
21
  if (result && typeof result === "object" && "rowCount" in result) count = result.rowCount;
22
+ else if (result && typeof result === "object" && typeof result.count === "number") count = result.count;
19
23
  else if (Array.isArray(result)) count = result.length > 0 && hasDriverRowCount(result[0]) ? readDriverRowCount(result[0]) : result.length;
20
24
  else if (hasDriverRowCount(result)) count = readDriverRowCount(result);
21
25
  if (typeof count !== "number" || !Number.isFinite(count)) {
@@ -27,12 +31,19 @@ function getAffectedRowCount(result, operation, context) {
27
31
  }
28
32
  return count;
29
33
  }
30
- function hasDriverRowCount(result) {
31
- return !!result && typeof result === "object" && ("affectedRows" in result || "rowsAffected" in result || "changes" in result);
32
- }
33
34
  function readDriverRowCount(result) {
34
- const r = result;
35
- return r.affectedRows ?? r.rowsAffected ?? r.changes;
35
+ if (!result || typeof result !== "object") return void 0;
36
+ const driverResult = result;
37
+ if ("affectedRows" in driverResult) return driverResult.affectedRows;
38
+ if ("rowsAffected" in driverResult) return driverResult.rowsAffected;
39
+ if ("changes" in driverResult) return driverResult.changes;
40
+ if ("meta" in driverResult) {
41
+ const meta = driverResult.meta;
42
+ if (meta && typeof meta === "object" && "changes" in meta) return meta.changes;
43
+ }
44
+ }
45
+ function hasDriverRowCount(result) {
46
+ return readDriverRowCount(result) !== void 0;
36
47
  }
37
48
  const drizzleAdapter = (db, config) => {
38
49
  let lazyOptions = null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@better-auth/drizzle-adapter",
3
- "version": "1.7.0-rc.0",
3
+ "version": "1.7.0-rc.1",
4
4
  "bugs": {
5
5
  "url": "https://github.com/better-auth/better-auth/issues"
6
6
  },
@@ -45,7 +45,7 @@
45
45
  "peerDependencies": {
46
46
  "@better-auth/utils": "0.4.2",
47
47
  "drizzle-orm": "^0.45.2 || >=1.0.0-rc.1 <2.0.0",
48
- "@better-auth/core": "^1.7.0-rc.0"
48
+ "@better-auth/core": "^1.7.0-rc.1"
49
49
  },
50
50
  "peerDependenciesMeta": {
51
51
  "drizzle-orm": {
@@ -57,7 +57,7 @@
57
57
  "drizzle-orm": "^0.45.2",
58
58
  "tsdown": "0.21.1",
59
59
  "typescript": "^5.9.3",
60
- "@better-auth/core": "1.7.0-rc.0"
60
+ "@better-auth/core": "1.7.0-rc.1"
61
61
  },
62
62
  "scripts": {
63
63
  "build": "tsdown",