masterrecord 0.3.68 → 1.0.1
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/.claude/settings.local.json +3 -1
- package/CHANGES.md +31 -2
- package/Cache/QueryCache.js +10 -4
- package/Cache/RedisQueryCache.js +3 -2
- package/Entity/entityModel.js +1 -1
- package/Entity/entityModelBuilder.js +2 -2
- package/Entity/entityTrackerModel.js +3 -3
- package/Entity/fieldTransformer.js +1 -1
- package/FOREIGN_KEY_STRING_FIX.md +2 -2
- package/MasterRecord.js +6 -43
- package/Migrations/cli.js +44 -113
- package/Migrations/dependencyGraph.js +1 -1
- package/Migrations/migrationMySQLQuery.js +1 -1
- package/Migrations/migrationPostgresQuery.js +1 -1
- package/Migrations/migrationSQLiteQuery.js +1 -1
- package/Migrations/migrationTemplate.js +8 -39
- package/Migrations/migrations.js +16 -14
- package/Migrations/pathUtils.js +2 -5
- package/Migrations/pathUtils.test.js +33 -49
- package/Migrations/schema.js +52 -89
- package/QueryLanguage/queryMethods.js +4 -4
- package/QueryLanguage/queryParameters.js +1 -1
- package/QueryLanguage/queryScript.js +4 -3
- package/SQLLiteEngine.js +30 -8
- package/Tools.js +10 -8
- package/context.js +43 -42
- package/deleteManager.js +2 -2
- package/docs/MIGRATIONS_GUIDE.md +237 -532
- package/docs/POSTGRESQL_SETUP.md +4 -4
- package/docs/QUERY_CACHING_GUIDE.md +8 -7
- package/eslint.config.js +40 -0
- package/insertManager.js +28 -20
- package/mySQLConnect.js +2 -2
- package/mySQLEngine.js +161 -16
- package/package.json +34 -5
- package/postgresEngine.js +74 -26
- package/postgresSyncConnect.js +5 -3
- package/prettier.config.js +22 -0
- package/readme.md +35 -27
- package/test/context-errors.test.js +43 -0
- package/test/count-chained-where.test.js +136 -0
- package/test/esm-imports.test.js +110 -0
- package/test/fixtures/count-chained-where/config/environments/env.countchained.json +1 -0
- package/test/fixtures/count-chained-where/db/countctx.sqlite +0 -0
- package/test/fixtures/new-setter-large-string/config/environments/env.newsetter.json +1 -0
- package/test/fixtures/new-setter-large-string/db/newsettercontext.sqlite +0 -0
- package/test/fixtures/query-builder-parity/config/environments/env.parity.json +1 -0
- package/test/fixtures/query-builder-parity/db/parityctx.sqlite +0 -0
- package/test/fixtures/sqlite-lifecycle/config/environments/env.test.json +1 -0
- package/test/fixtures/sqlite-lifecycle/db/lifecyclecontext.sqlite +0 -0
- package/test/migration-template.test.js +47 -0
- package/test/new-setter-large-string.test.js +191 -0
- package/test/pool-key.test.js +43 -0
- package/test/query-builder-parity.test.js +260 -0
- package/test/sqlite-lifecycle.test.js +76 -0
- package/.eslintrc.js +0 -290
- package/.prettierrc.js +0 -109
- package/test/anyCommaStringTest.js +0 -237
- package/test/anyMethodTest.js +0 -176
- package/test/attachDetached.test.js +0 -303
- package/test/bulk-operations-test.js +0 -235
- package/test/cache-toObject-test.js +0 -105
- package/test/cacheIntegration.test.js +0 -319
- package/test/config-glob-pattern-test.js +0 -232
- package/test/double-where-bug-test.js +0 -71
- package/test/ensure-ready-test.js +0 -251
- package/test/entity-deduplication-test.js +0 -174
- package/test/entity-methods-test.js +0 -269
- package/test/findByIdTest.js +0 -227
- package/test/foreign-key-string-value-test.js +0 -406
- package/test/global-model-registry-test.js +0 -538
- package/test/id-setting-validation.js +0 -202
- package/test/includesFeatureTest.js +0 -183
- package/test/includesTransformTest.js +0 -110
- package/test/index-bug-fix-test.js +0 -188
- package/test/insert-return-test.js +0 -39
- package/test/lifecycle-hooks-test.js +0 -258
- package/test/multiContextCache.test.js +0 -230
- package/test/new-entity-migration-test.js +0 -162
- package/test/newMethodTest.js +0 -330
- package/test/newMethodUnitTest.js +0 -320
- package/test/optInCache.test.js +0 -221
- package/test/parameterizedPlaceholderTest.js +0 -159
- package/test/pool-registry-test.js +0 -303
- package/test/post-insert-tracking-test.js +0 -258
- package/test/postgresEngineTest.js +0 -463
- package/test/postgresIntegrationTest.js +0 -381
- package/test/promise-detection-test.js +0 -290
- package/test/qa-context-pattern-test.js +0 -319
- package/test/query-helpers-test.js +0 -258
- package/test/query-isolation-test.js +0 -59
- package/test/queryCache.test.js +0 -148
- package/test/securityTest.js +0 -268
- package/test/seed-data-test.js +0 -212
- package/test/seed-deduplication-test.js +0 -313
- package/test/seed-features-integration-test.js +0 -418
- package/test/seed-migration-template-test.js +0 -220
- package/test/singleDollarPlaceholderTest.js +0 -238
- package/test/tablePrefixTest.js +0 -100
- package/test/transformerTest.js +0 -287
- package/test/v0.3.34-bug-fixes-test.js +0 -428
- package/test/validation-test.js +0 -302
- package/test/whereChainingTest.js +0 -88
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"NewSetterContext":{"type":"better-sqlite3","connection":"/Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/test/fixtures/new-setter-large-string/db/"}}
|
|
Binary file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"ParityCtx":{"type":"better-sqlite3","connection":"/Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/test/fixtures/query-builder-parity/db/"}}
|
|
Binary file
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"LifecycleContext":{"type":"better-sqlite3","connection":"/Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/test/fixtures/sqlite-lifecycle/db/"}}
|
|
File without changes
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Verifies MigrationTemplate emits valid ESM migration source.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { test } from 'node:test';
|
|
6
|
+
import assert from 'node:assert/strict';
|
|
7
|
+
import MigrationTemplate from '../Migrations/migrationTemplate.js';
|
|
8
|
+
|
|
9
|
+
test('template emits ESM import of masterrecord', () => {
|
|
10
|
+
const tpl = new MigrationTemplate('CreateUser');
|
|
11
|
+
const src = tpl.get();
|
|
12
|
+
assert.match(src, /^import masterrecord from 'masterrecord';/m);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test('template emits ESM export default of class name', () => {
|
|
16
|
+
const tpl = new MigrationTemplate('CreateUser');
|
|
17
|
+
const src = tpl.get();
|
|
18
|
+
assert.match(src, /export default CreateUser;/);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('template never emits CJS syntax', () => {
|
|
22
|
+
const tpl = new MigrationTemplate('AddIndex');
|
|
23
|
+
const src = tpl.get();
|
|
24
|
+
assert.doesNotMatch(src, /require\(/);
|
|
25
|
+
assert.doesNotMatch(src, /module\.exports/);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('template class extends masterrecord.schema', () => {
|
|
29
|
+
const tpl = new MigrationTemplate('RenameColumn');
|
|
30
|
+
const src = tpl.get();
|
|
31
|
+
assert.match(src, /class RenameColumn extends masterrecord\.schema/);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('template has async up and down methods', () => {
|
|
35
|
+
const tpl = new MigrationTemplate('DropTable');
|
|
36
|
+
const src = tpl.get();
|
|
37
|
+
assert.match(src, /async up\(table\)/);
|
|
38
|
+
assert.match(src, /async down\(table\)/);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('createTable appends to up and down methods', () => {
|
|
42
|
+
const tpl = new MigrationTemplate('AddUsers');
|
|
43
|
+
tpl.createTable('up', 'users');
|
|
44
|
+
tpl.createTable('down', 'users');
|
|
45
|
+
const src = tpl.get();
|
|
46
|
+
assert.match(src, /await this\.createTable\(table\.users\);/);
|
|
47
|
+
});
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reproduction test for the `.new()` setter bug where larger fields
|
|
3
|
+
* (e.g. a big JSON string) get lost between the setter and the
|
|
4
|
+
* SQL INSERT. This file proves the bug exists before we fix it, and
|
|
5
|
+
* then serves as the regression test after the fix.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { test } from 'node:test';
|
|
9
|
+
import assert from 'node:assert/strict';
|
|
10
|
+
import fs from 'node:fs';
|
|
11
|
+
import path from 'node:path';
|
|
12
|
+
import { fileURLToPath } from 'node:url';
|
|
13
|
+
|
|
14
|
+
process.env.master = 'newsetter';
|
|
15
|
+
|
|
16
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
17
|
+
const fixturesDir = path.join(__dirname, 'fixtures', 'new-setter-large-string');
|
|
18
|
+
const envDir = path.join(fixturesDir, 'config', 'environments');
|
|
19
|
+
const dbDir = path.join(fixturesDir, 'db');
|
|
20
|
+
const dbFile = path.join(dbDir, 'NewSetterContext.sqlite');
|
|
21
|
+
|
|
22
|
+
fs.mkdirSync(envDir, { recursive: true });
|
|
23
|
+
fs.mkdirSync(dbDir, { recursive: true });
|
|
24
|
+
if (fs.existsSync(dbFile)) fs.rmSync(dbFile);
|
|
25
|
+
|
|
26
|
+
fs.writeFileSync(
|
|
27
|
+
path.join(envDir, 'env.newsetter.json'),
|
|
28
|
+
JSON.stringify({
|
|
29
|
+
NewSetterContext: {
|
|
30
|
+
type: 'better-sqlite3',
|
|
31
|
+
connection: dbDir + path.sep,
|
|
32
|
+
},
|
|
33
|
+
})
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
const { default: context } = await import('../context.js');
|
|
37
|
+
|
|
38
|
+
class Template {
|
|
39
|
+
id(db) { db.integer().primary().auto(); }
|
|
40
|
+
name(db) { db.string(); }
|
|
41
|
+
task_type(db) { db.string(); }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
class TemplateVersion {
|
|
45
|
+
id(db) { db.integer().primary().auto(); }
|
|
46
|
+
template_id(db) { db.integer(); }
|
|
47
|
+
version(db) { db.integer(); }
|
|
48
|
+
schema_json(db) {
|
|
49
|
+
db.text().transform({
|
|
50
|
+
toDatabase: (v) => (typeof v === 'string' ? v : JSON.stringify(v)),
|
|
51
|
+
fromDatabase: (v) => {
|
|
52
|
+
if (typeof v !== 'string') return v;
|
|
53
|
+
try { return JSON.parse(v); } catch { return v; }
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
Template(db) { db.belongsTo('Template'); }
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
class NewSetterContext extends context {
|
|
61
|
+
constructor() {
|
|
62
|
+
super();
|
|
63
|
+
this.env(envDir);
|
|
64
|
+
this.dbset(Template);
|
|
65
|
+
this.dbset(TemplateVersion);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Create tables via raw SQL so we bypass the migration machinery.
|
|
70
|
+
{
|
|
71
|
+
const ctx = new NewSetterContext();
|
|
72
|
+
ctx._SQLEngine.db.exec(`
|
|
73
|
+
CREATE TABLE IF NOT EXISTS Template (
|
|
74
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
75
|
+
name TEXT,
|
|
76
|
+
task_type TEXT
|
|
77
|
+
);
|
|
78
|
+
CREATE TABLE IF NOT EXISTS TemplateVersion (
|
|
79
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
80
|
+
template_id INTEGER,
|
|
81
|
+
version INTEGER,
|
|
82
|
+
schema_json TEXT
|
|
83
|
+
);
|
|
84
|
+
`);
|
|
85
|
+
await ctx.close();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const largeJson = JSON.stringify({
|
|
89
|
+
type: 'object',
|
|
90
|
+
properties: {
|
|
91
|
+
name: { type: 'string', minLength: 1, maxLength: 200 },
|
|
92
|
+
payload: {
|
|
93
|
+
type: 'object',
|
|
94
|
+
additionalProperties: false,
|
|
95
|
+
properties: Object.fromEntries(
|
|
96
|
+
Array.from({ length: 25 }, (_, i) => [
|
|
97
|
+
`field_${i}`,
|
|
98
|
+
{ type: 'string', description: `Description for field ${i} that is long enough to matter.` },
|
|
99
|
+
])
|
|
100
|
+
),
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
required: ['name', 'payload'],
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
test('.new() persists version row when schema_json is set as a STRING (transformer passthrough)', async () => {
|
|
107
|
+
const db = new NewSetterContext();
|
|
108
|
+
|
|
109
|
+
// Create the template row first
|
|
110
|
+
const tpl = db.Template.new();
|
|
111
|
+
tpl.name = 'governance-template';
|
|
112
|
+
tpl.task_type = 'evaluation';
|
|
113
|
+
await tpl.save();
|
|
114
|
+
|
|
115
|
+
const tplRow = await db.Template.where('t => t.name == $$', 'governance-template').single();
|
|
116
|
+
assert.ok(tplRow, 'template persisted');
|
|
117
|
+
assert.equal(tplRow.name, 'governance-template');
|
|
118
|
+
assert.equal(tplRow.task_type, 'evaluation');
|
|
119
|
+
|
|
120
|
+
// Set schema_json as a string — the transformer passes strings through
|
|
121
|
+
// on write and JSON.parses them on read.
|
|
122
|
+
const ver = db.TemplateVersion.new();
|
|
123
|
+
ver.template_id = tplRow.id;
|
|
124
|
+
ver.version = 1;
|
|
125
|
+
ver.schema_json = largeJson;
|
|
126
|
+
await ver.save();
|
|
127
|
+
|
|
128
|
+
const verRow = await db.TemplateVersion
|
|
129
|
+
.where('v => v.template_id == $$', tplRow.id)
|
|
130
|
+
.single();
|
|
131
|
+
assert.ok(verRow, 'version row persisted');
|
|
132
|
+
assert.equal(verRow.template_id, tplRow.id);
|
|
133
|
+
assert.equal(verRow.version, 1);
|
|
134
|
+
// fromDatabase parses the JSON string back into an object on read
|
|
135
|
+
assert.deepEqual(verRow.schema_json, JSON.parse(largeJson));
|
|
136
|
+
await db.close();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
test('.new() persists template and version rows when reusing the same context', async () => {
|
|
140
|
+
const db = new NewSetterContext();
|
|
141
|
+
|
|
142
|
+
const tpl = db.Template.new();
|
|
143
|
+
tpl.name = 'second-template';
|
|
144
|
+
tpl.task_type = 'classification';
|
|
145
|
+
await tpl.save();
|
|
146
|
+
|
|
147
|
+
const tplRow = await db.Template.where('t => t.name == $$', 'second-template').single();
|
|
148
|
+
|
|
149
|
+
const ver = db.TemplateVersion.new();
|
|
150
|
+
ver.template_id = tplRow.id;
|
|
151
|
+
ver.version = 1;
|
|
152
|
+
ver.schema_json = largeJson;
|
|
153
|
+
await ver.save();
|
|
154
|
+
|
|
155
|
+
const verRow = await db.TemplateVersion
|
|
156
|
+
.where('v => v.template_id == $$', tplRow.id)
|
|
157
|
+
.single();
|
|
158
|
+
assert.ok(verRow, 'version row persisted');
|
|
159
|
+
assert.deepEqual(verRow.schema_json, JSON.parse(largeJson));
|
|
160
|
+
await db.close();
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test('.new() persists version row when schema_json is passed as an OBJECT (transformer path)', async () => {
|
|
164
|
+
const db = new NewSetterContext();
|
|
165
|
+
|
|
166
|
+
const tpl = db.Template.new();
|
|
167
|
+
tpl.name = 'object-template';
|
|
168
|
+
tpl.task_type = 'classification';
|
|
169
|
+
await tpl.save();
|
|
170
|
+
|
|
171
|
+
const tplRow = await db.Template.where('t => t.name == $$', 'object-template').single();
|
|
172
|
+
|
|
173
|
+
const ver = db.TemplateVersion.new();
|
|
174
|
+
ver.template_id = tplRow.id;
|
|
175
|
+
ver.version = 1;
|
|
176
|
+
// Pass an OBJECT — the transform is supposed to JSON.stringify it.
|
|
177
|
+
// This is the path that governance-style seeders use.
|
|
178
|
+
ver.schema_json = JSON.parse(largeJson);
|
|
179
|
+
await ver.save();
|
|
180
|
+
|
|
181
|
+
const verRow = await db.TemplateVersion
|
|
182
|
+
.where('v => v.template_id == $$', tplRow.id)
|
|
183
|
+
.single();
|
|
184
|
+
assert.ok(verRow, 'version row persisted');
|
|
185
|
+
assert.ok(
|
|
186
|
+
verRow.schema_json && typeof verRow.schema_json === 'object',
|
|
187
|
+
'schema_json should come back as an object via the transformer'
|
|
188
|
+
);
|
|
189
|
+
assert.deepEqual(verRow.schema_json, JSON.parse(largeJson));
|
|
190
|
+
await db.close();
|
|
191
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,260 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* End-to-end SQLite lifecycle test.
|
|
3
|
+
* Creates a context, an entity, inserts, reads, updates, deletes, and closes.
|
|
4
|
+
*
|
|
5
|
+
* This is the smoke test that proves the ESM conversion didn't break the
|
|
6
|
+
* core ORM flow for the default database driver.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { test, after } from 'node:test';
|
|
10
|
+
import assert from 'node:assert/strict';
|
|
11
|
+
import fs from 'node:fs';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
import { fileURLToPath } from 'node:url';
|
|
14
|
+
|
|
15
|
+
// Point the context at our fixture config
|
|
16
|
+
process.env.master = 'test';
|
|
17
|
+
|
|
18
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
19
|
+
const fixturesDir = path.join(__dirname, 'fixtures', 'sqlite-lifecycle');
|
|
20
|
+
const envDir = path.join(fixturesDir, 'config', 'environments');
|
|
21
|
+
const dbDir = path.join(fixturesDir, 'db');
|
|
22
|
+
const dbFile = path.join(dbDir, 'LifecycleContext.sqlite');
|
|
23
|
+
|
|
24
|
+
// Clean state
|
|
25
|
+
fs.mkdirSync(envDir, { recursive: true });
|
|
26
|
+
fs.mkdirSync(dbDir, { recursive: true });
|
|
27
|
+
if (fs.existsSync(dbFile)) fs.rmSync(dbFile);
|
|
28
|
+
|
|
29
|
+
fs.writeFileSync(
|
|
30
|
+
path.join(envDir, 'env.test.json'),
|
|
31
|
+
JSON.stringify({
|
|
32
|
+
LifecycleContext: {
|
|
33
|
+
type: 'better-sqlite3',
|
|
34
|
+
connection: dbDir + path.sep,
|
|
35
|
+
},
|
|
36
|
+
})
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
const { default: context } = await import('../context.js');
|
|
40
|
+
|
|
41
|
+
class Widget {
|
|
42
|
+
id(db) { db.integer().primary().auto(); }
|
|
43
|
+
name(db) { db.string(); }
|
|
44
|
+
qty(db) { db.integer(); }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
class LifecycleContext extends context {
|
|
48
|
+
constructor() {
|
|
49
|
+
super();
|
|
50
|
+
this.env(envDir);
|
|
51
|
+
this.dbset(Widget);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const ctx = new LifecycleContext();
|
|
56
|
+
|
|
57
|
+
after(async () => {
|
|
58
|
+
if (ctx && typeof ctx.close === 'function') {
|
|
59
|
+
await ctx.close();
|
|
60
|
+
}
|
|
61
|
+
// Leave fixtures on disk for debugging; they're regenerated each run.
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
test('context instantiates as SQLite', () => {
|
|
65
|
+
assert.equal(ctx.isSQLite, true);
|
|
66
|
+
assert.equal(ctx.isMySQL, false);
|
|
67
|
+
assert.equal(ctx.isPostgres, false);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('context exposes the registered entity', () => {
|
|
71
|
+
assert.ok(ctx.Widget, 'Widget dbset is attached');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('context has a SQL engine', () => {
|
|
75
|
+
assert.ok(ctx._SQLEngine, 'SQL engine was initialized');
|
|
76
|
+
});
|