|
40 | 40 | * - the artifact envelope (`packages`, a nested `manifest`), because one |
41 | 41 | * import copies one package. |
42 | 42 | * |
43 | | - * ## Translations are copied; seeds and deployment configuration are not |
| 43 | + * ## Translations are copied; seeds only when the door asks; deployment configuration never |
44 | 44 | * |
45 | 45 | * - `translations` (i18n bundles) are COPIED, converted: each locale of each |
46 | 46 | * bundle becomes one `translation` item named `<namespace>_<locale>` (the |
|
51 | 51 | * ledger entry, so its bundles live where environment translations live — |
52 | 52 | * in the environment ledger, reloaded at every boot by the |
53 | 53 | * authored-translation sync; |
54 | | - * - `data` (seed datasets) is data, never a metadata item (the artifact loader |
55 | | - * says so beside its collection map), so a copy of the metadata cannot carry |
56 | | - * it; the install door that calls this verb states what it did with it; |
| 54 | + * - `data` (seed datasets) is data, never a metadata item of the package (the |
| 55 | + * artifact loader says so beside its collection map), so a copy of the |
| 56 | + * metadata does not carry it; the install door that calls this verb states |
| 57 | + * what it did with it. [#30808] When that door ASKS for the sample data |
| 58 | + * (`seedData`, door-stated like `actor`, off by default), each dataset is |
| 59 | + * written into the base as one `seed` item ({@link unmanagedSeedItems}) — the |
| 60 | + * copy's SEED RECEIPT in the environment ledger. The door loads the rows |
| 61 | + * from those stored bodies, owned by the installer, and the purge door |
| 62 | + * deletes exactly them by the same `externalId` match it runs for a managed |
| 63 | + * install's ledger body, so no row gains a marker key. Under a rename only |
| 64 | + * the dataset's `object` follows the copy's object name: the records are |
| 65 | + * data, written verbatim, so each `externalId` value stays the one the |
| 66 | + * package shipped; |
57 | 67 | * - deployment configuration (`i18n`, `requires`, `datasourceMapping`, `api`, |
58 | 68 | * …) is read by the BOOT of the stack that declares it, never by an install |
59 | 69 | * of a package, in either mode. |
@@ -109,7 +119,12 @@ import { ASSEMBLED_VIEW_ITEMS_KEY, expandViewContainer, isAggregatedViewContaine |
109 | 119 | import { postureEnforcesWall, type TenancyPosture } from '@objectstack/spec/security'; |
110 | 120 | import { resolveTenancyPosture } from '@objectstack/types'; |
111 | 121 | import { deriveViewContainerObject } from '@objectstack/metadata/view-container'; |
112 | | -import { PLATFORM_RESERVED_NAMESPACES, planNamespaceRename, type NamespaceRename } from './package-namespace-rename.js'; |
| 122 | +import { |
| 123 | + METADATA_ITEM_NAME_STORAGE_LIMIT, |
| 124 | + PLATFORM_RESERVED_NAMESPACES, |
| 125 | + planNamespaceRename, |
| 126 | + type NamespaceRename, |
| 127 | +} from './package-namespace-rename.js'; |
113 | 128 |
|
114 | 129 | /** |
115 | 130 | * Every collection the import copies: manifest key → metadata type. The |
@@ -366,6 +381,13 @@ export function planUnmanagedImport( |
366 | 381 | * source's. Judged here, by the manifest key's own declaration. |
367 | 382 | */ |
368 | 383 | targetNamespace?: string; |
| 384 | + /** |
| 385 | + * [#30808] The install door asked for the package's sample data: each |
| 386 | + * `data` dataset is planned as a `seed` item of the base, after every |
| 387 | + * other item ({@link unmanagedSeedItems}). Absent or `false`, `data` is |
| 388 | + * left to the door, as before. |
| 389 | + */ |
| 390 | + seedData?: boolean; |
369 | 391 | isRuntimeCreatable: (type: string) => boolean; |
370 | 392 | loadOrderOf: (type: string) => number; |
371 | 393 | /** |
@@ -443,8 +465,20 @@ export function planUnmanagedImport( |
443 | 465 | }); |
444 | 466 | } |
445 | 467 | break; |
446 | | - case 'base': |
447 | 468 | case 'door': |
| 469 | + // [#30808] Asked for, the sample data is copied as `seed` items — |
| 470 | + // refused whole if that type ever stops being one the save door |
| 471 | + // may create, as `translations` is above. |
| 472 | + if (key === SEED_DATA_KEY && options.seedData === true && unmanagedInstallValueCarries(value) |
| 473 | + && !options.isRuntimeCreatable(UNMANAGED_SEED_TYPE)) { |
| 474 | + findings.push({ |
| 475 | + key, |
| 476 | + reason: `the sample data was asked for and is copied as '${UNMANAGED_SEED_TYPE}' items, and the ` |
| 477 | + + `metadata-type registry declares '${UNMANAGED_SEED_TYPE}' runtime-uncreatable`, |
| 478 | + }); |
| 479 | + } |
| 480 | + break; |
| 481 | + case 'base': |
448 | 482 | case 'deployment': |
449 | 483 | break; |
450 | 484 | default: |
@@ -574,6 +608,13 @@ export function planUnmanagedImport( |
574 | 608 | })); |
575 | 609 | rename = planned; |
576 | 610 | } |
| 611 | + // [#30808] The sample data, when the door asked for it: one `seed` item per |
| 612 | + // dataset, after every other item, so each object a dataset fills is |
| 613 | + // written (and its table made) before the seed naming it. |
| 614 | + if (options.seedData === true && unmanagedInstallValueCarries(manifest[SEED_DATA_KEY])) { |
| 615 | + const objectRenames = rename?.objectRenames; |
| 616 | + items = [...items, ...unmanagedSeedItems(manifest[SEED_DATA_KEY], (object) => objectRenames?.get(object) ?? object)]; |
| 617 | + } |
577 | 618 | const sourceVersion = stringKey(manifest, 'version'); |
578 | 619 | const baseManifest: Record<string, unknown> = { |
579 | 620 | ...identity, |
@@ -628,6 +669,74 @@ export function unmanagedInstallRenameRefusal( |
628 | 669 | /** The metadata type a `translations` bundle's locales are copied as. */ |
629 | 670 | const TRANSLATION_TYPE = 'translation'; |
630 | 671 |
|
| 672 | +/** [#30808] The package body key that carries the seed datasets. */ |
| 673 | +const SEED_DATA_KEY = 'data'; |
| 674 | + |
| 675 | +/** [#30808] The metadata type a seed dataset is copied as when the door asks for the sample data. */ |
| 676 | +export const UNMANAGED_SEED_TYPE = 'seed'; |
| 677 | + |
| 678 | +/** |
| 679 | + * [#30808] The `data` datasets as the copy's `seed` items — the seed receipt |
| 680 | + * the install door loads the rows from and the purge door deletes them by. |
| 681 | + * |
| 682 | + * - Each dataset is ONE item, in declaration order, named after the object it |
| 683 | + * fills as the copy names it: `<object>_seed`, then `<object>_seed_2`, … for |
| 684 | + * a second dataset of one object (a per-locale pair, say). |
| 685 | + * - The body is the dataset as the package declared it, with `object` set to |
| 686 | + * the copy's name for it (`copyObjectName`, the rename's own object map). |
| 687 | + * ⛔ Nothing else is rewritten: the records are data, not metadata, so the |
| 688 | + * rename engine's free-text rewrite — which turns any value spelled like a |
| 689 | + * source object name into the copy's — never reaches them, and every |
| 690 | + * `externalId` value (the purge's identity) is the one the package shipped. |
| 691 | + * - A dataset that is not an object, or names no object or no records, refuses |
| 692 | + * the import before anything is written (`INVALID_REQUEST` / 400), as an |
| 693 | + * unnamed item does; so does an item name the save door's grammar refuses. |
| 694 | + */ |
| 695 | +export function unmanagedSeedItems( |
| 696 | + value: unknown, |
| 697 | + copyObjectName: (object: string) => string, |
| 698 | +): UnmanagedImportItem[] { |
| 699 | + const problems: string[] = []; |
| 700 | + if (!Array.isArray(value)) { |
| 701 | + problems.push(`\`${SEED_DATA_KEY}\` is not an array of seed datasets`); |
| 702 | + } |
| 703 | + const items: UnmanagedImportItem[] = []; |
| 704 | + const perObject = new Map<string, number>(); |
| 705 | + (Array.isArray(value) ? value : []).forEach((dataset: unknown, index) => { |
| 706 | + if (!isPlainRecord(dataset)) { |
| 707 | + problems.push(`\`${SEED_DATA_KEY}[${index}]\` is not a seed dataset object`); |
| 708 | + return; |
| 709 | + } |
| 710 | + if (typeof dataset.object !== 'string' || dataset.object === '') { |
| 711 | + problems.push(`\`${SEED_DATA_KEY}[${index}]\` names no \`object\``); |
| 712 | + return; |
| 713 | + } |
| 714 | + if (!Array.isArray(dataset.records)) { |
| 715 | + problems.push(`\`${SEED_DATA_KEY}[${index}]\` (object '${dataset.object}') has no \`records\` array`); |
| 716 | + return; |
| 717 | + } |
| 718 | + const object = copyObjectName(dataset.object); |
| 719 | + const nth = (perObject.get(object) ?? 0) + 1; |
| 720 | + perObject.set(object, nth); |
| 721 | + const name = nth === 1 ? `${object}_seed` : `${object}_seed_${nth}`; |
| 722 | + if (!METADATA_ITEM_NAME_PATTERN.test(name) || name.length > METADATA_ITEM_NAME_STORAGE_LIMIT) { |
| 723 | + problems.push(`\`${SEED_DATA_KEY}[${index}]\` would be copied as the seed item ${JSON.stringify(name)}, which is not a legal metadata item name`); |
| 724 | + return; |
| 725 | + } |
| 726 | + items.push({ type: UNMANAGED_SEED_TYPE, name, key: SEED_DATA_KEY, body: { ...dataset, object }, expands: [] }); |
| 727 | + }); |
| 728 | + if (problems.length > 0) { |
| 729 | + const shown = problems.slice(0, 5).join('; '); |
| 730 | + throw refusal( |
| 731 | + 'INVALID_REQUEST', |
| 732 | + 400, |
| 733 | + `The package's sample data cannot be copied as written: ${shown}` |
| 734 | + + `${problems.length > 5 ? ` (+${problems.length - 5} more)` : ''}. Nothing was installed.`, |
| 735 | + ); |
| 736 | + } |
| 737 | + return items; |
| 738 | +} |
| 739 | + |
631 | 740 | /** A plain `{ … }` object — the shape a bundle, and each group in it, takes. */ |
632 | 741 | function isPlainRecord(value: unknown): value is Record<string, unknown> { |
633 | 742 | return value !== null && typeof value === 'object' && !Array.isArray(value); |
|
0 commit comments