@better-auth/kysely-adapter 1.6.20 → 1.6.21

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.d.mts CHANGED
@@ -21,6 +21,19 @@ declare const createKyselyAdapter: (config: BetterAuthOptions) => Promise<{
21
21
  interface KyselyAdapterConfig {
22
22
  /**
23
23
  * Database type.
24
+ *
25
+ * For `"mysql"`, this adapter depends on the driver returning
26
+ * "rows matched" counts from `UPDATE`/`DELETE` operations (in
27
+ * mysql2: `affectedRows`, exposed by Kysely as `numUpdatedRows`).
28
+ * By default, `mysql2` enables this via the `FOUND_ROWS` client
29
+ * flag.
30
+ *
31
+ * Do not disable this flag. If you remove it (e.g. with
32
+ * `flags: '-FOUND_ROWS'` in your pool config), MySQL will report
33
+ * "rows changed" semantics: an idempotent `UPDATE` (where the new
34
+ * value equals the old value) will show zero affected rows, causing
35
+ * adapter methods like `update`, `incrementOne`, or `updateMany` to
36
+ * return `null` or `0` even if a row matched the predicate.
24
37
  */
25
38
  type?: KyselyDatabaseType | undefined;
26
39
  /**
package/dist/index.mjs CHANGED
@@ -156,15 +156,28 @@ const kyselyAdapter = (db, config) => {
156
156
  };
157
157
  const withReturning = async (values, builder, model, where) => {
158
158
  if (config?.type === "mysql") {
159
- await builder.execute();
160
159
  if (where.length > 0) {
161
- const field = values.id ? "id" : where[0]?.field ? where[0].field : "id";
162
- const value = values[field] !== void 0 ? values[field] : where[0]?.value;
160
+ const updateResult = await builder.executeTakeFirst();
161
+ if (!updateResult || Number(updateResult.numUpdatedRows ?? 0) === 0) return null;
162
+ const idEqualityWhere = where.find((w) => w.field === "id" && (w.operator === void 0 || w.operator === "eq") && w.connector !== "OR" && w.value !== void 0 && w.value !== null);
163
+ let reselectField;
164
+ let reselectValue;
165
+ if (values.id !== void 0 && values.id !== null) {
166
+ reselectField = "id";
167
+ reselectValue = values.id;
168
+ } else if (idEqualityWhere) {
169
+ reselectField = "id";
170
+ reselectValue = idEqualityWhere.value;
171
+ } else if (where[0]?.field) {
172
+ reselectField = where[0].field;
173
+ reselectValue = values[reselectField] !== void 0 ? values[reselectField] : where[0].value;
174
+ } else return null;
163
175
  return await db.selectFrom(model).selectAll().where(getFieldName({
164
176
  model,
165
- field
166
- }), value === null ? "is" : "=", value).limit(1).executeTakeFirst();
177
+ field: reselectField
178
+ }), reselectValue === null ? "is" : "=", reselectValue).limit(1).executeTakeFirst();
167
179
  }
180
+ await builder.execute();
168
181
  const fetchInserted = async (trx) => {
169
182
  if (values.id) return await trx.selectFrom(model).selectAll().where(getFieldName({
170
183
  model,
@@ -417,6 +430,7 @@ const kyselyAdapter = (db, config) => {
417
430
  return res;
418
431
  },
419
432
  async update({ model, where, update: values }) {
433
+ if (where.length === 0) return null;
420
434
  const { and, or } = convertWhereClause(model, where);
421
435
  let query = db.updateTable(model).set(values);
422
436
  if (and) query = query.where((eb) => eb.and(and.map((expr) => expr(eb))));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@better-auth/kysely-adapter",
3
- "version": "1.6.20",
3
+ "version": "1.6.21",
4
4
  "description": "Kysely adapter for Better Auth",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -42,7 +42,7 @@
42
42
  "peerDependencies": {
43
43
  "@better-auth/utils": "0.4.2",
44
44
  "kysely": "^0.28.17 || ^0.29.0",
45
- "@better-auth/core": "^1.6.20"
45
+ "@better-auth/core": "^1.6.21"
46
46
  },
47
47
  "peerDependenciesMeta": {
48
48
  "kysely": {
@@ -55,7 +55,7 @@
55
55
  "kysely": "^0.28.17 || ^0.29.0",
56
56
  "tsdown": "0.21.1",
57
57
  "typescript": "^5.9.3",
58
- "@better-auth/core": "1.6.20"
58
+ "@better-auth/core": "1.6.21"
59
59
  },
60
60
  "scripts": {
61
61
  "build": "tsdown",