@virtocommerce/test-automation-framework 0.0.0-stage → 0.1.0-alpha.1
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/.biome/formatter.json +11 -0
- package/.biome/imports.json +52 -0
- package/.biome/preset.json +4 -0
- package/.biome/rules.json +45 -0
- package/.claude/agents/qa-automation-expert.md +119 -0
- package/.claude/agents/qa-coverage.md +96 -0
- package/.claude/agents/qa-explorer.md +112 -0
- package/.claude/agents/qa-healer.md +88 -0
- package/.claude/agents/qa-judge.md +102 -0
- package/.claude/agents/qa-planner.md +104 -0
- package/.claude/agents/qa-reviewer.md +121 -0
- package/.claude/checks/browser-secrets.mts +107 -0
- package/.claude/checks/contract.mts +469 -0
- package/.claude/checks/expectation-guard.mts +665 -0
- package/.claude/checks/explore-extractors.mts +4 -0
- package/.claude/checks/judge-verdict.mts +165 -0
- package/.claude/checks/project-facts.mts +491 -0
- package/.claude/checks/prove-fails.mts +770 -0
- package/.claude/checks/rank-gaps.mts +177 -0
- package/.claude/checks/readonly-shell.mts +740 -0
- package/.claude/checks/reconcile-run.mts +270 -0
- package/.claude/checks/scan-prompts.mts +351 -0
- package/.claude/checks/stand.mts +45 -0
- package/.claude/checks/test-duration.mts +134 -0
- package/.claude/checks/test-shape.mts +224 -0
- package/.claude/checks/ticket-card.mts +195 -0
- package/.claude/commands/qa-coverage.md +13 -0
- package/.claude/commands/qa-explore.md +17 -0
- package/.claude/commands/qa-heal.md +44 -0
- package/.claude/commands/qa-implement.md +60 -0
- package/.claude/commands/qa-plan.md +55 -0
- package/.claude/commands/qa-review.md +90 -0
- package/.claude/commands/qa.md +156 -0
- package/.claude/hooks/readonly-shell.mts +65 -0
- package/.claude/settings.json +15 -0
- package/.claude/skills/coverage-analysis/SKILL.md +107 -0
- package/.claude/skills/create-graphql-layer/SKILL.md +159 -0
- package/.claude/skills/create-ui-layer/SKILL.md +189 -0
- package/.claude/skills/explore-api/SKILL.md +74 -0
- package/.claude/skills/explore-frontend/SKILL.md +85 -0
- package/.claude/skills/framework-rules/SKILL.md +189 -0
- package/.claude/skills/heal-tests/SKILL.md +197 -0
- package/.claude/skills/heal-tests/guard-recipe.md +53 -0
- package/.claude/skills/judge-criteria/SKILL.md +40 -0
- package/.claude/skills/judge-criteria/coverage.md +9 -0
- package/.claude/skills/judge-criteria/expert.md +23 -0
- package/.claude/skills/judge-criteria/explorer.md +9 -0
- package/.claude/skills/judge-criteria/healer.md +13 -0
- package/.claude/skills/judge-criteria/planner.md +12 -0
- package/.claude/skills/plan-implementation/SKILL.md +82 -0
- package/.claude/skills/plan-implementation/contract-template.md +72 -0
- package/.claude/skills/plan-test-strategy/SKILL.md +100 -0
- package/.claude/skills/project-facts/SKILL.md +130 -0
- package/.claude/skills/read-change/SKILL.md +64 -0
- package/.claude/skills/read-ticket/SKILL.md +63 -0
- package/.claude/skills/read-ticket/providers.md +58 -0
- package/.claude/skills/review-pr/SKILL.md +152 -0
- package/.claude/skills/review-test-code/SKILL.md +208 -0
- package/.claude/skills/write-e2e-test/SKILL.md +175 -0
- package/.claude/skills/write-graphql-test/SKILL.md +158 -0
- package/.claude/skills/write-rest-api-test/SKILL.md +182 -0
- package/.claude/skills/write-security-test/SKILL.md +253 -0
- package/.qa/project.example.json +71 -0
- package/CHANGELOG.md +1 -0
- package/LICENSE +12 -0
- package/README.md +53 -2
- package/bin/install-qa-agents.mjs +94 -0
- package/dist/auth/credentials.d.ts +10 -0
- package/dist/auth/credentials.js +2 -0
- package/dist/auth/credentials.js.map +1 -0
- package/dist/auth/frontend-browser-sessions.d.ts +71 -0
- package/dist/auth/frontend-browser-sessions.js +130 -0
- package/dist/auth/frontend-browser-sessions.js.map +1 -0
- package/dist/auth/index.d.ts +9 -0
- package/dist/auth/index.js +5 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/auth/platform-browser-sessions.d.ts +47 -0
- package/dist/auth/platform-browser-sessions.js +92 -0
- package/dist/auth/platform-browser-sessions.js.map +1 -0
- package/dist/auth/sign-in-lock.d.ts +14 -0
- package/dist/auth/sign-in-lock.js +70 -0
- package/dist/auth/sign-in-lock.js.map +1 -0
- package/dist/auth/storage-state.d.ts +3 -0
- package/dist/auth/storage-state.js +2 -0
- package/dist/auth/storage-state.js.map +1 -0
- package/dist/auth/token-client.d.ts +82 -0
- package/dist/auth/token-client.js +111 -0
- package/dist/auth/token-client.js.map +1 -0
- package/dist/auth/token-manager.d.ts +67 -0
- package/dist/auth/token-manager.js +138 -0
- package/dist/auth/token-manager.js.map +1 -0
- package/dist/core/default-reporters.d.ts +31 -0
- package/dist/core/default-reporters.js +36 -0
- package/dist/core/default-reporters.js.map +1 -0
- package/dist/core/default-use.d.ts +28 -0
- package/dist/core/default-use.js +25 -0
- package/dist/core/default-use.js.map +1 -0
- package/dist/core/env.d.ts +24 -0
- package/dist/core/env.js +33 -0
- package/dist/core/env.js.map +1 -0
- package/dist/core/error-message.d.ts +10 -0
- package/dist/core/error-message.js +13 -0
- package/dist/core/error-message.js.map +1 -0
- package/dist/core/index.d.ts +16 -0
- package/dist/core/index.js +11 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/is-ci.d.ts +14 -0
- package/dist/core/is-ci.js +18 -0
- package/dist/core/is-ci.js.map +1 -0
- package/dist/core/parse-env.d.ts +13 -0
- package/dist/core/parse-env.js +21 -0
- package/dist/core/parse-env.js.map +1 -0
- package/dist/core/projects.d.ts +24 -0
- package/dist/core/projects.js +33 -0
- package/dist/core/projects.js.map +1 -0
- package/dist/core/run-options.d.ts +31 -0
- package/dist/core/run-options.js +43 -0
- package/dist/core/run-options.js.map +1 -0
- package/dist/core/secret.d.ts +105 -0
- package/dist/core/secret.js +167 -0
- package/dist/core/secret.js.map +1 -0
- package/dist/core/string-schemas.d.ts +39 -0
- package/dist/core/string-schemas.js +56 -0
- package/dist/core/string-schemas.js.map +1 -0
- package/dist/core/url-path.d.ts +7 -0
- package/dist/core/url-path.js +2 -0
- package/dist/core/url-path.js.map +1 -0
- package/dist/dataset/collection.d.ts +44 -0
- package/dist/dataset/collection.js +76 -0
- package/dist/dataset/collection.js.map +1 -0
- package/dist/dataset/index.d.ts +11 -0
- package/dist/dataset/index.js +6 -0
- package/dist/dataset/index.js.map +1 -0
- package/dist/dataset/load-dataset.d.ts +39 -0
- package/dist/dataset/load-dataset.js +62 -0
- package/dist/dataset/load-dataset.js.map +1 -0
- package/dist/dataset/manifest.d.ts +77 -0
- package/dist/dataset/manifest.js +10 -0
- package/dist/dataset/manifest.js.map +1 -0
- package/dist/dataset/placeholders.d.ts +66 -0
- package/dist/dataset/placeholders.js +87 -0
- package/dist/dataset/placeholders.js.map +1 -0
- package/dist/dataset/seed-collection.d.ts +53 -0
- package/dist/dataset/seed-collection.js +115 -0
- package/dist/dataset/seed-collection.js.map +1 -0
- package/dist/dataset/seed-dataset.d.ts +70 -0
- package/dist/dataset/seed-dataset.js +138 -0
- package/dist/dataset/seed-dataset.js.map +1 -0
- package/dist/dataset/seed-events.d.ts +81 -0
- package/dist/dataset/seed-events.js +2 -0
- package/dist/dataset/seed-events.js.map +1 -0
- package/dist/dataset/seed-requests.d.ts +102 -0
- package/dist/dataset/seed-requests.js +160 -0
- package/dist/dataset/seed-requests.js.map +1 -0
- package/dist/dataset/seed-scope.d.ts +39 -0
- package/dist/dataset/seed-scope.js +67 -0
- package/dist/dataset/seed-scope.js.map +1 -0
- package/dist/fixtures/default-fixtures.d.ts +93 -0
- package/dist/fixtures/default-fixtures.js +105 -0
- package/dist/fixtures/default-fixtures.js.map +1 -0
- package/dist/fixtures/index.d.ts +2 -0
- package/dist/fixtures/index.js +2 -0
- package/dist/fixtures/index.js.map +1 -0
- package/dist/graphql/graphql-client.d.ts +78 -0
- package/dist/graphql/graphql-client.js +83 -0
- package/dist/graphql/graphql-client.js.map +1 -0
- package/dist/graphql/graphql-codegen.d.ts +56 -0
- package/dist/graphql/graphql-codegen.js +42 -0
- package/dist/graphql/graphql-codegen.js.map +1 -0
- package/dist/graphql/graphql-error.d.ts +54 -0
- package/dist/graphql/graphql-error.js +74 -0
- package/dist/graphql/graphql-error.js.map +1 -0
- package/dist/graphql/index.d.ts +6 -0
- package/dist/graphql/index.js +4 -0
- package/dist/graphql/index.js.map +1 -0
- package/dist/http/authentication.d.ts +16 -0
- package/dist/http/authentication.js +2 -0
- package/dist/http/authentication.js.map +1 -0
- package/dist/http/http-client.d.ts +110 -0
- package/dist/http/http-client.js +237 -0
- package/dist/http/http-client.js.map +1 -0
- package/dist/http/http-error.d.ts +26 -0
- package/dist/http/http-error.js +34 -0
- package/dist/http/http-error.js.map +1 -0
- package/dist/http/http-method.d.ts +2 -0
- package/dist/http/http-method.js +2 -0
- package/dist/http/http-method.js.map +1 -0
- package/dist/http/http-response.d.ts +137 -0
- package/dist/http/http-response.js +166 -0
- package/dist/http/http-response.js.map +1 -0
- package/dist/http/index.d.ts +7 -0
- package/dist/http/index.js +4 -0
- package/dist/http/index.js.map +1 -0
- package/dist/http/request-options.d.ts +62 -0
- package/dist/http/request-options.js +2 -0
- package/dist/http/request-options.js.map +1 -0
- package/dist/modules/index.d.ts +2 -0
- package/dist/modules/index.js +2 -0
- package/dist/modules/index.js.map +1 -0
- package/dist/modules/installed-modules.d.ts +86 -0
- package/dist/modules/installed-modules.js +135 -0
- package/dist/modules/installed-modules.js.map +1 -0
- package/docs/README.md +43 -0
- package/docs/concepts/areas.md +38 -0
- package/docs/concepts/auth.md +92 -0
- package/docs/concepts/config.md +102 -0
- package/docs/concepts/dataset.md +183 -0
- package/docs/concepts/environment.md +54 -0
- package/docs/concepts/fixtures.md +109 -0
- package/docs/concepts/graphql.md +76 -0
- package/docs/concepts/http.md +88 -0
- package/docs/concepts/modules.md +63 -0
- package/docs/concepts/secrets.md +50 -0
- package/docs/contributing/architecture.md +67 -0
- package/docs/contributing/conventions.md +39 -0
- package/docs/contributing/documentation.md +37 -0
- package/docs/contributing/releasing.md +76 -0
- package/docs/examples/auth.ts +20 -0
- package/docs/examples/config.ts +36 -0
- package/docs/examples/dataset.ts +70 -0
- package/docs/examples/environment.ts +8 -0
- package/docs/examples/fixtures.ts +41 -0
- package/docs/examples/getting-started.ts +22 -0
- package/docs/examples/graphql-codegen.ts +9 -0
- package/docs/examples/graphql.ts +32 -0
- package/docs/examples/http.ts +18 -0
- package/docs/examples/modules.ts +19 -0
- package/docs/examples/secrets.ts +11 -0
- package/docs/examples/tsconfig.json +10 -0
- package/docs/getting-started.md +106 -0
- package/docs/guides/generating-types.md +75 -0
- package/docs/guides/qa-agents.md +66 -0
- package/package.json +156 -4
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { errorMessage, maskSecrets } from "../core/index.js";
|
|
2
|
+
import { attempt, seedCollection } from "./seed-collection.js";
|
|
3
|
+
import { checkSpec, labeled, pathResolver } from "./seed-requests.js";
|
|
4
|
+
import { resolveSeedScope } from "./seed-scope.js";
|
|
5
|
+
/**
|
|
6
|
+
* Seeds a dataset into the platform, collection by collection in the order of the manifest.
|
|
7
|
+
*
|
|
8
|
+
* @remarks
|
|
9
|
+
* - The run covers every collection, or the ones in `only`, except those whose module is not installed or not in its
|
|
10
|
+
* range of versions. Collections without records are skipped too.
|
|
11
|
+
* - The manifest entries of the collections in the run are checked before the first request. A collection sent in one
|
|
12
|
+
* request cannot have a `parentField` or `${RECORD:field}` placeholders in its `path` and `ifMatchPath`.
|
|
13
|
+
* - A record that the `findPath` of its collection finds is left as it is.
|
|
14
|
+
* - The finisher of a collection runs right after the collection.
|
|
15
|
+
* - The first failure stops the run. A success status with `succeeded: false` in the body also counts as a failure,
|
|
16
|
+
* because some endpoints refuse a record that way.
|
|
17
|
+
* - `onEvent` receives each step, as a {@link SeedEvent}; a failure is reported before it is thrown, and the run
|
|
18
|
+
* always ends with a `finished` event.
|
|
19
|
+
*
|
|
20
|
+
* @param run - The client, the manifest, the dataset, the installed modules and the options.
|
|
21
|
+
* @returns What the run seeded and skipped.
|
|
22
|
+
* @throws {Error} A manifest entry is invalid, the installed version of a module cannot be compared with its range, a
|
|
23
|
+
* record cannot be sent, a request fails, a finisher fails, or `onEvent` fails. The message names the collection and,
|
|
24
|
+
* where there is one, the record, and has secrets masked. The cause is the original error.
|
|
25
|
+
* @throws {AggregateError} The run failed, and `onEvent` failed too while it reported the failure. The errors are the
|
|
26
|
+
* failure of the run, then the failure of `onEvent`.
|
|
27
|
+
*/
|
|
28
|
+
export async function seedDataset(run) {
|
|
29
|
+
const notify = notifier(run.onEvent);
|
|
30
|
+
const startedAt = Date.now();
|
|
31
|
+
const totals = { seeded: [], progress: [], skipped: [] };
|
|
32
|
+
function finished(failed) {
|
|
33
|
+
return {
|
|
34
|
+
kind: "finished",
|
|
35
|
+
seeded: totals.seeded.length,
|
|
36
|
+
skipped: totals.skipped.length,
|
|
37
|
+
failed,
|
|
38
|
+
sent: sum(totals.progress, "sent"),
|
|
39
|
+
existing: sum(totals.progress, "existing"),
|
|
40
|
+
seconds: secondsSince(startedAt),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
const scope = await reported(notify, () => ({ kind: "failed", step: "manifest", seconds: secondsSince(startedAt) }), () => scopeOf(run));
|
|
45
|
+
totals.skipped = scope.skipped;
|
|
46
|
+
await seedScope(run, scope, notify, totals);
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
throw withListenerFailure(error, () => notify(finished(1)));
|
|
50
|
+
}
|
|
51
|
+
notify(finished(0));
|
|
52
|
+
return {
|
|
53
|
+
collections: totals.seeded,
|
|
54
|
+
skipped: totals.skipped,
|
|
55
|
+
sent: sum(totals.progress, "sent"),
|
|
56
|
+
existing: sum(totals.progress, "existing"),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function scopeOf(run) {
|
|
60
|
+
const scope = resolveSeedScope(run.manifest, run.installed, (name) => run.dataset[name].count, run.only);
|
|
61
|
+
for (const [name, spec] of scope.collections) {
|
|
62
|
+
checkSpec(name, spec);
|
|
63
|
+
}
|
|
64
|
+
return scope;
|
|
65
|
+
}
|
|
66
|
+
async function seedScope(run, scope, notify, totals) {
|
|
67
|
+
for (const { name, reason } of scope.skipped) {
|
|
68
|
+
notify({ kind: "skipped", name, reason });
|
|
69
|
+
}
|
|
70
|
+
notify({ kind: "started", collections: scope.collections.map(([name]) => name) });
|
|
71
|
+
const values = run.placeholders ?? {};
|
|
72
|
+
const resolve = pathResolver(values);
|
|
73
|
+
for (const [name, spec] of scope.collections) {
|
|
74
|
+
const records = run.dataset[name].all();
|
|
75
|
+
const collectionStartedAt = Date.now();
|
|
76
|
+
const progress = { sent: 0, existing: 0, subject: undefined };
|
|
77
|
+
totals.progress.push(progress);
|
|
78
|
+
const collection = { http: run.http, name, spec, values, resolve, progress };
|
|
79
|
+
await reported(notify, () => collectionFailed(name, "records", countsOf(records.length, progress), collectionStartedAt, progress.subject), () => seedCollection(collection, labeled(spec, records)));
|
|
80
|
+
totals.seeded.push(name);
|
|
81
|
+
const counts = countsOf(records.length, progress);
|
|
82
|
+
notify({ kind: "seeded", name, counts, seconds: secondsSince(collectionStartedAt) });
|
|
83
|
+
const finisher = run.finishers?.[name];
|
|
84
|
+
if (finisher !== undefined) {
|
|
85
|
+
const finisherStartedAt = Date.now();
|
|
86
|
+
await reported(notify, () => collectionFailed(name, "finisher", counts, finisherStartedAt), () => attempt(name, "finisher", finisher));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
async function reported(notify, failed, work) {
|
|
91
|
+
try {
|
|
92
|
+
return await work();
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
throw withListenerFailure(error, () => notify(failed()));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function notifier(onEvent) {
|
|
99
|
+
return function notify(event) {
|
|
100
|
+
try {
|
|
101
|
+
onEvent?.(event);
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
throw new Error(`The seed event listener failed on a ${event.kind} event: ${errorMessage(error)}`, {
|
|
105
|
+
cause: error,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
function withListenerFailure(error, report) {
|
|
111
|
+
try {
|
|
112
|
+
report();
|
|
113
|
+
return error;
|
|
114
|
+
}
|
|
115
|
+
catch (listenerError) {
|
|
116
|
+
return new AggregateError([error, listenerError], maskSecrets(`${errorMessage(error)}; ${errorMessage(listenerError)}`));
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
function collectionFailed(name, step, counts, startedAt, subject) {
|
|
120
|
+
return {
|
|
121
|
+
kind: "failed",
|
|
122
|
+
step,
|
|
123
|
+
name,
|
|
124
|
+
...(subject === undefined ? {} : { subject }),
|
|
125
|
+
counts,
|
|
126
|
+
seconds: secondsSince(startedAt),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
function countsOf(records, progress) {
|
|
130
|
+
return { records, sent: progress.sent, existing: progress.existing };
|
|
131
|
+
}
|
|
132
|
+
function sum(progress, field) {
|
|
133
|
+
return progress.reduce((total, entry) => total + entry[field], 0);
|
|
134
|
+
}
|
|
135
|
+
function secondsSince(startedAt) {
|
|
136
|
+
return (Date.now() - startedAt) / 1000;
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=seed-dataset.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"seed-dataset.js","sourceRoot":"","sources":["../../src/dataset/seed-dataset.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAEnD,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC/D,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACtE,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAqDnD;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAC/B,GAAuB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,IAAY,EACZ,IAAkC,EAClC,MAAkB,EAClB,SAAiB,EACjB,OAAgB;IAEhB,OAAO;QACL,IAAI,EAAE,QAAQ;QACd,IAAI;QACJ,IAAI;QACJ,GAAG,CAAC,OAAO,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC;QAC7C,MAAM;QACN,OAAO,EAAE,YAAY,CAAC,SAAS,CAAC;KACjC,CAAC;AACJ,CAAC;AAED,SAAS,QAAQ,CAAC,OAAe,EAAE,QAAkB;IACnD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,CAAC,IAAI,EAAE,QAAQ,EAAE,QAAQ,CAAC,QAAQ,EAAE,CAAC;AACvE,CAAC;AAED,SAAS,GAAG,CAAC,QAA6B,EAAE,KAA0B;IACpE,OAAO,QAAQ,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC;AACpE,CAAC;AAED,SAAS,YAAY,CAAC,SAAiB;IACrC,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC,GAAG,IAAI,CAAC;AACzC,CAAC","sourcesContent":["import type { HttpClient } from \"@/http\";\nimport type { InstalledModules } from \"@/modules\";\nimport type { Collection } from \"./collection.ts\";\nimport type { CollectionName, Manifest } from \"./manifest.ts\";\nimport type { PlaceholderValues } from \"./placeholders.ts\";\nimport type { Progress } from \"./seed-collection.ts\";\nimport type { SeedCollectionFailed, SeedCounts, SeedEvent, SeedFinished } from \"./seed-events.ts\";\nimport type { SeedScope, SkippedCollection } from \"./seed-scope.ts\";\n\nimport { errorMessage, maskSecrets } from \"@/core\";\n\nimport { attempt, seedCollection } from \"./seed-collection.ts\";\nimport { checkSpec, labeled, pathResolver } from \"./seed-requests.ts\";\nimport { resolveSeedScope } from \"./seed-scope.ts\";\n\n/** Work that follows the seeding of a collection, such as setting the status of the pages just seeded. */\nexport type SeedFinisher = () => Promise<void>;\n\n/** A dataset as the seeder reads it: the records of each collection of a manifest. */\nexport type SeedableDataset<TManifest extends Manifest> = {\n readonly [Name in CollectionName<TManifest>]: Collection<object>;\n};\n\n/** The options of {@link seedDataset}. */\nexport interface SeedRun<TManifest extends Manifest> {\n /** The client that sends the seed requests, signed in as a platform administrator. */\n readonly http: HttpClient;\n /** The collections, in seed order, and how each one is sent. */\n readonly manifest: TManifest;\n /** The records to seed. */\n readonly dataset: SeedableDataset<TManifest>;\n /** The modules installed on the platform. A collection whose module does not qualify is skipped. */\n readonly installed: InstalledModules;\n /** The collections to seed, in any order, or `undefined` for every collection. */\n readonly only?: readonly CollectionName<TManifest>[] | undefined;\n /** The values of the `${ENV:key}` placeholders in the paths of the manifest. */\n readonly placeholders?: PlaceholderValues | undefined;\n /** Work to run right after a collection is seeded, by collection. It is skipped with its collection. */\n readonly finishers?: Partial<Readonly<Record<CollectionName<TManifest>, SeedFinisher>>> | undefined;\n /**\n * Receives each step of the run, such as a seeded or a skipped collection. The default discards them. An error that\n * it throws stops the run; it does not replace the failure that it reports.\n */\n readonly onEvent?: ((event: SeedEvent) => void) | undefined;\n}\n\n/** The result of {@link seedDataset}. */\nexport interface SeedSummary<TManifest extends Manifest> {\n /** The collections that were seeded, in seed order. */\n readonly collections: readonly CollectionName<TManifest>[];\n /** The selected collections whose module does not qualify or that have no records, in seed order. */\n readonly skipped: readonly SkippedCollection<TManifest>[];\n /** The number of records that were sent. */\n readonly sent: number;\n /** The number of records that the platform already stored and that were left as they were. */\n readonly existing: number;\n}\n\ninterface Totals<TManifest extends Manifest> {\n readonly seeded: CollectionName<TManifest>[];\n readonly progress: Progress[];\n skipped: readonly SkippedCollection<TManifest>[];\n}\n\ntype Notify = (event: SeedEvent) => void;\n\n/**\n * Seeds a dataset into the platform, collection by collection in the order of the manifest.\n *\n * @remarks\n * - The run covers every collection, or the ones in `only`, except those whose module is not installed or not in its\n * range of versions. Collections without records are skipped too.\n * - The manifest entries of the collections in the run are checked before the first request. A collection sent in one\n * request cannot have a `parentField` or `${RECORD:field}` placeholders in its `path` and `ifMatchPath`.\n * - A record that the `findPath` of its collection finds is left as it is.\n * - The finisher of a collection runs right after the collection.\n * - The first failure stops the run. A success status with `succeeded: false` in the body also counts as a failure,\n * because some endpoints refuse a record that way.\n * - `onEvent` receives each step, as a {@link SeedEvent}; a failure is reported before it is thrown, and the run\n * always ends with a `finished` event.\n *\n * @param run - The client, the manifest, the dataset, the installed modules and the options.\n * @returns What the run seeded and skipped.\n * @throws {Error} A manifest entry is invalid, the installed version of a module cannot be compared with its range, a\n * record cannot be sent, a request fails, a finisher fails, or `onEvent` fails. The message names the collection and,\n * where there is one, the record, and has secrets masked. The cause is the original error.\n * @throws {AggregateError} The run failed, and `onEvent` failed too while it reported the failure. The errors are the\n * failure of the run, then the failure of `onEvent`.\n */\nexport async function seedDataset<TManifest extends Manifest>(\n run: SeedRun<TManifest>,\n): Promise<SeedSummary<TManifest>> {\n const notify = notifier(run.onEvent);\n const startedAt = Date.now();\n const totals: Totals<TManifest> = { seeded: [], progress: [], skipped: [] };\n function finished(failed: number): SeedFinished {\n return {\n kind: \"finished\",\n seeded: totals.seeded.length,\n skipped: totals.skipped.length,\n failed,\n sent: sum(totals.progress, \"sent\"),\n existing: sum(totals.progress, \"existing\"),\n seconds: secondsSince(startedAt),\n };\n }\n try {\n const scope = await reported(\n notify,\n () => ({ kind: \"failed\", step: \"manifest\", seconds: secondsSince(startedAt) }),\n () => scopeOf(run),\n );\n totals.skipped = scope.skipped;\n await seedScope(run, scope, notify, totals);\n } catch (error) {\n throw withListenerFailure(error, () => notify(finished(1)));\n }\n notify(finished(0));\n return {\n collections: totals.seeded,\n skipped: totals.skipped,\n sent: sum(totals.progress, \"sent\"),\n existing: sum(totals.progress, \"existing\"),\n };\n}\n\nfunction scopeOf<TManifest extends Manifest>(run: SeedRun<TManifest>): SeedScope<TManifest> {\n const scope = resolveSeedScope(run.manifest, run.installed, (name) => run.dataset[name].count, run.only);\n for (const [name, spec] of scope.collections) {\n checkSpec(name, spec);\n }\n return scope;\n}\n\nasync function seedScope<TManifest extends Manifest>(\n run: SeedRun<TManifest>,\n scope: SeedScope<TManifest>,\n notify: Notify,\n totals: Totals<TManifest>,\n): Promise<void> {\n for (const { name, reason } of scope.skipped) {\n notify({ kind: \"skipped\", name, reason });\n }\n notify({ kind: \"started\", collections: scope.collections.map(([name]) => name) });\n const values = run.placeholders ?? {};\n const resolve = pathResolver(values);\n for (const [name, spec] of scope.collections) {\n const records = run.dataset[name].all();\n const collectionStartedAt = Date.now();\n const progress: Progress = { sent: 0, existing: 0, subject: undefined };\n totals.progress.push(progress);\n const collection = { http: run.http, name, spec, values, resolve, progress };\n await reported(\n notify,\n () =>\n collectionFailed(name, \"records\", countsOf(records.length, progress), collectionStartedAt, progress.subject),\n () => seedCollection(collection, labeled(spec, records)),\n );\n totals.seeded.push(name);\n const counts = countsOf(records.length, progress);\n notify({ kind: \"seeded\", name, counts, seconds: secondsSince(collectionStartedAt) });\n const finisher = run.finishers?.[name];\n if (finisher !== undefined) {\n const finisherStartedAt = Date.now();\n await reported(\n notify,\n () => collectionFailed(name, \"finisher\", counts, finisherStartedAt),\n () => attempt(name, \"finisher\", finisher),\n );\n }\n }\n}\n\nasync function reported<T>(notify: Notify, failed: () => SeedEvent, work: () => T | Promise<T>): Promise<T> {\n try {\n return await work();\n } catch (error) {\n throw withListenerFailure(error, () => notify(failed()));\n }\n}\n\nfunction notifier(onEvent: ((event: SeedEvent) => void) | undefined): Notify {\n return function notify(event: SeedEvent): void {\n try {\n onEvent?.(event);\n } catch (error) {\n throw new Error(`The seed event listener failed on a ${event.kind} event: ${errorMessage(error)}`, {\n cause: error,\n });\n }\n };\n}\n\nfunction withListenerFailure(error: unknown, report: () => void): unknown {\n try {\n report();\n return error;\n } catch (listenerError) {\n return new AggregateError(\n [error, listenerError],\n maskSecrets(`${errorMessage(error)}; ${errorMessage(listenerError)}`),\n );\n }\n}\n\nfunction collectionFailed(\n name: string,\n step: SeedCollectionFailed[\"step\"],\n counts: SeedCounts,\n startedAt: number,\n subject?: string,\n): SeedCollectionFailed {\n return {\n kind: \"failed\",\n step,\n name,\n ...(subject === undefined ? {} : { subject }),\n counts,\n seconds: secondsSince(startedAt),\n };\n}\n\nfunction countsOf(records: number, progress: Progress): SeedCounts {\n return { records, sent: progress.sent, existing: progress.existing };\n}\n\nfunction sum(progress: readonly Progress[], field: \"sent\" | \"existing\"): number {\n return progress.reduce((total, entry) => total + entry[field], 0);\n}\n\nfunction secondsSince(startedAt: number): number {\n return (Date.now() - startedAt) / 1000;\n}\n"]}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/** The records of a collection, and what happened to them. */
|
|
2
|
+
export interface SeedCounts {
|
|
3
|
+
/** The number of records in the collection. */
|
|
4
|
+
readonly records: number;
|
|
5
|
+
/** The number of records that were sent. */
|
|
6
|
+
readonly sent: number;
|
|
7
|
+
/** The number of records that the platform already stored and that were left as they were. */
|
|
8
|
+
readonly existing: number;
|
|
9
|
+
}
|
|
10
|
+
/** The manifest is checked, and the collections are about to be seeded. */
|
|
11
|
+
export interface SeedStarted {
|
|
12
|
+
/** The kind of the event. */
|
|
13
|
+
readonly kind: "started";
|
|
14
|
+
/** The collections to seed, in seed order. */
|
|
15
|
+
readonly collections: readonly string[];
|
|
16
|
+
}
|
|
17
|
+
/** A collection is left out, because its module does not qualify or it has no records. */
|
|
18
|
+
export interface SeedSkipped {
|
|
19
|
+
/** The kind of the event. */
|
|
20
|
+
readonly kind: "skipped";
|
|
21
|
+
/** The name of the collection. */
|
|
22
|
+
readonly name: string;
|
|
23
|
+
/** Why it is left out, such as `VirtoCommerce.XPickup is not installed` or `no records`. */
|
|
24
|
+
readonly reason: string;
|
|
25
|
+
}
|
|
26
|
+
/** Every record of a collection is sent or already stored. */
|
|
27
|
+
export interface SeedSeeded {
|
|
28
|
+
/** The kind of the event. */
|
|
29
|
+
readonly kind: "seeded";
|
|
30
|
+
/** The name of the collection. */
|
|
31
|
+
readonly name: string;
|
|
32
|
+
/** The records of the collection, and what happened to them. */
|
|
33
|
+
readonly counts: SeedCounts;
|
|
34
|
+
/** How long the collection took, in seconds. */
|
|
35
|
+
readonly seconds: number;
|
|
36
|
+
}
|
|
37
|
+
/** The manifest is invalid, so no request is sent. */
|
|
38
|
+
export interface SeedManifestFailed {
|
|
39
|
+
/** The kind of the event. */
|
|
40
|
+
readonly kind: "failed";
|
|
41
|
+
/** What failed. */
|
|
42
|
+
readonly step: "manifest";
|
|
43
|
+
/** How long the check took, in seconds. */
|
|
44
|
+
readonly seconds: number;
|
|
45
|
+
}
|
|
46
|
+
/** A record or a request of a collection failed, or the finisher of a seeded collection failed. */
|
|
47
|
+
export interface SeedCollectionFailed {
|
|
48
|
+
/** The kind of the event. */
|
|
49
|
+
readonly kind: "failed";
|
|
50
|
+
/** What failed: the records of the collection, or its finisher. */
|
|
51
|
+
readonly step: "records" | "finisher";
|
|
52
|
+
/** The name of the collection. */
|
|
53
|
+
readonly name: string;
|
|
54
|
+
/** The record or the request that failed, such as `record "product-1"` or `12 records`, where there is one. */
|
|
55
|
+
readonly subject?: string;
|
|
56
|
+
/** The counts reached before the failure. */
|
|
57
|
+
readonly counts: SeedCounts;
|
|
58
|
+
/** How long the step took until it failed, in seconds. */
|
|
59
|
+
readonly seconds: number;
|
|
60
|
+
}
|
|
61
|
+
/** The run is over, after the last collection or after a failure. */
|
|
62
|
+
export interface SeedFinished {
|
|
63
|
+
/** The kind of the event. */
|
|
64
|
+
readonly kind: "finished";
|
|
65
|
+
/** The number of collections that were seeded. */
|
|
66
|
+
readonly seeded: number;
|
|
67
|
+
/** The number of collections that were skipped. */
|
|
68
|
+
readonly skipped: number;
|
|
69
|
+
/** The number of failures: 0, or 1 for the failure that stopped the run. */
|
|
70
|
+
readonly failed: number;
|
|
71
|
+
/** The number of records that were sent. */
|
|
72
|
+
readonly sent: number;
|
|
73
|
+
/** The number of records that the platform already stored. */
|
|
74
|
+
readonly existing: number;
|
|
75
|
+
/** How long the run took, in seconds. */
|
|
76
|
+
readonly seconds: number;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* A step of a seed run, as `onEvent` of {@link seedDataset} receives it. A run always ends with {@link SeedFinished}.
|
|
80
|
+
*/
|
|
81
|
+
export type SeedEvent = SeedStarted | SeedSkipped | SeedSeeded | SeedManifestFailed | SeedCollectionFailed | SeedFinished;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"seed-events.js","sourceRoot":"","sources":["../../src/dataset/seed-events.ts"],"names":[],"mappings":"","sourcesContent":["/** The records of a collection, and what happened to them. */\nexport interface SeedCounts {\n /** The number of records in the collection. */\n readonly records: number;\n /** The number of records that were sent. */\n readonly sent: number;\n /** The number of records that the platform already stored and that were left as they were. */\n readonly existing: number;\n}\n\n/** The manifest is checked, and the collections are about to be seeded. */\nexport interface SeedStarted {\n /** The kind of the event. */\n readonly kind: \"started\";\n /** The collections to seed, in seed order. */\n readonly collections: readonly string[];\n}\n\n/** A collection is left out, because its module does not qualify or it has no records. */\nexport interface SeedSkipped {\n /** The kind of the event. */\n readonly kind: \"skipped\";\n /** The name of the collection. */\n readonly name: string;\n /** Why it is left out, such as `VirtoCommerce.XPickup is not installed` or `no records`. */\n readonly reason: string;\n}\n\n/** Every record of a collection is sent or already stored. */\nexport interface SeedSeeded {\n /** The kind of the event. */\n readonly kind: \"seeded\";\n /** The name of the collection. */\n readonly name: string;\n /** The records of the collection, and what happened to them. */\n readonly counts: SeedCounts;\n /** How long the collection took, in seconds. */\n readonly seconds: number;\n}\n\n/** The manifest is invalid, so no request is sent. */\nexport interface SeedManifestFailed {\n /** The kind of the event. */\n readonly kind: \"failed\";\n /** What failed. */\n readonly step: \"manifest\";\n /** How long the check took, in seconds. */\n readonly seconds: number;\n}\n\n/** A record or a request of a collection failed, or the finisher of a seeded collection failed. */\nexport interface SeedCollectionFailed {\n /** The kind of the event. */\n readonly kind: \"failed\";\n /** What failed: the records of the collection, or its finisher. */\n readonly step: \"records\" | \"finisher\";\n /** The name of the collection. */\n readonly name: string;\n /** The record or the request that failed, such as `record \"product-1\"` or `12 records`, where there is one. */\n readonly subject?: string;\n /** The counts reached before the failure. */\n readonly counts: SeedCounts;\n /** How long the step took until it failed, in seconds. */\n readonly seconds: number;\n}\n\n/** The run is over, after the last collection or after a failure. */\nexport interface SeedFinished {\n /** The kind of the event. */\n readonly kind: \"finished\";\n /** The number of collections that were seeded. */\n readonly seeded: number;\n /** The number of collections that were skipped. */\n readonly skipped: number;\n /** The number of failures: 0, or 1 for the failure that stopped the run. */\n readonly failed: number;\n /** The number of records that were sent. */\n readonly sent: number;\n /** The number of records that the platform already stored. */\n readonly existing: number;\n /** How long the run took, in seconds. */\n readonly seconds: number;\n}\n\n/**\n * A step of a seed run, as `onEvent` of {@link seedDataset} receives it. A run always ends with {@link SeedFinished}.\n */\nexport type SeedEvent =\n | SeedStarted\n | SeedSkipped\n | SeedSeeded\n | SeedManifestFailed\n | SeedCollectionFailed\n | SeedFinished;\n"]}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import type { CollectionSpec } from "./manifest.ts";
|
|
2
|
+
import type { PlaceholderValues } from "./placeholders.ts";
|
|
3
|
+
/** A dataset record with the label that names it in logs and errors. */
|
|
4
|
+
export interface LabeledRecord {
|
|
5
|
+
/** The record. */
|
|
6
|
+
readonly record: object;
|
|
7
|
+
/** The label, such as `record "catalog-b2b"` or `record #3`. */
|
|
8
|
+
readonly label: string;
|
|
9
|
+
}
|
|
10
|
+
/** A seed request with its placeholders filled in. */
|
|
11
|
+
export interface SeedRequest {
|
|
12
|
+
/** The path of the request. */
|
|
13
|
+
readonly path: string;
|
|
14
|
+
/** The path whose `ETag` is sent as `If-Match`, or `undefined` when none is sent. */
|
|
15
|
+
readonly ifMatchPath: string | undefined;
|
|
16
|
+
/** The body: a record, or the records of a collection sent in one request. */
|
|
17
|
+
readonly body: unknown;
|
|
18
|
+
}
|
|
19
|
+
/** A search for the stored copy of a record, with its placeholders filled in. */
|
|
20
|
+
export interface StoredIdSearch {
|
|
21
|
+
/** The path of the search. */
|
|
22
|
+
readonly path: string;
|
|
23
|
+
/** The criteria of the search. */
|
|
24
|
+
readonly criteria: unknown;
|
|
25
|
+
/** The field that a stored record shares with the dataset record. */
|
|
26
|
+
readonly match: string;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Fills the placeholders of a path, URL-encoded: `${ENV:key}` placeholders from the placeholder values of the run and,
|
|
30
|
+
* when a record is given, `${RECORD:field}` placeholders from the record.
|
|
31
|
+
*
|
|
32
|
+
* @param template - The path with placeholders.
|
|
33
|
+
* @param record - The record, or `undefined` for a path without record placeholders.
|
|
34
|
+
* @returns The path with every placeholder filled in.
|
|
35
|
+
* @throws {Error} A placeholder has no value.
|
|
36
|
+
*/
|
|
37
|
+
export type PathResolver = (template: string, record?: object) => string;
|
|
38
|
+
/**
|
|
39
|
+
* Returns the {@link PathResolver} of a seed run.
|
|
40
|
+
*
|
|
41
|
+
* @param values - The values of the `${ENV:key}` placeholders.
|
|
42
|
+
* @returns The resolver.
|
|
43
|
+
*/
|
|
44
|
+
export declare function pathResolver(values: PlaceholderValues): PathResolver;
|
|
45
|
+
/**
|
|
46
|
+
* Ensures that a manifest entry can be seeded: a collection sent in one request has no `parentField` and no
|
|
47
|
+
* `${RECORD:field}` placeholders in its `path` and `ifMatchPath`.
|
|
48
|
+
*
|
|
49
|
+
* @param name - The name of the collection.
|
|
50
|
+
* @param spec - The manifest entry of the collection.
|
|
51
|
+
* @throws {Error} The entry cannot be seeded. The message names the collection.
|
|
52
|
+
*/
|
|
53
|
+
export declare function checkSpec(name: string, spec: CollectionSpec): void;
|
|
54
|
+
/**
|
|
55
|
+
* Labels the records of a collection in file order: by `id`, by the `key` of the collection when the record has every
|
|
56
|
+
* key field, or by position.
|
|
57
|
+
*
|
|
58
|
+
* @param spec - The manifest entry of the collection.
|
|
59
|
+
* @param records - The records, in file order.
|
|
60
|
+
* @returns The labeled records, in the same order.
|
|
61
|
+
*/
|
|
62
|
+
export declare function labeled(spec: CollectionSpec, records: readonly object[]): LabeledRecord[];
|
|
63
|
+
/**
|
|
64
|
+
* Returns a seed request.
|
|
65
|
+
*
|
|
66
|
+
* @param spec - The manifest entry of the collection.
|
|
67
|
+
* @param resolve - The path resolver of the run.
|
|
68
|
+
* @param body - The body: one record, or the records of a collection sent in one request.
|
|
69
|
+
* @param record - The record whose fields fill the `${RECORD:field}` placeholders, or `undefined` for a collection sent
|
|
70
|
+
* in one request.
|
|
71
|
+
* @returns The request.
|
|
72
|
+
* @throws {Error} A placeholder has no value.
|
|
73
|
+
*/
|
|
74
|
+
export declare function requestOf(spec: CollectionSpec, resolve: PathResolver, body: unknown, record?: object): SeedRequest;
|
|
75
|
+
/**
|
|
76
|
+
* Returns the search for the stored copy of a record whose identifier the platform assigns.
|
|
77
|
+
*
|
|
78
|
+
* @param spec - The manifest entry of the collection.
|
|
79
|
+
* @param record - The record.
|
|
80
|
+
* @param values - The values of the `${ENV:key}` placeholders.
|
|
81
|
+
* @returns The search with its placeholders filled in, or `undefined` when the collection has no `storedIdFrom`.
|
|
82
|
+
* @throws {Error} A placeholder has no value.
|
|
83
|
+
*/
|
|
84
|
+
export declare function storedIdSearchOf(spec: CollectionSpec, record: object, values: PlaceholderValues): StoredIdSearch | undefined;
|
|
85
|
+
/**
|
|
86
|
+
* Returns a field of a record.
|
|
87
|
+
*
|
|
88
|
+
* @param record - The record.
|
|
89
|
+
* @param field - The name of the field.
|
|
90
|
+
* @returns The value of the field, or `undefined` when the record has no such field.
|
|
91
|
+
*/
|
|
92
|
+
export declare function fieldOf(record: object, field: string): unknown;
|
|
93
|
+
/**
|
|
94
|
+
* Orders records so that each parent comes before its children. Records keep their order otherwise, and a parent that
|
|
95
|
+
* is not among the records does not hold back its children.
|
|
96
|
+
*
|
|
97
|
+
* @param records - The labeled records, in file order.
|
|
98
|
+
* @param parentField - The field that holds the `id` of the parent.
|
|
99
|
+
* @returns The records in seed order.
|
|
100
|
+
* @throws {Error} Records reference each other as parents. The message lists the identifiers of the cycle.
|
|
101
|
+
*/
|
|
102
|
+
export declare function parentsFirst(records: readonly LabeledRecord[], parentField: string): LabeledRecord[];
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { envSource, fillPlaceholders, hasPlaceholder, jsonStringContent } from "./placeholders.js";
|
|
2
|
+
/**
|
|
3
|
+
* Returns the {@link PathResolver} of a seed run.
|
|
4
|
+
*
|
|
5
|
+
* @param values - The values of the `${ENV:key}` placeholders.
|
|
6
|
+
* @returns The resolver.
|
|
7
|
+
*/
|
|
8
|
+
export function pathResolver(values) {
|
|
9
|
+
return function resolve(template, record) {
|
|
10
|
+
return fillPlaceholders(template, sourcesOf(values, record), encodeURIComponent);
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Ensures that a manifest entry can be seeded: a collection sent in one request has no `parentField` and no
|
|
15
|
+
* `${RECORD:field}` placeholders in its `path` and `ifMatchPath`.
|
|
16
|
+
*
|
|
17
|
+
* @param name - The name of the collection.
|
|
18
|
+
* @param spec - The manifest entry of the collection.
|
|
19
|
+
* @throws {Error} The entry cannot be seeded. The message names the collection.
|
|
20
|
+
*/
|
|
21
|
+
export function checkSpec(name, spec) {
|
|
22
|
+
if (spec.send !== "all") {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (spec.parentField !== undefined) {
|
|
26
|
+
throw new Error(`The collection ${name} is sent in one request, so it cannot have a parentField`);
|
|
27
|
+
}
|
|
28
|
+
for (const path of [spec.path, spec.ifMatchPath]) {
|
|
29
|
+
if (path !== undefined && hasPlaceholder(path, "RECORD")) {
|
|
30
|
+
throw new Error(`The collection ${name} is sent in one request, so ${path} cannot have record placeholders`);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Labels the records of a collection in file order: by `id`, by the `key` of the collection when the record has every
|
|
36
|
+
* key field, or by position.
|
|
37
|
+
*
|
|
38
|
+
* @param spec - The manifest entry of the collection.
|
|
39
|
+
* @param records - The records, in file order.
|
|
40
|
+
* @returns The labeled records, in the same order.
|
|
41
|
+
*/
|
|
42
|
+
export function labeled(spec, records) {
|
|
43
|
+
return records.map((record, index) => ({ record, label: labelOf(spec, record, index) }));
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Returns a seed request.
|
|
47
|
+
*
|
|
48
|
+
* @param spec - The manifest entry of the collection.
|
|
49
|
+
* @param resolve - The path resolver of the run.
|
|
50
|
+
* @param body - The body: one record, or the records of a collection sent in one request.
|
|
51
|
+
* @param record - The record whose fields fill the `${RECORD:field}` placeholders, or `undefined` for a collection sent
|
|
52
|
+
* in one request.
|
|
53
|
+
* @returns The request.
|
|
54
|
+
* @throws {Error} A placeholder has no value.
|
|
55
|
+
*/
|
|
56
|
+
export function requestOf(spec, resolve, body, record) {
|
|
57
|
+
return {
|
|
58
|
+
path: resolve(spec.path, record),
|
|
59
|
+
ifMatchPath: spec.ifMatchPath === undefined ? undefined : resolve(spec.ifMatchPath, record),
|
|
60
|
+
body,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Returns the search for the stored copy of a record whose identifier the platform assigns.
|
|
65
|
+
*
|
|
66
|
+
* @param spec - The manifest entry of the collection.
|
|
67
|
+
* @param record - The record.
|
|
68
|
+
* @param values - The values of the `${ENV:key}` placeholders.
|
|
69
|
+
* @returns The search with its placeholders filled in, or `undefined` when the collection has no `storedIdFrom`.
|
|
70
|
+
* @throws {Error} A placeholder has no value.
|
|
71
|
+
*/
|
|
72
|
+
export function storedIdSearchOf(spec, record, values) {
|
|
73
|
+
const lookup = spec.storedIdFrom;
|
|
74
|
+
if (lookup === undefined) {
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
const sources = sourcesOf(values, record);
|
|
78
|
+
return {
|
|
79
|
+
path: fillPlaceholders(lookup.searchPath, sources, encodeURIComponent),
|
|
80
|
+
criteria: JSON.parse(fillPlaceholders(JSON.stringify(lookup.criteria), sources, jsonStringContent)),
|
|
81
|
+
match: lookup.match,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Returns a field of a record.
|
|
86
|
+
*
|
|
87
|
+
* @param record - The record.
|
|
88
|
+
* @param field - The name of the field.
|
|
89
|
+
* @returns The value of the field, or `undefined` when the record has no such field.
|
|
90
|
+
*/
|
|
91
|
+
export function fieldOf(record, field) {
|
|
92
|
+
return Reflect.get(record, field);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Orders records so that each parent comes before its children. Records keep their order otherwise, and a parent that
|
|
96
|
+
* is not among the records does not hold back its children.
|
|
97
|
+
*
|
|
98
|
+
* @param records - The labeled records, in file order.
|
|
99
|
+
* @param parentField - The field that holds the `id` of the parent.
|
|
100
|
+
* @returns The records in seed order.
|
|
101
|
+
* @throws {Error} Records reference each other as parents. The message lists the identifiers of the cycle.
|
|
102
|
+
*/
|
|
103
|
+
export function parentsFirst(records, parentField) {
|
|
104
|
+
const byId = new Map();
|
|
105
|
+
for (const entry of records) {
|
|
106
|
+
const id = fieldOf(entry.record, "id");
|
|
107
|
+
if (typeof id === "string") {
|
|
108
|
+
byId.set(id, entry);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
const sorted = [];
|
|
112
|
+
const visiting = new Set();
|
|
113
|
+
const done = new Set();
|
|
114
|
+
function visit(entry, chain) {
|
|
115
|
+
if (done.has(entry)) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
const id = String(fieldOf(entry.record, "id"));
|
|
119
|
+
if (visiting.has(entry)) {
|
|
120
|
+
throw new Error(`The records reference each other as parents: ${[...chain, id].join(" → ")}`);
|
|
121
|
+
}
|
|
122
|
+
visiting.add(entry);
|
|
123
|
+
const parentId = fieldOf(entry.record, parentField);
|
|
124
|
+
const parent = typeof parentId === "string" ? byId.get(parentId) : undefined;
|
|
125
|
+
if (parent !== undefined) {
|
|
126
|
+
visit(parent, [...chain, id]);
|
|
127
|
+
}
|
|
128
|
+
visiting.delete(entry);
|
|
129
|
+
done.add(entry);
|
|
130
|
+
sorted.push(entry);
|
|
131
|
+
}
|
|
132
|
+
for (const entry of records) {
|
|
133
|
+
visit(entry, []);
|
|
134
|
+
}
|
|
135
|
+
return sorted;
|
|
136
|
+
}
|
|
137
|
+
function labelOf({ key }, record, index) {
|
|
138
|
+
const id = fieldOf(record, "id");
|
|
139
|
+
if (typeof id === "string") {
|
|
140
|
+
return `record "${id}"`;
|
|
141
|
+
}
|
|
142
|
+
const values = (typeof key === "string" ? [key] : (key ?? [])).map((field) => fieldOf(record, field));
|
|
143
|
+
if (values.length > 0 && values.every((value) => value !== undefined && value !== null)) {
|
|
144
|
+
return `record "${values.map(String).join(" + ")}"`;
|
|
145
|
+
}
|
|
146
|
+
return `record #${index + 1}`;
|
|
147
|
+
}
|
|
148
|
+
function sourcesOf(values, record) {
|
|
149
|
+
const env = envSource(values);
|
|
150
|
+
if (record === undefined) {
|
|
151
|
+
return { ENV: env };
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
ENV: env,
|
|
155
|
+
RECORD: function recordField(field) {
|
|
156
|
+
return fieldOf(record, field);
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
//# sourceMappingURL=seed-requests.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"seed-requests.js","sourceRoot":"","sources":["../../src/dataset/seed-requests.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAyCnG;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,MAAyB;IACpD,OAAO,SAAS,OAAO,CAAC,QAAgB,EAAE,MAAe;QACvD,OAAO,g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,EAAE,MAAc,EAAE,KAAa;IACrE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IACjC,IAAI,OAAO,EAAE,KAAK,QAAQ,EAAE,CAAC;QAC3B,OAAO,WAAW,EAAE,GAAG,CAAC;IAC1B,CAAC;IACD,MAAM,MAAM,GAAG,CAAC,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,CAAC;IACtG,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,CAAC,EAAE,CAAC;QACxF,OAAO,WAAW,MAAM,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC;IACtD,CAAC;IACD,OAAO,WAAW,KAAK,GAAG,CAAC,EAAE,CAAC;AAChC,CAAC;AAED,SAAS,SAAS,CAAC,MAAyB,EAAE,MAA0B;IACtE,MAAM,GAAG,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC;IAC9B,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACzB,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,CAAC;IACtB,CAAC;IACD,OAAO;QACL,GAAG,EAAE,GAAG;QACR,MAAM,EAAE,SAAS,WAAW,CAAC,KAAa;YACxC,OAAO,OAAO,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;QAChC,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["import type { CollectionSpec } from \"./manifest.ts\";\nimport type { PlaceholderSources, PlaceholderValues } from \"./placeholders.ts\";\n\nimport { envSource, fillPlaceholders, hasPlaceholder, jsonStringContent } from \"./placeholders.ts\";\n\n/** A dataset record with the label that names it in logs and errors. */\nexport interface LabeledRecord {\n /** The record. */\n readonly record: object;\n /** The label, such as `record \"catalog-b2b\"` or `record #3`. */\n readonly label: string;\n}\n\n/** A seed request with its placeholders filled in. */\nexport interface SeedRequest {\n /** The path of the request. */\n readonly path: string;\n /** The path whose `ETag` is sent as `If-Match`, or `undefined` when none is sent. */\n readonly ifMatchPath: string | undefined;\n /** The body: a record, or the records of a collection sent in one request. */\n readonly body: unknown;\n}\n\n/** A search for the stored copy of a record, with its placeholders filled in. */\nexport interface StoredIdSearch {\n /** The path of the search. */\n readonly path: string;\n /** The criteria of the search. */\n readonly criteria: unknown;\n /** The field that a stored record shares with the dataset record. */\n readonly match: string;\n}\n\n/**\n * Fills the placeholders of a path, URL-encoded: `${ENV:key}` placeholders from the placeholder values of the run and,\n * when a record is given, `${RECORD:field}` placeholders from the record.\n *\n * @param template - The path with placeholders.\n * @param record - The record, or `undefined` for a path without record placeholders.\n * @returns The path with every placeholder filled in.\n * @throws {Error} A placeholder has no value.\n */\nexport type PathResolver = (template: string, record?: object) => string;\n\n/**\n * Returns the {@link PathResolver} of a seed run.\n *\n * @param values - The values of the `${ENV:key}` placeholders.\n * @returns The resolver.\n */\nexport function pathResolver(values: PlaceholderValues): PathResolver {\n return function resolve(template: string, record?: object): string {\n return fillPlaceholders(template, sourcesOf(values, record), encodeURIComponent);\n };\n}\n\n/**\n * Ensures that a manifest entry can be seeded: a collection sent in one request has no `parentField` and no\n * `${RECORD:field}` placeholders in its `path` and `ifMatchPath`.\n *\n * @param name - The name of the collection.\n * @param spec - The manifest entry of the collection.\n * @throws {Error} The entry cannot be seeded. The message names the collection.\n */\nexport function checkSpec(name: string, spec: CollectionSpec): void {\n if (spec.send !== \"all\") {\n return;\n }\n if (spec.parentField !== undefined) {\n throw new Error(`The collection ${name} is sent in one request, so it cannot have a parentField`);\n }\n for (const path of [spec.path, spec.ifMatchPath]) {\n if (path !== undefined && hasPlaceholder(path, \"RECORD\")) {\n throw new Error(`The collection ${name} is sent in one request, so ${path} cannot have record placeholders`);\n }\n }\n}\n\n/**\n * Labels the records of a collection in file order: by `id`, by the `key` of the collection when the record has every\n * key field, or by position.\n *\n * @param spec - The manifest entry of the collection.\n * @param records - The records, in file order.\n * @returns The labeled records, in the same order.\n */\nexport function labeled(spec: CollectionSpec, records: readonly object[]): LabeledRecord[] {\n return records.map((record, index) => ({ record, label: labelOf(spec, record, index) }));\n}\n\n/**\n * Returns a seed request.\n *\n * @param spec - The manifest entry of the collection.\n * @param resolve - The path resolver of the run.\n * @param body - The body: one record, or the records of a collection sent in one request.\n * @param record - The record whose fields fill the `${RECORD:field}` placeholders, or `undefined` for a collection sent\n * in one request.\n * @returns The request.\n * @throws {Error} A placeholder has no value.\n */\nexport function requestOf(spec: CollectionSpec, resolve: PathResolver, body: unknown, record?: object): SeedRequest {\n return {\n path: resolve(spec.path, record),\n ifMatchPath: spec.ifMatchPath === undefined ? undefined : resolve(spec.ifMatchPath, record),\n body,\n };\n}\n\n/**\n * Returns the search for the stored copy of a record whose identifier the platform assigns.\n *\n * @param spec - The manifest entry of the collection.\n * @param record - The record.\n * @param values - The values of the `${ENV:key}` placeholders.\n * @returns The search with its placeholders filled in, or `undefined` when the collection has no `storedIdFrom`.\n * @throws {Error} A placeholder has no value.\n */\nexport function storedIdSearchOf(\n spec: CollectionSpec,\n record: object,\n values: PlaceholderValues,\n): StoredIdSearch | undefined {\n const lookup = spec.storedIdFrom;\n if (lookup === undefined) {\n return undefined;\n }\n const sources = sourcesOf(values, record);\n return {\n path: fillPlaceholders(lookup.searchPath, sources, encodeURIComponent),\n criteria: JSON.parse(fillPlaceholders(JSON.stringify(lookup.criteria), sources, jsonStringContent)),\n match: lookup.match,\n };\n}\n\n/**\n * Returns a field of a record.\n *\n * @param record - The record.\n * @param field - The name of the field.\n * @returns The value of the field, or `undefined` when the record has no such field.\n */\nexport function fieldOf(record: object, field: string): unknown {\n return Reflect.get(record, field);\n}\n\n/**\n * Orders records so that each parent comes before its children. Records keep their order otherwise, and a parent that\n * is not among the records does not hold back its children.\n *\n * @param records - The labeled records, in file order.\n * @param parentField - The field that holds the `id` of the parent.\n * @returns The records in seed order.\n * @throws {Error} Records reference each other as parents. The message lists the identifiers of the cycle.\n */\nexport function parentsFirst(records: readonly LabeledRecord[], parentField: string): LabeledRecord[] {\n const byId = new Map<string, LabeledRecord>();\n for (const entry of records) {\n const id = fieldOf(entry.record, \"id\");\n if (typeof id === \"string\") {\n byId.set(id, entry);\n }\n }\n const sorted: LabeledRecord[] = [];\n const visiting = new Set<LabeledRecord>();\n const done = new Set<LabeledRecord>();\n function visit(entry: LabeledRecord, chain: readonly string[]): void {\n if (done.has(entry)) {\n return;\n }\n const id = String(fieldOf(entry.record, \"id\"));\n if (visiting.has(entry)) {\n throw new Error(`The records reference each other as parents: ${[...chain, id].join(\" → \")}`);\n }\n visiting.add(entry);\n const parentId = fieldOf(entry.record, parentField);\n const parent = typeof parentId === \"string\" ? byId.get(parentId) : undefined;\n if (parent !== undefined) {\n visit(parent, [...chain, id]);\n }\n visiting.delete(entry);\n done.add(entry);\n sorted.push(entry);\n }\n for (const entry of records) {\n visit(entry, []);\n }\n return sorted;\n}\n\nfunction labelOf({ key }: CollectionSpec, record: object, index: number): string {\n const id = fieldOf(record, \"id\");\n if (typeof id === \"string\") {\n return `record \"${id}\"`;\n }\n const values = (typeof key === \"string\" ? [key] : (key ?? [])).map((field) => fieldOf(record, field));\n if (values.length > 0 && values.every((value) => value !== undefined && value !== null)) {\n return `record \"${values.map(String).join(\" + \")}\"`;\n }\n return `record #${index + 1}`;\n}\n\nfunction sourcesOf(values: PlaceholderValues, record: object | undefined): PlaceholderSources {\n const env = envSource(values);\n if (record === undefined) {\n return { ENV: env };\n }\n return {\n ENV: env,\n RECORD: function recordField(field: string): unknown {\n return fieldOf(record, field);\n },\n };\n}\n"]}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { InstalledModules } from "../modules/index.js";
|
|
2
|
+
import type { CollectionName, CollectionSpec, Manifest } from "./manifest.ts";
|
|
3
|
+
/** A collection that a seed run leaves out, and the reason. */
|
|
4
|
+
export interface SkippedCollection<TManifest extends Manifest> {
|
|
5
|
+
/** The name of the collection. */
|
|
6
|
+
readonly name: CollectionName<TManifest>;
|
|
7
|
+
/** Why the collection is left out, such as `VirtoCommerce.XPickup is not installed` or `no records`. */
|
|
8
|
+
readonly reason: string;
|
|
9
|
+
}
|
|
10
|
+
/** The collections that a seed run covers. */
|
|
11
|
+
export interface SeedScope<TManifest extends Manifest> {
|
|
12
|
+
/** The collections to seed, each with its manifest entry, in seed order. */
|
|
13
|
+
readonly collections: readonly (readonly [CollectionName<TManifest>, CollectionSpec])[];
|
|
14
|
+
/** The selected collections whose module does not qualify or that have no records, in seed order. */
|
|
15
|
+
readonly skipped: readonly SkippedCollection<TManifest>[];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Decides which collections a seed run covers: every collection, or the selected ones, except those whose module is
|
|
19
|
+
* not installed or not in its range of versions, and those without records.
|
|
20
|
+
*
|
|
21
|
+
* @param manifest - The collections, in seed order.
|
|
22
|
+
* @param installed - The modules installed on the platform.
|
|
23
|
+
* @param recordCount - Returns the number of records of a collection.
|
|
24
|
+
* @param only - The selected collections, or `undefined` for every collection. Their order does not matter.
|
|
25
|
+
* @returns The scope, in the seed order of the manifest.
|
|
26
|
+
* @throws {Error} The installed version of a module cannot be compared with its range. The message starts with the
|
|
27
|
+
* collection, and the cause is the original error.
|
|
28
|
+
*/
|
|
29
|
+
export declare function resolveSeedScope<TManifest extends Manifest>(manifest: TManifest, installed: InstalledModules, recordCount: (name: CollectionName<TManifest>) => number, only?: readonly CollectionName<TManifest>[] | undefined): SeedScope<TManifest>;
|
|
30
|
+
/**
|
|
31
|
+
* Parses a comma-separated list of collection names, such as `products, prices`, for example from an environment
|
|
32
|
+
* variable.
|
|
33
|
+
*
|
|
34
|
+
* @param manifest - The known collections.
|
|
35
|
+
* @param value - The list, or `undefined` for every collection.
|
|
36
|
+
* @returns The listed collections, or `undefined` for every collection.
|
|
37
|
+
* @throws {Error} The list is empty or names an unknown collection. The message lists the known collections.
|
|
38
|
+
*/
|
|
39
|
+
export declare function parseCollectionNames<TManifest extends Manifest>(manifest: TManifest, value: string | undefined): readonly CollectionName<TManifest>[] | undefined;
|