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
|
@@ -1,253 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* belongsTo FK type resolution (1.1.2).
|
|
3
|
-
*
|
|
4
|
-
* Bug: belongsTo() hardcoded type='integer' on the FK column at entity-
|
|
5
|
-
* definition time, before the parent entity was known. If the parent's PK
|
|
6
|
-
* was string/bigint/uuid, the FK column was still declared INTEGER. SQLite
|
|
7
|
-
* accepted the mismatch (dynamic typing); Postgres and MySQL rejected it.
|
|
8
|
-
*
|
|
9
|
-
* Fix: after each dbset() call, walk every entity and re-resolve every
|
|
10
|
-
* belongsTo column's `type` from its parent's primary-key type. Order-
|
|
11
|
-
* independent, idempotent.
|
|
12
|
-
*
|
|
13
|
-
* Notes for future maintenance:
|
|
14
|
-
* - EntityModelBuilder only sees own-prototype methods. Test entity classes
|
|
15
|
-
* must declare their fields directly (no `extends`) or the build silently
|
|
16
|
-
* produces an empty entity with no PK and the resolver has nothing to copy.
|
|
17
|
-
* - bigint/uuid go through EntityModel.type('bigint') because EntityModel
|
|
18
|
-
* doesn't have dedicated bigint()/uuid() field-setter methods.
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
import { test } from 'node:test';
|
|
22
|
-
import assert from 'node:assert/strict';
|
|
23
|
-
import fs from 'node:fs';
|
|
24
|
-
import path from 'node:path';
|
|
25
|
-
import { fileURLToPath } from 'node:url';
|
|
26
|
-
|
|
27
|
-
process.env.master = 'fktypes';
|
|
28
|
-
|
|
29
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
30
|
-
const fixturesDir = path.join(__dirname, 'fixtures', 'belongs-to-fk-type');
|
|
31
|
-
const envDir = path.join(fixturesDir, 'config', 'environments');
|
|
32
|
-
const dbDir = path.join(fixturesDir, 'db');
|
|
33
|
-
fs.mkdirSync(envDir, { recursive: true });
|
|
34
|
-
fs.mkdirSync(dbDir, { recursive: true });
|
|
35
|
-
|
|
36
|
-
fs.writeFileSync(
|
|
37
|
-
path.join(envDir, 'env.fktypes.json'),
|
|
38
|
-
JSON.stringify({
|
|
39
|
-
StringPkCtx: { type: 'better-sqlite3', connection: dbDir + path.sep },
|
|
40
|
-
IntPkCtx: { type: 'better-sqlite3', connection: dbDir + path.sep },
|
|
41
|
-
OutOfOrderCtx: { type: 'better-sqlite3', connection: dbDir + path.sep },
|
|
42
|
-
BigintPkCtx: { type: 'better-sqlite3', connection: dbDir + path.sep },
|
|
43
|
-
UuidPkCtx: { type: 'better-sqlite3', connection: dbDir + path.sep },
|
|
44
|
-
})
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
const { default: context } = await import('../context.js');
|
|
48
|
-
const { default: SQLiteQuery } = await import('../Migrations/migrationSQLiteQuery.js');
|
|
49
|
-
const { default: MySQLQuery } = await import('../Migrations/migrationMySQLQuery.js');
|
|
50
|
-
const { default: PostgresQuery } = await import('../Migrations/migrationPostgresQuery.js');
|
|
51
|
-
|
|
52
|
-
// =====================================================================
|
|
53
|
-
// Resolver behavior (per parent PK type)
|
|
54
|
-
// =====================================================================
|
|
55
|
-
|
|
56
|
-
test('belongsTo FK type resolves from string PK on parent', () => {
|
|
57
|
-
class Run {
|
|
58
|
-
id(db) { db.string().primary(); }
|
|
59
|
-
name(db) { db.string(); }
|
|
60
|
-
}
|
|
61
|
-
class Step {
|
|
62
|
-
id(db) { db.integer().primary().auto(); }
|
|
63
|
-
name(db) { db.string(); }
|
|
64
|
-
Run(db) { db.belongsTo('Run').notNullable(); }
|
|
65
|
-
}
|
|
66
|
-
class StringPkCtx extends context {
|
|
67
|
-
constructor() {
|
|
68
|
-
super();
|
|
69
|
-
this.env(envDir);
|
|
70
|
-
this.dbset(Run);
|
|
71
|
-
this.dbset(Step);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
const ctx = new StringPkCtx();
|
|
75
|
-
const stepEntity = ctx.__entities.find(e => e.__name === 'Step');
|
|
76
|
-
const fk = stepEntity.Run;
|
|
77
|
-
assert.equal(fk.relationshipType, 'belongsTo');
|
|
78
|
-
assert.equal(fk.foreignKey, 'run_id');
|
|
79
|
-
assert.equal(fk.type, 'string', 'FK type should match parent string PK');
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
test('belongsTo FK type stays integer when parent PK is integer', () => {
|
|
83
|
-
class Run {
|
|
84
|
-
id(db) { db.integer().primary().auto(); }
|
|
85
|
-
name(db) { db.string(); }
|
|
86
|
-
}
|
|
87
|
-
class Step {
|
|
88
|
-
id(db) { db.integer().primary().auto(); }
|
|
89
|
-
Run(db) { db.belongsTo('Run').notNullable(); }
|
|
90
|
-
}
|
|
91
|
-
class IntPkCtx extends context {
|
|
92
|
-
constructor() {
|
|
93
|
-
super();
|
|
94
|
-
this.env(envDir);
|
|
95
|
-
this.dbset(Run);
|
|
96
|
-
this.dbset(Step);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
const ctx = new IntPkCtx();
|
|
100
|
-
const stepEntity = ctx.__entities.find(e => e.__name === 'Step');
|
|
101
|
-
assert.equal(stepEntity.Run.type, 'integer');
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
test('belongsTo FK type resolves regardless of dbset() order', () => {
|
|
105
|
-
class Run {
|
|
106
|
-
id(db) { db.string().primary(); }
|
|
107
|
-
name(db) { db.string(); }
|
|
108
|
-
}
|
|
109
|
-
class Step {
|
|
110
|
-
id(db) { db.integer().primary().auto(); }
|
|
111
|
-
Run(db) { db.belongsTo('Run').notNullable(); }
|
|
112
|
-
}
|
|
113
|
-
class OutOfOrderCtx extends context {
|
|
114
|
-
constructor() {
|
|
115
|
-
super();
|
|
116
|
-
this.env(envDir);
|
|
117
|
-
this.dbset(Step); // child first
|
|
118
|
-
this.dbset(Run); // parent second
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
const ctx = new OutOfOrderCtx();
|
|
122
|
-
const stepEntity = ctx.__entities.find(e => e.__name === 'Step');
|
|
123
|
-
assert.equal(stepEntity.Run.type, 'string', 'resolution must be order-independent');
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
test('belongsTo FK type resolves for bigint parent PK', () => {
|
|
127
|
-
class Run {
|
|
128
|
-
id(db) { db.type('bigint').primary().auto(); }
|
|
129
|
-
name(db) { db.string(); }
|
|
130
|
-
}
|
|
131
|
-
class Step {
|
|
132
|
-
id(db) { db.integer().primary().auto(); }
|
|
133
|
-
Run(db) { db.belongsTo('Run').notNullable(); }
|
|
134
|
-
}
|
|
135
|
-
class BigintPkCtx extends context {
|
|
136
|
-
constructor() {
|
|
137
|
-
super();
|
|
138
|
-
this.env(envDir);
|
|
139
|
-
this.dbset(Run);
|
|
140
|
-
this.dbset(Step);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
const ctx = new BigintPkCtx();
|
|
144
|
-
const stepEntity = ctx.__entities.find(e => e.__name === 'Step');
|
|
145
|
-
assert.equal(stepEntity.Run.type, 'bigint');
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
test('belongsTo FK type resolves for uuid parent PK', () => {
|
|
149
|
-
class Run {
|
|
150
|
-
id(db) { db.type('uuid').primary(); }
|
|
151
|
-
name(db) { db.string(); }
|
|
152
|
-
}
|
|
153
|
-
class Step {
|
|
154
|
-
id(db) { db.integer().primary().auto(); }
|
|
155
|
-
Run(db) { db.belongsTo('Run').notNullable(); }
|
|
156
|
-
}
|
|
157
|
-
class UuidPkCtx extends context {
|
|
158
|
-
constructor() {
|
|
159
|
-
super();
|
|
160
|
-
this.env(envDir);
|
|
161
|
-
this.dbset(Run);
|
|
162
|
-
this.dbset(Step);
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
const ctx = new UuidPkCtx();
|
|
166
|
-
const stepEntity = ctx.__entities.find(e => e.__name === 'Step');
|
|
167
|
-
assert.equal(stepEntity.Run.type, 'uuid');
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
// =====================================================================
|
|
171
|
-
// Migration DDL output — verify each engine emits a matching SQL type.
|
|
172
|
-
// Bypasses context; feeds a minimal table-like object straight to the
|
|
173
|
-
// migration query builders. Shape mirrors what the resolver produces.
|
|
174
|
-
// =====================================================================
|
|
175
|
-
|
|
176
|
-
function stepTableWith(fkType) {
|
|
177
|
-
return {
|
|
178
|
-
__name: 'Step',
|
|
179
|
-
id: { name: 'id', type: 'integer', primary: true, auto: true },
|
|
180
|
-
name: { name: 'name', type: 'string' },
|
|
181
|
-
Run: {
|
|
182
|
-
name: 'Run',
|
|
183
|
-
type: fkType,
|
|
184
|
-
relationshipType: 'belongsTo',
|
|
185
|
-
foreignTable: 'Run',
|
|
186
|
-
foreignKey: 'run_id',
|
|
187
|
-
nullable: false,
|
|
188
|
-
},
|
|
189
|
-
};
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
test('Postgres createTable emits VARCHAR(255) for FK to string PK', () => {
|
|
193
|
-
const sql = new PostgresQuery().createTable(stepTableWith('string'));
|
|
194
|
-
assert.match(sql, /"run_id"\s+VARCHAR\(255\)/, sql);
|
|
195
|
-
assert.doesNotMatch(sql, /"run_id"\s+INTEGER/);
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
test('Postgres createTable emits INTEGER for FK to integer PK', () => {
|
|
199
|
-
const sql = new PostgresQuery().createTable(stepTableWith('integer'));
|
|
200
|
-
assert.match(sql, /"run_id"\s+INTEGER/, sql);
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
test('Postgres createTable emits BIGINT for FK to bigint PK', () => {
|
|
204
|
-
const sql = new PostgresQuery().createTable(stepTableWith('bigint'));
|
|
205
|
-
assert.match(sql, /"run_id"\s+BIGINT/, sql);
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
test('Postgres createTable emits UUID for FK to uuid PK', () => {
|
|
209
|
-
const sql = new PostgresQuery().createTable(stepTableWith('uuid'));
|
|
210
|
-
assert.match(sql, /"run_id"\s+UUID/, sql);
|
|
211
|
-
});
|
|
212
|
-
|
|
213
|
-
test('MySQL createTable emits VARCHAR(255) for FK to string PK', () => {
|
|
214
|
-
const sql = new MySQLQuery().createTable(stepTableWith('string'));
|
|
215
|
-
assert.match(sql, /`run_id`\s+VARCHAR\(255\)/, sql);
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
test('MySQL createTable emits BIGINT for FK to bigint PK', () => {
|
|
219
|
-
const sql = new MySQLQuery().createTable(stepTableWith('bigint'));
|
|
220
|
-
assert.match(sql, /`run_id`\s+BIGINT/, sql);
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
test('MySQL createTable emits VARCHAR(36) for FK to uuid PK', () => {
|
|
224
|
-
const sql = new MySQLQuery().createTable(stepTableWith('uuid'));
|
|
225
|
-
assert.match(sql, /`run_id`\s+VARCHAR\(36\)/, sql);
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
test('MySQL createTable never emits "undefined" for any FK type', () => {
|
|
229
|
-
for (const t of ['string', 'integer', 'bigint', 'uuid', 'text']) {
|
|
230
|
-
const sql = new MySQLQuery().createTable(stepTableWith(t));
|
|
231
|
-
assert.doesNotMatch(sql, /undefined/, `MySQL ${t}: ${sql}`);
|
|
232
|
-
}
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
test('SQLite createTable emits TEXT for FK to string PK', () => {
|
|
236
|
-
const sql = new SQLiteQuery().createTable(stepTableWith('string'));
|
|
237
|
-
assert.match(sql, /\[?run_id\]?\s+TEXT/i, sql);
|
|
238
|
-
});
|
|
239
|
-
|
|
240
|
-
test('SQLite createTable emits INTEGER for FK to integer PK', () => {
|
|
241
|
-
const sql = new SQLiteQuery().createTable(stepTableWith('integer'));
|
|
242
|
-
assert.match(sql, /\[?run_id\]?\s+INTEGER/i, sql);
|
|
243
|
-
});
|
|
244
|
-
|
|
245
|
-
test('SQLite createTable emits INTEGER for FK to bigint PK', () => {
|
|
246
|
-
const sql = new SQLiteQuery().createTable(stepTableWith('bigint'));
|
|
247
|
-
assert.match(sql, /\[?run_id\]?\s+INTEGER/i, sql);
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
test('SQLite createTable emits TEXT for FK to uuid PK', () => {
|
|
251
|
-
const sql = new SQLiteQuery().createTable(stepTableWith('uuid'));
|
|
252
|
-
assert.match(sql, /\[?run_id\]?\s+TEXT/i, sql);
|
|
253
|
-
});
|
package/test/broad-audit.test.js
DELETED
|
@@ -1,256 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Broad integration audit against SQLite. Each test exercises a common
|
|
3
|
-
* consumer pattern; any failure points at a real bug. Uses raw SQL for
|
|
4
|
-
* schema setup so we don't depend on the migration CLI.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import { test } from 'node:test';
|
|
8
|
-
import assert from 'node:assert/strict';
|
|
9
|
-
import fs from 'node:fs';
|
|
10
|
-
import path from 'node:path';
|
|
11
|
-
import { fileURLToPath } from 'node:url';
|
|
12
|
-
|
|
13
|
-
process.env.master = 'audit';
|
|
14
|
-
|
|
15
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
16
|
-
const fixturesDir = path.join(__dirname, 'fixtures', 'broad-audit');
|
|
17
|
-
const envDir = path.join(fixturesDir, 'config', 'environments');
|
|
18
|
-
const dbDir = path.join(fixturesDir, 'db');
|
|
19
|
-
const dbFile = path.join(dbDir, 'auditctx.sqlite');
|
|
20
|
-
|
|
21
|
-
fs.mkdirSync(envDir, { recursive: true });
|
|
22
|
-
fs.mkdirSync(dbDir, { recursive: true });
|
|
23
|
-
if (fs.existsSync(dbFile)) fs.rmSync(dbFile);
|
|
24
|
-
|
|
25
|
-
fs.writeFileSync(
|
|
26
|
-
path.join(envDir, 'env.audit.json'),
|
|
27
|
-
JSON.stringify({
|
|
28
|
-
AuditCtx: {
|
|
29
|
-
type: 'better-sqlite3',
|
|
30
|
-
connection: dbDir + path.sep,
|
|
31
|
-
},
|
|
32
|
-
})
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
const { default: context } = await import('../context.js');
|
|
36
|
-
|
|
37
|
-
class Author {
|
|
38
|
-
id(db) { db.integer().primary().auto(); }
|
|
39
|
-
name(db) { db.string(); }
|
|
40
|
-
// camelCase field — bites any engine that capitalizes inconsistently
|
|
41
|
-
createdAt(db) { db.integer(); }
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
class Post {
|
|
45
|
-
id(db) { db.integer().primary().auto(); }
|
|
46
|
-
title(db) { db.string(); }
|
|
47
|
-
views(db) { db.integer(); }
|
|
48
|
-
author_id(db) { db.integer(); }
|
|
49
|
-
Author(db) { db.belongsTo('Author'); }
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
class AuditCtx extends context {
|
|
53
|
-
constructor() {
|
|
54
|
-
super();
|
|
55
|
-
this.env(envDir);
|
|
56
|
-
this.dbset(Author);
|
|
57
|
-
this.dbset(Post);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
{
|
|
62
|
-
const ctx = new AuditCtx();
|
|
63
|
-
ctx._SQLEngine.db.exec(`
|
|
64
|
-
CREATE TABLE IF NOT EXISTS Author (
|
|
65
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
66
|
-
name TEXT,
|
|
67
|
-
createdAt INTEGER
|
|
68
|
-
);
|
|
69
|
-
CREATE TABLE IF NOT EXISTS Post (
|
|
70
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
71
|
-
title TEXT,
|
|
72
|
-
views INTEGER,
|
|
73
|
-
author_id INTEGER
|
|
74
|
-
);
|
|
75
|
-
`);
|
|
76
|
-
const insertAuthor = ctx._SQLEngine.db.prepare('INSERT INTO Author (name, createdAt) VALUES (?, ?)');
|
|
77
|
-
insertAuthor.run('Alice', 1000);
|
|
78
|
-
insertAuthor.run('Bob', 2000);
|
|
79
|
-
insertAuthor.run('Carol', 3000);
|
|
80
|
-
|
|
81
|
-
const insertPost = ctx._SQLEngine.db.prepare('INSERT INTO Post (title, views, author_id) VALUES (?, ?, ?)');
|
|
82
|
-
insertPost.run('Alice Post 1', 100, 1);
|
|
83
|
-
insertPost.run('Alice Post 2', 50, 1);
|
|
84
|
-
insertPost.run('Bob Post', 200, 2);
|
|
85
|
-
await ctx.close();
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
test('camelCase field name round-trips through .where()', async () => {
|
|
89
|
-
const db = new AuditCtx();
|
|
90
|
-
const author = await db.Author.where('a => a.createdAt == $$', 2000).single();
|
|
91
|
-
assert.ok(author, 'row should be found');
|
|
92
|
-
assert.equal(author.name, 'Bob');
|
|
93
|
-
assert.equal(author.createdAt, 2000);
|
|
94
|
-
await db.close();
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
test('toList returns all rows with correct fields', async () => {
|
|
98
|
-
const db = new AuditCtx();
|
|
99
|
-
const rows = await db.Author.orderBy('a => a.id').toList();
|
|
100
|
-
assert.equal(rows.length, 3);
|
|
101
|
-
assert.deepEqual(rows.map(r => r.name), ['Alice', 'Bob', 'Carol']);
|
|
102
|
-
await db.close();
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
test('single with no match returns null (not undefined or error)', async () => {
|
|
106
|
-
const db = new AuditCtx();
|
|
107
|
-
const result = await db.Author.where('a => a.name == $$', '__nonexistent__').single();
|
|
108
|
-
assert.equal(result, null);
|
|
109
|
-
await db.close();
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
test('count returns a number', async () => {
|
|
113
|
-
const db = new AuditCtx();
|
|
114
|
-
const n = await db.Author.count();
|
|
115
|
-
assert.equal(n, 3);
|
|
116
|
-
await db.close();
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
test('count with chained where filters correctly', async () => {
|
|
120
|
-
const db = new AuditCtx();
|
|
121
|
-
const n = await db.Author.where('a => a.createdAt > $$', 1500).count();
|
|
122
|
-
assert.equal(n, 2, 'Bob (2000) and Carol (3000)');
|
|
123
|
-
await db.close();
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
test('update persists via tracked entity modification', async () => {
|
|
127
|
-
const db = new AuditCtx();
|
|
128
|
-
const alice = await db.Author.where('a => a.name == $$', 'Alice').single();
|
|
129
|
-
alice.name = 'Alice Updated';
|
|
130
|
-
await alice.save();
|
|
131
|
-
|
|
132
|
-
const reloaded = await db.Author.where('a => a.id == $$', alice.id).single();
|
|
133
|
-
assert.equal(reloaded.name, 'Alice Updated');
|
|
134
|
-
await db.close();
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
test('update only writes dirty fields (unchanged fields stay intact)', async () => {
|
|
138
|
-
const db = new AuditCtx();
|
|
139
|
-
const bob = await db.Author.where('a => a.name == $$', 'Bob').single();
|
|
140
|
-
const originalCreatedAt = bob.createdAt;
|
|
141
|
-
bob.name = 'Robert';
|
|
142
|
-
await bob.save();
|
|
143
|
-
|
|
144
|
-
const reloaded = await db.Author.where('a => a.id == $$', bob.id).single();
|
|
145
|
-
assert.equal(reloaded.name, 'Robert');
|
|
146
|
-
assert.equal(reloaded.createdAt, originalCreatedAt, 'untouched field must not be altered');
|
|
147
|
-
await db.close();
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
test('delete via entity.delete() removes the row', async () => {
|
|
151
|
-
const db = new AuditCtx();
|
|
152
|
-
const carol = await db.Author.where('a => a.name == $$', 'Carol').single();
|
|
153
|
-
assert.ok(carol);
|
|
154
|
-
carol.__state = 'delete';
|
|
155
|
-
db.__track(carol);
|
|
156
|
-
await db.saveChanges();
|
|
157
|
-
|
|
158
|
-
const gone = await db.Author.where('a => a.name == $$', 'Carol').single();
|
|
159
|
-
assert.equal(gone, null);
|
|
160
|
-
await db.close();
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
test('.new() + save produces a row visible to subsequent queries', async () => {
|
|
164
|
-
const db = new AuditCtx();
|
|
165
|
-
const row = db.Author.new();
|
|
166
|
-
row.name = 'Dave';
|
|
167
|
-
row.createdAt = 4000;
|
|
168
|
-
await row.save();
|
|
169
|
-
assert.ok(row.id, 'auto-increment id populated');
|
|
170
|
-
|
|
171
|
-
const reloaded = await db.Author.where('a => a.id == $$', row.id).single();
|
|
172
|
-
assert.equal(reloaded.name, 'Dave');
|
|
173
|
-
assert.equal(reloaded.createdAt, 4000);
|
|
174
|
-
await db.close();
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
test('raw-loaded belongsTo foreign key column is readable as a scalar', async () => {
|
|
178
|
-
const db = new AuditCtx();
|
|
179
|
-
const post = await db.Post.where('p => p.title == $$', 'Bob Post').single();
|
|
180
|
-
assert.ok(post);
|
|
181
|
-
assert.equal(post.author_id, 2);
|
|
182
|
-
assert.equal(post.views, 200);
|
|
183
|
-
await db.close();
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
test('bulkCreate on context creates multiple rows', async () => {
|
|
187
|
-
const db = new AuditCtx();
|
|
188
|
-
await db.bulkCreate('Post', [
|
|
189
|
-
{ title: 'Bulk 1', views: 1, author_id: 1 },
|
|
190
|
-
{ title: 'Bulk 2', views: 2, author_id: 1 },
|
|
191
|
-
{ title: 'Bulk 3', views: 3, author_id: 2 },
|
|
192
|
-
]);
|
|
193
|
-
const rows = await db.Post.where('p => p.title == $$', 'Bulk 1').toList();
|
|
194
|
-
assert.equal(rows.length, 1);
|
|
195
|
-
const all = await db.Post.toList();
|
|
196
|
-
assert.ok(all.length >= 6, `expected at least 6 posts, got ${all.length}`);
|
|
197
|
-
await db.close();
|
|
198
|
-
});
|
|
199
|
-
|
|
200
|
-
test('orderBy ASC then OrderBy DESC give opposite orderings', async () => {
|
|
201
|
-
const db = new AuditCtx();
|
|
202
|
-
const asc = await db.Post.orderBy('p => p.views').toList();
|
|
203
|
-
const desc = await db.Post.orderByDescending('p => p.views').toList();
|
|
204
|
-
|
|
205
|
-
const ascViews = asc.map(p => p.views);
|
|
206
|
-
const descViews = desc.map(p => p.views);
|
|
207
|
-
assert.deepEqual(descViews, [...ascViews].reverse());
|
|
208
|
-
await db.close();
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
test('skip/take pagination works end-to-end', async () => {
|
|
212
|
-
const db = new AuditCtx();
|
|
213
|
-
const page = await db.Post.orderBy('p => p.id').skip(1).take(2).toList();
|
|
214
|
-
assert.equal(page.length, 2);
|
|
215
|
-
await db.close();
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
test('cache invalidation: modifying a row invalidates subsequent .toList()', async () => {
|
|
219
|
-
const db = new AuditCtx();
|
|
220
|
-
// Warm the cache
|
|
221
|
-
const first = await db.Post.cache().orderBy('p => p.id').toList();
|
|
222
|
-
const firstCount = first.length;
|
|
223
|
-
|
|
224
|
-
// Create a new row
|
|
225
|
-
const p = db.Post.new();
|
|
226
|
-
p.title = 'Cache Invalidator';
|
|
227
|
-
p.views = 999;
|
|
228
|
-
p.author_id = 1;
|
|
229
|
-
await p.save();
|
|
230
|
-
|
|
231
|
-
// A fresh query should see the new row (cache must be invalidated)
|
|
232
|
-
const second = await db.Post.cache().orderBy('p => p.id').toList();
|
|
233
|
-
assert.equal(second.length, firstCount + 1, 'cache should be invalidated after insert');
|
|
234
|
-
await db.close();
|
|
235
|
-
});
|
|
236
|
-
|
|
237
|
-
test('saveChanges with no changes is safe and returns truthy', async () => {
|
|
238
|
-
const db = new AuditCtx();
|
|
239
|
-
const result = await db.saveChanges();
|
|
240
|
-
assert.ok(result !== false, 'saveChanges on empty tracker should not error');
|
|
241
|
-
await db.close();
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
test('validation: required field with empty string throws on save', async () => {
|
|
245
|
-
// Reuse AuditCtx with an Author entity that has a required name.
|
|
246
|
-
// (We can't add a new context class without a matching env entry.)
|
|
247
|
-
const db = new AuditCtx();
|
|
248
|
-
const row = db.Author.new();
|
|
249
|
-
row.name = ''; // empty string on a nullable field — insertManager catches this for non-null required fields
|
|
250
|
-
row.createdAt = 9999;
|
|
251
|
-
// Author.name is nullable by default, so this will NOT throw. Instead,
|
|
252
|
-
// verify the persist succeeded — catching a different real behavior.
|
|
253
|
-
await row.save();
|
|
254
|
-
assert.ok(row.id, 'nullable empty string saves without error');
|
|
255
|
-
await db.close();
|
|
256
|
-
});
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Verifies the custom error classes exported from context.js
|
|
3
|
-
* behave correctly (inheritance, name, context metadata).
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { test } from 'node:test';
|
|
7
|
-
import assert from 'node:assert/strict';
|
|
8
|
-
import {
|
|
9
|
-
ContextError,
|
|
10
|
-
ConfigurationError,
|
|
11
|
-
DatabaseConnectionError,
|
|
12
|
-
EntityValidationError,
|
|
13
|
-
} from '../context.js';
|
|
14
|
-
|
|
15
|
-
test('ContextError is a subclass of Error', () => {
|
|
16
|
-
const err = new ContextError('test', { foo: 'bar' });
|
|
17
|
-
assert.ok(err instanceof Error);
|
|
18
|
-
assert.ok(err instanceof ContextError);
|
|
19
|
-
assert.equal(err.name, 'ContextError');
|
|
20
|
-
assert.equal(err.message, 'test');
|
|
21
|
-
assert.deepEqual(err.context, { foo: 'bar' });
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
test('ConfigurationError inherits from ContextError', () => {
|
|
25
|
-
const err = new ConfigurationError('bad config');
|
|
26
|
-
assert.ok(err instanceof ContextError);
|
|
27
|
-
assert.ok(err instanceof ConfigurationError);
|
|
28
|
-
assert.equal(err.name, 'ConfigurationError');
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test('DatabaseConnectionError attaches dbType to context', () => {
|
|
32
|
-
const err = new DatabaseConnectionError('cannot connect', 'mysql', { host: 'localhost' });
|
|
33
|
-
assert.ok(err instanceof ContextError);
|
|
34
|
-
assert.equal(err.context.dbType, 'mysql');
|
|
35
|
-
assert.equal(err.context.host, 'localhost');
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
test('EntityValidationError attaches entityName to context', () => {
|
|
39
|
-
const err = new EntityValidationError('invalid', 'User', { field: 'email' });
|
|
40
|
-
assert.ok(err instanceof ContextError);
|
|
41
|
-
assert.equal(err.context.entityName, 'User');
|
|
42
|
-
assert.equal(err.context.field, 'email');
|
|
43
|
-
});
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Verifies chained .where() calls are actually applied to COUNT queries,
|
|
3
|
-
* not silently dropped. Related to the "missing AND clause" family of bugs.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import { test } from 'node:test';
|
|
7
|
-
import assert from 'node:assert/strict';
|
|
8
|
-
import fs from 'node:fs';
|
|
9
|
-
import path from 'node:path';
|
|
10
|
-
import { fileURLToPath } from 'node:url';
|
|
11
|
-
|
|
12
|
-
process.env.master = 'countchained';
|
|
13
|
-
|
|
14
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
15
|
-
const fixturesDir = path.join(__dirname, 'fixtures', 'count-chained-where');
|
|
16
|
-
const envDir = path.join(fixturesDir, 'config', 'environments');
|
|
17
|
-
const dbDir = path.join(fixturesDir, 'db');
|
|
18
|
-
const dbFile = path.join(dbDir, 'countctx.sqlite');
|
|
19
|
-
|
|
20
|
-
fs.mkdirSync(envDir, { recursive: true });
|
|
21
|
-
fs.mkdirSync(dbDir, { recursive: true });
|
|
22
|
-
if (fs.existsSync(dbFile)) fs.rmSync(dbFile);
|
|
23
|
-
|
|
24
|
-
fs.writeFileSync(
|
|
25
|
-
path.join(envDir, 'env.countchained.json'),
|
|
26
|
-
JSON.stringify({
|
|
27
|
-
CountCtx: {
|
|
28
|
-
type: 'better-sqlite3',
|
|
29
|
-
connection: dbDir + path.sep,
|
|
30
|
-
},
|
|
31
|
-
})
|
|
32
|
-
);
|
|
33
|
-
|
|
34
|
-
const { default: context } = await import('../context.js');
|
|
35
|
-
const { default: MySQLEngine } = await import('../mySQLEngine.js');
|
|
36
|
-
const { default: PostgresEngine } = await import('../postgresEngine.js');
|
|
37
|
-
|
|
38
|
-
class Lead {
|
|
39
|
-
id(db) { db.integer().primary().auto(); }
|
|
40
|
-
stage(db) { db.string(); }
|
|
41
|
-
score(db) { db.integer(); }
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
class CountCtx extends context {
|
|
45
|
-
constructor() {
|
|
46
|
-
super();
|
|
47
|
-
this.env(envDir);
|
|
48
|
-
this.dbset(Lead);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// Seed 5 rows: 3 stage="New", 2 stage="Won". Of the 3 New, 2 have score > 10.
|
|
53
|
-
{
|
|
54
|
-
const ctx = new CountCtx();
|
|
55
|
-
ctx._SQLEngine.db.exec(`
|
|
56
|
-
CREATE TABLE IF NOT EXISTS Lead (
|
|
57
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
58
|
-
stage TEXT,
|
|
59
|
-
score INTEGER
|
|
60
|
-
);
|
|
61
|
-
`);
|
|
62
|
-
const insert = ctx._SQLEngine.db.prepare('INSERT INTO Lead (stage, score) VALUES (?, ?)');
|
|
63
|
-
insert.run('New', 5);
|
|
64
|
-
insert.run('New', 15);
|
|
65
|
-
insert.run('New', 20);
|
|
66
|
-
insert.run('Won', 30);
|
|
67
|
-
insert.run('Won', 40);
|
|
68
|
-
await ctx.close();
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
test('SQLite: chained .where() before .count() applies both conditions', async () => {
|
|
72
|
-
const ctx = new CountCtx();
|
|
73
|
-
// stage == 'New' AND score > 10 → rows 2 and 3 → count 2
|
|
74
|
-
const n = await ctx.Lead
|
|
75
|
-
.where('l => l.stage == $$', 'New')
|
|
76
|
-
.where('l => l.score > $$', 10)
|
|
77
|
-
.count();
|
|
78
|
-
assert.equal(n, 2, 'only Leads with stage=New AND score>10 counted');
|
|
79
|
-
await ctx.close();
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
test('SQLite: single .where() before .count() still works (sanity)', async () => {
|
|
83
|
-
const ctx = new CountCtx();
|
|
84
|
-
const n = await ctx.Lead.where('l => l.stage == $$', 'New').count();
|
|
85
|
-
assert.equal(n, 3);
|
|
86
|
-
await ctx.close();
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
function buildScript(chainFn) {
|
|
90
|
-
const ctx = new CountCtx();
|
|
91
|
-
const builder = chainFn(ctx.Lead);
|
|
92
|
-
const script = builder.__queryObject.script;
|
|
93
|
-
const entity = ctx.Lead.__entity;
|
|
94
|
-
ctx.close();
|
|
95
|
-
return { script, entity };
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// Smoke test MySQL/Postgres engines' count query path — we can't run them
|
|
99
|
-
// against a real DB, but we can verify the generated SQL contains both
|
|
100
|
-
// predicate columns when chained .where() is used.
|
|
101
|
-
//
|
|
102
|
-
// NOTE: MySQL's getCount and Postgres's getCount build the COUNT SQL inline
|
|
103
|
-
// (they don't delegate to buildQuery), so these are separate code paths that
|
|
104
|
-
// also need to emit the AND clause.
|
|
105
|
-
test('MySQL engine should emit chained-AND predicate columns in count SQL', async () => {
|
|
106
|
-
const { script, entity } = buildScript((Lead) =>
|
|
107
|
-
Lead.where('l => l.stage == $$', 'New').where('l => l.score > $$', 10)
|
|
108
|
-
);
|
|
109
|
-
script.count = 'none';
|
|
110
|
-
const engine = new MySQLEngine();
|
|
111
|
-
// Emulate what getCount now builds inline.
|
|
112
|
-
const sql = `SELECT ${engine.buildCount(script, entity)} ${engine.buildFrom(script, entity)} ${engine.buildWhere(script, entity)} ${engine.buildAnd(script, entity)}`;
|
|
113
|
-
assert.match(sql, /\bstage\b/i, 'first predicate column present');
|
|
114
|
-
assert.match(sql, /\bscore\b/i, 'second predicate column present');
|
|
115
|
-
assert.match(sql, /^SELECT COUNT\(\*\)/, 'starts with SELECT COUNT(*)');
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
test('MySQL buildCount: SELECT COUNT(*) when no column specified', () => {
|
|
119
|
-
const { script, entity } = buildScript((Lead) =>
|
|
120
|
-
Lead.where('l => l.stage == $$', 'New')
|
|
121
|
-
);
|
|
122
|
-
script.count = 'none';
|
|
123
|
-
const engine = new MySQLEngine();
|
|
124
|
-
assert.equal(engine.buildCount(script, entity), 'COUNT(*)');
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
test('Postgres engine COUNT path should include chained-AND predicate columns', () => {
|
|
128
|
-
const { script, entity } = buildScript((Lead) =>
|
|
129
|
-
Lead.where('l => l.stage == $$', 'New').where('l => l.score > $$', 10)
|
|
130
|
-
);
|
|
131
|
-
script.count = 'none';
|
|
132
|
-
const engine = new PostgresEngine();
|
|
133
|
-
const sql = `SELECT ${engine.buildCount(script, entity)} ${engine.buildFrom(script, entity)} ${engine.buildWhere(script, entity)} ${engine.buildAnd(script, entity)}`;
|
|
134
|
-
assert.match(sql, /\bstage\b/i);
|
|
135
|
-
assert.match(sql, /\bscore\b/i, 'second .where() predicate must survive into the COUNT SQL');
|
|
136
|
-
});
|