masterrecord 1.2.7 → 1.2.8
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/CHANGES.md +8 -0
- package/SQLLiteEngine.js +16 -0
- package/mySQLEngine.js +13 -0
- package/package.json +1 -1
- package/postgresEngine.js +13 -0
package/CHANGES.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# MasterRecord Changelog
|
|
2
2
|
|
|
3
|
+
## v1.2.8 — batched-insert auto-increment PK handling
|
|
4
|
+
|
|
5
|
+
**Bug:** the batched-insert path could fail with `Type mismatch for <Entity>.id: Expected integer, got function` for an unset auto-increment primary key, then fall back to individual inserts (which handled it). The INSERT builder (`_buildSQLInsertObjectParameterized`, shared by single + batch inserts) only skipped the auto-PK when its value was `undefined`/`null`. When the unset PK surfaced as its **schema-definition function** (`id(db){…}`, e.g. when the row is a class instance rather than a `.new()` data instance), the builder fell through to the type validator and threw.
|
|
6
|
+
|
|
7
|
+
**Fix (all three engines):** an auto-increment primary key is database-assigned and is now **never emitted in the INSERT unless the caller set an explicit value** — "unset" is recognized whether it surfaces as `undefined`, `null`, or a function. A function value is treated as never-valid for any column (a leaked schema method/getter) and skipped. Single- and batched-insert paths share the builder, so both behave identically; an explicitly-set PK value is still honored.
|
|
8
|
+
|
|
9
|
+
New test: `test/bulk-insert-autopk.test.js` — builder excludes a function-valued auto-PK (and an `undefined` one), honors an explicit value, and an end-to-end `bulkCreate` assigns auto IDs across a batch with no fallback. Verified on SQLite; the MySQL/Postgres builders received the identical change.
|
|
10
|
+
|
|
3
11
|
## v1.2.7 — public engine-agnostic raw-SQL escape hatch (`ctx.query` / `ctx.execute`)
|
|
4
12
|
|
|
5
13
|
Adds a public, portable raw-SQL API so apps stop reaching into `ctx.db` (the raw, engine-specific driver). `ctx.db.prepare()` is better-sqlite3's synchronous API; mysql2/pg have no `.prepare()`, so code written against `ctx.db` "works on SQLite, breaks on MySQL." The only engine-agnostic path was the private `context._execute` (the migration/DDL path, which logs as a migration and doesn't return rows on SQLite).
|
package/SQLLiteEngine.js
CHANGED
|
@@ -1212,6 +1212,22 @@ class SQLLiteEngine {
|
|
|
1212
1212
|
fieldColumn = fields[modelEntity[column].foreignKey];
|
|
1213
1213
|
}
|
|
1214
1214
|
|
|
1215
|
+
// Auto-increment primary keys are assigned by the database, so
|
|
1216
|
+
// they must never be emitted in the INSERT unless the caller
|
|
1217
|
+
// set an explicit value. An unset auto PK surfaces either as
|
|
1218
|
+
// undefined/null (an unread `.new()` getter) or as the schema-
|
|
1219
|
+
// definition function `id(db){…}` (when the row is a class
|
|
1220
|
+
// instance). A function is also never a valid value for any
|
|
1221
|
+
// column. Skip in those cases so the batched-insert path
|
|
1222
|
+
// behaves like the single-insert path instead of failing type
|
|
1223
|
+
// validation with "Expected integer, got function".
|
|
1224
|
+
const _columnDef = modelEntity[column];
|
|
1225
|
+
const _isAutoPrimaryKey = _columnDef && _columnDef.primary === true && _columnDef.auto === true;
|
|
1226
|
+
if (typeof fieldColumn === 'function' ||
|
|
1227
|
+
(_isAutoPrimaryKey && (fieldColumn === undefined || fieldColumn === null))) {
|
|
1228
|
+
continue;
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1215
1231
|
if (fieldColumn !== undefined && fieldColumn !== null) {
|
|
1216
1232
|
// 🔥 Apply toDatabase transformer FIRST — transformers may turn
|
|
1217
1233
|
// objects into scalars (e.g. JSON.stringify), so running them
|
package/mySQLEngine.js
CHANGED
|
@@ -863,6 +863,19 @@ class MySQLEngine {
|
|
|
863
863
|
fieldColumn = fields[modelEntity[column].foreignKey];
|
|
864
864
|
}
|
|
865
865
|
|
|
866
|
+
// Auto-increment PKs are DB-assigned — never emit an unset one.
|
|
867
|
+
// "Unset" surfaces as undefined/null (a .new() getter) or as the
|
|
868
|
+
// schema-definition function `id(db){…}` (a class instance); a
|
|
869
|
+
// function is never a valid column value. This keeps the batched
|
|
870
|
+
// multi-row INSERT consistent with the single-insert path
|
|
871
|
+
// (otherwise it threw "Expected integer, got function").
|
|
872
|
+
const _columnDef = modelEntity[column];
|
|
873
|
+
const _isAutoPrimaryKey = _columnDef && _columnDef.primary === true && _columnDef.auto === true;
|
|
874
|
+
if (typeof fieldColumn === 'function' ||
|
|
875
|
+
(_isAutoPrimaryKey && (fieldColumn === undefined || fieldColumn === null))) {
|
|
876
|
+
continue;
|
|
877
|
+
}
|
|
878
|
+
|
|
866
879
|
if (fieldColumn !== undefined && fieldColumn !== null) {
|
|
867
880
|
// 🔥 Apply toDatabase transformer FIRST — transformers may turn
|
|
868
881
|
// objects into scalars (e.g. JSON.stringify) so running them
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "masterrecord",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.8",
|
|
4
4
|
"description": "An Object-relational mapping for the Master framework. Master Record connects classes to relational database tables to establish a database with almost zero-configuration ",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./MasterRecord.js",
|
package/postgresEngine.js
CHANGED
|
@@ -828,6 +828,19 @@ class postgresEngine {
|
|
|
828
828
|
fieldColumn = fields[modelEntity[column].foreignKey];
|
|
829
829
|
}
|
|
830
830
|
|
|
831
|
+
// Auto-increment PKs are DB-assigned — never emit an unset one.
|
|
832
|
+
// "Unset" surfaces as undefined/null (a .new() getter) or as the
|
|
833
|
+
// schema-definition function `id(db){…}` (a class instance); a
|
|
834
|
+
// function is never a valid column value. Keeps the batched
|
|
835
|
+
// multi-row INSERT consistent with the single-insert path
|
|
836
|
+
// (otherwise it threw "Expected integer, got function").
|
|
837
|
+
const _columnDef = modelEntity[column];
|
|
838
|
+
const _isAutoPrimaryKey = _columnDef && _columnDef.primary === true && _columnDef.auto === true;
|
|
839
|
+
if (typeof fieldColumn === 'function' ||
|
|
840
|
+
(_isAutoPrimaryKey && (fieldColumn === undefined || fieldColumn === null))) {
|
|
841
|
+
continue;
|
|
842
|
+
}
|
|
843
|
+
|
|
831
844
|
if (fieldColumn !== undefined && fieldColumn !== null) {
|
|
832
845
|
// 🔥 Apply toDatabase transformer FIRST — transformers may turn
|
|
833
846
|
// objects into scalars (e.g. JSON.stringify) so running them
|