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,340 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Full-text search across all three engines (1.1.0).
|
|
3
|
-
*
|
|
4
|
-
* Schema-side: createFullTextIndex / dropFullTextIndex emit:
|
|
5
|
-
* - SQLite: FTS5 virtual table + AFTER INSERT/UPDATE/DELETE sync triggers
|
|
6
|
-
* - Postgres: tsvector column + GIN index + maintenance trigger
|
|
7
|
-
* - MySQL: ALTER TABLE ADD FULLTEXT INDEX
|
|
8
|
-
*
|
|
9
|
-
* Runtime: ctx.<Entity>.search({ in: [cols], query: 'term' }) emits the
|
|
10
|
-
* right SELECT for the engine and exposes a `__rank` column for ordering.
|
|
11
|
-
*
|
|
12
|
-
* SQLite end-to-end covers real ranking; MySQL/Postgres get string-output
|
|
13
|
-
* tests since we can't run them without live connections.
|
|
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 = 'fts';
|
|
23
|
-
|
|
24
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
25
|
-
const fixturesDir = path.join(__dirname, 'fixtures', 'fts');
|
|
26
|
-
const envDir = path.join(fixturesDir, 'config', 'environments');
|
|
27
|
-
const dbDir = path.join(fixturesDir, 'db');
|
|
28
|
-
const dbFile = path.join(dbDir, 'ftsctx.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.fts.json'),
|
|
36
|
-
JSON.stringify({
|
|
37
|
-
FtsCtx: {
|
|
38
|
-
type: 'better-sqlite3',
|
|
39
|
-
connection: dbDir + path.sep,
|
|
40
|
-
},
|
|
41
|
-
})
|
|
42
|
-
);
|
|
43
|
-
|
|
44
|
-
const { default: context } = await import('../context.js');
|
|
45
|
-
const { default: schemaCls } = await import('../Migrations/schema.js');
|
|
46
|
-
const { default: SQLiteQuery } = await import('../Migrations/migrationSQLiteQuery.js');
|
|
47
|
-
const { default: MySQLQuery } = await import('../Migrations/migrationMySQLQuery.js');
|
|
48
|
-
const { default: PostgresQuery } = await import('../Migrations/migrationPostgresQuery.js');
|
|
49
|
-
const { default: SQLiteEngine } = await import('../SQLLiteEngine.js');
|
|
50
|
-
const { default: MySQLEngine } = await import('../mySQLEngine.js');
|
|
51
|
-
const { default: PostgresEngine } = await import('../postgresEngine.js');
|
|
52
|
-
|
|
53
|
-
// =====================================================================
|
|
54
|
-
// DDL string output — no DB required
|
|
55
|
-
// =====================================================================
|
|
56
|
-
|
|
57
|
-
test('SQLite createFullTextIndex emits FTS5 virtual table + sync triggers', () => {
|
|
58
|
-
const q = new SQLiteQuery();
|
|
59
|
-
const stmts = q.createFullTextIndex({
|
|
60
|
-
tableName: 'MemoryDoc',
|
|
61
|
-
columns: ['title', 'body'],
|
|
62
|
-
});
|
|
63
|
-
// Virtual table with external content
|
|
64
|
-
assert.match(stmts[0], /CREATE VIRTUAL TABLE IF NOT EXISTS \[MemoryDoc_fts\] USING fts5/);
|
|
65
|
-
assert.match(stmts[0], /title, body/);
|
|
66
|
-
assert.match(stmts[0], /content=\[MemoryDoc\]/);
|
|
67
|
-
assert.match(stmts[0], /content_rowid=id/);
|
|
68
|
-
// Three sync triggers (AFTER INSERT, DELETE, UPDATE)
|
|
69
|
-
assert.match(stmts[1], /AFTER INSERT ON \[MemoryDoc\]/);
|
|
70
|
-
assert.match(stmts[2], /AFTER DELETE ON \[MemoryDoc\]/);
|
|
71
|
-
assert.match(stmts[3], /AFTER UPDATE ON \[MemoryDoc\]/);
|
|
72
|
-
assert.equal(stmts.length, 4);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
test('SQLite dropFullTextIndex drops triggers + virtual table in reverse order', () => {
|
|
76
|
-
const q = new SQLiteQuery();
|
|
77
|
-
const stmts = q.dropFullTextIndex({ tableName: 'MemoryDoc' });
|
|
78
|
-
assert.match(stmts[0], /DROP TRIGGER IF EXISTS \[MemoryDoc_au\]/);
|
|
79
|
-
assert.match(stmts[1], /DROP TRIGGER IF EXISTS \[MemoryDoc_ad\]/);
|
|
80
|
-
assert.match(stmts[2], /DROP TRIGGER IF EXISTS \[MemoryDoc_ai\]/);
|
|
81
|
-
assert.match(stmts[3], /DROP TABLE IF EXISTS \[MemoryDoc_fts\]/);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
test('Postgres createFullTextIndex emits tsvector + GIN + trigger', () => {
|
|
85
|
-
const q = new PostgresQuery();
|
|
86
|
-
const stmts = q.createFullTextIndex({
|
|
87
|
-
tableName: 'MemoryDoc',
|
|
88
|
-
columns: ['title', 'body'],
|
|
89
|
-
});
|
|
90
|
-
// tsvector column
|
|
91
|
-
assert.match(stmts[0], /ALTER TABLE "MemoryDoc" ADD COLUMN IF NOT EXISTS "__tsv" tsvector/);
|
|
92
|
-
// Backfill
|
|
93
|
-
assert.match(stmts[1], /UPDATE "MemoryDoc" SET "__tsv" = to_tsvector\('english'/);
|
|
94
|
-
// GIN index
|
|
95
|
-
assert.match(stmts[2], /CREATE INDEX IF NOT EXISTS "idx_memorydoc_fts" ON "MemoryDoc" USING GIN \("__tsv"\)/);
|
|
96
|
-
// Trigger function exists and references NEW columns
|
|
97
|
-
assert.match(stmts[3], /CREATE OR REPLACE FUNCTION "memorydoc_tsv_update"/);
|
|
98
|
-
assert.match(stmts[3], /NEW\."title"/);
|
|
99
|
-
assert.match(stmts[3], /NEW\."body"/);
|
|
100
|
-
assert.match(stmts[3], /\$masterrecord\$/);
|
|
101
|
-
// DROP+CREATE trigger
|
|
102
|
-
assert.match(stmts[4], /DROP TRIGGER IF EXISTS "memorydoc_tsv_trigger" ON "MemoryDoc"/);
|
|
103
|
-
assert.match(stmts[5], /CREATE TRIGGER "memorydoc_tsv_trigger" BEFORE INSERT OR UPDATE ON "MemoryDoc"/);
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
test('Postgres dropFullTextIndex drops trigger, function, index, and column', () => {
|
|
107
|
-
const q = new PostgresQuery();
|
|
108
|
-
const stmts = q.dropFullTextIndex({ tableName: 'MemoryDoc' });
|
|
109
|
-
assert.match(stmts[0], /DROP TRIGGER IF EXISTS "memorydoc_tsv_trigger" ON "MemoryDoc"/);
|
|
110
|
-
assert.match(stmts[1], /DROP FUNCTION IF EXISTS "memorydoc_tsv_update"/);
|
|
111
|
-
assert.match(stmts[2], /DROP INDEX IF EXISTS "idx_memorydoc_fts"/);
|
|
112
|
-
assert.match(stmts[3], /ALTER TABLE "MemoryDoc" DROP COLUMN IF EXISTS "__tsv"/);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
test('Postgres createFullTextIndex honors custom config (language)', () => {
|
|
116
|
-
const q = new PostgresQuery();
|
|
117
|
-
const stmts = q.createFullTextIndex({
|
|
118
|
-
tableName: 'Doc',
|
|
119
|
-
columns: ['body'],
|
|
120
|
-
config: 'spanish',
|
|
121
|
-
});
|
|
122
|
-
assert.match(stmts[1], /to_tsvector\('spanish'/);
|
|
123
|
-
assert.match(stmts[3], /to_tsvector\('spanish'/);
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
test('MySQL createFullTextIndex emits ALTER TABLE ADD FULLTEXT INDEX', () => {
|
|
127
|
-
const q = new MySQLQuery();
|
|
128
|
-
const stmts = q.createFullTextIndex({
|
|
129
|
-
tableName: 'MemoryDoc',
|
|
130
|
-
columns: ['title', 'body'],
|
|
131
|
-
});
|
|
132
|
-
assert.equal(stmts.length, 1);
|
|
133
|
-
assert.match(stmts[0], /ALTER TABLE `MemoryDoc` ADD FULLTEXT INDEX `idx_memorydoc_fts` \(`title`, `body`\)/);
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
test('MySQL dropFullTextIndex emits ALTER TABLE DROP INDEX', () => {
|
|
137
|
-
const q = new MySQLQuery();
|
|
138
|
-
const stmts = q.dropFullTextIndex({ tableName: 'MemoryDoc' });
|
|
139
|
-
assert.equal(stmts.length, 1);
|
|
140
|
-
assert.match(stmts[0], /ALTER TABLE `MemoryDoc` DROP INDEX `idx_memorydoc_fts`/);
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
// =====================================================================
|
|
144
|
-
// queryMethods.search() input validation
|
|
145
|
-
// =====================================================================
|
|
146
|
-
|
|
147
|
-
class MemoryDoc {
|
|
148
|
-
id(db) { db.integer().primary().auto(); }
|
|
149
|
-
title(db) { db.string(); }
|
|
150
|
-
body(db) { db.text(); }
|
|
151
|
-
workspaceId(db) { db.integer(); }
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
class FtsCtx extends context {
|
|
155
|
-
constructor() {
|
|
156
|
-
super();
|
|
157
|
-
this.env(envDir);
|
|
158
|
-
this.dbset(MemoryDoc);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
test('search() requires { in: [...], query: ... }', () => {
|
|
163
|
-
const ctx = new FtsCtx();
|
|
164
|
-
assert.throws(() => ctx.MemoryDoc.search('bare-string'), /requires \{ in/);
|
|
165
|
-
assert.throws(() => ctx.MemoryDoc.search({}), /requires `in:/);
|
|
166
|
-
assert.throws(() => ctx.MemoryDoc.search({ in: [] }), /non-empty array/);
|
|
167
|
-
assert.throws(() => ctx.MemoryDoc.search({ in: ['title'] }), /`query` to be a non-empty string/);
|
|
168
|
-
assert.throws(() => ctx.MemoryDoc.search({ in: ['title'], query: '' }), /non-empty string/);
|
|
169
|
-
ctx.close();
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
// =====================================================================
|
|
173
|
-
// SQLite end-to-end ranked search
|
|
174
|
-
// =====================================================================
|
|
175
|
-
|
|
176
|
-
{
|
|
177
|
-
// Seed the SQLite DB + FTS5 index via the real schema API.
|
|
178
|
-
const ctx = new FtsCtx();
|
|
179
|
-
ctx._SQLEngine.db.exec(`
|
|
180
|
-
CREATE TABLE IF NOT EXISTS MemoryDoc (
|
|
181
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
182
|
-
title TEXT,
|
|
183
|
-
body TEXT,
|
|
184
|
-
workspaceId INTEGER
|
|
185
|
-
);
|
|
186
|
-
`);
|
|
187
|
-
|
|
188
|
-
const sch = new schemaCls(FtsCtx);
|
|
189
|
-
await sch._ensureReady();
|
|
190
|
-
sch.fullTable = { __name: 'MemoryDoc' };
|
|
191
|
-
await sch.createFullTextIndex({ tableName: 'MemoryDoc', columns: ['title', 'body'] });
|
|
192
|
-
|
|
193
|
-
// Seed rows AFTER the FTS index so triggers populate the virtual table.
|
|
194
|
-
const insert = ctx._SQLEngine.db.prepare(
|
|
195
|
-
'INSERT INTO MemoryDoc (title, body, workspaceId) VALUES (?, ?, ?)'
|
|
196
|
-
);
|
|
197
|
-
insert.run('Authentication flow', 'user login session token', 1);
|
|
198
|
-
insert.run('Database migrations', 'schema changes postgres sqlite', 1);
|
|
199
|
-
insert.run('Login button styling', 'css button color hover', 1);
|
|
200
|
-
insert.run('Unrelated document', 'absolutely nothing matches here', 2);
|
|
201
|
-
|
|
202
|
-
await ctx.close();
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
test('SQLite search returns ranked results with __rank populated', async () => {
|
|
206
|
-
const ctx = new FtsCtx();
|
|
207
|
-
const rows = await ctx.MemoryDoc
|
|
208
|
-
.search({ in: ['title', 'body'], query: 'login' })
|
|
209
|
-
.toList();
|
|
210
|
-
assert.ok(rows.length >= 2, `expected at least 2 results, got ${rows.length}`);
|
|
211
|
-
// Top result must mention 'login' in title or body
|
|
212
|
-
const titles = rows.map(r => r.title);
|
|
213
|
-
assert.ok(titles.some(t => /login/i.test(t)));
|
|
214
|
-
// Every returned row exposes __rank
|
|
215
|
-
for (const r of rows) {
|
|
216
|
-
assert.equal(typeof r.__rank, 'number', `row ${r.id} missing __rank`);
|
|
217
|
-
}
|
|
218
|
-
await ctx.close();
|
|
219
|
-
});
|
|
220
|
-
|
|
221
|
-
test('SQLite search composes with .where() (workspace scoping)', async () => {
|
|
222
|
-
const ctx = new FtsCtx();
|
|
223
|
-
const rows = await ctx.MemoryDoc
|
|
224
|
-
.search({ in: ['title', 'body'], query: 'login' })
|
|
225
|
-
.where('d => d.workspaceId == $$', 1)
|
|
226
|
-
.toList();
|
|
227
|
-
assert.ok(rows.length >= 1);
|
|
228
|
-
for (const r of rows) {
|
|
229
|
-
assert.equal(r.workspaceId, 1, `row ${r.id} not scoped to workspace 1`);
|
|
230
|
-
}
|
|
231
|
-
await ctx.close();
|
|
232
|
-
});
|
|
233
|
-
|
|
234
|
-
test('SQLite search composes with .take() for pagination', async () => {
|
|
235
|
-
const ctx = new FtsCtx();
|
|
236
|
-
const rows = await ctx.MemoryDoc
|
|
237
|
-
.search({ in: ['title', 'body'], query: 'login' })
|
|
238
|
-
.take(1)
|
|
239
|
-
.toList();
|
|
240
|
-
assert.equal(rows.length, 1);
|
|
241
|
-
await ctx.close();
|
|
242
|
-
});
|
|
243
|
-
|
|
244
|
-
test('SQLite search returns empty for terms with no matches', async () => {
|
|
245
|
-
const ctx = new FtsCtx();
|
|
246
|
-
const rows = await ctx.MemoryDoc
|
|
247
|
-
.search({ in: ['title', 'body'], query: 'nonexistentword' })
|
|
248
|
-
.toList();
|
|
249
|
-
assert.equal(rows.length, 0);
|
|
250
|
-
await ctx.close();
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
// =====================================================================
|
|
254
|
-
// MySQL/Postgres SQL string output — no live DB needed
|
|
255
|
-
// =====================================================================
|
|
256
|
-
|
|
257
|
-
function buildScript(chainFn) {
|
|
258
|
-
const ctx = new FtsCtx();
|
|
259
|
-
const builder = chainFn(ctx);
|
|
260
|
-
// Mirror what `.toList()` does: bootstrap the entityMap when no prior
|
|
261
|
-
// .where()/.orderBy() chained call has touched it, otherwise buildFrom
|
|
262
|
-
// emits an empty alias and the assertions fail with `.col` (no alias).
|
|
263
|
-
if (builder.__queryObject.script.entityMap.length === 0) {
|
|
264
|
-
builder.__queryObject.skipClause(builder.__entity.__name);
|
|
265
|
-
}
|
|
266
|
-
const script = builder.__queryObject.script;
|
|
267
|
-
const entity = builder.__entity;
|
|
268
|
-
ctx.close();
|
|
269
|
-
return { script, entity };
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
test('MySQL search SQL contains MATCH(...) AGAINST(...) twice and ORDER BY __rank DESC', () => {
|
|
273
|
-
const { script, entity } = buildScript((ctx) =>
|
|
274
|
-
ctx.MemoryDoc.search({ in: ['title', 'body'], query: 'auth login' })
|
|
275
|
-
);
|
|
276
|
-
const engine = new MySQLEngine();
|
|
277
|
-
const out = engine.buildQuery(script, entity, {});
|
|
278
|
-
assert.match(out.query, /MATCH\(\w+\.`title`, \w+\.`body`\) AGAINST\(\? IN NATURAL LANGUAGE MODE\) AS __rank/);
|
|
279
|
-
assert.match(out.query, /WHERE MATCH\(\w+\.`title`, \w+\.`body`\) AGAINST\(\? IN NATURAL LANGUAGE MODE\)/);
|
|
280
|
-
assert.match(out.query, /ORDER BY __rank DESC/);
|
|
281
|
-
});
|
|
282
|
-
|
|
283
|
-
test('Postgres search SQL contains ts_rank, @@ predicate, and ORDER BY __rank DESC', () => {
|
|
284
|
-
const { script, entity } = buildScript((ctx) =>
|
|
285
|
-
ctx.MemoryDoc.search({ in: ['title', 'body'], query: 'auth login' })
|
|
286
|
-
);
|
|
287
|
-
const engine = new PostgresEngine();
|
|
288
|
-
const out = engine.buildQuery(script, entity, {});
|
|
289
|
-
assert.match(out.query, /ts_rank\(\w+\."__tsv", plainto_tsquery\(\$\d+\)\) AS __rank/);
|
|
290
|
-
assert.match(out.query, /WHERE \w+\."__tsv" @@ plainto_tsquery\(\$\d+\)/);
|
|
291
|
-
assert.match(out.query, /ORDER BY __rank DESC/);
|
|
292
|
-
// Search term bound twice (once for SELECT, once for WHERE)
|
|
293
|
-
assert.equal(out.params.length, 2);
|
|
294
|
-
assert.equal(out.params[0], 'auth login');
|
|
295
|
-
assert.equal(out.params[1], 'auth login');
|
|
296
|
-
});
|
|
297
|
-
|
|
298
|
-
test('Postgres search composes with prior .where() (parameter ordering preserved)', () => {
|
|
299
|
-
const { script, entity } = buildScript((ctx) =>
|
|
300
|
-
ctx.MemoryDoc
|
|
301
|
-
.where('d => d.workspaceId == $$', 42)
|
|
302
|
-
.search({ in: ['title', 'body'], query: 'login' })
|
|
303
|
-
);
|
|
304
|
-
const engine = new PostgresEngine();
|
|
305
|
-
const out = engine.buildQuery(script, entity, {});
|
|
306
|
-
// WHERE has the workspace predicate AND the @@ predicate
|
|
307
|
-
assert.match(out.query, /WHERE.*workspaceId.*AND.*@@/s);
|
|
308
|
-
// Params: workspace 42 first, then search term twice
|
|
309
|
-
assert.equal(out.params[0], 42);
|
|
310
|
-
assert.equal(out.params[1], 'login');
|
|
311
|
-
assert.equal(out.params[2], 'login');
|
|
312
|
-
});
|
|
313
|
-
|
|
314
|
-
test('MySQL search clauses appear in correct SQL order (WHERE before ORDER BY before LIMIT)', () => {
|
|
315
|
-
const { script, entity } = buildScript((ctx) =>
|
|
316
|
-
ctx.MemoryDoc.search({ in: ['title', 'body'], query: 'login' }).take(5)
|
|
317
|
-
);
|
|
318
|
-
const engine = new MySQLEngine();
|
|
319
|
-
const out = engine.buildQuery(script, entity, {});
|
|
320
|
-
const whereIdx = out.query.indexOf('WHERE');
|
|
321
|
-
const orderIdx = out.query.indexOf('ORDER BY');
|
|
322
|
-
const limitIdx = out.query.indexOf('LIMIT');
|
|
323
|
-
assert.ok(whereIdx >= 0 && whereIdx < orderIdx, 'WHERE before ORDER BY');
|
|
324
|
-
assert.ok(orderIdx < limitIdx, 'ORDER BY before LIMIT');
|
|
325
|
-
});
|
|
326
|
-
|
|
327
|
-
test('All engines: schema.createFullTextIndex throws if columns missing', async () => {
|
|
328
|
-
const ctx = new FtsCtx();
|
|
329
|
-
const sch = new schemaCls(FtsCtx);
|
|
330
|
-
await sch._ensureReady();
|
|
331
|
-
await assert.rejects(
|
|
332
|
-
sch.createFullTextIndex({ tableName: 'MemoryDoc' }),
|
|
333
|
-
/requires \{ tableName, columns/
|
|
334
|
-
);
|
|
335
|
-
await assert.rejects(
|
|
336
|
-
sch.createFullTextIndex({ columns: ['title'] }),
|
|
337
|
-
/requires \{ tableName, columns/
|
|
338
|
-
);
|
|
339
|
-
await ctx.close();
|
|
340
|
-
});
|
|
@@ -1,47 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,191 +0,0 @@
|
|
|
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
|
-
});
|
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
|
-
});
|