masterrecord 1.0.1 → 1.0.3

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.
Files changed (34) hide show
  1. package/Entity/entityTrackerModel.js +8 -1
  2. package/Migrations/cli.js +155 -23
  3. package/QueryLanguage/queryMethods.js +9 -1
  4. package/context.js +20 -3
  5. package/package.json +1 -1
  6. package/postgresEngine.js +15 -0
  7. package/test/fixtures/multi-migration-MFc2cl/app/models/db/migrations/1700000001000_CreateAuthor_migration.js +14 -0
  8. package/test/fixtures/multi-migration-MFc2cl/app/models/db/migrations/1700000002000_CreateBook_migration.js +14 -0
  9. package/test/fixtures/multi-migration-MFc2cl/app/models/db/migrations/1700000003000_CreateReview_migration.js +14 -0
  10. package/test/fixtures/multi-migration-MFc2cl/app/models/db/migrations/multimigctx_contextSnapShot.json +104 -0
  11. package/test/fixtures/multi-migration-MFc2cl/app/models/multimigctx.js +26 -0
  12. package/test/fixtures/multi-migration-MFc2cl/config/environments/env.multimig.json +1 -0
  13. package/test/fixtures/multi-migration-MFc2cl/db/multimigctx.sqlite +0 -0
  14. package/test/fixtures/multi-migration-MFc2cl/package.json +1 -0
  15. package/test/fixtures/multi-migration-gWKaXD/app/models/db/migrations/1700000001000_CreateAuthor_migration.js +14 -0
  16. package/test/fixtures/multi-migration-gWKaXD/app/models/db/migrations/1700000002000_CreateBook_migration.js +14 -0
  17. package/test/fixtures/multi-migration-gWKaXD/app/models/db/migrations/1700000003000_CreateReview_migration.js +14 -0
  18. package/test/fixtures/multi-migration-gWKaXD/app/models/db/migrations/multimigctx_contextSnapShot.json +96 -0
  19. package/test/fixtures/multi-migration-gWKaXD/app/models/multimigctx.js +26 -0
  20. package/test/fixtures/multi-migration-gWKaXD/config/environments/env.multimig.json +1 -0
  21. package/test/fixtures/multi-migration-gWKaXD/db/multimigctx.sqlite +0 -0
  22. package/test/fixtures/multi-migration-gWKaXD/package.json +1 -0
  23. package/test/fixtures/multi-migration-vsWfKD/app/models/db/migrations/1700000001000_CreateAuthor_migration.js +14 -0
  24. package/test/fixtures/multi-migration-vsWfKD/app/models/db/migrations/1700000002000_CreateBook_migration.js +14 -0
  25. package/test/fixtures/multi-migration-vsWfKD/app/models/db/migrations/1700000003000_CreateReview_migration.js +14 -0
  26. package/test/fixtures/multi-migration-vsWfKD/app/models/db/migrations/multimigctx_contextSnapShot.json +96 -0
  27. package/test/fixtures/multi-migration-vsWfKD/app/models/multimigctx.js +26 -0
  28. package/test/fixtures/multi-migration-vsWfKD/config/environments/env.multimig.json +1 -0
  29. package/test/fixtures/multi-migration-vsWfKD/db/multimigctx.sqlite +0 -0
  30. package/test/fixtures/multi-migration-vsWfKD/package.json +1 -0
  31. package/test/fixtures/setter-and-update-drop/config/environments/env.setterdrop.json +1 -0
  32. package/test/fixtures/setter-and-update-drop/db/setdropctx.sqlite +0 -0
  33. package/test/setter-and-update-drop.test.js +168 -0
  34. package/test/update-database-multi.test.js +239 -0
@@ -0,0 +1,96 @@
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
+ }
@@ -0,0 +1,26 @@
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-gWKaXD/config/environments");
21
+ this.dbset(Author);
22
+ this.dbset(Book);
23
+ this.dbset(Review);
24
+ }
25
+ }
26
+ export default multimigctx;
@@ -0,0 +1 @@
1
+ {"multimigctx":{"type":"better-sqlite3","connection":"/Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/test/fixtures/multi-migration-gWKaXD/db/"}}
@@ -0,0 +1 @@
1
+ {"name":"multi-migration-fixture","type":"module"}
@@ -0,0 +1,14 @@
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;
@@ -0,0 +1,14 @@
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;
@@ -0,0 +1,14 @@
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;
@@ -0,0 +1,96 @@
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
+ }
@@ -0,0 +1,26 @@
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-vsWfKD/config/environments");
21
+ this.dbset(Author);
22
+ this.dbset(Book);
23
+ this.dbset(Review);
24
+ }
25
+ }
26
+ export default multimigctx;
@@ -0,0 +1 @@
1
+ {"multimigctx":{"type":"better-sqlite3","connection":"/Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/test/fixtures/multi-migration-vsWfKD/db/"}}
@@ -0,0 +1 @@
1
+ {"name":"multi-migration-fixture","type":"module"}
@@ -0,0 +1 @@
1
+ {"SetDropCtx":{"type":"better-sqlite3","connection":"/Users/alexanderrich/Documents/development/opensourceHQ/MasterRecord/test/fixtures/setter-and-update-drop/db/"}}
@@ -0,0 +1,168 @@
1
+ /**
2
+ * Reproducers for the "silently dropped writes" family of bugs that
3
+ * downstream consumers have been working around with raw _execute.
4
+ *
5
+ * 1. `.new()` setter does NOT apply `fieldDef.set` — tracker-entity setter
6
+ * does. Inconsistent behavior between INSERT and UPDATE paths.
7
+ * 2. `_processBatchUpdates` reads `currentModel._entity` (typo) instead of
8
+ * `__entity`, so `removePrimarykeyandVirtual` gets `undefined`.
9
+ * 3. After UPDATE, `__state` and `__dirtyFields` are not reset. On the next
10
+ * `saveChanges()` call, the same fields are re-written to the DB, which
11
+ * can silently overwrite concurrent changes from other processes.
12
+ * 4. Tracker setter pushes to `__dirtyFields` without de-duplication, so
13
+ * setting the same field multiple times produces duplicate assignments
14
+ * in the UPDATE SET clause. In Postgres this is a hard error
15
+ * ("multiple assignments to same column"); in SQLite/MySQL it may or
16
+ * may not work depending on driver version.
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 = 'setterdrop';
26
+
27
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
28
+ const fixturesDir = path.join(__dirname, 'fixtures', 'setter-and-update-drop');
29
+ const envDir = path.join(fixturesDir, 'config', 'environments');
30
+ const dbDir = path.join(fixturesDir, 'db');
31
+ const dbFile = path.join(dbDir, 'setdropctx.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.setterdrop.json'),
39
+ JSON.stringify({
40
+ SetDropCtx: {
41
+ type: 'better-sqlite3',
42
+ connection: dbDir + path.sep,
43
+ },
44
+ })
45
+ );
46
+
47
+ const { default: context } = await import('../context.js');
48
+
49
+ class User {
50
+ id(db) { db.integer().primary().auto(); }
51
+ // `name` has a .set() transform that trims and lowercases — the
52
+ // canonical test for whether the setter path applies fieldDef.set.
53
+ name(db) { db.string().set(v => (typeof v === 'string' ? v.trim().toLowerCase() : v)); }
54
+ email(db) { db.string(); }
55
+ score(db) { db.integer(); }
56
+ }
57
+
58
+ class SetDropCtx extends context {
59
+ constructor() {
60
+ super();
61
+ this.env(envDir);
62
+ this.dbset(User);
63
+ }
64
+ }
65
+
66
+ {
67
+ const ctx = new SetDropCtx();
68
+ ctx._SQLEngine.db.exec(`
69
+ CREATE TABLE IF NOT EXISTS User (
70
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
71
+ name TEXT,
72
+ email TEXT,
73
+ score INTEGER
74
+ );
75
+ `);
76
+ await ctx.close();
77
+ }
78
+
79
+ //
80
+ // Bug #1 — .new() setter must apply fieldDef.set
81
+ //
82
+ test('.new() setter applies fieldDef.set transform at write time', async () => {
83
+ const ctx = new SetDropCtx();
84
+ const u = ctx.User.new();
85
+ u.name = ' ALICE '; // whitespace + uppercase
86
+ u.email = 'alice@example.com';
87
+ u.score = 10;
88
+ await u.save();
89
+
90
+ const row = ctx._SQLEngine.db.prepare('SELECT name FROM User WHERE email = ?').get('alice@example.com');
91
+ assert.equal(row.name, 'alice', `.set() transform should have been applied on INSERT; got "${row.name}"`);
92
+ await ctx.close();
93
+ });
94
+
95
+ test('Tracker setter applies fieldDef.set transform on UPDATE (baseline — already works)', async () => {
96
+ const ctx = new SetDropCtx();
97
+ ctx._SQLEngine.db.prepare('INSERT INTO User (name, email, score) VALUES (?, ?, ?)').run('bob', 'bob@example.com', 5);
98
+
99
+ const loaded = await ctx.User.where('u => u.email == $$', 'bob@example.com').single();
100
+ loaded.name = ' BOB-UPDATED ';
101
+ await loaded.save();
102
+
103
+ const row = ctx._SQLEngine.db.prepare('SELECT name FROM User WHERE email = ?').get('bob@example.com');
104
+ assert.equal(row.name, 'bob-updated', `.set() transform should apply on UPDATE; got "${row.name}"`);
105
+ await ctx.close();
106
+ });
107
+
108
+ //
109
+ // Bug #2 — after UPDATE, __dirtyFields and __state are not reset;
110
+ // calling saveChanges twice for the same entity silently re-updates the
111
+ // same fields.
112
+ //
113
+ test('After UPDATE, __dirtyFields is cleared and __state is reset to track', async () => {
114
+ const ctx = new SetDropCtx();
115
+ ctx._SQLEngine.db.prepare('INSERT INTO User (name, email, score) VALUES (?, ?, ?)').run('carol', 'carol@example.com', 1);
116
+
117
+ const loaded = await ctx.User.where('u => u.email == $$', 'carol@example.com').single();
118
+ loaded.score = 2;
119
+ await loaded.save();
120
+
121
+ assert.deepEqual(loaded.__dirtyFields, [], '__dirtyFields should be empty after UPDATE');
122
+ assert.equal(loaded.__state, 'track', '__state should be "track" after UPDATE');
123
+ await ctx.close();
124
+ });
125
+
126
+ test('saveChanges re-called with no modifications after UPDATE is a no-op', async () => {
127
+ const ctx = new SetDropCtx();
128
+ ctx._SQLEngine.db.prepare('INSERT INTO User (name, email, score) VALUES (?, ?, ?)').run('dave', 'dave@example.com', 1);
129
+
130
+ const loaded = await ctx.User.where('u => u.email == $$', 'dave@example.com').single();
131
+ loaded.score = 99;
132
+ await loaded.save();
133
+
134
+ // Simulate concurrent external write: another process updates score to 42.
135
+ ctx._SQLEngine.db.prepare('UPDATE User SET score = 42 WHERE email = ?').run('dave@example.com');
136
+
137
+ // Re-saving without touching the entity should NOT overwrite the external
138
+ // change; the entity is no longer modified.
139
+ await ctx.saveChanges();
140
+
141
+ const row = ctx._SQLEngine.db.prepare('SELECT score FROM User WHERE email = ?').get('dave@example.com');
142
+ assert.equal(row.score, 42, 'second saveChanges must not silently re-apply the prior write');
143
+ await ctx.close();
144
+ });
145
+
146
+ //
147
+ // Bug #3 — setting the same field twice must not produce duplicate
148
+ // SET assignments (SQLite errors on duplicates; Postgres always errors).
149
+ //
150
+ test('Setting the same field twice produces only one assignment in UPDATE', async () => {
151
+ const ctx = new SetDropCtx();
152
+ ctx._SQLEngine.db.prepare('INSERT INTO User (name, email, score) VALUES (?, ?, ?)').run('eve', 'eve@example.com', 1);
153
+
154
+ const loaded = await ctx.User.where('u => u.email == $$', 'eve@example.com').single();
155
+ loaded.score = 2;
156
+ loaded.score = 3; // second assignment — must not push duplicate into __dirtyFields
157
+ loaded.score = 4;
158
+
159
+ // Only one occurrence of 'score' in dirtyFields
160
+ const dupCount = loaded.__dirtyFields.filter(f => f === 'score').length;
161
+ assert.equal(dupCount, 1, `score should appear exactly once in __dirtyFields; got ${dupCount} (${JSON.stringify(loaded.__dirtyFields)})`);
162
+
163
+ // And the UPDATE must succeed and persist the last value.
164
+ await loaded.save();
165
+ const row = ctx._SQLEngine.db.prepare('SELECT score FROM User WHERE email = ?').get('eve@example.com');
166
+ assert.equal(row.score, 4, 'last assigned value must be persisted');
167
+ await ctx.close();
168
+ });