masterrecord 0.3.68 → 1.0.0
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 +26 -7
- 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 +26 -6
- package/package.json +34 -5
- package/postgresEngine.js +29 -8
- 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/esm-imports.test.js +110 -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/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/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,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Verifies that every public entry point in the exports map loads cleanly
|
|
3
|
+
* as ESM and produces the expected default export shape.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { test } from 'node:test';
|
|
7
|
+
import assert from 'node:assert/strict';
|
|
8
|
+
|
|
9
|
+
test('MasterRecord.js default export has context and schema', async () => {
|
|
10
|
+
const mr = (await import('../MasterRecord.js')).default;
|
|
11
|
+
assert.ok(mr, 'default export exists');
|
|
12
|
+
assert.ok(mr.context, 'has context');
|
|
13
|
+
assert.ok(mr.schema, 'has schema');
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
test('context.js exports default class and named error classes', async () => {
|
|
17
|
+
const mod = await import('../context.js');
|
|
18
|
+
assert.equal(typeof mod.default, 'function', 'default is a class');
|
|
19
|
+
assert.equal(typeof mod.ContextError, 'function');
|
|
20
|
+
assert.equal(typeof mod.ConfigurationError, 'function');
|
|
21
|
+
assert.equal(typeof mod.DatabaseConnectionError, 'function');
|
|
22
|
+
assert.equal(typeof mod.EntityValidationError, 'function');
|
|
23
|
+
assert.equal(typeof mod._poolKey, 'function');
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
test('Tools.js loads as default export', async () => {
|
|
27
|
+
const Tools = (await import('../Tools.js')).default;
|
|
28
|
+
assert.equal(typeof Tools, 'function');
|
|
29
|
+
assert.equal(typeof Tools.capitalize, 'function');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('QueryCache loads and instantiates', async () => {
|
|
33
|
+
const QueryCache = (await import('../Cache/QueryCache.js')).default;
|
|
34
|
+
const cache = new QueryCache({ ttl: 1000, maxSize: 10 });
|
|
35
|
+
assert.ok(cache);
|
|
36
|
+
assert.equal(typeof cache.generateKey, 'function');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('Entity/entityModel loads', async () => {
|
|
40
|
+
const EntityModel = (await import('../Entity/entityModel.js')).default;
|
|
41
|
+
const em = new EntityModel('test');
|
|
42
|
+
assert.equal(em.obj.name, 'test');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test('Entity/entityModelBuilder loads', async () => {
|
|
46
|
+
const { default: EMB } = await import('../Entity/entityModelBuilder.js');
|
|
47
|
+
assert.equal(typeof EMB, 'function');
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test('Entity/entityTrackerModel loads', async () => {
|
|
51
|
+
const { default: ETM } = await import('../Entity/entityTrackerModel.js');
|
|
52
|
+
assert.equal(typeof ETM, 'function');
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test('Entity/fieldTransformer loads', async () => {
|
|
56
|
+
const { default: FT } = await import('../Entity/fieldTransformer.js');
|
|
57
|
+
assert.equal(typeof FT.hasTransformer, 'function');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
test('QueryLanguage modules load', async () => {
|
|
61
|
+
const qm = (await import('../QueryLanguage/queryMethods.js')).default;
|
|
62
|
+
const qp = (await import('../QueryLanguage/queryParameters.js')).default;
|
|
63
|
+
const qs = (await import('../QueryLanguage/queryScript.js')).default;
|
|
64
|
+
assert.equal(typeof qm, 'function');
|
|
65
|
+
assert.equal(typeof qp, 'function');
|
|
66
|
+
assert.equal(typeof qs, 'function');
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test('SQL engines load', async () => {
|
|
70
|
+
const { default: SQLiteEngine } = await import('../SQLLiteEngine.js');
|
|
71
|
+
const { default: MySQLEngine } = await import('../mySQLEngine.js');
|
|
72
|
+
const { default: PostgresEngine } = await import('../postgresEngine.js');
|
|
73
|
+
assert.equal(typeof SQLiteEngine, 'function');
|
|
74
|
+
assert.equal(typeof MySQLEngine, 'function');
|
|
75
|
+
assert.equal(typeof PostgresEngine, 'function');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test('Connection clients load', async () => {
|
|
79
|
+
const { default: MySQLAsyncClient } = await import('../mySQLConnect.js');
|
|
80
|
+
const { default: PostgresSyncConnect } = await import('../postgresSyncConnect.js');
|
|
81
|
+
assert.equal(typeof MySQLAsyncClient, 'function');
|
|
82
|
+
assert.equal(typeof PostgresSyncConnect, 'function');
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('insertManager and deleteManager load', async () => {
|
|
86
|
+
const { default: InsertManager } = await import('../insertManager.js');
|
|
87
|
+
const { default: DeleteManager } = await import('../deleteManager.js');
|
|
88
|
+
assert.equal(typeof InsertManager, 'function');
|
|
89
|
+
assert.equal(typeof DeleteManager, 'function');
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
test('Migrations/schema loads', async () => {
|
|
93
|
+
const { default: schema } = await import('../Migrations/schema.js');
|
|
94
|
+
assert.equal(typeof schema, 'function');
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test('Migrations/migrations loads', async () => {
|
|
98
|
+
const { default: Migrations } = await import('../Migrations/migrations.js');
|
|
99
|
+
assert.equal(typeof Migrations, 'function');
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test('Migrations/migrationTemplate loads', async () => {
|
|
103
|
+
const { default: MT } = await import('../Migrations/migrationTemplate.js');
|
|
104
|
+
assert.equal(typeof MT, 'function');
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
test('Migrations/dependencyGraph loads', async () => {
|
|
108
|
+
const { default: DG } = await import('../Migrations/dependencyGraph.js');
|
|
109
|
+
assert.equal(typeof DG, 'function');
|
|
110
|
+
});
|
|
@@ -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
|
+
{"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,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
|
+
});
|