masterrecord 1.1.4 → 1.1.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/CHANGES.md +24 -0
- package/Entity/entityModelBuilder.js +7 -7
- package/Entity/entityTrackerModel.js +15 -15
- package/Migrations/cli.js +126 -121
- package/Migrations/migrationMySQLQuery.js +21 -27
- package/Migrations/migrationPostgresQuery.js +16 -16
- package/Migrations/migrationSQLiteQuery.js +14 -14
- package/Migrations/migrations.js +41 -45
- package/Migrations/schema.js +3 -3
- package/QueryLanguage/queryMethods.js +26 -26
- package/QueryLanguage/queryScript.js +28 -28
- package/SQLLiteEngine.js +116 -118
- package/Tools.js +19 -19
- package/context.js +1 -1
- package/mySQLEngine.js +3 -3
- package/package.json +22 -2
- package/postgresEngine.js +6 -6
- package/.claude/settings.local.json +0 -10
- package/Migrations/pathUtils.test.js +0 -37
- package/eslint.config.js +0 -40
- package/prettier.config.js +0 -22
- package/test/belongs-to-fk-setter.test.js +0 -154
- package/test/belongs-to-fk-type.test.js +0 -253
- package/test/broad-audit.test.js +0 -256
- package/test/context-errors.test.js +0 -43
- package/test/count-chained-where.test.js +0 -136
- package/test/count-no-from-audit.test.js +0 -124
- package/test/ctx-dollar-placeholder.test.js +0 -142
- package/test/drop-column-idempotent.test.js +0 -181
- package/test/edge-cases-audit.test.js +0 -180
- package/test/esm-imports.test.js +0 -110
- package/test/full-text-search.test.js +0 -340
- package/test/migration-template.test.js +0 -47
- package/test/new-setter-large-string.test.js +0 -191
- package/test/pool-key.test.js +0 -43
- package/test/postgres-camelcase-quoting.test.js +0 -249
- package/test/query-builder-parity.test.js +0 -260
- package/test/setter-and-update-drop.test.js +0 -168
- package/test/sqlite-lifecycle.test.js +0 -76
- package/test/text-types.test.js +0 -167
- package/test/update-database-multi.test.js +0 -239
package/Tools.js
CHANGED
|
@@ -19,9 +19,9 @@ class Tools{
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
static returnEntityList(list, entityList ){
|
|
22
|
-
|
|
23
|
-
for(
|
|
24
|
-
|
|
22
|
+
const newList = [];
|
|
23
|
+
for(let max = 0; max < list.length; max++ ){
|
|
24
|
+
const ent = entityList[list[max]];
|
|
25
25
|
if(ent){
|
|
26
26
|
if(ent.relationshipType === "hasMany" || ent.relationshipType === "hasOne"){
|
|
27
27
|
newList.push(ent.name);
|
|
@@ -38,15 +38,15 @@ class Tools{
|
|
|
38
38
|
// this will remove everthing from back slash amount
|
|
39
39
|
static removeBackwardSlashSection(string, amount, type){
|
|
40
40
|
type = type === undefined ? "\\" : type;
|
|
41
|
-
|
|
42
|
-
for(
|
|
41
|
+
const stringArray = string.split(type);
|
|
42
|
+
for(let i = 0; i < amount; i++){
|
|
43
43
|
stringArray.pop();
|
|
44
44
|
}
|
|
45
45
|
return stringArray.join(type);
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
static getPrimaryKeyObject(model){
|
|
49
|
-
for (
|
|
49
|
+
for (const key in model) {
|
|
50
50
|
if (model.hasOwnProperty(key)) {
|
|
51
51
|
if(model[key].primary){
|
|
52
52
|
if(model[key].primary === true){
|
|
@@ -58,7 +58,7 @@ class Tools{
|
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
static findForeignTable(name, model){
|
|
61
|
-
for (
|
|
61
|
+
for (const key in model) {
|
|
62
62
|
if (model.hasOwnProperty(key)) {
|
|
63
63
|
if(model[key].foreignTable){
|
|
64
64
|
if(model[key].foreignTable === name){
|
|
@@ -85,7 +85,7 @@ class Tools{
|
|
|
85
85
|
|
|
86
86
|
static clearAllProto(proto){
|
|
87
87
|
|
|
88
|
-
|
|
88
|
+
const newproto = {}
|
|
89
89
|
if(proto.__proto__ ){
|
|
90
90
|
// Include non-enumerable own properties so we don't lose values defined via getters
|
|
91
91
|
const keys = Object.getOwnPropertyNames(proto);
|
|
@@ -127,10 +127,10 @@ class Tools{
|
|
|
127
127
|
}
|
|
128
128
|
|
|
129
129
|
static removePrimarykeyandVirtual(currentModel, modelEntity){
|
|
130
|
-
|
|
130
|
+
const newCurrentModel = Object.create(currentModel);
|
|
131
131
|
|
|
132
|
-
for(
|
|
133
|
-
|
|
132
|
+
for(const entity in modelEntity) {
|
|
133
|
+
const currentEntity = modelEntity[entity];
|
|
134
134
|
if (modelEntity.hasOwnProperty(entity)) {
|
|
135
135
|
if(currentEntity.primary === true){
|
|
136
136
|
delete newCurrentModel[`_${entity}`];
|
|
@@ -146,8 +146,8 @@ class Tools{
|
|
|
146
146
|
}
|
|
147
147
|
|
|
148
148
|
static getEntity(name, modelEntity){
|
|
149
|
-
for(
|
|
150
|
-
|
|
149
|
+
for(const entity in modelEntity) {
|
|
150
|
+
const currentEntity = modelEntity[entity];
|
|
151
151
|
if (modelEntity.hasOwnProperty(entity)) {
|
|
152
152
|
if(currentEntity.__name === name){
|
|
153
153
|
return currentEntity;
|
|
@@ -168,14 +168,14 @@ class Tools{
|
|
|
168
168
|
|
|
169
169
|
// return randome letter that is not the skip letter
|
|
170
170
|
static getRandomLetter(length, skip){
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
171
|
+
let result = '';
|
|
172
|
+
const characters = 'abcdefghijklmnopqrstuvwxyz';
|
|
173
|
+
const charactersLength = characters.length;
|
|
174
174
|
|
|
175
|
-
for (
|
|
175
|
+
for ( let i = 0; i < length; i++ ) {
|
|
176
176
|
result += characters.charAt(Math.floor(Math.random() * charactersLength));
|
|
177
177
|
if(skip){
|
|
178
|
-
for (
|
|
178
|
+
for ( let b = 0; b < skip.length; b++ ) {
|
|
179
179
|
if(result === skip[i].entity){
|
|
180
180
|
result = "";
|
|
181
181
|
i--;
|
|
@@ -192,7 +192,7 @@ class Tools{
|
|
|
192
192
|
|
|
193
193
|
static convertEntityToSelectParameterString(obj){
|
|
194
194
|
// todo: loop throgh object and append string with comma to
|
|
195
|
-
|
|
195
|
+
let mainString = "";
|
|
196
196
|
const entries = Object.keys(obj);
|
|
197
197
|
for (const key of entries) {
|
|
198
198
|
// Skip lifecycle hooks - they are not database columns
|
package/context.js
CHANGED
|
@@ -1069,7 +1069,7 @@ class context {
|
|
|
1069
1069
|
}
|
|
1070
1070
|
|
|
1071
1071
|
// Normalize type — require explicit type, no silent inference
|
|
1072
|
-
|
|
1072
|
+
const type = (options.type || '').toString().toLowerCase();
|
|
1073
1073
|
if (!type) {
|
|
1074
1074
|
throw new ConfigurationError(
|
|
1075
1075
|
'Database type is required. Please specify type: "sqlite", "mysql", or "postgres" in your configuration.',
|
package/mySQLEngine.js
CHANGED
|
@@ -472,7 +472,7 @@ class MySQLEngine {
|
|
|
472
472
|
const includeQueries = [];
|
|
473
473
|
const $that = this;
|
|
474
474
|
|
|
475
|
-
for (
|
|
475
|
+
for (const part in query.include) {
|
|
476
476
|
const includeEntity = query.include[part];
|
|
477
477
|
if (includeEntity) {
|
|
478
478
|
const parentObj = includeEntity[query.parentName];
|
|
@@ -537,7 +537,7 @@ class MySQLEngine {
|
|
|
537
537
|
}
|
|
538
538
|
}
|
|
539
539
|
let func = expr.func;
|
|
540
|
-
|
|
540
|
+
const arg = expr.arg;
|
|
541
541
|
if ((!func && typeof arg === 'undefined')) {
|
|
542
542
|
return null;
|
|
543
543
|
}
|
|
@@ -613,7 +613,7 @@ class MySQLEngine {
|
|
|
613
613
|
}
|
|
614
614
|
|
|
615
615
|
getEntity(name, maps) {
|
|
616
|
-
for (
|
|
616
|
+
for (const item in maps) {
|
|
617
617
|
const map = maps[item];
|
|
618
618
|
if (tools.capitalizeFirstLetter(name) === map.name) {
|
|
619
619
|
return map.entity;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "masterrecord",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.6",
|
|
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",
|
|
@@ -32,11 +32,26 @@
|
|
|
32
32
|
"bin": {
|
|
33
33
|
"masterrecord": "./Migrations/cli.js"
|
|
34
34
|
},
|
|
35
|
+
"files": [
|
|
36
|
+
"*.js",
|
|
37
|
+
"Cache/**/*.js",
|
|
38
|
+
"Entity/**/*.js",
|
|
39
|
+
"QueryLanguage/**/*.js",
|
|
40
|
+
"Migrations/**/*.js",
|
|
41
|
+
"!**/*.test.js",
|
|
42
|
+
"!eslint.config.js",
|
|
43
|
+
"!prettier.config.js",
|
|
44
|
+
"docs/**/*.md",
|
|
45
|
+
"CHANGES.md",
|
|
46
|
+
"LICENSE"
|
|
47
|
+
],
|
|
35
48
|
"engines": {
|
|
36
49
|
"node": ">=20.0.0"
|
|
37
50
|
},
|
|
38
51
|
"scripts": {
|
|
39
|
-
"test": "node --test test/ Migrations/pathUtils.test.js"
|
|
52
|
+
"test": "node --test test/ Migrations/pathUtils.test.js",
|
|
53
|
+
"lint": "eslint . --fix",
|
|
54
|
+
"lint:check": "eslint ."
|
|
40
55
|
},
|
|
41
56
|
"author": "Alexandr Rich",
|
|
42
57
|
"license": "MIT",
|
|
@@ -63,5 +78,10 @@
|
|
|
63
78
|
"mysql2": "^3.11.5",
|
|
64
79
|
"app-root-path": "^3.1.0",
|
|
65
80
|
"better-sqlite3": "^12.6.2"
|
|
81
|
+
},
|
|
82
|
+
"devDependencies": {
|
|
83
|
+
"@eslint/js": "^9.17.0",
|
|
84
|
+
"eslint": "^9.17.0",
|
|
85
|
+
"globals": "^15.14.0"
|
|
66
86
|
}
|
|
67
87
|
}
|
package/postgresEngine.js
CHANGED
|
@@ -462,12 +462,12 @@ class postgresEngine {
|
|
|
462
462
|
const entity = this.getEntity(query.parentName, query.entityMap);
|
|
463
463
|
const andList = [];
|
|
464
464
|
|
|
465
|
-
for (
|
|
465
|
+
for (const entityPart in andEntity) {
|
|
466
466
|
const itemEntity = andEntity[entityPart];
|
|
467
|
-
for (
|
|
467
|
+
for (const table in itemEntity[query.parentName]) {
|
|
468
468
|
const item = itemEntity[query.parentName][table];
|
|
469
469
|
const expressions = [];
|
|
470
|
-
for (
|
|
470
|
+
for (const exp in item.expressions) {
|
|
471
471
|
// Use the field name verbatim for SQL emission so it
|
|
472
472
|
// matches the actual column case. The capitalized form
|
|
473
473
|
// is only useful for the navigational-relationship
|
|
@@ -530,7 +530,7 @@ class postgresEngine {
|
|
|
530
530
|
const item = whereEntity[query.parentName].query;
|
|
531
531
|
const conditions = [];
|
|
532
532
|
|
|
533
|
-
for (
|
|
533
|
+
for (const exp in item.expressions) {
|
|
534
534
|
// Use the field name verbatim for SQL emission. See buildAnd for full rationale.
|
|
535
535
|
const originalField = item.expressions[exp].field;
|
|
536
536
|
const capitalized = tools.capitalizeFirstLetter(originalField);
|
|
@@ -643,7 +643,7 @@ class postgresEngine {
|
|
|
643
643
|
const dirtyFields = model.__dirtyFields;
|
|
644
644
|
let paramIndex = 1;
|
|
645
645
|
|
|
646
|
-
for (
|
|
646
|
+
for (const column in dirtyFields) {
|
|
647
647
|
const fieldName = dirtyFields[column];
|
|
648
648
|
const entityDef = model.__entity[fieldName];
|
|
649
649
|
|
|
@@ -795,7 +795,7 @@ class postgresEngine {
|
|
|
795
795
|
const $that = this;
|
|
796
796
|
const columnNames = [];
|
|
797
797
|
const params = [];
|
|
798
|
-
|
|
798
|
+
const paramIndex = 1;
|
|
799
799
|
|
|
800
800
|
for (const column in modelEntity) {
|
|
801
801
|
if (column.indexOf("__") === -1) {
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"permissions": {
|
|
3
|
-
"allow": [
|
|
4
|
-
"Bash(node --test --test-timeout=20000 test/text-types.test.js)",
|
|
5
|
-
"Bash(node --check /Users/alexanderrich/Documents/development/opensourceHQ/MasterController/MasterActionFilters.js)",
|
|
6
|
-
"Bash(node --test test/action-filters-mixin.test.js)",
|
|
7
|
-
"Bash(npm test *)"
|
|
8
|
-
]
|
|
9
|
-
}
|
|
10
|
-
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Tests for pathUtils.js - Path resolution for migrations
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import path from 'node:path';
|
|
6
|
-
import { test } from 'node:test';
|
|
7
|
-
import assert from 'node:assert/strict';
|
|
8
|
-
import { resolveMigrationsDirectory, isInMigrationsDirectory } from './pathUtils.js';
|
|
9
|
-
|
|
10
|
-
test('resolveMigrationsDirectory: context NOT in migrations folder', () => {
|
|
11
|
-
const result = resolveMigrationsDirectory('/app/models/Context.js');
|
|
12
|
-
const expected = path.join('/app/models', 'db', 'migrations');
|
|
13
|
-
assert.equal(result, expected);
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
test('resolveMigrationsDirectory: context already in migrations folder', () => {
|
|
17
|
-
const result = resolveMigrationsDirectory('/app/models/db/migrations/Context.js');
|
|
18
|
-
assert.equal(result, '/app/models/db/migrations');
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
test('resolveMigrationsDirectory: context in nested migrations folder', () => {
|
|
22
|
-
const result = resolveMigrationsDirectory('/components/qa/app/models/db/migrations/qaContext.js');
|
|
23
|
-
assert.equal(result, '/components/qa/app/models/db/migrations');
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
test('isInMigrationsDirectory: true for path with db/migrations', () => {
|
|
27
|
-
assert.equal(isInMigrationsDirectory('/app/models/db/migrations/Context.js'), true);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test('isInMigrationsDirectory: false for path without db/migrations', () => {
|
|
31
|
-
assert.equal(isInMigrationsDirectory('/app/models/Context.js'), false);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
test('isInMigrationsDirectory: handles Windows paths', () => {
|
|
35
|
-
const winPath = '/app\\models\\db\\migrations\\Context.js'.replace(/\\/g, path.sep);
|
|
36
|
-
assert.equal(isInMigrationsDirectory(winPath), true);
|
|
37
|
-
});
|
package/eslint.config.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import js from '@eslint/js';
|
|
2
|
-
|
|
3
|
-
export default [
|
|
4
|
-
js.configs.recommended,
|
|
5
|
-
{
|
|
6
|
-
languageOptions: {
|
|
7
|
-
ecmaVersion: 2023,
|
|
8
|
-
sourceType: 'module',
|
|
9
|
-
globals: {
|
|
10
|
-
console: 'readonly',
|
|
11
|
-
process: 'readonly',
|
|
12
|
-
Buffer: 'readonly',
|
|
13
|
-
global: 'readonly',
|
|
14
|
-
setTimeout: 'readonly',
|
|
15
|
-
clearTimeout: 'readonly',
|
|
16
|
-
setInterval: 'readonly',
|
|
17
|
-
clearInterval: 'readonly',
|
|
18
|
-
URL: 'readonly',
|
|
19
|
-
},
|
|
20
|
-
},
|
|
21
|
-
rules: {
|
|
22
|
-
'no-var': 'error',
|
|
23
|
-
'prefer-const': 'error',
|
|
24
|
-
'no-unused-vars': ['error', {
|
|
25
|
-
vars: 'all',
|
|
26
|
-
args: 'after-used',
|
|
27
|
-
ignoreRestSiblings: true,
|
|
28
|
-
argsIgnorePattern: '^_',
|
|
29
|
-
varsIgnorePattern: '^_',
|
|
30
|
-
}],
|
|
31
|
-
'no-console': 'off',
|
|
32
|
-
eqeqeq: ['error', 'always'],
|
|
33
|
-
'no-eval': 'error',
|
|
34
|
-
'no-implied-eval': 'error',
|
|
35
|
-
},
|
|
36
|
-
},
|
|
37
|
-
{
|
|
38
|
-
ignores: ['node_modules/**', '_migrations/**', 'test/**'],
|
|
39
|
-
},
|
|
40
|
-
];
|
package/prettier.config.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
export default {
|
|
2
|
-
printWidth: 100,
|
|
3
|
-
tabWidth: 4,
|
|
4
|
-
useTabs: false,
|
|
5
|
-
semi: true,
|
|
6
|
-
singleQuote: true,
|
|
7
|
-
quoteProps: 'as-needed',
|
|
8
|
-
trailingComma: 'es5',
|
|
9
|
-
bracketSpacing: true,
|
|
10
|
-
bracketSameLine: false,
|
|
11
|
-
arrowParens: 'avoid',
|
|
12
|
-
endOfLine: 'lf',
|
|
13
|
-
proseWrap: 'preserve',
|
|
14
|
-
htmlWhitespaceSensitivity: 'css',
|
|
15
|
-
singleAttributePerLine: false,
|
|
16
|
-
overrides: [
|
|
17
|
-
{ files: '*.md', options: { printWidth: 80, proseWrap: 'always' } },
|
|
18
|
-
{ files: '*.json', options: { tabWidth: 2 } },
|
|
19
|
-
{ files: ['*.yml', '*.yaml'], options: { tabWidth: 2 } },
|
|
20
|
-
{ files: 'package.json', options: { tabWidth: 2 } },
|
|
21
|
-
],
|
|
22
|
-
};
|
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* belongsTo FK column setter on `.new()` entities.
|
|
3
|
-
*
|
|
4
|
-
* Bug: `belongsTo('Run')` declares a navigation property `Run` and an
|
|
5
|
-
* implicit foreign-key column `run_id`. `.new()` walks the entity
|
|
6
|
-
* definition and creates a setter for `Run`, but NOT for `run_id` (the
|
|
7
|
-
* FK column doesn't appear as a top-level field in `__entity`).
|
|
8
|
-
*
|
|
9
|
-
* Effect: `step.run_id = 'run_xyz'` lands as a plain JS property — the
|
|
10
|
-
* tracker never sees it, the INSERT builder never picks it up, the row
|
|
11
|
-
* goes in without the FK. Users were forced to either use the
|
|
12
|
-
* navigation setter (`step.Run = id`) or drop to raw SQL.
|
|
13
|
-
*
|
|
14
|
-
* Fix: when defining `.new()` setters, for each `belongsTo` column,
|
|
15
|
-
* also define a setter on the foreignKey field name that stores the
|
|
16
|
-
* value and marks the FK as dirty.
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
import { test } from 'node:test';
|
|
20
|
-
import assert from 'node:assert/strict';
|
|
21
|
-
import fs from 'node:fs';
|
|
22
|
-
import path from 'node:path';
|
|
23
|
-
import { fileURLToPath } from 'node:url';
|
|
24
|
-
|
|
25
|
-
process.env.master = 'fk-setter';
|
|
26
|
-
|
|
27
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
28
|
-
const fixturesDir = path.join(__dirname, 'fixtures', 'belongs-to-fk-setter');
|
|
29
|
-
const envDir = path.join(fixturesDir, 'config', 'environments');
|
|
30
|
-
const dbDir = path.join(fixturesDir, 'db');
|
|
31
|
-
fs.mkdirSync(envDir, { recursive: true });
|
|
32
|
-
fs.mkdirSync(dbDir, { recursive: true });
|
|
33
|
-
|
|
34
|
-
fs.writeFileSync(
|
|
35
|
-
path.join(envDir, 'env.fk-setter.json'),
|
|
36
|
-
JSON.stringify({
|
|
37
|
-
FkSetterCtx: { type: 'better-sqlite3', connection: dbDir + path.sep },
|
|
38
|
-
})
|
|
39
|
-
);
|
|
40
|
-
|
|
41
|
-
const { default: context } = await import('../context.js');
|
|
42
|
-
|
|
43
|
-
class Run {
|
|
44
|
-
id(db) { db.string().primary(); }
|
|
45
|
-
name(db) { db.string(); }
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
class Step {
|
|
49
|
-
id(db) { db.integer().primary().auto(); }
|
|
50
|
-
name(db) { db.string(); }
|
|
51
|
-
Run(db) { db.belongsTo('Run').notNullable(); }
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
class FkSetterCtx extends context {
|
|
55
|
-
constructor() {
|
|
56
|
-
super();
|
|
57
|
-
this.env(envDir);
|
|
58
|
-
this.dbset(Run);
|
|
59
|
-
this.dbset(Step);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
test('.new() entity has a setter for the belongsTo foreignKey column', () => {
|
|
64
|
-
const ctx = new FkSetterCtx();
|
|
65
|
-
const step = ctx.Step.new();
|
|
66
|
-
|
|
67
|
-
// The navigation property setter has always worked
|
|
68
|
-
step.Run = 'run_abc';
|
|
69
|
-
assert.equal(step.Run, 'run_abc');
|
|
70
|
-
|
|
71
|
-
// The FK column setter is what was broken
|
|
72
|
-
step.run_id = 'run_xyz';
|
|
73
|
-
assert.equal(step.run_id, 'run_xyz', 'run_id should be readable');
|
|
74
|
-
// The assignment must be tracked. Internally we canonicalize to the
|
|
75
|
-
// navigation-property name ('Run') so the engine UPDATE/INSERT
|
|
76
|
-
// builders' existing belongsTo handling picks it up. Either name in
|
|
77
|
-
// __dirtyFields is acceptable; what matters is that the entity is
|
|
78
|
-
// marked as having a pending change.
|
|
79
|
-
const dirtyHas = step.__dirtyFields.includes('Run') ||
|
|
80
|
-
step.__dirtyFields.includes('run_id');
|
|
81
|
-
assert.ok(
|
|
82
|
-
dirtyHas,
|
|
83
|
-
`FK assignment must be marked dirty, got: ${JSON.stringify(step.__dirtyFields)}`
|
|
84
|
-
);
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
test('loaded entity allows reassigning run_id and persists the UPDATE', async () => {
|
|
88
|
-
const ctx = new FkSetterCtx();
|
|
89
|
-
ctx._SQLEngine.db.exec(`
|
|
90
|
-
CREATE TABLE IF NOT EXISTS Run (
|
|
91
|
-
id TEXT PRIMARY KEY,
|
|
92
|
-
name TEXT
|
|
93
|
-
);
|
|
94
|
-
CREATE TABLE IF NOT EXISTS Step (
|
|
95
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
96
|
-
name TEXT,
|
|
97
|
-
run_id TEXT NOT NULL
|
|
98
|
-
);
|
|
99
|
-
DELETE FROM Step;
|
|
100
|
-
DELETE FROM Run;
|
|
101
|
-
INSERT INTO Run (id, name) VALUES ('run_alpha', 'first');
|
|
102
|
-
INSERT INTO Run (id, name) VALUES ('run_beta', 'second');
|
|
103
|
-
INSERT INTO Step (id, name, run_id) VALUES (1, 'step 1', 'run_alpha');
|
|
104
|
-
`);
|
|
105
|
-
|
|
106
|
-
const step = await ctx.Step.where(s => s.id == $$, 1).single();
|
|
107
|
-
assert.equal(step.run_id, 'run_alpha', 'loaded step has the FK');
|
|
108
|
-
|
|
109
|
-
// Reassign the FK. Pre-fix this throws because the tracker setter
|
|
110
|
-
// does `currentEntity[modelField].set` without a null guard — and
|
|
111
|
-
// currentEntity has no 'run_id' key (only 'Run' with foreignKey).
|
|
112
|
-
step.run_id = 'run_beta';
|
|
113
|
-
assert.equal(step.run_id, 'run_beta');
|
|
114
|
-
const dirtyHas = step.__dirtyFields.includes('Run') ||
|
|
115
|
-
step.__dirtyFields.includes('run_id');
|
|
116
|
-
assert.ok(dirtyHas, `dirty fields: ${JSON.stringify(step.__dirtyFields)}`);
|
|
117
|
-
|
|
118
|
-
await ctx.saveChanges();
|
|
119
|
-
|
|
120
|
-
const after = ctx._SQLEngine.db.prepare('SELECT run_id FROM Step WHERE id = 1').get();
|
|
121
|
-
assert.equal(after.run_id, 'run_beta');
|
|
122
|
-
|
|
123
|
-
await ctx.close();
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
test('.new() then setting run_id ends up in SQLite INSERT', async () => {
|
|
127
|
-
const ctx = new FkSetterCtx();
|
|
128
|
-
ctx._SQLEngine.db.exec(`
|
|
129
|
-
CREATE TABLE IF NOT EXISTS Run (
|
|
130
|
-
id TEXT PRIMARY KEY,
|
|
131
|
-
name TEXT
|
|
132
|
-
);
|
|
133
|
-
CREATE TABLE IF NOT EXISTS Step (
|
|
134
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
135
|
-
name TEXT,
|
|
136
|
-
run_id TEXT NOT NULL
|
|
137
|
-
);
|
|
138
|
-
DELETE FROM Step;
|
|
139
|
-
DELETE FROM Run;
|
|
140
|
-
INSERT INTO Run (id, name) VALUES ('run_abc', 'first');
|
|
141
|
-
`);
|
|
142
|
-
|
|
143
|
-
const step = ctx.Step.new();
|
|
144
|
-
step.name = 'step 1';
|
|
145
|
-
step.run_id = 'run_abc'; // ← this is the case the bug forbade
|
|
146
|
-
await ctx.saveChanges();
|
|
147
|
-
|
|
148
|
-
const rows = ctx._SQLEngine.db.prepare('SELECT * FROM Step').all();
|
|
149
|
-
assert.equal(rows.length, 1);
|
|
150
|
-
assert.equal(rows[0].run_id, 'run_abc');
|
|
151
|
-
assert.equal(rows[0].name, 'step 1');
|
|
152
|
-
|
|
153
|
-
await ctx.close();
|
|
154
|
-
});
|