@atscript/db-mysql 0.1.168 → 0.1.169

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.cjs CHANGED
@@ -675,6 +675,7 @@ var MysqlAdapter = class MysqlAdapter extends _atscript_db.BaseDbAdapter {
675
675
  _replaceColumnsMemo;
676
676
  _fulltextMemo;
677
677
  _searchIndexesMemo;
678
+ _incrementPkMemo;
678
679
  /**
679
680
  * Schema name for INFORMATION_SCHEMA queries — `@db.schema` of the bound
680
681
  * table, or `null` (→ `DATABASE()`, the pool's database) when the table
@@ -994,7 +995,7 @@ var MysqlAdapter = class MysqlAdapter extends _atscript_db.BaseDbAdapter {
994
995
  const { sql, params } = buildInsert(this.resolveTableName(), data);
995
996
  this._log(sql, params);
996
997
  const result = await this._wrapConstraintError(() => this._exec().run(sql, params));
997
- return { insertedId: this._resolveInsertedId(data, result.insertId) };
998
+ return { insertedId: this._insertedIdOf(data, result.insertId, result.insertId) };
998
999
  }
999
1000
  async insertMany(data) {
1000
1001
  if (data.length === 0) return {
@@ -1072,9 +1073,25 @@ var MysqlAdapter = class MysqlAdapter extends _atscript_db.BaseDbAdapter {
1072
1073
  }
1073
1074
  /** Physical column of the single-column AUTO_INCREMENT primary key, if the table has one. */
1074
1075
  _autoIncrementPk() {
1075
- const pks = this._table.primaryKeys;
1076
- if (pks.length !== 1 || !this._incrementFields.has(pks[0])) return void 0;
1077
- return this._table.getMetadata().physicalPath(pks[0]);
1076
+ const { column, composite } = this._incrementPk();
1077
+ return composite ? void 0 : column;
1078
+ }
1079
+ /**
1080
+ * Physical column of the primary key's `@db.default.increment` part (if any)
1081
+ * and whether the key is composite — computed once per table.
1082
+ */
1083
+ _incrementPk() {
1084
+ const src = this._table.primaryKeys;
1085
+ let memo = this._incrementPkMemo;
1086
+ if (memo?.src !== src) {
1087
+ const part = src.find((pk) => this._incrementFields.has(pk));
1088
+ memo = this._incrementPkMemo = {
1089
+ src,
1090
+ column: part === void 0 ? void 0 : this._table.physicalPath(part),
1091
+ composite: src.length > 1
1092
+ };
1093
+ }
1094
+ return memo;
1078
1095
  }
1079
1096
  /**
1080
1097
  * Splits `rows` into runs that each map to ONE statement with a derivable
@@ -1164,11 +1181,30 @@ var MysqlAdapter = class MysqlAdapter extends _atscript_db.BaseDbAdapter {
1164
1181
  async _groupInsertedIds(group, insertId, step) {
1165
1182
  const first = Number(insertId);
1166
1183
  const stride = group.generated && group.rows.length > 1 && first > 0 ? await step() : 1;
1184
+ const partId = group.rows.length === 1 ? insertId : void 0;
1167
1185
  return group.rows.map((row, i) => {
1168
1186
  const generatedId = first > 0 ? first + i * stride : 0;
1169
- return group.generated ? generatedId : this._resolveInsertedId(row, generatedId);
1187
+ return group.generated ? generatedId : this._insertedIdOf(row, generatedId, partId);
1170
1188
  });
1171
1189
  }
1190
+ /**
1191
+ * The inserted id of `row` (see `_resolveInsertedId`). For a COMPOSITE key
1192
+ * with an AUTO_INCREMENT part, `partId` is what the statement generated for
1193
+ * that part — pass it only when exact (a ONE-row INSERT); a row carrying the
1194
+ * part keeps its own value. An explicit `0` in the part counts as absent:
1195
+ * MySQL generates a value for it (unless `NO_AUTO_VALUE_ON_ZERO`), so only
1196
+ * `partId` knows it — without that the id is `undefined`, never a wrong `0`.
1197
+ */
1198
+ _insertedIdOf(row, generatedId, partId) {
1199
+ const { column, composite } = this._incrementPk();
1200
+ if (!composite || column === void 0) return this._resolveInsertedId(row, generatedId);
1201
+ const source = isZero(row[column]) ? {
1202
+ ...row,
1203
+ [column]: void 0
1204
+ } : row;
1205
+ const part = Number(partId) > 0 ? { [column]: partId } : void 0;
1206
+ return this._resolveInsertedId(source, generatedId, part);
1207
+ }
1172
1208
  supportsInsertIgnore() {
1173
1209
  return true;
1174
1210
  }
package/dist/index.d.cts CHANGED
@@ -141,6 +141,7 @@ declare class MysqlAdapter extends BaseDbAdapter {
141
141
  private _replaceColumnsMemo?;
142
142
  private _fulltextMemo?;
143
143
  private _searchIndexesMemo?;
144
+ private _incrementPkMemo?;
144
145
  /**
145
146
  * Schema name for INFORMATION_SCHEMA queries — `@db.schema` of the bound
146
147
  * table, or `null` (→ `DATABASE()`, the pool's database) when the table
@@ -260,6 +261,11 @@ declare class MysqlAdapter extends BaseDbAdapter {
260
261
  private _uniformIdGroup;
261
262
  /** Physical column of the single-column AUTO_INCREMENT primary key, if the table has one. */
262
263
  private _autoIncrementPk;
264
+ /**
265
+ * Physical column of the primary key's `@db.default.increment` part (if any)
266
+ * and whether the key is composite — computed once per table.
267
+ */
268
+ private _incrementPk;
263
269
  /**
264
270
  * Splits `rows` into runs that each map to ONE statement with a derivable
265
271
  * id sequence. MySQL reports only the first GENERATED id of a statement, and
@@ -298,6 +304,15 @@ declare class MysqlAdapter extends BaseDbAdapter {
298
304
  * count — mode 2, the 8.0 default), explicit rows keep their own value.
299
305
  */
300
306
  private _groupInsertedIds;
307
+ /**
308
+ * The inserted id of `row` (see `_resolveInsertedId`). For a COMPOSITE key
309
+ * with an AUTO_INCREMENT part, `partId` is what the statement generated for
310
+ * that part — pass it only when exact (a ONE-row INSERT); a row carrying the
311
+ * part keeps its own value. An explicit `0` in the part counts as absent:
312
+ * MySQL generates a value for it (unless `NO_AUTO_VALUE_ON_ZERO`), so only
313
+ * `partId` knows it — without that the id is `undefined`, never a wrong `0`.
314
+ */
315
+ private _insertedIdOf;
301
316
  supportsInsertIgnore(): boolean;
302
317
  /**
303
318
  * Standalone (no caller transaction): per chunk ONE optimistic multi-row
package/dist/index.d.mts CHANGED
@@ -141,6 +141,7 @@ declare class MysqlAdapter extends BaseDbAdapter {
141
141
  private _replaceColumnsMemo?;
142
142
  private _fulltextMemo?;
143
143
  private _searchIndexesMemo?;
144
+ private _incrementPkMemo?;
144
145
  /**
145
146
  * Schema name for INFORMATION_SCHEMA queries — `@db.schema` of the bound
146
147
  * table, or `null` (→ `DATABASE()`, the pool's database) when the table
@@ -260,6 +261,11 @@ declare class MysqlAdapter extends BaseDbAdapter {
260
261
  private _uniformIdGroup;
261
262
  /** Physical column of the single-column AUTO_INCREMENT primary key, if the table has one. */
262
263
  private _autoIncrementPk;
264
+ /**
265
+ * Physical column of the primary key's `@db.default.increment` part (if any)
266
+ * and whether the key is composite — computed once per table.
267
+ */
268
+ private _incrementPk;
263
269
  /**
264
270
  * Splits `rows` into runs that each map to ONE statement with a derivable
265
271
  * id sequence. MySQL reports only the first GENERATED id of a statement, and
@@ -298,6 +304,15 @@ declare class MysqlAdapter extends BaseDbAdapter {
298
304
  * count — mode 2, the 8.0 default), explicit rows keep their own value.
299
305
  */
300
306
  private _groupInsertedIds;
307
+ /**
308
+ * The inserted id of `row` (see `_resolveInsertedId`). For a COMPOSITE key
309
+ * with an AUTO_INCREMENT part, `partId` is what the statement generated for
310
+ * that part — pass it only when exact (a ONE-row INSERT); a row carrying the
311
+ * part keeps its own value. An explicit `0` in the part counts as absent:
312
+ * MySQL generates a value for it (unless `NO_AUTO_VALUE_ON_ZERO`), so only
313
+ * `partId` knows it — without that the id is `undefined`, never a wrong `0`.
314
+ */
315
+ private _insertedIdOf;
301
316
  supportsInsertIgnore(): boolean;
302
317
  /**
303
318
  * Standalone (no caller transaction): per chunk ONE optimistic multi-row
package/dist/index.mjs CHANGED
@@ -674,6 +674,7 @@ var MysqlAdapter = class MysqlAdapter extends BaseDbAdapter {
674
674
  _replaceColumnsMemo;
675
675
  _fulltextMemo;
676
676
  _searchIndexesMemo;
677
+ _incrementPkMemo;
677
678
  /**
678
679
  * Schema name for INFORMATION_SCHEMA queries — `@db.schema` of the bound
679
680
  * table, or `null` (→ `DATABASE()`, the pool's database) when the table
@@ -993,7 +994,7 @@ var MysqlAdapter = class MysqlAdapter extends BaseDbAdapter {
993
994
  const { sql, params } = buildInsert$1(this.resolveTableName(), data);
994
995
  this._log(sql, params);
995
996
  const result = await this._wrapConstraintError(() => this._exec().run(sql, params));
996
- return { insertedId: this._resolveInsertedId(data, result.insertId) };
997
+ return { insertedId: this._insertedIdOf(data, result.insertId, result.insertId) };
997
998
  }
998
999
  async insertMany(data) {
999
1000
  if (data.length === 0) return {
@@ -1071,9 +1072,25 @@ var MysqlAdapter = class MysqlAdapter extends BaseDbAdapter {
1071
1072
  }
1072
1073
  /** Physical column of the single-column AUTO_INCREMENT primary key, if the table has one. */
1073
1074
  _autoIncrementPk() {
1074
- const pks = this._table.primaryKeys;
1075
- if (pks.length !== 1 || !this._incrementFields.has(pks[0])) return void 0;
1076
- return this._table.getMetadata().physicalPath(pks[0]);
1075
+ const { column, composite } = this._incrementPk();
1076
+ return composite ? void 0 : column;
1077
+ }
1078
+ /**
1079
+ * Physical column of the primary key's `@db.default.increment` part (if any)
1080
+ * and whether the key is composite — computed once per table.
1081
+ */
1082
+ _incrementPk() {
1083
+ const src = this._table.primaryKeys;
1084
+ let memo = this._incrementPkMemo;
1085
+ if (memo?.src !== src) {
1086
+ const part = src.find((pk) => this._incrementFields.has(pk));
1087
+ memo = this._incrementPkMemo = {
1088
+ src,
1089
+ column: part === void 0 ? void 0 : this._table.physicalPath(part),
1090
+ composite: src.length > 1
1091
+ };
1092
+ }
1093
+ return memo;
1077
1094
  }
1078
1095
  /**
1079
1096
  * Splits `rows` into runs that each map to ONE statement with a derivable
@@ -1163,11 +1180,30 @@ var MysqlAdapter = class MysqlAdapter extends BaseDbAdapter {
1163
1180
  async _groupInsertedIds(group, insertId, step) {
1164
1181
  const first = Number(insertId);
1165
1182
  const stride = group.generated && group.rows.length > 1 && first > 0 ? await step() : 1;
1183
+ const partId = group.rows.length === 1 ? insertId : void 0;
1166
1184
  return group.rows.map((row, i) => {
1167
1185
  const generatedId = first > 0 ? first + i * stride : 0;
1168
- return group.generated ? generatedId : this._resolveInsertedId(row, generatedId);
1186
+ return group.generated ? generatedId : this._insertedIdOf(row, generatedId, partId);
1169
1187
  });
1170
1188
  }
1189
+ /**
1190
+ * The inserted id of `row` (see `_resolveInsertedId`). For a COMPOSITE key
1191
+ * with an AUTO_INCREMENT part, `partId` is what the statement generated for
1192
+ * that part — pass it only when exact (a ONE-row INSERT); a row carrying the
1193
+ * part keeps its own value. An explicit `0` in the part counts as absent:
1194
+ * MySQL generates a value for it (unless `NO_AUTO_VALUE_ON_ZERO`), so only
1195
+ * `partId` knows it — without that the id is `undefined`, never a wrong `0`.
1196
+ */
1197
+ _insertedIdOf(row, generatedId, partId) {
1198
+ const { column, composite } = this._incrementPk();
1199
+ if (!composite || column === void 0) return this._resolveInsertedId(row, generatedId);
1200
+ const source = isZero(row[column]) ? {
1201
+ ...row,
1202
+ [column]: void 0
1203
+ } : row;
1204
+ const part = Number(partId) > 0 ? { [column]: partId } : void 0;
1205
+ return this._resolveInsertedId(source, generatedId, part);
1206
+ }
1171
1207
  supportsInsertIgnore() {
1172
1208
  return true;
1173
1209
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/db-mysql",
3
- "version": "0.1.168",
3
+ "version": "0.1.169",
4
4
  "description": "MySQL adapter for @atscript/db with mysql2 driver support.",
5
5
  "keywords": [
6
6
  "atscript",
@@ -41,7 +41,7 @@
41
41
  "access": "public"
42
42
  },
43
43
  "dependencies": {
44
- "@atscript/db-sql-tools": "^0.1.168"
44
+ "@atscript/db-sql-tools": "^0.1.169"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@atscript/core": "^0.2.4",
@@ -55,7 +55,7 @@
55
55
  "@atscript/typescript": "^0.2.4",
56
56
  "@uniqu/core": "^0.2.0",
57
57
  "mysql2": ">=3.0.0",
58
- "@atscript/db": "^0.1.168"
58
+ "@atscript/db": "^0.1.169"
59
59
  },
60
60
  "peerDependenciesMeta": {
61
61
  "mysql2": {