|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { ObjectSchema, Field } from '@objectstack/spec/data'; |
| 4 | + |
| 5 | +/** |
| 6 | + * sys_seed_landed_key — which declared seed rows have landed in an |
| 7 | + * organization, one row per seed key (#31021). |
| 8 | + * |
| 9 | + * A seed is replayed long after its first load: on every single-tenant boot, |
| 10 | + * and per organization whenever a host replays the seed union (a package |
| 11 | + * upgrade that changes it). The create-only rule (`SeedSchema.mode`'s |
| 12 | + * `ignore` default) inserts every declared row it does not find, so a row a |
| 13 | + * person deleted came back at the next replay under a new id. This table is |
| 14 | + * what tells "never landed here" from "landed here and since removed": the |
| 15 | + * seed loader records the key of every keyed row it lands — inserted, updated |
| 16 | + * or matched — under the organization it seeds, and does not insert a |
| 17 | + * recorded key again once its row is gone. A key never recorded still lands, |
| 18 | + * so a row an upgrade adds to the seed is inserted as before. |
| 19 | + * |
| 20 | + * Row identity: (organization_id, object_name, seed_key). `seed_key` is the |
| 21 | + * dataset's natural key — its `externalId` field names, each with the value |
| 22 | + * the loader matched the row by — JSON-encoded (`[["name","Acme"]]`), so a |
| 23 | + * composite key needs no separator byte and two datasets keyed on different |
| 24 | + * fields of one object never share a key. An `id` key is recorded under the |
| 25 | + * id the seed AUTHORED, not the per-organization id a replay may give the row |
| 26 | + * (#21665), so it reads the same in every organization. |
| 27 | + * |
| 28 | + * Writer and reader: `SeedLoaderService` (`@objectstack/metadata-protocol`) |
| 29 | + * alone, under the seed write context, on the loads that ask for this record |
| 30 | + * (`landedKeys`): the boot seed, the per-organization replay and the dev |
| 31 | + * hot-reload seeder of `@objectstack/runtime`'s AppPlugin. Internal state, |
| 32 | + * not a setting: ⛔ no data API, no settings key, no environment variable. |
| 33 | + * |
| 34 | + * Clearing it is never needed for a re-seed: the deliberate re-seed (the |
| 35 | + * `seed-replayer` service called with `{ reseed: true }`) inserts every |
| 36 | + * missing row whatever this table says and records them again, and a new |
| 37 | + * database starts with no rows. Deleting an organization deletes its rows. |
| 38 | + * |
| 39 | + * Lifecycle: **no `lifecycle` block on purpose** — durable. Reaping a row |
| 40 | + * would bring the seed row a person deleted back at the next replay. |
| 41 | + * |
| 42 | + * Registered by `PlatformObjectsPlugin` (`../plugin.ts`). |
| 43 | + * |
| 44 | + * @namespace sys |
| 45 | + */ |
| 46 | +export const SysSeedLandedKey = ObjectSchema.create({ |
| 47 | + name: 'sys_seed_landed_key', |
| 48 | + label: 'Seed Landed Key', |
| 49 | + pluralLabel: 'Seed Landed Keys', |
| 50 | + icon: 'history', |
| 51 | + isSystem: true, |
| 52 | + managedBy: 'engine-owned', |
| 53 | + // Organization-owned: a key landed in one organization says nothing about |
| 54 | + // another. Who may read is object permission: the platform-only capability. |
| 55 | + requiredPermissions: ['manage_platform_settings'], |
| 56 | + description: |
| 57 | + 'Which declared seed rows have landed in this organization, one row per seed key: a seed replay does not insert a recorded key again once its row is gone.', |
| 58 | + nameField: 'seed_key', // [ADR-0079] canonical primary-title pointer |
| 59 | + highlightFields: ['object_name', 'seed_key', 'created_at'], |
| 60 | + |
| 61 | + fields: { |
| 62 | + id: Field.text({ |
| 63 | + label: 'ID', |
| 64 | + readonly: true, |
| 65 | + }), |
| 66 | + |
| 67 | + object_name: Field.text({ |
| 68 | + label: 'Object', |
| 69 | + required: true, |
| 70 | + readonly: true, |
| 71 | + maxLength: 255, |
| 72 | + description: 'The seeded object the key belongs to.', |
| 73 | + }), |
| 74 | + |
| 75 | + seed_key: Field.textarea({ |
| 76 | + label: 'Seed Key', |
| 77 | + required: true, |
| 78 | + readonly: true, |
| 79 | + description: |
| 80 | + 'The seed row\'s natural key: its dataset\'s externalId field names with the values the row was matched by, JSON-encoded (`[["name","Acme"]]`).', |
| 81 | + }), |
| 82 | + |
| 83 | + organization_id: Field.lookup('sys_organization', { |
| 84 | + label: 'Organization', |
| 85 | + required: false, |
| 86 | + readonly: true, |
| 87 | + // The organization's delete takes its record with it. |
| 88 | + deleteBehavior: 'cascade', |
| 89 | + description: 'The organization the seed row landed in. Deleting the organization deletes its rows.', |
| 90 | + }), |
| 91 | + |
| 92 | + created_at: Field.datetime({ |
| 93 | + label: 'Landed At', |
| 94 | + readonly: true, |
| 95 | + description: 'When the seed loader first recorded the key in this organization.', |
| 96 | + }), |
| 97 | + }, |
| 98 | + |
| 99 | + indexes: [ |
| 100 | + // The loader's one access path: the keys of one object in one organization. |
| 101 | + { fields: ['organization_id', 'object_name'], unique: false }, |
| 102 | + ], |
| 103 | + |
| 104 | + enable: { |
| 105 | + trackHistory: false, |
| 106 | + searchable: false, |
| 107 | + // Internal state — no data API at all. The seed loader reads and writes |
| 108 | + // it in process, under the seed write context. |
| 109 | + apiEnabled: false, |
| 110 | + apiMethods: [], |
| 111 | + }, |
| 112 | +}); |
0 commit comments