masterrecord 1.1.3 → 1.1.5
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 +18 -0
- package/package.json +14 -1
- 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,249 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Regression for the Postgres CamelCase-identifier bug.
|
|
3
|
-
*
|
|
4
|
-
* Symptom: DDL was correctly emitted as CREATE TABLE "SchedulerLeader" (...)
|
|
5
|
-
* but runtime SELECT/UPDATE/DELETE used unquoted identifiers, so Postgres
|
|
6
|
-
* folded `SchedulerLeader` to `schedulerleader` and threw:
|
|
7
|
-
* ERROR: relation "schedulerleader" does not exist
|
|
8
|
-
*
|
|
9
|
-
* The fix double-quotes every table and column name in generated SQL.
|
|
10
|
-
*
|
|
11
|
-
* These tests build SQL via the engine's pure-function builders (no live
|
|
12
|
-
* Postgres connection required) and assert the resulting SQL contains
|
|
13
|
-
* properly-quoted identifiers.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import { test } from 'node:test';
|
|
17
|
-
import assert from 'node:assert/strict';
|
|
18
|
-
import fs from 'node:fs';
|
|
19
|
-
import path from 'node:path';
|
|
20
|
-
import { fileURLToPath } from 'node:url';
|
|
21
|
-
|
|
22
|
-
process.env.master = 'pgcamelcase';
|
|
23
|
-
|
|
24
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
25
|
-
const fixturesDir = path.join(__dirname, 'fixtures', 'pg-camelcase');
|
|
26
|
-
const envDir = path.join(fixturesDir, 'config', 'environments');
|
|
27
|
-
const dbDir = path.join(fixturesDir, 'db');
|
|
28
|
-
const dbFile = path.join(dbDir, 'pgcamelctx.sqlite');
|
|
29
|
-
|
|
30
|
-
fs.mkdirSync(envDir, { recursive: true });
|
|
31
|
-
fs.mkdirSync(dbDir, { recursive: true });
|
|
32
|
-
if (fs.existsSync(dbFile)) fs.rmSync(dbFile);
|
|
33
|
-
|
|
34
|
-
fs.writeFileSync(
|
|
35
|
-
path.join(envDir, 'env.pgcamelcase.json'),
|
|
36
|
-
JSON.stringify({
|
|
37
|
-
PgCamelCtx: {
|
|
38
|
-
type: 'better-sqlite3',
|
|
39
|
-
connection: dbDir + path.sep,
|
|
40
|
-
},
|
|
41
|
-
})
|
|
42
|
-
);
|
|
43
|
-
|
|
44
|
-
const { default: context } = await import('../context.js');
|
|
45
|
-
const { default: PostgresEngine } = await import('../postgresEngine.js');
|
|
46
|
-
|
|
47
|
-
// CamelCase entities — the exact pattern that triggered the production bug.
|
|
48
|
-
class SchedulerLeader {
|
|
49
|
-
id(db) { db.integer().primary().auto(); }
|
|
50
|
-
nodeName(db) { db.string(); }
|
|
51
|
-
acquiredAt(db) { db.integer(); }
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
class MemoryDoc {
|
|
55
|
-
id(db) { db.integer().primary().auto(); }
|
|
56
|
-
title(db) { db.string(); }
|
|
57
|
-
body(db) { db.text(); }
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
class PgCamelCtx extends context {
|
|
61
|
-
constructor() {
|
|
62
|
-
super();
|
|
63
|
-
this.env(envDir);
|
|
64
|
-
this.dbset(SchedulerLeader);
|
|
65
|
-
this.dbset(MemoryDoc);
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
// Helper: drive the fluent API through SQLite to get a populated query
|
|
70
|
-
// script, then feed it to the Postgres engine builders.
|
|
71
|
-
function buildScript(chainFn) {
|
|
72
|
-
const ctx = new PgCamelCtx();
|
|
73
|
-
const builder = chainFn(ctx);
|
|
74
|
-
const script = builder.__queryObject.script;
|
|
75
|
-
const entity = builder.__entity;
|
|
76
|
-
ctx.close();
|
|
77
|
-
return { script, entity };
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
test('Postgres _q helper quotes CamelCase identifiers', () => {
|
|
81
|
-
const engine = new PostgresEngine();
|
|
82
|
-
assert.equal(engine._q('SchedulerLeader'), '"SchedulerLeader"');
|
|
83
|
-
assert.equal(engine._q('nodeName'), '"nodeName"');
|
|
84
|
-
assert.equal(engine._q('lower'), '"lower"');
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
test('Postgres _q helper passes through *', () => {
|
|
88
|
-
const engine = new PostgresEngine();
|
|
89
|
-
assert.equal(engine._q('*'), '*');
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
test('Postgres _q helper escapes embedded double-quotes', () => {
|
|
93
|
-
const engine = new PostgresEngine();
|
|
94
|
-
assert.equal(engine._q('weird"name'), '"weird""name"');
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
test('Postgres _q helper is idempotent (already-quoted passes through)', () => {
|
|
98
|
-
const engine = new PostgresEngine();
|
|
99
|
-
assert.equal(engine._q('"Quoted"'), '"Quoted"');
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
test('Postgres buildFrom emits quoted CamelCase table name with alias', () => {
|
|
103
|
-
const { script, entity } = buildScript((ctx) =>
|
|
104
|
-
ctx.SchedulerLeader.where('l => l.nodeName == $$', 'node-1')
|
|
105
|
-
);
|
|
106
|
-
const engine = new PostgresEngine();
|
|
107
|
-
const from = engine.buildFrom(script, entity);
|
|
108
|
-
assert.match(from, /FROM\s+"SchedulerLeader"/, `FROM clause must quote the CamelCase table name. Got: ${from}`);
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
test('Postgres buildWhere emits quoted CamelCase column name and does NOT capitalize it', () => {
|
|
112
|
-
const { script, entity } = buildScript((ctx) =>
|
|
113
|
-
ctx.SchedulerLeader.where('l => l.nodeName == $$', 'node-1')
|
|
114
|
-
);
|
|
115
|
-
const engine = new PostgresEngine();
|
|
116
|
-
const where = engine.buildWhere(script, entity);
|
|
117
|
-
assert.match(where, /"nodeName"/, `WHERE clause must use quoted "nodeName" (original case). Got: ${where}`);
|
|
118
|
-
assert.doesNotMatch(where, /"NodeName"/, 'WHERE must NOT capitalize the field — Postgres preserves case when quoted, so "NodeName" would not match the actual "nodeName" column');
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
test('Postgres full buildQuery emits quoted table, alias, and columns', () => {
|
|
122
|
-
const { script, entity } = buildScript((ctx) =>
|
|
123
|
-
ctx.SchedulerLeader
|
|
124
|
-
.where('l => l.nodeName == $$', 'node-1')
|
|
125
|
-
.orderBy('l => l.acquiredAt')
|
|
126
|
-
);
|
|
127
|
-
const engine = new PostgresEngine();
|
|
128
|
-
const out = engine.buildQuery(script, entity, {});
|
|
129
|
-
|
|
130
|
-
// Table quoted with alias
|
|
131
|
-
assert.match(out.query, /FROM\s+"SchedulerLeader"\s+AS\s+\w+/);
|
|
132
|
-
// Column names in SELECT are quoted
|
|
133
|
-
assert.match(out.query, /\w+\."id"/);
|
|
134
|
-
assert.match(out.query, /\w+\."nodeName"/);
|
|
135
|
-
// ORDER BY uses original case
|
|
136
|
-
assert.match(out.query, /ORDER BY\s+\w+\."acquiredAt"\s+ASC/);
|
|
137
|
-
// No unquoted CamelCase table name leaking through
|
|
138
|
-
assert.doesNotMatch(out.query, /FROM\s+SchedulerLeader\b/, 'unquoted CamelCase table must not appear');
|
|
139
|
-
});
|
|
140
|
-
|
|
141
|
-
test('Postgres INSERT emits quoted CamelCase table and columns', () => {
|
|
142
|
-
const engine = new PostgresEngine();
|
|
143
|
-
const fields = {
|
|
144
|
-
__entity: { __name: 'MemoryDoc' },
|
|
145
|
-
title: 'hello',
|
|
146
|
-
body: 'world',
|
|
147
|
-
};
|
|
148
|
-
const modelEntity = {
|
|
149
|
-
__name: 'MemoryDoc',
|
|
150
|
-
id: { name: 'id', type: 'integer', primary: true, auto: true },
|
|
151
|
-
title: { name: 'title', type: 'string' },
|
|
152
|
-
body: { name: 'body', type: 'text' },
|
|
153
|
-
};
|
|
154
|
-
const sqlObj = engine._buildSQLInsertObjectParameterized(fields, modelEntity);
|
|
155
|
-
assert.notEqual(sqlObj, -1);
|
|
156
|
-
assert.equal(sqlObj.tableName, 'MemoryDoc');
|
|
157
|
-
assert.match(sqlObj.columns, /"title"/);
|
|
158
|
-
assert.match(sqlObj.columns, /"body"/);
|
|
159
|
-
});
|
|
160
|
-
|
|
161
|
-
test('Postgres UPDATE SET clause quotes each column name', () => {
|
|
162
|
-
const engine = new PostgresEngine();
|
|
163
|
-
const model = {
|
|
164
|
-
__entity: {
|
|
165
|
-
__name: 'SchedulerLeader',
|
|
166
|
-
nodeName: { name: 'nodeName', type: 'string', nullable: true },
|
|
167
|
-
acquiredAt: { name: 'acquiredAt', type: 'integer', nullable: true },
|
|
168
|
-
},
|
|
169
|
-
__dirtyFields: ['nodeName', 'acquiredAt'],
|
|
170
|
-
_nodeName: 'node-2',
|
|
171
|
-
_acquiredAt: 12345,
|
|
172
|
-
};
|
|
173
|
-
const argu = engine._buildSQLEqualToParameterized(model);
|
|
174
|
-
assert.notEqual(argu, -1);
|
|
175
|
-
assert.match(argu.query, /"nodeName"\s*=\s*\$1/);
|
|
176
|
-
assert.match(argu.query, /"acquiredAt"\s*=\s*\$2/);
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
test('Postgres COUNT(*) for untyped count (not COUNT(alias.*) which is invalid)', () => {
|
|
180
|
-
const engine = new PostgresEngine();
|
|
181
|
-
const script = { count: 'none', entityMap: [], parentName: 'SchedulerLeader' };
|
|
182
|
-
const entity = { __name: 'SchedulerLeader' };
|
|
183
|
-
assert.equal(engine.buildCount(script, entity), 'COUNT(*)');
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
test('Postgres DELETE emits quoted CamelCase table and primary key', async () => {
|
|
187
|
-
const engine = new PostgresEngine();
|
|
188
|
-
// Stub out _runWithParams so we can capture the generated SQL without
|
|
189
|
-
// needing a live Postgres connection.
|
|
190
|
-
let capturedSql;
|
|
191
|
-
let capturedParams;
|
|
192
|
-
engine._runWithParams = async (sql, params) => {
|
|
193
|
-
capturedSql = sql;
|
|
194
|
-
capturedParams = params;
|
|
195
|
-
return { rows: [], rowCount: 0 };
|
|
196
|
-
};
|
|
197
|
-
const queryObject = {
|
|
198
|
-
__entity: {
|
|
199
|
-
__name: 'SchedulerLeader',
|
|
200
|
-
id: { name: 'id', primary: true, auto: true },
|
|
201
|
-
nodeName: { name: 'nodeName' },
|
|
202
|
-
},
|
|
203
|
-
id: 42,
|
|
204
|
-
};
|
|
205
|
-
await engine.delete(queryObject);
|
|
206
|
-
assert.match(capturedSql, /DELETE FROM "SchedulerLeader" WHERE "SchedulerLeader"\."id"\s*=\s*\$1/);
|
|
207
|
-
assert.deepEqual(capturedParams, [42]);
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
test('Postgres UPDATE wraps table and primary key', async () => {
|
|
211
|
-
const engine = new PostgresEngine();
|
|
212
|
-
let capturedSql;
|
|
213
|
-
engine._runWithParams = async (sql, params) => {
|
|
214
|
-
capturedSql = sql;
|
|
215
|
-
return { rows: [], rowCount: 0 };
|
|
216
|
-
};
|
|
217
|
-
await engine.update({
|
|
218
|
-
tableName: 'SchedulerLeader',
|
|
219
|
-
primaryKey: 'id',
|
|
220
|
-
primaryKeyValue: 7,
|
|
221
|
-
arg: {
|
|
222
|
-
query: '"nodeName" = $1',
|
|
223
|
-
params: ['node-9'],
|
|
224
|
-
},
|
|
225
|
-
});
|
|
226
|
-
assert.match(capturedSql, /UPDATE "SchedulerLeader" SET "nodeName" = \$1 WHERE "SchedulerLeader"\."id" = \$2/);
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
test('Postgres bulkDelete uses entity primary key (not hardcoded id) and quotes it', async () => {
|
|
230
|
-
const engine = new PostgresEngine();
|
|
231
|
-
let capturedSql;
|
|
232
|
-
engine._runWithParams = async (sql, params) => {
|
|
233
|
-
capturedSql = sql;
|
|
234
|
-
return { rows: [], rowCount: 0 };
|
|
235
|
-
};
|
|
236
|
-
await engine.bulkDelete('MemoryDoc', [1, 2, 3], 'docId');
|
|
237
|
-
assert.match(capturedSql, /DELETE FROM "MemoryDoc" WHERE "docId" IN \(\$1, \$2, \$3\)/);
|
|
238
|
-
});
|
|
239
|
-
|
|
240
|
-
test('Postgres bulkDelete defaults to id for back-compat', async () => {
|
|
241
|
-
const engine = new PostgresEngine();
|
|
242
|
-
let capturedSql;
|
|
243
|
-
engine._runWithParams = async (sql, params) => {
|
|
244
|
-
capturedSql = sql;
|
|
245
|
-
return { rows: [], rowCount: 0 };
|
|
246
|
-
};
|
|
247
|
-
await engine.bulkDelete('SchedulerLeader', [10, 20]);
|
|
248
|
-
assert.match(capturedSql, /DELETE FROM "SchedulerLeader" WHERE "id" IN \(\$1, \$2\)/);
|
|
249
|
-
});
|
|
@@ -1,260 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Regression tests for cross-engine query-builder feature parity.
|
|
3
|
-
*
|
|
4
|
-
* Covers bug report `mySQLEngine.buildQuery() silently drops ORDER BY /
|
|
5
|
-
* LIMIT / OFFSET / chained AND`, plus the equivalent Postgres bug where
|
|
6
|
-
* `buildOrderBy` treated `query.orderBy` as a string (never matching the
|
|
7
|
-
* object shape the fluent API actually produces).
|
|
8
|
-
*
|
|
9
|
-
* Strategy:
|
|
10
|
-
* - Use the fluent `.orderBy()` / `.orderByDescending()` / `.skip()` /
|
|
11
|
-
* `.take()` / chained `.where()` APIs to build up a query against a
|
|
12
|
-
* real SQLite database (which exercises end-to-end).
|
|
13
|
-
* - Snapshot the internal `queryScript` state at that point.
|
|
14
|
-
* - Feed the same state into MySQL and Postgres engine `buildQuery()`
|
|
15
|
-
* methods (no connection needed — the builders are pure functions)
|
|
16
|
-
* and assert the generated SQL contains the expected clauses.
|
|
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 = 'parity';
|
|
26
|
-
|
|
27
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
28
|
-
const fixturesDir = path.join(__dirname, 'fixtures', 'query-builder-parity');
|
|
29
|
-
const envDir = path.join(fixturesDir, 'config', 'environments');
|
|
30
|
-
const dbDir = path.join(fixturesDir, 'db');
|
|
31
|
-
const dbFile = path.join(dbDir, 'parityctx.sqlite');
|
|
32
|
-
|
|
33
|
-
fs.mkdirSync(envDir, { recursive: true });
|
|
34
|
-
fs.mkdirSync(dbDir, { recursive: true });
|
|
35
|
-
if (fs.existsSync(dbFile)) fs.rmSync(dbFile);
|
|
36
|
-
|
|
37
|
-
fs.writeFileSync(
|
|
38
|
-
path.join(envDir, 'env.parity.json'),
|
|
39
|
-
JSON.stringify({
|
|
40
|
-
ParityCtx: {
|
|
41
|
-
type: 'better-sqlite3',
|
|
42
|
-
connection: dbDir + path.sep,
|
|
43
|
-
},
|
|
44
|
-
})
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
const { default: context } = await import('../context.js');
|
|
48
|
-
const { default: MySQLEngine } = await import('../mySQLEngine.js');
|
|
49
|
-
const { default: PostgresEngine } = await import('../postgresEngine.js');
|
|
50
|
-
|
|
51
|
-
class Lead {
|
|
52
|
-
id(db) { db.integer().primary().auto(); }
|
|
53
|
-
stage(db) { db.string(); }
|
|
54
|
-
updatedAt(db) { db.integer(); }
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
class ParityCtx extends context {
|
|
58
|
-
constructor() {
|
|
59
|
-
super();
|
|
60
|
-
this.env(envDir);
|
|
61
|
-
this.dbset(Lead);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// Seed the SQLite DB with 5 rows — distinct updatedAt values so ordering is unambiguous.
|
|
66
|
-
{
|
|
67
|
-
const ctx = new ParityCtx();
|
|
68
|
-
ctx._SQLEngine.db.exec(`
|
|
69
|
-
CREATE TABLE IF NOT EXISTS Lead (
|
|
70
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
71
|
-
stage TEXT,
|
|
72
|
-
updatedAt INTEGER
|
|
73
|
-
);
|
|
74
|
-
`);
|
|
75
|
-
const rows = [
|
|
76
|
-
{ stage: 'New', updatedAt: 1000 },
|
|
77
|
-
{ stage: 'New', updatedAt: 5000 },
|
|
78
|
-
{ stage: 'New', updatedAt: 3000 },
|
|
79
|
-
{ stage: 'New', updatedAt: 2000 },
|
|
80
|
-
{ stage: 'Won', updatedAt: 4000 },
|
|
81
|
-
];
|
|
82
|
-
const insert = ctx._SQLEngine.db.prepare('INSERT INTO Lead (stage, updatedAt) VALUES (?, ?)');
|
|
83
|
-
for (const r of rows) insert.run(r.stage, r.updatedAt);
|
|
84
|
-
await ctx.close();
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
//
|
|
88
|
-
// SQLite — end-to-end: the query actually hits the DB.
|
|
89
|
-
// These confirm the fluent API itself is wired correctly and that the
|
|
90
|
-
// SQLite engine applies the clauses (prior behavior; regression guard).
|
|
91
|
-
//
|
|
92
|
-
test('SQLite: orderBy(updatedAt) returns rows in ascending order', async () => {
|
|
93
|
-
const ctx = new ParityCtx();
|
|
94
|
-
const rows = await ctx.Lead
|
|
95
|
-
.where('l => l.stage == $$', 'New')
|
|
96
|
-
.orderBy('l => l.updatedAt')
|
|
97
|
-
.toList();
|
|
98
|
-
const ts = rows.map(r => r.updatedAt);
|
|
99
|
-
assert.deepEqual(ts, [1000, 2000, 3000, 5000], 'ascending updatedAt');
|
|
100
|
-
await ctx.close();
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
test('SQLite: orderByDescending(updatedAt) returns rows in descending order', async () => {
|
|
104
|
-
const ctx = new ParityCtx();
|
|
105
|
-
const rows = await ctx.Lead
|
|
106
|
-
.where('l => l.stage == $$', 'New')
|
|
107
|
-
.orderByDescending('l => l.updatedAt')
|
|
108
|
-
.toList();
|
|
109
|
-
const ts = rows.map(r => r.updatedAt);
|
|
110
|
-
assert.deepEqual(ts, [5000, 3000, 2000, 1000], 'descending updatedAt');
|
|
111
|
-
await ctx.close();
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
test('SQLite: skip/take paginates correctly', async () => {
|
|
115
|
-
const ctx = new ParityCtx();
|
|
116
|
-
const rows = await ctx.Lead
|
|
117
|
-
.orderBy('l => l.updatedAt')
|
|
118
|
-
.skip(1).take(2)
|
|
119
|
-
.toList();
|
|
120
|
-
const ts = rows.map(r => r.updatedAt);
|
|
121
|
-
assert.deepEqual(ts, [2000, 3000], 'pagination skips first row, takes next two');
|
|
122
|
-
await ctx.close();
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
test('SQLite: chained where() calls AND together', async () => {
|
|
126
|
-
const ctx = new ParityCtx();
|
|
127
|
-
const rows = await ctx.Lead
|
|
128
|
-
.where('l => l.stage == $$', 'New')
|
|
129
|
-
.where('l => l.updatedAt > $$', 1500)
|
|
130
|
-
.orderBy('l => l.updatedAt')
|
|
131
|
-
.toList();
|
|
132
|
-
const ts = rows.map(r => r.updatedAt);
|
|
133
|
-
assert.deepEqual(ts, [2000, 3000, 5000], 'chained where filters correctly');
|
|
134
|
-
await ctx.close();
|
|
135
|
-
});
|
|
136
|
-
|
|
137
|
-
//
|
|
138
|
-
// Helper: build a query state with the fluent API, return the script.
|
|
139
|
-
// We don't execute it — we just need the internal `script` that gets
|
|
140
|
-
// handed to an engine's buildQuery().
|
|
141
|
-
//
|
|
142
|
-
function buildScript(chainFn) {
|
|
143
|
-
// Use the SQLite context to drive the fluent API, then snapshot the script.
|
|
144
|
-
const ctx = new ParityCtx();
|
|
145
|
-
const builder = chainFn(ctx.Lead);
|
|
146
|
-
const script = builder.__queryObject.script;
|
|
147
|
-
// Ensure the entity we pass matches what the engine expects
|
|
148
|
-
const entity = ctx.Lead.__entity;
|
|
149
|
-
ctx.close();
|
|
150
|
-
return { script, entity };
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
//
|
|
154
|
-
// MySQL — string-output tests. Engine builders are pure functions; no
|
|
155
|
-
// connection is required to call buildQuery().
|
|
156
|
-
//
|
|
157
|
-
test('MySQL buildQuery emits ORDER BY ... ASC for .orderBy()', () => {
|
|
158
|
-
const { script, entity } = buildScript((Lead) =>
|
|
159
|
-
Lead.where('l => l.stage == $$', 'New').orderBy('l => l.updatedAt')
|
|
160
|
-
);
|
|
161
|
-
const engine = new MySQLEngine();
|
|
162
|
-
const out = engine.buildQuery(script, entity, {});
|
|
163
|
-
assert.match(out.query, /ORDER BY\s+\w+\.`updatedAt`\s+ASC/, 'ORDER BY updatedAt ASC with backtick-quoted column');
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
test('MySQL buildQuery emits ORDER BY ... DESC for .orderByDescending()', () => {
|
|
167
|
-
const { script, entity } = buildScript((Lead) =>
|
|
168
|
-
Lead.where('l => l.stage == $$', 'New').orderByDescending('l => l.updatedAt')
|
|
169
|
-
);
|
|
170
|
-
const engine = new MySQLEngine();
|
|
171
|
-
const out = engine.buildQuery(script, entity, {});
|
|
172
|
-
assert.match(out.query, /ORDER BY\s+\w+\.`updatedAt`\s+DESC/, 'ORDER BY updatedAt DESC');
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
test('MySQL buildQuery emits LIMIT and OFFSET for .take()/.skip()', () => {
|
|
176
|
-
const { script, entity } = buildScript((Lead) =>
|
|
177
|
-
Lead.orderBy('l => l.updatedAt').skip(10).take(5)
|
|
178
|
-
);
|
|
179
|
-
const engine = new MySQLEngine();
|
|
180
|
-
const out = engine.buildQuery(script, entity, {});
|
|
181
|
-
assert.match(out.query, /LIMIT\s+5/, 'LIMIT 5 present');
|
|
182
|
-
assert.match(out.query, /OFFSET\s+10/, 'OFFSET 10 present');
|
|
183
|
-
});
|
|
184
|
-
|
|
185
|
-
test('MySQL buildQuery emits AND for chained .where()', () => {
|
|
186
|
-
const { script, entity } = buildScript((Lead) =>
|
|
187
|
-
Lead.where('l => l.stage == $$', 'New').where('l => l.updatedAt > $$', 1500)
|
|
188
|
-
);
|
|
189
|
-
const engine = new MySQLEngine();
|
|
190
|
-
const out = engine.buildQuery(script, entity, {});
|
|
191
|
-
// Chained .where() can merge into a single WHERE in some engines; either
|
|
192
|
-
// an explicit "AND <expr>" or a WHERE clause that contains the second
|
|
193
|
-
// column is acceptable as long as both conditions land in the SQL.
|
|
194
|
-
assert.match(out.query, /\bstage\b/, 'first predicate column present');
|
|
195
|
-
assert.match(out.query, /\bupdatedAt\b/, 'second predicate column present');
|
|
196
|
-
});
|
|
197
|
-
|
|
198
|
-
test('MySQL buildQuery clauses appear in correct SQL order (WHERE before ORDER BY before LIMIT before OFFSET)', () => {
|
|
199
|
-
const { script, entity } = buildScript((Lead) =>
|
|
200
|
-
Lead.where('l => l.stage == $$', 'New')
|
|
201
|
-
.orderBy('l => l.updatedAt')
|
|
202
|
-
.skip(2).take(3)
|
|
203
|
-
);
|
|
204
|
-
const engine = new MySQLEngine();
|
|
205
|
-
const out = engine.buildQuery(script, entity, {});
|
|
206
|
-
const whereIdx = out.query.indexOf('WHERE');
|
|
207
|
-
const orderIdx = out.query.indexOf('ORDER BY');
|
|
208
|
-
const limitIdx = out.query.indexOf('LIMIT');
|
|
209
|
-
const offsetIdx = out.query.indexOf('OFFSET');
|
|
210
|
-
assert.ok(whereIdx >= 0 && whereIdx < orderIdx, 'WHERE before ORDER BY');
|
|
211
|
-
assert.ok(orderIdx < limitIdx, 'ORDER BY before LIMIT');
|
|
212
|
-
assert.ok(limitIdx < offsetIdx, 'LIMIT before OFFSET');
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
//
|
|
216
|
-
// Postgres — same string-output tests.
|
|
217
|
-
//
|
|
218
|
-
test('Postgres buildQuery emits ORDER BY ... ASC for .orderBy()', () => {
|
|
219
|
-
const { script, entity } = buildScript((Lead) =>
|
|
220
|
-
Lead.where('l => l.stage == $$', 'New').orderBy('l => l.updatedAt')
|
|
221
|
-
);
|
|
222
|
-
const engine = new PostgresEngine();
|
|
223
|
-
const out = engine.buildQuery(script, entity, {});
|
|
224
|
-
assert.match(out.query, /ORDER BY\s+\w+\."?updatedAt"?\s+ASC/);
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
test('Postgres buildQuery emits ORDER BY ... DESC for .orderByDescending()', () => {
|
|
228
|
-
const { script, entity } = buildScript((Lead) =>
|
|
229
|
-
Lead.where('l => l.stage == $$', 'New').orderByDescending('l => l.updatedAt')
|
|
230
|
-
);
|
|
231
|
-
const engine = new PostgresEngine();
|
|
232
|
-
const out = engine.buildQuery(script, entity, {});
|
|
233
|
-
assert.match(out.query, /ORDER BY\s+\w+\."?updatedAt"?\s+DESC/);
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
test('Postgres buildQuery emits LIMIT and OFFSET', () => {
|
|
237
|
-
const { script, entity } = buildScript((Lead) =>
|
|
238
|
-
Lead.orderBy('l => l.updatedAt').skip(10).take(5)
|
|
239
|
-
);
|
|
240
|
-
const engine = new PostgresEngine();
|
|
241
|
-
const out = engine.buildQuery(script, entity, {});
|
|
242
|
-
assert.match(out.query, /LIMIT\s+5/);
|
|
243
|
-
assert.match(out.query, /OFFSET\s+10/);
|
|
244
|
-
});
|
|
245
|
-
|
|
246
|
-
test('Postgres buildQuery clauses appear in correct SQL order (ORDER BY before LIMIT before OFFSET)', () => {
|
|
247
|
-
const { script, entity } = buildScript((Lead) =>
|
|
248
|
-
Lead.where('l => l.stage == $$', 'New')
|
|
249
|
-
.orderBy('l => l.updatedAt')
|
|
250
|
-
.skip(2).take(3)
|
|
251
|
-
);
|
|
252
|
-
const engine = new PostgresEngine();
|
|
253
|
-
const out = engine.buildQuery(script, entity, {});
|
|
254
|
-
const orderIdx = out.query.indexOf('ORDER BY');
|
|
255
|
-
const limitIdx = out.query.indexOf('LIMIT');
|
|
256
|
-
const offsetIdx = out.query.indexOf('OFFSET');
|
|
257
|
-
assert.ok(orderIdx >= 0, 'ORDER BY emitted');
|
|
258
|
-
assert.ok(orderIdx < limitIdx, 'ORDER BY before LIMIT');
|
|
259
|
-
assert.ok(limitIdx < offsetIdx, 'LIMIT before OFFSET');
|
|
260
|
-
});
|
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Reproducers for the "silently dropped writes" family of bugs that
|
|
3
|
-
* downstream consumers have been working around with raw _execute.
|
|
4
|
-
*
|
|
5
|
-
* 1. `.new()` setter does NOT apply `fieldDef.set` — tracker-entity setter
|
|
6
|
-
* does. Inconsistent behavior between INSERT and UPDATE paths.
|
|
7
|
-
* 2. `_processBatchUpdates` reads `currentModel._entity` (typo) instead of
|
|
8
|
-
* `__entity`, so `removePrimarykeyandVirtual` gets `undefined`.
|
|
9
|
-
* 3. After UPDATE, `__state` and `__dirtyFields` are not reset. On the next
|
|
10
|
-
* `saveChanges()` call, the same fields are re-written to the DB, which
|
|
11
|
-
* can silently overwrite concurrent changes from other processes.
|
|
12
|
-
* 4. Tracker setter pushes to `__dirtyFields` without de-duplication, so
|
|
13
|
-
* setting the same field multiple times produces duplicate assignments
|
|
14
|
-
* in the UPDATE SET clause. In Postgres this is a hard error
|
|
15
|
-
* ("multiple assignments to same column"); in SQLite/MySQL it may or
|
|
16
|
-
* may not work depending on driver version.
|
|
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 = 'setterdrop';
|
|
26
|
-
|
|
27
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
28
|
-
const fixturesDir = path.join(__dirname, 'fixtures', 'setter-and-update-drop');
|
|
29
|
-
const envDir = path.join(fixturesDir, 'config', 'environments');
|
|
30
|
-
const dbDir = path.join(fixturesDir, 'db');
|
|
31
|
-
const dbFile = path.join(dbDir, 'setdropctx.sqlite');
|
|
32
|
-
|
|
33
|
-
fs.mkdirSync(envDir, { recursive: true });
|
|
34
|
-
fs.mkdirSync(dbDir, { recursive: true });
|
|
35
|
-
if (fs.existsSync(dbFile)) fs.rmSync(dbFile);
|
|
36
|
-
|
|
37
|
-
fs.writeFileSync(
|
|
38
|
-
path.join(envDir, 'env.setterdrop.json'),
|
|
39
|
-
JSON.stringify({
|
|
40
|
-
SetDropCtx: {
|
|
41
|
-
type: 'better-sqlite3',
|
|
42
|
-
connection: dbDir + path.sep,
|
|
43
|
-
},
|
|
44
|
-
})
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
const { default: context } = await import('../context.js');
|
|
48
|
-
|
|
49
|
-
class User {
|
|
50
|
-
id(db) { db.integer().primary().auto(); }
|
|
51
|
-
// `name` has a .set() transform that trims and lowercases — the
|
|
52
|
-
// canonical test for whether the setter path applies fieldDef.set.
|
|
53
|
-
name(db) { db.string().set(v => (typeof v === 'string' ? v.trim().toLowerCase() : v)); }
|
|
54
|
-
email(db) { db.string(); }
|
|
55
|
-
score(db) { db.integer(); }
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
class SetDropCtx extends context {
|
|
59
|
-
constructor() {
|
|
60
|
-
super();
|
|
61
|
-
this.env(envDir);
|
|
62
|
-
this.dbset(User);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
{
|
|
67
|
-
const ctx = new SetDropCtx();
|
|
68
|
-
ctx._SQLEngine.db.exec(`
|
|
69
|
-
CREATE TABLE IF NOT EXISTS User (
|
|
70
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
71
|
-
name TEXT,
|
|
72
|
-
email TEXT,
|
|
73
|
-
score INTEGER
|
|
74
|
-
);
|
|
75
|
-
`);
|
|
76
|
-
await ctx.close();
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
//
|
|
80
|
-
// Bug #1 — .new() setter must apply fieldDef.set
|
|
81
|
-
//
|
|
82
|
-
test('.new() setter applies fieldDef.set transform at write time', async () => {
|
|
83
|
-
const ctx = new SetDropCtx();
|
|
84
|
-
const u = ctx.User.new();
|
|
85
|
-
u.name = ' ALICE '; // whitespace + uppercase
|
|
86
|
-
u.email = 'alice@example.com';
|
|
87
|
-
u.score = 10;
|
|
88
|
-
await u.save();
|
|
89
|
-
|
|
90
|
-
const row = ctx._SQLEngine.db.prepare('SELECT name FROM User WHERE email = ?').get('alice@example.com');
|
|
91
|
-
assert.equal(row.name, 'alice', `.set() transform should have been applied on INSERT; got "${row.name}"`);
|
|
92
|
-
await ctx.close();
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
test('Tracker setter applies fieldDef.set transform on UPDATE (baseline — already works)', async () => {
|
|
96
|
-
const ctx = new SetDropCtx();
|
|
97
|
-
ctx._SQLEngine.db.prepare('INSERT INTO User (name, email, score) VALUES (?, ?, ?)').run('bob', 'bob@example.com', 5);
|
|
98
|
-
|
|
99
|
-
const loaded = await ctx.User.where('u => u.email == $$', 'bob@example.com').single();
|
|
100
|
-
loaded.name = ' BOB-UPDATED ';
|
|
101
|
-
await loaded.save();
|
|
102
|
-
|
|
103
|
-
const row = ctx._SQLEngine.db.prepare('SELECT name FROM User WHERE email = ?').get('bob@example.com');
|
|
104
|
-
assert.equal(row.name, 'bob-updated', `.set() transform should apply on UPDATE; got "${row.name}"`);
|
|
105
|
-
await ctx.close();
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
//
|
|
109
|
-
// Bug #2 — after UPDATE, __dirtyFields and __state are not reset;
|
|
110
|
-
// calling saveChanges twice for the same entity silently re-updates the
|
|
111
|
-
// same fields.
|
|
112
|
-
//
|
|
113
|
-
test('After UPDATE, __dirtyFields is cleared and __state is reset to track', async () => {
|
|
114
|
-
const ctx = new SetDropCtx();
|
|
115
|
-
ctx._SQLEngine.db.prepare('INSERT INTO User (name, email, score) VALUES (?, ?, ?)').run('carol', 'carol@example.com', 1);
|
|
116
|
-
|
|
117
|
-
const loaded = await ctx.User.where('u => u.email == $$', 'carol@example.com').single();
|
|
118
|
-
loaded.score = 2;
|
|
119
|
-
await loaded.save();
|
|
120
|
-
|
|
121
|
-
assert.deepEqual(loaded.__dirtyFields, [], '__dirtyFields should be empty after UPDATE');
|
|
122
|
-
assert.equal(loaded.__state, 'track', '__state should be "track" after UPDATE');
|
|
123
|
-
await ctx.close();
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
test('saveChanges re-called with no modifications after UPDATE is a no-op', async () => {
|
|
127
|
-
const ctx = new SetDropCtx();
|
|
128
|
-
ctx._SQLEngine.db.prepare('INSERT INTO User (name, email, score) VALUES (?, ?, ?)').run('dave', 'dave@example.com', 1);
|
|
129
|
-
|
|
130
|
-
const loaded = await ctx.User.where('u => u.email == $$', 'dave@example.com').single();
|
|
131
|
-
loaded.score = 99;
|
|
132
|
-
await loaded.save();
|
|
133
|
-
|
|
134
|
-
// Simulate concurrent external write: another process updates score to 42.
|
|
135
|
-
ctx._SQLEngine.db.prepare('UPDATE User SET score = 42 WHERE email = ?').run('dave@example.com');
|
|
136
|
-
|
|
137
|
-
// Re-saving without touching the entity should NOT overwrite the external
|
|
138
|
-
// change; the entity is no longer modified.
|
|
139
|
-
await ctx.saveChanges();
|
|
140
|
-
|
|
141
|
-
const row = ctx._SQLEngine.db.prepare('SELECT score FROM User WHERE email = ?').get('dave@example.com');
|
|
142
|
-
assert.equal(row.score, 42, 'second saveChanges must not silently re-apply the prior write');
|
|
143
|
-
await ctx.close();
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
//
|
|
147
|
-
// Bug #3 — setting the same field twice must not produce duplicate
|
|
148
|
-
// SET assignments (SQLite errors on duplicates; Postgres always errors).
|
|
149
|
-
//
|
|
150
|
-
test('Setting the same field twice produces only one assignment in UPDATE', async () => {
|
|
151
|
-
const ctx = new SetDropCtx();
|
|
152
|
-
ctx._SQLEngine.db.prepare('INSERT INTO User (name, email, score) VALUES (?, ?, ?)').run('eve', 'eve@example.com', 1);
|
|
153
|
-
|
|
154
|
-
const loaded = await ctx.User.where('u => u.email == $$', 'eve@example.com').single();
|
|
155
|
-
loaded.score = 2;
|
|
156
|
-
loaded.score = 3; // second assignment — must not push duplicate into __dirtyFields
|
|
157
|
-
loaded.score = 4;
|
|
158
|
-
|
|
159
|
-
// Only one occurrence of 'score' in dirtyFields
|
|
160
|
-
const dupCount = loaded.__dirtyFields.filter(f => f === 'score').length;
|
|
161
|
-
assert.equal(dupCount, 1, `score should appear exactly once in __dirtyFields; got ${dupCount} (${JSON.stringify(loaded.__dirtyFields)})`);
|
|
162
|
-
|
|
163
|
-
// And the UPDATE must succeed and persist the last value.
|
|
164
|
-
await loaded.save();
|
|
165
|
-
const row = ctx._SQLEngine.db.prepare('SELECT score FROM User WHERE email = ?').get('eve@example.com');
|
|
166
|
-
assert.equal(row.score, 4, 'last assigned value must be persisted');
|
|
167
|
-
await ctx.close();
|
|
168
|
-
});
|