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/test/pool-key.test.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Verifies the internal _poolKey helper generates stable, unique keys.
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { test } from 'node:test';
|
|
6
|
-
import assert from 'node:assert/strict';
|
|
7
|
-
import { _poolKey } from '../context.js';
|
|
8
|
-
|
|
9
|
-
test('_poolKey generates distinct keys for different MySQL databases', () => {
|
|
10
|
-
const key1 = _poolKey('mysql', { user: 'root', host: 'localhost', port: 3306, database: 'db1' });
|
|
11
|
-
const key2 = _poolKey('mysql', { user: 'root', host: 'localhost', port: 3306, database: 'db2' });
|
|
12
|
-
assert.notEqual(key1, key2);
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
test('_poolKey generates identical keys for identical MySQL configs', () => {
|
|
16
|
-
const cfg = { user: 'root', host: 'localhost', port: 3306, database: 'app' };
|
|
17
|
-
assert.equal(_poolKey('mysql', cfg), _poolKey('mysql', cfg));
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
test('_poolKey uses default port 3306 for MySQL when missing', () => {
|
|
21
|
-
const key = _poolKey('mysql', { user: 'root', host: 'localhost', database: 'app' });
|
|
22
|
-
assert.match(key, /:3306\//);
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
test('_poolKey uses default port 5432 for Postgres when missing', () => {
|
|
26
|
-
const key = _poolKey('postgres', { user: 'postgres', host: 'localhost', database: 'app' });
|
|
27
|
-
assert.match(key, /:5432\//);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test('_poolKey distinguishes MySQL and Postgres for same database', () => {
|
|
31
|
-
const cfg = { user: 'u', host: 'h', database: 'app' };
|
|
32
|
-
assert.notEqual(_poolKey('mysql', cfg), _poolKey('postgres', cfg));
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
test('_poolKey uses connection path for sqlite', () => {
|
|
36
|
-
const key = _poolKey('sqlite', { connection: '/tmp/foo.db' });
|
|
37
|
-
assert.equal(key, 'sqlite:/tmp/foo.db');
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
test('_poolKey prefers completeConnection over connection for sqlite', () => {
|
|
41
|
-
const key = _poolKey('sqlite', { connection: '/a', completeConnection: '/b/full.db' });
|
|
42
|
-
assert.equal(key, 'sqlite:/b/full.db');
|
|
43
|
-
});
|
|
@@ -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
|
-
});
|