masterrecord 1.0.6 → 1.0.7
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 +4 -1
- package/QueryLanguage/queryMethods.js +8 -0
- package/QueryLanguage/queryScript.js +18 -8
- package/SQLLiteEngine.js +17 -7
- package/context.js +34 -7
- package/mySQLEngine.js +23 -7
- package/package.json +1 -1
- package/postgresEngine.js +123 -35
- package/test/count-no-from-audit.test.js +1 -1
- package/test/ctx-dollar-placeholder.test.js +142 -0
- package/test/postgres-camelcase-quoting.test.js +249 -0
- package/test/query-builder-parity.test.js +2 -2
- package/test/fixtures/broad-audit/config/environments/env.audit.json +0 -1
- package/test/fixtures/broad-audit/db/auditctx.sqlite +0 -0
- package/test/fixtures/count-chained-where/config/environments/env.countchained.json +0 -1
- package/test/fixtures/count-chained-where/db/countctx.sqlite +0 -0
- package/test/fixtures/count-no-from/config/environments/env.countfrom.json +0 -1
- package/test/fixtures/count-no-from/db/countfromctx.sqlite +0 -0
- package/test/fixtures/edge-cases-audit/config/environments/env.edgeaudit.json +0 -1
- package/test/fixtures/edge-cases-audit/db/edgectx.sqlite +0 -0
- package/test/fixtures/multi-migration-N4bUYy/app/models/db/migrations/1700000001000_CreateAuthor_migration.js +0 -14
- package/test/fixtures/multi-migration-N4bUYy/app/models/db/migrations/1700000002000_CreateBook_migration.js +0 -14
- package/test/fixtures/multi-migration-N4bUYy/app/models/db/migrations/1700000003000_CreateReview_migration.js +0 -14
- package/test/fixtures/multi-migration-N4bUYy/app/models/db/migrations/multimigctx_contextSnapShot.json +0 -96
- package/test/fixtures/multi-migration-N4bUYy/app/models/multimigctx.js +0 -26
- package/test/fixtures/multi-migration-N4bUYy/config/environments/env.multimig.json +0 -1
- package/test/fixtures/multi-migration-N4bUYy/db/multimigctx.sqlite +0 -0
- package/test/fixtures/multi-migration-N4bUYy/package.json +0 -1
- package/test/fixtures/multi-migration-Qu42ZE/app/models/db/migrations/1700000001000_CreateAuthor_migration.js +0 -14
- package/test/fixtures/multi-migration-Qu42ZE/app/models/db/migrations/1700000002000_CreateBook_migration.js +0 -14
- package/test/fixtures/multi-migration-Qu42ZE/app/models/db/migrations/1700000003000_CreateReview_migration.js +0 -14
- package/test/fixtures/multi-migration-Qu42ZE/app/models/db/migrations/multimigctx_contextSnapShot.json +0 -96
- package/test/fixtures/multi-migration-Qu42ZE/app/models/multimigctx.js +0 -26
- package/test/fixtures/multi-migration-Qu42ZE/config/environments/env.multimig.json +0 -1
- package/test/fixtures/multi-migration-Qu42ZE/db/multimigctx.sqlite +0 -0
- package/test/fixtures/multi-migration-Qu42ZE/package.json +0 -1
- package/test/fixtures/multi-migration-xDfXbJ/app/models/db/migrations/1700000001000_CreateAuthor_migration.js +0 -14
- package/test/fixtures/multi-migration-xDfXbJ/app/models/db/migrations/1700000002000_CreateBook_migration.js +0 -14
- package/test/fixtures/multi-migration-xDfXbJ/app/models/db/migrations/1700000003000_CreateReview_migration.js +0 -14
- package/test/fixtures/multi-migration-xDfXbJ/app/models/db/migrations/multimigctx_contextSnapShot.json +0 -104
- package/test/fixtures/multi-migration-xDfXbJ/app/models/multimigctx.js +0 -26
- package/test/fixtures/multi-migration-xDfXbJ/config/environments/env.multimig.json +0 -1
- package/test/fixtures/multi-migration-xDfXbJ/db/multimigctx.sqlite +0 -0
- package/test/fixtures/multi-migration-xDfXbJ/package.json +0 -1
- package/test/fixtures/new-setter-large-string/config/environments/env.newsetter.json +0 -1
- package/test/fixtures/new-setter-large-string/db/newsettercontext.sqlite +0 -0
- package/test/fixtures/query-builder-parity/config/environments/env.parity.json +0 -1
- package/test/fixtures/query-builder-parity/db/parityctx.sqlite +0 -0
- package/test/fixtures/setter-and-update-drop/config/environments/env.setterdrop.json +0 -1
- package/test/fixtures/setter-and-update-drop/db/setdropctx.sqlite +0 -0
- package/test/fixtures/sqlite-lifecycle/config/environments/env.test.json +0 -1
- package/test/fixtures/sqlite-lifecycle/db/lifecyclecontext.sqlite +0 -0
- package/test/fixtures/text-types/config/environments/env.texttypes.json +0 -1
- package/test/fixtures/text-types/db/textctx.sqlite +0 -0
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Verifies `ctx.$$` (and `this.$$` from inside a context method) work as
|
|
3
|
+
* a TypeScript/ESLint-clean alias for the bare `$$` placeholder.
|
|
4
|
+
*
|
|
5
|
+
* ctx.User.where(u => u.id == ctx.$$, 42)
|
|
6
|
+
* ctx.User.where(u => u.id == this.$$, 42)
|
|
7
|
+
* ctx.User.where('u => u.id == $$', 42) // legacy bare form still works
|
|
8
|
+
*
|
|
9
|
+
* All three must produce the same SQL parameters and return the same rows.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { test } from 'node:test';
|
|
13
|
+
import assert from 'node:assert/strict';
|
|
14
|
+
import fs from 'node:fs';
|
|
15
|
+
import path from 'node:path';
|
|
16
|
+
import { fileURLToPath } from 'node:url';
|
|
17
|
+
|
|
18
|
+
process.env.master = 'ctxdollar';
|
|
19
|
+
|
|
20
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
21
|
+
const fixturesDir = path.join(__dirname, 'fixtures', 'ctx-dollar');
|
|
22
|
+
const envDir = path.join(fixturesDir, 'config', 'environments');
|
|
23
|
+
const dbDir = path.join(fixturesDir, 'db');
|
|
24
|
+
const dbFile = path.join(dbDir, 'dollarctx.sqlite');
|
|
25
|
+
|
|
26
|
+
fs.mkdirSync(envDir, { recursive: true });
|
|
27
|
+
fs.mkdirSync(dbDir, { recursive: true });
|
|
28
|
+
if (fs.existsSync(dbFile)) fs.rmSync(dbFile);
|
|
29
|
+
|
|
30
|
+
fs.writeFileSync(
|
|
31
|
+
path.join(envDir, 'env.ctxdollar.json'),
|
|
32
|
+
JSON.stringify({
|
|
33
|
+
DollarCtx: {
|
|
34
|
+
type: 'better-sqlite3',
|
|
35
|
+
connection: dbDir + path.sep,
|
|
36
|
+
},
|
|
37
|
+
})
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
const { default: context } = await import('../context.js');
|
|
41
|
+
|
|
42
|
+
class Plugin {
|
|
43
|
+
id(db) { db.integer().primary().auto(); }
|
|
44
|
+
name(db) { db.string(); }
|
|
45
|
+
version(db) { db.string(); }
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
class DollarCtx extends context {
|
|
49
|
+
constructor() {
|
|
50
|
+
super();
|
|
51
|
+
this.env(envDir);
|
|
52
|
+
this.dbset(Plugin);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
{
|
|
57
|
+
const ctx = new DollarCtx();
|
|
58
|
+
ctx._SQLEngine.db.exec(`
|
|
59
|
+
CREATE TABLE IF NOT EXISTS Plugin (
|
|
60
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
61
|
+
name TEXT,
|
|
62
|
+
version TEXT
|
|
63
|
+
);
|
|
64
|
+
`);
|
|
65
|
+
const insert = ctx._SQLEngine.db.prepare('INSERT INTO Plugin (name, version) VALUES (?, ?)');
|
|
66
|
+
insert.run('provider-openai', '1.0');
|
|
67
|
+
insert.run('provider-anthropic', '1.2');
|
|
68
|
+
insert.run('provider-google', '0.9');
|
|
69
|
+
await ctx.close();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
test('context.$$ getter returns the literal "$$" string', () => {
|
|
73
|
+
const ctx = new DollarCtx();
|
|
74
|
+
assert.equal(ctx.$$, '$$');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test('bare $$ in a string lambda still works (legacy form)', async () => {
|
|
78
|
+
const ctx = new DollarCtx();
|
|
79
|
+
const row = await ctx.Plugin
|
|
80
|
+
.where('p => p.name == $$', 'provider-openai')
|
|
81
|
+
.single();
|
|
82
|
+
assert.ok(row);
|
|
83
|
+
assert.equal(row.name, 'provider-openai');
|
|
84
|
+
await ctx.close();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
test('ctx.$$ in an arrow lambda matches the same row', async () => {
|
|
88
|
+
const ctx = new DollarCtx();
|
|
89
|
+
const row = await ctx.Plugin
|
|
90
|
+
.where((p) => p.name == ctx.$$, 'provider-openai')
|
|
91
|
+
.single();
|
|
92
|
+
assert.ok(row);
|
|
93
|
+
assert.equal(row.name, 'provider-openai');
|
|
94
|
+
await ctx.close();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
test('this.$$ inside a context method works the same way', async () => {
|
|
98
|
+
const ctx = new DollarCtx();
|
|
99
|
+
// Bind `this` to the context. This mirrors what user code looks like
|
|
100
|
+
// when `findByName` is defined as an instance method on the context
|
|
101
|
+
// class itself.
|
|
102
|
+
const findByName = async function (name) {
|
|
103
|
+
return this.Plugin.where((p) => p.name == this.$$, name).single();
|
|
104
|
+
};
|
|
105
|
+
const row = await findByName.call(ctx, 'provider-anthropic');
|
|
106
|
+
assert.ok(row);
|
|
107
|
+
assert.equal(row.name, 'provider-anthropic');
|
|
108
|
+
await ctx.close();
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test('multiple ctx.$$ placeholders in one lambda', async () => {
|
|
112
|
+
const ctx = new DollarCtx();
|
|
113
|
+
const rows = await ctx.Plugin
|
|
114
|
+
.where((p) => p.name == ctx.$$ || p.name == ctx.$$, 'provider-openai', 'provider-google')
|
|
115
|
+
.toList();
|
|
116
|
+
const names = rows.map(r => r.name).sort();
|
|
117
|
+
assert.deepEqual(names, ['provider-google', 'provider-openai']);
|
|
118
|
+
await ctx.close();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
test('mixing ctx.$$ and bare $$ in the same lambda', async () => {
|
|
122
|
+
const ctx = new DollarCtx();
|
|
123
|
+
// First placeholder uses ctx.$$, second uses bare $$
|
|
124
|
+
const rows = await ctx.Plugin
|
|
125
|
+
.where(`p => p.name == ctx.$$ || p.version == $$`, 'provider-openai', '1.2')
|
|
126
|
+
.toList();
|
|
127
|
+
const names = rows.map(r => r.name).sort();
|
|
128
|
+
assert.deepEqual(names, ['provider-anthropic', 'provider-openai']);
|
|
129
|
+
await ctx.close();
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
test('orderBy and other clauses also accept ctx.$$', async () => {
|
|
133
|
+
const ctx = new DollarCtx();
|
|
134
|
+
// orderBy doesn't take placeholders, but where chained with orderBy should still work
|
|
135
|
+
const rows = await ctx.Plugin
|
|
136
|
+
.where((p) => p.version != ctx.$$, '0.9')
|
|
137
|
+
.orderBy('p => p.name')
|
|
138
|
+
.toList();
|
|
139
|
+
assert.equal(rows.length, 2);
|
|
140
|
+
assert.deepEqual(rows.map(r => r.name), ['provider-anthropic', 'provider-openai']);
|
|
141
|
+
await ctx.close();
|
|
142
|
+
});
|
|
@@ -0,0 +1,249 @@
|
|
|
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
|
+
});
|
|
@@ -221,7 +221,7 @@ test('Postgres buildQuery emits ORDER BY ... ASC for .orderBy()', () => {
|
|
|
221
221
|
);
|
|
222
222
|
const engine = new PostgresEngine();
|
|
223
223
|
const out = engine.buildQuery(script, entity, {});
|
|
224
|
-
assert.match(out.query, /ORDER BY\s+\w+\.updatedAt
|
|
224
|
+
assert.match(out.query, /ORDER BY\s+\w+\."?updatedAt"?\s+ASC/);
|
|
225
225
|
});
|
|
226
226
|
|
|
227
227
|
test('Postgres buildQuery emits ORDER BY ... DESC for .orderByDescending()', () => {
|
|
@@ -230,7 +230,7 @@ test('Postgres buildQuery emits ORDER BY ... DESC for .orderByDescending()', ()
|
|
|
230
230
|
);
|
|
231
231
|
const engine = new PostgresEngine();
|
|
232
232
|
const out = engine.buildQuery(script, entity, {});
|
|
233
|
-
assert.match(out.query, /ORDER BY\s+\w+\.updatedAt
|
|
233
|
+
assert.match(out.query, /ORDER BY\s+\w+\."?updatedAt"?\s+DESC/);
|
|
234
234
|
});
|
|
235
235
|
|
|
236
236
|
test('Postgres buildQuery emits LIMIT and OFFSET', () => {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"AuditCtx":{"type":"better-sqlite3","connection":"/Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/test/fixtures/broad-audit/db/"}}
|
|
Binary file
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"CountCtx":{"type":"better-sqlite3","connection":"/Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/test/fixtures/count-chained-where/db/"}}
|
|
Binary file
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"CountFromCtx":{"type":"better-sqlite3","connection":"/Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/test/fixtures/count-no-from/db/"}}
|
|
Binary file
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"EdgeCtx":{"type":"better-sqlite3","connection":"/Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/test/fixtures/edge-cases-audit/db/"}}
|
|
Binary file
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import masterrecord from "file:///Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/MasterRecord.js";
|
|
3
|
-
class CreateAuthor extends masterrecord.schema {
|
|
4
|
-
constructor(context){ super(context); }
|
|
5
|
-
async up(table){
|
|
6
|
-
await this.init(table);
|
|
7
|
-
await this.createTable(table.Author);
|
|
8
|
-
}
|
|
9
|
-
async down(table){
|
|
10
|
-
await this.init(table);
|
|
11
|
-
await this.dropTable(table.Author);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
export default CreateAuthor;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import masterrecord from "file:///Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/MasterRecord.js";
|
|
3
|
-
class CreateBook extends masterrecord.schema {
|
|
4
|
-
constructor(context){ super(context); }
|
|
5
|
-
async up(table){
|
|
6
|
-
await this.init(table);
|
|
7
|
-
await this.createTable(table.Book);
|
|
8
|
-
}
|
|
9
|
-
async down(table){
|
|
10
|
-
await this.init(table);
|
|
11
|
-
await this.dropTable(table.Book);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
export default CreateBook;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import masterrecord from "file:///Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/MasterRecord.js";
|
|
3
|
-
class CreateReview extends masterrecord.schema {
|
|
4
|
-
constructor(context){ super(context); }
|
|
5
|
-
async up(table){
|
|
6
|
-
await this.init(table);
|
|
7
|
-
await this.createTable(table.Review);
|
|
8
|
-
}
|
|
9
|
-
async down(table){
|
|
10
|
-
await this.init(table);
|
|
11
|
-
await this.dropTable(table.Review);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
export default CreateReview;
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"contextLocation": "../../multimigctx.js",
|
|
3
|
-
"migrationFolder": ".",
|
|
4
|
-
"snapShotLocation": "multimigctx_contextSnapShot.json",
|
|
5
|
-
"schema": [
|
|
6
|
-
{
|
|
7
|
-
"id": {
|
|
8
|
-
"name": "id",
|
|
9
|
-
"type": "integer",
|
|
10
|
-
"primary": true,
|
|
11
|
-
"nullable": false,
|
|
12
|
-
"unique": true,
|
|
13
|
-
"auto": true,
|
|
14
|
-
"cascadeOnDelete": true,
|
|
15
|
-
"lazyLoading": true,
|
|
16
|
-
"isNavigational": false,
|
|
17
|
-
"skipGetFunction": false,
|
|
18
|
-
"valueConversion": true
|
|
19
|
-
},
|
|
20
|
-
"name": {
|
|
21
|
-
"name": "name",
|
|
22
|
-
"type": "string",
|
|
23
|
-
"nullable": true,
|
|
24
|
-
"unique": false,
|
|
25
|
-
"auto": false,
|
|
26
|
-
"cascadeOnDelete": true,
|
|
27
|
-
"lazyLoading": true,
|
|
28
|
-
"isNavigational": false,
|
|
29
|
-
"skipGetFunction": false,
|
|
30
|
-
"valueConversion": true
|
|
31
|
-
},
|
|
32
|
-
"__compositeIndexes": [],
|
|
33
|
-
"__name": "Author"
|
|
34
|
-
},
|
|
35
|
-
{
|
|
36
|
-
"id": {
|
|
37
|
-
"name": "id",
|
|
38
|
-
"type": "integer",
|
|
39
|
-
"primary": true,
|
|
40
|
-
"nullable": false,
|
|
41
|
-
"unique": true,
|
|
42
|
-
"auto": true,
|
|
43
|
-
"cascadeOnDelete": true,
|
|
44
|
-
"lazyLoading": true,
|
|
45
|
-
"isNavigational": false,
|
|
46
|
-
"skipGetFunction": false,
|
|
47
|
-
"valueConversion": true
|
|
48
|
-
},
|
|
49
|
-
"title": {
|
|
50
|
-
"name": "title",
|
|
51
|
-
"type": "string",
|
|
52
|
-
"nullable": true,
|
|
53
|
-
"unique": false,
|
|
54
|
-
"auto": false,
|
|
55
|
-
"cascadeOnDelete": true,
|
|
56
|
-
"lazyLoading": true,
|
|
57
|
-
"isNavigational": false,
|
|
58
|
-
"skipGetFunction": false,
|
|
59
|
-
"valueConversion": true
|
|
60
|
-
},
|
|
61
|
-
"__compositeIndexes": [],
|
|
62
|
-
"__name": "Book"
|
|
63
|
-
},
|
|
64
|
-
{
|
|
65
|
-
"id": {
|
|
66
|
-
"name": "id",
|
|
67
|
-
"type": "integer",
|
|
68
|
-
"primary": true,
|
|
69
|
-
"nullable": false,
|
|
70
|
-
"unique": true,
|
|
71
|
-
"auto": true,
|
|
72
|
-
"cascadeOnDelete": true,
|
|
73
|
-
"lazyLoading": true,
|
|
74
|
-
"isNavigational": false,
|
|
75
|
-
"skipGetFunction": false,
|
|
76
|
-
"valueConversion": true
|
|
77
|
-
},
|
|
78
|
-
"body": {
|
|
79
|
-
"name": "body",
|
|
80
|
-
"type": "string",
|
|
81
|
-
"nullable": true,
|
|
82
|
-
"unique": false,
|
|
83
|
-
"auto": false,
|
|
84
|
-
"cascadeOnDelete": true,
|
|
85
|
-
"lazyLoading": true,
|
|
86
|
-
"isNavigational": false,
|
|
87
|
-
"skipGetFunction": false,
|
|
88
|
-
"valueConversion": true
|
|
89
|
-
},
|
|
90
|
-
"__compositeIndexes": [],
|
|
91
|
-
"__name": "Review"
|
|
92
|
-
}
|
|
93
|
-
],
|
|
94
|
-
"seedData": {},
|
|
95
|
-
"seedConfig": {}
|
|
96
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import context from "file:///Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/context.js";
|
|
3
|
-
|
|
4
|
-
class Author {
|
|
5
|
-
id(db) { db.integer().primary().auto(); }
|
|
6
|
-
name(db) { db.string(); }
|
|
7
|
-
}
|
|
8
|
-
class Book {
|
|
9
|
-
id(db) { db.integer().primary().auto(); }
|
|
10
|
-
title(db) { db.string(); }
|
|
11
|
-
}
|
|
12
|
-
class Review {
|
|
13
|
-
id(db) { db.integer().primary().auto(); }
|
|
14
|
-
body(db) { db.string(); }
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
class multimigctx extends context {
|
|
18
|
-
constructor() {
|
|
19
|
-
super();
|
|
20
|
-
this.env("/Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/test/fixtures/multi-migration-N4bUYy/config/environments");
|
|
21
|
-
this.dbset(Author);
|
|
22
|
-
this.dbset(Book);
|
|
23
|
-
this.dbset(Review);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
export default multimigctx;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"multimigctx":{"type":"better-sqlite3","connection":"/Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/test/fixtures/multi-migration-N4bUYy/db/"}}
|
|
Binary file
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"name":"multi-migration-fixture","type":"module"}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import masterrecord from "file:///Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/MasterRecord.js";
|
|
3
|
-
class CreateAuthor extends masterrecord.schema {
|
|
4
|
-
constructor(context){ super(context); }
|
|
5
|
-
async up(table){
|
|
6
|
-
await this.init(table);
|
|
7
|
-
await this.createTable(table.Author);
|
|
8
|
-
}
|
|
9
|
-
async down(table){
|
|
10
|
-
await this.init(table);
|
|
11
|
-
await this.dropTable(table.Author);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
export default CreateAuthor;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import masterrecord from "file:///Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/MasterRecord.js";
|
|
3
|
-
class CreateBook extends masterrecord.schema {
|
|
4
|
-
constructor(context){ super(context); }
|
|
5
|
-
async up(table){
|
|
6
|
-
await this.init(table);
|
|
7
|
-
await this.createTable(table.Book);
|
|
8
|
-
}
|
|
9
|
-
async down(table){
|
|
10
|
-
await this.init(table);
|
|
11
|
-
await this.dropTable(table.Book);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
export default CreateBook;
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import masterrecord from "file:///Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/MasterRecord.js";
|
|
3
|
-
class CreateReview extends masterrecord.schema {
|
|
4
|
-
constructor(context){ super(context); }
|
|
5
|
-
async up(table){
|
|
6
|
-
await this.init(table);
|
|
7
|
-
await this.createTable(table.Review);
|
|
8
|
-
}
|
|
9
|
-
async down(table){
|
|
10
|
-
await this.init(table);
|
|
11
|
-
await this.dropTable(table.Review);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
export default CreateReview;
|