masterrecord 1.1.8 → 1.1.9

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 (3) hide show
  1. package/CHANGES.md +11 -0
  2. package/package.json +1 -1
  3. package/readme.md +38 -52
package/CHANGES.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # MasterRecord Changelog
2
2
 
3
+ ## v1.1.9 — docs: correct the seed-data section
4
+
5
+ README "Seed Data" docs described a code path that doesn't exist. Corrected to match what `add-migration` actually generates (verified against `Migrations/migrationTemplate.js`):
6
+
7
+ - Context-level `this.dbset(T).seed({…})` compiles to idempotent **`this.seed('T', …)`** calls — not `table.T.create({…})` (which is not a real method and never emitted).
8
+ - **Both** routes (context-level and hand-written `this.seed()`) are idempotent (`INSERT OR IGNORE` / `INSERT IGNORE` / `ON CONFLICT DO NOTHING`); the old docs wrongly claimed context-level seeds were non-idempotent and had to be removed after the first run.
9
+ - Removed the false "triggers lifecycle hooks / validators" benefit — generated seeds are raw parameterized INSERTs and do **not** run `beforeSave`/`afterSave`/validators (added a note to use `context.Entity.new()` + `save()` when you need those).
10
+ - Fixed the upsert example to the real generated form (`this.context.T.where(...).single()` + `existing.save()` + `this.seed(...)`).
11
+
12
+ Docs-only; no code changes.
13
+
3
14
  ## v1.1.8 — entity internals/methods are non-enumerable (fixes spread/JSON data loss)
4
15
 
5
16
  **Bug:** entity instances exposed their internals (`__entity`, `__context`, `__ID`, `__dirtyFields`, `__state`, `__name`) and helper methods (`toJSON`, `toObject`, `save`, `delete`, `reload`, `clone`) as **enumerable** own properties (attached by object literal / plain assignment). So spreading or `Object.assign`-ing an entity:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "masterrecord",
3
- "version": "1.1.8",
3
+ "version": "1.1.9",
4
4
  "description": "An Object-relational mapping for the Master framework. Master Record connects classes to relational database tables to establish a database with almost zero-configuration ",
5
5
  "type": "module",
6
6
  "main": "./MasterRecord.js",
package/readme.md CHANGED
@@ -2215,16 +2215,17 @@ class AppContext extends context {
2215
2215
 
2216
2216
  ### Automatic Migration Generation
2217
2217
 
2218
- When you define seed data in the context, MasterRecord generates migration code using the ORM:
2218
+ When you define seed data on the context, `add-migration` compiles it into idempotent `this.seed(...)` calls in the generated migration:
2219
2219
 
2220
2220
  ```javascript
2221
2221
  // Your context definition triggers this migration
2222
2222
  class Migration_20250205_123456 extends masterrecord.schema {
2223
2223
  async up(table) {
2224
- this.init(table);
2224
+ await this.init(table);
2225
2225
 
2226
- // Generated ORM create calls
2227
- await table.User.create({
2226
+ // Generated idempotent seed calls — INSERT OR IGNORE (SQLite) /
2227
+ // INSERT IGNORE (MySQL) / ON CONFLICT DO NOTHING (PostgreSQL)
2228
+ await this.seed('User', {
2228
2229
  user_name: 'admin',
2229
2230
  first_name: 'System',
2230
2231
  last_name: 'Administrator',
@@ -2235,32 +2236,24 @@ class Migration_20250205_123456 extends masterrecord.schema {
2235
2236
  availability_status: 'online'
2236
2237
  });
2237
2238
 
2238
- await table.Post.create({
2239
- title: 'Welcome',
2240
- content: 'Hello world',
2241
- author_id: 1
2242
- });
2243
-
2244
- await table.Post.create({
2245
- title: 'Getting Started',
2246
- content: 'Tutorial',
2247
- author_id: 1
2248
- });
2239
+ await this.seed('Post', { title: 'Welcome', content: 'Hello world', author_id: 1 });
2240
+ await this.seed('Post', { title: 'Getting Started', content: 'Tutorial', author_id: 1 });
2249
2241
  }
2250
2242
 
2251
2243
  async down(table) {
2252
- this.init(table);
2244
+ await this.init(table);
2253
2245
  // Seed data typically not removed in down migrations
2254
2246
  }
2255
2247
  }
2256
2248
  ```
2257
2249
 
2258
- ### Benefits of ORM-Based Seeding
2250
+ ### Benefits of context-level seed
2251
+
2252
+ 1. **Declarative**: seed data lives with your context definition and is versioned alongside the schema.
2253
+ 2. **Idempotent**: the generated `this.seed()` calls use the engine's insert-or-ignore form, so re-running migrations never duplicates rows or errors on an existing key.
2254
+ 3. **Environment-aware**: scope seeds with `.when(env)`, and use upsert / `seedFactory` for generated data.
2259
2255
 
2260
- 1. **Lifecycle Hooks**: Triggers `beforeSave` and `afterSave` hooks
2261
- 2. **Validation**: Uses entity field definitions and validators
2262
- 3. **Type Safety**: Ensures fields match entity schema
2263
- 4. **Maintainable**: Changes to entity structure reflected automatically
2256
+ > **Note:** generated seeds are raw parameterized INSERTs (via `this.seed()`), so they do **not** run entity lifecycle hooks (`beforeSave`/`afterSave`) or validators. If you need those to run on seeded rows, insert them through `context.Entity.new()` + `save()` in application code instead.
2264
2257
 
2265
2258
  ### Manual Seed Methods (Advanced)
2266
2259
 
@@ -2294,44 +2287,37 @@ class Migration_20250205_123456 extends masterrecord.schema {
2294
2287
 
2295
2288
  ### Idempotency
2296
2289
 
2297
- **ORM approach** (context-level seed):
2298
- - Generates plain `create()` calls
2299
- - Fails if primary key exists (user must remove seed data after first migration)
2300
- - Best for one-time initial setup data
2301
-
2302
- **Manual approach** (idempotent):
2303
- - Uses database-specific INSERT OR IGNORE syntax
2290
+ **Both** routes are idempotent — context-level seed compiles to `this.seed()`, and `this.seed()` emits the engine's insert-or-ignore form:
2304
2291
  - SQLite: `INSERT OR IGNORE INTO`
2305
2292
  - MySQL: `INSERT IGNORE INTO`
2306
2293
  - PostgreSQL: `INSERT ... ON CONFLICT DO NOTHING`
2307
- - Best for repeatable migrations and re-seeding
2308
2294
 
2309
- Example:
2295
+ Re-running a migration never creates duplicates or throws on an existing primary key, so you do **not** need to remove seed data after the first run.
2296
+
2310
2297
  ```javascript
2311
- // Context-level (runs once)
2298
+ // Context-level (declarative) — generated into a this.seed() migration
2312
2299
  this.dbset(User).seed({ id: 1, name: 'admin' });
2313
- // After first migration, remove or comment out seed data
2314
2300
 
2315
- // Manual (repeatable)
2301
+ // Manual (in a migration) — identical idempotent behavior
2316
2302
  class Migration_xyz extends masterrecord.schema {
2317
2303
  async up(table) {
2318
- this.init(table);
2319
- // Can run multiple times without error
2320
- this.seed('User', { id: 1, name: 'admin' });
2304
+ await this.init(table);
2305
+ await this.seed('User', { id: 1, name: 'admin' }); // safe to re-run
2321
2306
  }
2322
2307
  }
2323
2308
  ```
2324
2309
 
2310
+ Idempotency relies on a primary key or unique constraint on the target table — that's what the engine's ignore/conflict clause keys on.
2311
+
2325
2312
  ### Best Practices
2326
2313
 
2327
- 1. **Use context-level seed** for one-time initial setup (admin users, default categories)
2328
- - Remove seed data from context after first successful migration
2329
- - Or comment out after initial setup
2330
- 2. **Use manual seed methods** for repeatable/idempotent seeding
2331
- 3. **Use manual bulkSeed** for large datasets (1000+ records) - more performant
2332
- 4. **Keep seed data minimal** - only essential bootstrap data
2333
- 5. **Use fixtures/factories** for test data, not seed methods
2334
- 6. **Don't delete seed data** in down migrations (can cause referential integrity issues)
2314
+ 1. **Use context-level seed** for declarative bootstrap data (admin users, default categories, lookup tables) you want versioned with the schema.
2315
+ 2. **Use manual `this.seed()` / `this.bulkSeed()`** when you want the seed written explicitly in a specific migration; `bulkSeed` is one INSERT, so prefer it for large datasets.
2316
+ 3. **Both are idempotent** — safe to leave in place and re-run; no need to delete after the first deploy.
2317
+ 4. **Keep seed data minimal** - only essential bootstrap data.
2318
+ 5. **Use fixtures/factories** for test data, not seed methods.
2319
+ 6. **Don't delete seed data** in down migrations (can cause referential integrity issues).
2320
+ 7. If you need lifecycle hooks/validators to run on seeded rows, insert via `context.Entity.new()` + `save()` in app code rather than `seed()`.
2335
2321
 
2336
2322
  ### Example: Multi-Tenant Seed Data
2337
2323
 
@@ -2394,15 +2380,15 @@ class AppContext extends context {
2394
2380
  **Generated Migration:**
2395
2381
  ```javascript
2396
2382
  async up(table) {
2397
- this.init(table);
2398
- await table.User.create({ id: 1, name: 'admin', email: 'admin@example.com' });
2383
+ await this.init(table);
2384
+ await this.seed('User', { id: 1, name: 'admin', email: 'admin@example.com' });
2399
2385
  }
2400
2386
 
2401
2387
  async down(table) {
2402
- this.init(table);
2388
+ await this.init(table);
2403
2389
  // Auto-generated rollback (reverse order for FK safety)
2404
2390
  try {
2405
- const record = await table.User.findById(1);
2391
+ const record = await this.context.User.findById(1);
2406
2392
  if (record) await record.delete();
2407
2393
  } catch (e) {
2408
2394
  console.warn('Seed rollback: User id=1 not found');
@@ -2554,7 +2540,7 @@ const factoryRecords = [
2554
2540
  // ... 8 more
2555
2541
  ];
2556
2542
  for (const record of factoryRecords) {
2557
- await table.User.create(record);
2543
+ await this.seed('User', record);
2558
2544
  }
2559
2545
  ```
2560
2546
 
@@ -2613,16 +2599,16 @@ class AppContext extends context {
2613
2599
  **Generated Migration:**
2614
2600
  ```javascript
2615
2601
  async up(table) {
2616
- this.init(table);
2602
+ await this.init(table);
2617
2603
 
2618
2604
  // Check-then-update pattern (database-agnostic)
2619
2605
  {
2620
- const existing = await table.User.where(r => r.email == 'admin@example.com').single();
2606
+ const existing = await this.context.User.where(r => r.email == 'admin@example.com').single();
2621
2607
  if (existing) {
2622
2608
  existing.name = 'Administrator';
2623
2609
  await existing.save();
2624
2610
  } else {
2625
- await table.User.create({ email: 'admin@example.com', name: 'Administrator' });
2611
+ await this.seed('User', { email: 'admin@example.com', name: 'Administrator' });
2626
2612
  }
2627
2613
  }
2628
2614
  }