@marinoscar/platform-db 0.0.0-stage → 0.1.0-next.4
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/CHANGELOG.md +74 -0
- package/LICENSE +21 -0
- package/README.md +525 -2
- package/bin/platform.js +28 -0
- package/dist/baseline/deps.d.ts +32 -0
- package/dist/baseline/deps.d.ts.map +1 -0
- package/dist/baseline/deps.js +66 -0
- package/dist/baseline/deps.js.map +1 -0
- package/dist/baseline/errors.d.ts +36 -0
- package/dist/baseline/errors.d.ts.map +1 -0
- package/dist/baseline/errors.js +23 -0
- package/dist/baseline/errors.js.map +1 -0
- package/dist/baseline/fs.d.ts +9 -0
- package/dist/baseline/fs.d.ts.map +1 -0
- package/dist/baseline/fs.js +39 -0
- package/dist/baseline/fs.js.map +1 -0
- package/dist/baseline/index.d.ts +8 -0
- package/dist/baseline/index.d.ts.map +1 -0
- package/dist/baseline/index.js +24 -0
- package/dist/baseline/index.js.map +1 -0
- package/dist/baseline/mapping.d.ts +43 -0
- package/dist/baseline/mapping.d.ts.map +1 -0
- package/dist/baseline/mapping.js +80 -0
- package/dist/baseline/mapping.js.map +1 -0
- package/dist/baseline/normalise.d.ts +42 -0
- package/dist/baseline/normalise.d.ts.map +1 -0
- package/dist/baseline/normalise.js +133 -0
- package/dist/baseline/normalise.js.map +1 -0
- package/dist/baseline/plan.d.ts +66 -0
- package/dist/baseline/plan.d.ts.map +1 -0
- package/dist/baseline/plan.js +105 -0
- package/dist/baseline/plan.js.map +1 -0
- package/dist/baseline/report.d.ts +127 -0
- package/dist/baseline/report.d.ts.map +1 -0
- package/dist/baseline/report.js +89 -0
- package/dist/baseline/report.js.map +1 -0
- package/dist/baseline/run.d.ts +117 -0
- package/dist/baseline/run.d.ts.map +1 -0
- package/dist/baseline/run.js +358 -0
- package/dist/baseline/run.js.map +1 -0
- package/dist/bin/db-commands.d.ts +31 -0
- package/dist/bin/db-commands.d.ts.map +1 -0
- package/dist/bin/db-commands.js +311 -0
- package/dist/bin/db-commands.js.map +1 -0
- package/dist/bin/fs-io.d.ts +7 -0
- package/dist/bin/fs-io.d.ts.map +1 -0
- package/dist/bin/fs-io.js +45 -0
- package/dist/bin/fs-io.js.map +1 -0
- package/dist/bin/platform.d.ts +21 -0
- package/dist/bin/platform.d.ts.map +1 -0
- package/dist/bin/platform.js +99 -0
- package/dist/bin/platform.js.map +1 -0
- package/dist/compose/api.d.ts +84 -0
- package/dist/compose/api.d.ts.map +1 -0
- package/dist/compose/api.js +101 -0
- package/dist/compose/api.js.map +1 -0
- package/dist/compose/compose.d.ts +51 -0
- package/dist/compose/compose.d.ts.map +1 -0
- package/dist/compose/compose.js +177 -0
- package/dist/compose/compose.js.map +1 -0
- package/dist/compose/diff.d.ts +7 -0
- package/dist/compose/diff.d.ts.map +1 -0
- package/dist/compose/diff.js +68 -0
- package/dist/compose/diff.js.map +1 -0
- package/dist/compose/errors.d.ts +31 -0
- package/dist/compose/errors.d.ts.map +1 -0
- package/dist/compose/errors.js +35 -0
- package/dist/compose/errors.js.map +1 -0
- package/dist/compose/index.d.ts +3 -0
- package/dist/compose/index.d.ts.map +1 -0
- package/dist/compose/index.js +11 -0
- package/dist/compose/index.js.map +1 -0
- package/dist/compose/parse.d.ts +65 -0
- package/dist/compose/parse.d.ts.map +1 -0
- package/dist/compose/parse.js +122 -0
- package/dist/compose/parse.js.map +1 -0
- package/dist/drift/app-objects.d.ts +107 -0
- package/dist/drift/app-objects.d.ts.map +1 -0
- package/dist/drift/app-objects.js +193 -0
- package/dist/drift/app-objects.js.map +1 -0
- package/dist/drift/db-conformance.d.ts +52 -0
- package/dist/drift/db-conformance.d.ts.map +1 -0
- package/dist/drift/db-conformance.js +89 -0
- package/dist/drift/db-conformance.js.map +1 -0
- package/dist/drift/index.d.ts +8 -0
- package/dist/drift/index.d.ts.map +1 -0
- package/dist/drift/index.js +43 -0
- package/dist/drift/index.js.map +1 -0
- package/dist/drift/raw-sql-indexes.d.ts +106 -0
- package/dist/drift/raw-sql-indexes.d.ts.map +1 -0
- package/dist/drift/raw-sql-indexes.js +113 -0
- package/dist/drift/raw-sql-indexes.js.map +1 -0
- package/dist/drift/raw-sql-tripwire.d.ts +191 -0
- package/dist/drift/raw-sql-tripwire.d.ts.map +1 -0
- package/dist/drift/raw-sql-tripwire.js +422 -0
- package/dist/drift/raw-sql-tripwire.js.map +1 -0
- package/dist/drift/rls-policies.d.ts +226 -0
- package/dist/drift/rls-policies.d.ts.map +1 -0
- package/dist/drift/rls-policies.js +317 -0
- package/dist/drift/rls-policies.js.map +1 -0
- package/dist/drift/run.d.ts +152 -0
- package/dist/drift/run.d.ts.map +1 -0
- package/dist/drift/run.js +252 -0
- package/dist/drift/run.js.map +1 -0
- package/dist/drift/sql-expression.d.ts +27 -0
- package/dist/drift/sql-expression.d.ts.map +1 -0
- package/dist/drift/sql-expression.js +64 -0
- package/dist/drift/sql-expression.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -0
- package/dist/lock/hash.d.ts +13 -0
- package/dist/lock/hash.d.ts.map +1 -0
- package/dist/lock/hash.js +19 -0
- package/dist/lock/hash.js.map +1 -0
- package/dist/lock/index.d.ts +5 -0
- package/dist/lock/index.d.ts.map +1 -0
- package/dist/lock/index.js +20 -0
- package/dist/lock/index.js.map +1 -0
- package/dist/lock/lock.d.ts +154 -0
- package/dist/lock/lock.d.ts.map +1 -0
- package/dist/lock/lock.js +151 -0
- package/dist/lock/lock.js.map +1 -0
- package/dist/lock/manifest.d.ts +68 -0
- package/dist/lock/manifest.d.ts.map +1 -0
- package/dist/lock/manifest.js +96 -0
- package/dist/lock/manifest.js.map +1 -0
- package/dist/lock/semver.d.ts +10 -0
- package/dist/lock/semver.d.ts.map +1 -0
- package/dist/lock/semver.js +13 -0
- package/dist/lock/semver.js.map +1 -0
- package/dist/seed/index.d.ts +5 -0
- package/dist/seed/index.d.ts.map +1 -0
- package/dist/seed/index.js +16 -0
- package/dist/seed/index.js.map +1 -0
- package/dist/seed/input.d.ts +52 -0
- package/dist/seed/input.d.ts.map +1 -0
- package/dist/seed/input.js +77 -0
- package/dist/seed/input.js.map +1 -0
- package/dist/seed/seed-platform.d.ts +41 -0
- package/dist/seed/seed-platform.d.ts.map +1 -0
- package/dist/seed/seed-platform.js +165 -0
- package/dist/seed/seed-platform.js.map +1 -0
- package/dist/seed/steps.d.ts +172 -0
- package/dist/seed/steps.d.ts.map +1 -0
- package/dist/seed/steps.js +136 -0
- package/dist/seed/steps.js.map +1 -0
- package/dist/seed/types.d.ts +287 -0
- package/dist/seed/types.d.ts.map +1 -0
- package/dist/seed/types.js +5 -0
- package/dist/seed/types.js.map +1 -0
- package/dist/sync/app-migrations.d.ts +52 -0
- package/dist/sync/app-migrations.d.ts.map +1 -0
- package/dist/sync/app-migrations.js +62 -0
- package/dist/sync/app-migrations.js.map +1 -0
- package/dist/sync/apply.d.ts +29 -0
- package/dist/sync/apply.d.ts.map +1 -0
- package/dist/sync/apply.js +38 -0
- package/dist/sync/apply.js.map +1 -0
- package/dist/sync/check.d.ts +59 -0
- package/dist/sync/check.d.ts.map +1 -0
- package/dist/sync/check.js +115 -0
- package/dist/sync/check.js.map +1 -0
- package/dist/sync/errors.d.ts +32 -0
- package/dist/sync/errors.d.ts.map +1 -0
- package/dist/sync/errors.js +23 -0
- package/dist/sync/errors.js.map +1 -0
- package/dist/sync/index.d.ts +10 -0
- package/dist/sync/index.d.ts.map +1 -0
- package/dist/sync/index.js +27 -0
- package/dist/sync/index.js.map +1 -0
- package/dist/sync/ledger.d.ts +65 -0
- package/dist/sync/ledger.d.ts.map +1 -0
- package/dist/sync/ledger.js +49 -0
- package/dist/sync/ledger.js.map +1 -0
- package/dist/sync/plan.d.ts +63 -0
- package/dist/sync/plan.d.ts.map +1 -0
- package/dist/sync/plan.js +86 -0
- package/dist/sync/plan.js.map +1 -0
- package/dist/sync/promote.d.ts +62 -0
- package/dist/sync/promote.d.ts.map +1 -0
- package/dist/sync/promote.js +95 -0
- package/dist/sync/promote.js.map +1 -0
- package/dist/sync/timestamps.d.ts +41 -0
- package/dist/sync/timestamps.d.ts.map +1 -0
- package/dist/sync/timestamps.js +68 -0
- package/dist/sync/timestamps.js.map +1 -0
- package/dist/sync/version.d.ts +28 -0
- package/dist/sync/version.d.ts.map +1 -0
- package/dist/sync/version.js +66 -0
- package/dist/sync/version.js.map +1 -0
- package/migrations/0001_initial/migration.sql +297 -0
- package/migrations/0002_add_personal_access_tokens/migration.sql +31 -0
- package/migrations/0003_add_credentials/migration.sql +20 -0
- package/migrations/0004_add_notification_deliveries/migration.sql +27 -0
- package/migrations/0005_drop_stale_uuid_defaults/migration.sql +35 -0
- package/migrations/0006_add_notifications/migration.sql +22 -0
- package/migrations/0007_add_push_subscriptions/migration.sql +25 -0
- package/migrations/0008_add_jobs/migration.sql +110 -0
- package/migrations/0009_add_worker_nodes/migration.sql +90 -0
- package/migrations/0010_add_database_backup_runs/migration.sql +104 -0
- package/migrations/0011_add_notification_broadcasts/migration.sql +57 -0
- package/migrations/0012_add_backup_run_job_link/migration.sql +87 -0
- package/migrations/0013_add_job_node_secrets/migration.sql +61 -0
- package/migrations/0014_add_backup_run_pg_dump_version/migration.sql +26 -0
- package/migrations/0015_add_job_claim_token/migration.sql +92 -0
- package/migrations/0016_add_ai_platform/migration.sql +108 -0
- package/migrations/0017_revoke_viewer_ai_use/migration.sql +32 -0
- package/migrations/0018_add_user_credentials/migration.sql +29 -0
- package/migrations/0019_add_device_session_credential_link/migration.sql +52 -0
- package/migrations/0020_add_worker_node_vitals/migration.sql +3 -0
- package/migrations/0021_add_job_trace_context/migration.sql +6 -0
- package/migrations/0022_add_retention_created_at_indexes/migration.sql +13 -0
- package/migrations/0023_add_organizations/migration.sql +142 -0
- package/migrations/0024_split_system_org_roles/migration.sql +131 -0
- package/migrations/0025_org_scoped_rls/migration.sql +215 -0
- package/migrations/0026_add_groups/migration.sql +165 -0
- package/migrations/0027_add_grants/migration.sql +117 -0
- package/migrations/0028_add_org_settings/migration.sql +49 -0
- package/migrations/0029_add_org_credentials/migration.sql +54 -0
- package/migrations/0030_add_jobs_org_id/migration.sql +18 -0
- package/migrations/0031_add_jobs_org_id_status_index/migration.sql +6 -0
- package/migrations/0032_add_broadcast_target_org/migration.sql +21 -0
- package/migrations/0033_add_android_app/migration.sql +60 -0
- package/migrations/0034_open_job_reason/migration.sql +16 -0
- package/migrations/0035_ai_usage_feature/migration.sql +37 -0
- package/migrations/0036_add_app_metadata/migration.sql +49 -0
- package/migrations/0037_open_sharing_vocabularies/migration.sql +97 -0
- package/migrations/manifest.json +379 -0
- package/package.json +78 -4
- package/raw-sql-indexes.json +94 -0
- package/rls-policies.json +74 -0
- package/schema/ai.prisma +444 -0
- package/schema/android-app.prisma +47 -0
- package/schema/base.prisma +11 -0
- package/schema/credentials.prisma +170 -0
- package/schema/db-backup.prisma +376 -0
- package/schema/identity.prisma +439 -0
- package/schema/jobs.prisma +760 -0
- package/schema/notifications.prisma +649 -0
- package/schema/settings.prisma +60 -0
- package/schema/sharing.prisma +223 -0
- package/schema/storage.prisma +84 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// seedPlatform (issue #712): the platform's idempotent seed, extracted from
|
|
3
|
+
// the reference app's prisma/seed.ts with the same writes, in the same order,
|
|
4
|
+
// under the same console lines.
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.seedPlatform = seedPlatform;
|
|
7
|
+
const steps_js_1 = require("./steps.js");
|
|
8
|
+
/** The `global` row's key: the one `system_settings` row the platform owns. */
|
|
9
|
+
const GLOBAL_SETTINGS_KEY = 'global';
|
|
10
|
+
/** The default organization when the input names none. Mirrors the migration's backfill row. */
|
|
11
|
+
const DEFAULT_ORGANIZATION = { name: 'Default organization', slug: 'default' };
|
|
12
|
+
/** The note on the initial administrator's allowlist row. */
|
|
13
|
+
const INITIAL_ADMIN_NOTE = 'Initial admin (auto-seeded)';
|
|
14
|
+
const SILENT = { info: () => undefined };
|
|
15
|
+
/** `{ scope }` when the entry declares one, `{}` otherwise (a catalog from before scopes). */
|
|
16
|
+
function scopeOf(entry) {
|
|
17
|
+
return entry.scope ? { scope: entry.scope } : {};
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Seed the platform's roles and permissions (each with its `system` or `org`
|
|
21
|
+
* scope, refreshed on every run like the description), default grants, the `global` system
|
|
22
|
+
* settings row, the initial administrator's allowlist entry and the default
|
|
23
|
+
* organization.
|
|
24
|
+
*
|
|
25
|
+
* Idempotent: every write is an upsert keyed on a natural unique (`name`,
|
|
26
|
+
* `roleId_permissionId`, `key`, `email`), so a second run updates the same
|
|
27
|
+
* rows. It **never deletes**: a permission that left the registry stays as a
|
|
28
|
+
* row until an explicit migration removes it (expand/contract). It never
|
|
29
|
+
* overwrites an admin-edited value either: the settings row and the allowlist
|
|
30
|
+
* row are created with `update: {}`. Writes are sequential, in a fixed order
|
|
31
|
+
* (roles, permissions, grants, settings, allowlist, default organization), so
|
|
32
|
+
* the log reads the same on every run. The default organization is created only
|
|
33
|
+
* when no organization is flagged default (found by `isDefault`, not by slug),
|
|
34
|
+
* so a renamed default is left alone and the one-default index is never hit.
|
|
35
|
+
*
|
|
36
|
+
* App steps (`extras`, see {@link SeedStep}) run after the default organization,
|
|
37
|
+
* in array order, each in its own transaction when the client has
|
|
38
|
+
* `$transaction`, under the same logger: `Seeding <id>...` then
|
|
39
|
+
* `✓ <id>: <created> created, <updated> updated`. They are validated before
|
|
40
|
+
* anything is written, and each must be idempotent ({@link assertSeedIdempotent}
|
|
41
|
+
* proves it).
|
|
42
|
+
*
|
|
43
|
+
* @param prisma - The app's Prisma client, or any object with the delegates of {@link SeedPrisma}.
|
|
44
|
+
* @param input - What to write; see {@link PlatformSeedInput}.
|
|
45
|
+
* @param options - Receives one line per step; default: silent. A bare {@link SeedLogger} (the previous third argument) is still accepted. Or {@link SeedPlatformOptions}: `{ log, extras }`.
|
|
46
|
+
* @returns What the run did.
|
|
47
|
+
* @throws SeedStepError for an invalid extra step, before any write; SeedStepError wrapping what a step threw; whatever the client throws otherwise. Nothing else is caught, so a failed seed fails loudly.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* await seedPlatform(prisma, input, { log: { info: console.log }, extras: [seedCoachDefaults] });
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @stability experimental
|
|
55
|
+
*/
|
|
56
|
+
async function seedPlatform(prisma, input, options = SILENT) {
|
|
57
|
+
const given = options;
|
|
58
|
+
const log = typeof given.info === 'function' ? given : (given.log ?? SILENT);
|
|
59
|
+
const extras = (typeof given.info === 'function' ? undefined : given.extras) ?? [];
|
|
60
|
+
(0, steps_js_1.assertSeedSteps)(extras);
|
|
61
|
+
log.info('Seeding roles...');
|
|
62
|
+
for (const role of input.roles) {
|
|
63
|
+
await prisma.role.upsert({
|
|
64
|
+
where: { name: role.name },
|
|
65
|
+
update: { description: role.description, ...scopeOf(role) },
|
|
66
|
+
create: { name: role.name, description: role.description, ...scopeOf(role) },
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
log.info(`✓ Seeded ${input.roles.length} roles`);
|
|
70
|
+
log.info('Seeding permissions...');
|
|
71
|
+
for (const permission of input.permissions) {
|
|
72
|
+
await prisma.permission.upsert({
|
|
73
|
+
where: { name: permission.name },
|
|
74
|
+
update: { description: permission.description, ...scopeOf(permission) },
|
|
75
|
+
create: { name: permission.name, description: permission.description, ...scopeOf(permission) },
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
log.info(`✓ Seeded ${input.permissions.length} permissions`);
|
|
79
|
+
log.info('Seeding role-permission mappings...');
|
|
80
|
+
let rolePermissions = 0;
|
|
81
|
+
const skippedGrants = [];
|
|
82
|
+
for (const [roleName, permissionNames] of Object.entries(input.roleGrants)) {
|
|
83
|
+
const role = await prisma.role.findUnique({ where: { name: roleName } });
|
|
84
|
+
if (!role) {
|
|
85
|
+
skippedGrants.push(roleName);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
for (const permissionName of permissionNames) {
|
|
89
|
+
const permission = await prisma.permission.findUnique({ where: { name: permissionName } });
|
|
90
|
+
if (!permission) {
|
|
91
|
+
skippedGrants.push(`${roleName}: ${permissionName}`);
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
await prisma.rolePermission.upsert({
|
|
95
|
+
where: { roleId_permissionId: { roleId: role.id, permissionId: permission.id } },
|
|
96
|
+
update: {},
|
|
97
|
+
create: { roleId: role.id, permissionId: permission.id },
|
|
98
|
+
});
|
|
99
|
+
rolePermissions++;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
log.info(`✓ Seeded ${rolePermissions} role-permission mappings`);
|
|
103
|
+
for (const skipped of skippedGrants)
|
|
104
|
+
log.info(`⊘ Skipped grant with no matching row: ${skipped}`);
|
|
105
|
+
log.info('Seeding system settings...');
|
|
106
|
+
await prisma.systemSettings.upsert({
|
|
107
|
+
where: { key: GLOBAL_SETTINGS_KEY },
|
|
108
|
+
update: {}, // Don't overwrite existing settings
|
|
109
|
+
create: { key: GLOBAL_SETTINGS_KEY, value: input.systemSettingsDefaults, version: 1 },
|
|
110
|
+
});
|
|
111
|
+
log.info('✓ Seeded default system settings');
|
|
112
|
+
log.info('Seeding initial admin allowlist...');
|
|
113
|
+
let allowlistedEmail = null;
|
|
114
|
+
if (input.initialAdminEmail) {
|
|
115
|
+
allowlistedEmail = input.initialAdminEmail.toLowerCase();
|
|
116
|
+
await prisma.allowedEmail.upsert({
|
|
117
|
+
where: { email: allowlistedEmail },
|
|
118
|
+
update: {},
|
|
119
|
+
create: { email: allowlistedEmail, notes: INITIAL_ADMIN_NOTE },
|
|
120
|
+
});
|
|
121
|
+
log.info(`✓ Added ${input.initialAdminEmail} to allowlist`);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
log.info('⊘ INITIAL_ADMIN_EMAIL not set, skipping allowlist seed');
|
|
125
|
+
}
|
|
126
|
+
log.info('Seeding default organization...');
|
|
127
|
+
const defaultOrganization = input.defaultOrganization ?? DEFAULT_ORGANIZATION;
|
|
128
|
+
// `select: { id }`: the seed reads and returns only the key, so it also runs against a database
|
|
129
|
+
// that is migrated only part of the way (a column added by a later migration is not read).
|
|
130
|
+
const existingDefault = await prisma.organization.findFirst({ where: { isDefault: true }, select: { id: true } });
|
|
131
|
+
const defaultOrganizationCreated = existingDefault === null;
|
|
132
|
+
if (defaultOrganizationCreated) {
|
|
133
|
+
await prisma.organization.upsert({
|
|
134
|
+
where: { slug: defaultOrganization.slug },
|
|
135
|
+
update: {}, // Don't overwrite an administrator's edits
|
|
136
|
+
create: { name: defaultOrganization.name, slug: defaultOrganization.slug, isDefault: true },
|
|
137
|
+
select: { id: true },
|
|
138
|
+
});
|
|
139
|
+
log.info(`✓ Created default organization "${defaultOrganization.slug}"`);
|
|
140
|
+
}
|
|
141
|
+
else {
|
|
142
|
+
log.info('✓ Default organization already exists');
|
|
143
|
+
}
|
|
144
|
+
const summary = {
|
|
145
|
+
roles: input.roles.length,
|
|
146
|
+
permissions: input.permissions.length,
|
|
147
|
+
rolePermissions,
|
|
148
|
+
skippedGrants,
|
|
149
|
+
allowlistedEmail,
|
|
150
|
+
defaultOrganizationCreated,
|
|
151
|
+
};
|
|
152
|
+
if (extras.length > 0) {
|
|
153
|
+
const ran = [];
|
|
154
|
+
const ctx = { log, input, platform: summary };
|
|
155
|
+
for (const step of extras) {
|
|
156
|
+
log.info(`Seeding ${step.id}...`);
|
|
157
|
+
const result = await (0, steps_js_1.runSeedStep)(prisma, step, ctx);
|
|
158
|
+
log.info(`✓ ${step.id}: ${result.created} created, ${result.updated} updated`);
|
|
159
|
+
ran.push({ id: step.id, ...result });
|
|
160
|
+
}
|
|
161
|
+
summary.extras = ran;
|
|
162
|
+
}
|
|
163
|
+
return summary;
|
|
164
|
+
}
|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,KAAK,MAAM,CAAC,OAAO,aAAa,MAAM,CAAC,OAAO,UAAU,CAAC,CAAC;YAC/E,GAAG,CAAC,IAAI,CAAC,EAAE,EAAE,EAAE,IAAI,CAAC,EAAE,EAAE,GAAG,MAAM,EAAE,CAAC,CAAC;QACvC,CAAC;QACD,OAAO,CAAC,MAAM,GAAG,GAAG,CAAC;IACvB,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC"}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import type { PlatformSeedInput, SeedLogger, SeedPrisma, SeedSummary } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* What a {@link SeedStep} reports: how many rows it inserted and how many it
|
|
4
|
+
* changed. The idempotence contract is about the first number: a second run
|
|
5
|
+
* over an unchanged database creates 0.
|
|
6
|
+
*
|
|
7
|
+
* @stability experimental
|
|
8
|
+
*/
|
|
9
|
+
export interface SeedStepResult {
|
|
10
|
+
/** Rows inserted by this run. A second run over the same database inserts none. */
|
|
11
|
+
created: number;
|
|
12
|
+
/** Rows an existing value was refreshed on. Not constrained by the contract (a step may rewrite a derived column every run). */
|
|
13
|
+
updated: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* What a {@link SeedStep} receives besides its client.
|
|
17
|
+
*
|
|
18
|
+
* @stability experimental
|
|
19
|
+
*/
|
|
20
|
+
export interface SeedStepContext {
|
|
21
|
+
/** The seed's logger: the step's own lines go through the same sink as the platform's, so one run reads as one log. */
|
|
22
|
+
log: SeedLogger;
|
|
23
|
+
/** The input the platform half ran with (roles, permissions, `initialAdminEmail`, ...). Read-only. */
|
|
24
|
+
input: Readonly<PlatformSeedInput>;
|
|
25
|
+
/** What the platform half did, so a step can depend on it (for example on `defaultOrganizationCreated`). */
|
|
26
|
+
platform: Readonly<SeedSummary>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* One app seed step: a named, idempotent write, run after the platform's own.
|
|
30
|
+
*
|
|
31
|
+
* Rules, the same as the platform's:
|
|
32
|
+
* - **Idempotent.** Every write is an upsert keyed on a natural unique, never a `findFirst` followed by a `create`. A second run reports `created: 0`.
|
|
33
|
+
* - **Never deletes**, and never overwrites a value an administrator may have edited (`update: {}`).
|
|
34
|
+
* - **Nest-free.** The seed runs under `ts-node --transpile-only` in an image that has no `src/`.
|
|
35
|
+
*
|
|
36
|
+
* `Tx` is the client type the step writes through: with a real Prisma client,
|
|
37
|
+
* `seedPlatform` runs the step inside `prisma.$transaction`, so `tx` is the
|
|
38
|
+
* transaction client and a failing step leaves none of its writes; with a
|
|
39
|
+
* client that has no `$transaction` (a test fake) `tx` is the client itself.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* // Prisma.TransactionClient is the generated client's transaction type.
|
|
44
|
+
* export const seedCoachDefaults: SeedStep<Prisma.TransactionClient> = {
|
|
45
|
+
* id: 'coach.defaults',
|
|
46
|
+
* async run(tx) {
|
|
47
|
+
* const before = await tx.allowedEmail.count({ where: { email: 'coach@example.test' } });
|
|
48
|
+
* await tx.allowedEmail.upsert({ where: { email: 'coach@example.test' }, update: {}, create: { email: 'coach@example.test', notes: 'Seeded coach' } });
|
|
49
|
+
* return { created: before === 0 ? 1 : 0, updated: 0 };
|
|
50
|
+
* },
|
|
51
|
+
* };
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @extensionPoint option
|
|
55
|
+
* @stability experimental
|
|
56
|
+
*/
|
|
57
|
+
export interface SeedStep<Tx = SeedPrisma> {
|
|
58
|
+
/**
|
|
59
|
+
* Stable id, `lower-case.words-or_dashes` (`coach.defaults`). It names the step in the log and in the summary, must be unique among the run's steps and must not be one of the platform's own step names ({@link PLATFORM_SEED_STEP_IDS}).
|
|
60
|
+
*/
|
|
61
|
+
id: string;
|
|
62
|
+
/**
|
|
63
|
+
* Writes the step's rows.
|
|
64
|
+
*
|
|
65
|
+
* @param tx - The transaction client (see the interface note).
|
|
66
|
+
* @param ctx - The logger, the platform input and what the platform half did.
|
|
67
|
+
* @returns How many rows were created and updated.
|
|
68
|
+
*/
|
|
69
|
+
run(tx: Tx, ctx: SeedStepContext): Promise<SeedStepResult>;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* What one extra step did in a run, as `SeedSummary.extras` lists it.
|
|
73
|
+
*
|
|
74
|
+
* @stability experimental
|
|
75
|
+
*/
|
|
76
|
+
export interface SeedStepSummary extends SeedStepResult {
|
|
77
|
+
/** The step's id. */
|
|
78
|
+
id: string;
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* The options of {@link seedPlatform}'s third argument (a bare `SeedLogger` is still accepted).
|
|
82
|
+
*
|
|
83
|
+
* @extensionPoint option
|
|
84
|
+
* @stability experimental
|
|
85
|
+
*/
|
|
86
|
+
export interface SeedPlatformOptions<Tx = SeedPrisma> {
|
|
87
|
+
/** Receives one line per step, the platform's and the extras'. Default: silent. */
|
|
88
|
+
log?: SeedLogger;
|
|
89
|
+
/** App steps, run in array order after the platform's own, each in its own transaction. */
|
|
90
|
+
extras?: ReadonlyArray<SeedStep<Tx>>;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The ids the platform half logs under; an extra step may not reuse one.
|
|
94
|
+
*
|
|
95
|
+
* @stability experimental
|
|
96
|
+
*/
|
|
97
|
+
export declare const PLATFORM_SEED_STEP_IDS: ReadonlyArray<string>;
|
|
98
|
+
/**
|
|
99
|
+
* A refusal of the seed's extra steps: a malformed or duplicate id, a step
|
|
100
|
+
* that returned a malformed result, or a step that threw.
|
|
101
|
+
*
|
|
102
|
+
* @stability experimental
|
|
103
|
+
*/
|
|
104
|
+
export declare class SeedStepError extends Error {
|
|
105
|
+
/** The step the error is about. */
|
|
106
|
+
readonly stepId: string;
|
|
107
|
+
/**
|
|
108
|
+
* @param stepId - The step.
|
|
109
|
+
* @param message - What is wrong.
|
|
110
|
+
* @param options - The thrown value, when a step threw.
|
|
111
|
+
*/
|
|
112
|
+
constructor(stepId: string, message: string, options?: {
|
|
113
|
+
cause?: unknown;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Checks a run's extra steps before anything is written.
|
|
118
|
+
*
|
|
119
|
+
* @param extras - The steps.
|
|
120
|
+
* @throws SeedStepError for a malformed id, a reserved id, a duplicate id or a `run` that is not a function.
|
|
121
|
+
* @stability experimental
|
|
122
|
+
*/
|
|
123
|
+
export declare function assertSeedSteps(extras: ReadonlyArray<SeedStep<never>>): void;
|
|
124
|
+
/**
|
|
125
|
+
* Validates what a step returned.
|
|
126
|
+
*
|
|
127
|
+
* @param id - The step.
|
|
128
|
+
* @param value - The value `run` resolved to.
|
|
129
|
+
* @returns The result, as `{ created, updated }`.
|
|
130
|
+
* @throws SeedStepError when a count is missing, negative, fractional or not a number.
|
|
131
|
+
* @stability experimental
|
|
132
|
+
*/
|
|
133
|
+
export declare function readSeedStepResult(id: string, value: unknown): SeedStepResult;
|
|
134
|
+
/**
|
|
135
|
+
* Both runs of {@link assertSeedIdempotent}.
|
|
136
|
+
*
|
|
137
|
+
* @stability experimental
|
|
138
|
+
*/
|
|
139
|
+
export interface SeedIdempotenceResult {
|
|
140
|
+
/** The summary of the first run. */
|
|
141
|
+
first: SeedSummary;
|
|
142
|
+
/** The summary of the second run, which created nothing. */
|
|
143
|
+
second: SeedSummary;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* The idempotence conformance helper: runs the seed twice over the same
|
|
147
|
+
* database and fails when the second run creates a row. Call it from the
|
|
148
|
+
* app's real-database test, with the same input and steps `seed.ts` uses.
|
|
149
|
+
* Jest- and Vitest-agnostic: it throws, it does not assert through a runner.
|
|
150
|
+
*
|
|
151
|
+
* @param seed - Runs the whole seed once and returns its summary: `() => seedPlatform(prisma, input, { extras })`.
|
|
152
|
+
* @returns Both summaries, for further assertions.
|
|
153
|
+
* @throws Error naming every step whose second run created rows (the platform's default organization included).
|
|
154
|
+
* @example
|
|
155
|
+
* ```ts
|
|
156
|
+
* await assertSeedIdempotent(() => seedPlatform(prisma, platformSeedInputFrom(SEED_SNAPSHOT, {}), { extras: appSeedSteps }));
|
|
157
|
+
* ```
|
|
158
|
+
* @stability experimental
|
|
159
|
+
*/
|
|
160
|
+
export declare function assertSeedIdempotent(seed: () => Promise<SeedSummary>): Promise<SeedIdempotenceResult>;
|
|
161
|
+
/** @internal The structural slice of a client `seedPlatform` needs to open a transaction. */
|
|
162
|
+
export interface SeedTransactionHost {
|
|
163
|
+
$transaction?: <R>(fn: (tx: never) => Promise<R>) => Promise<R>;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Runs one step: inside `prisma.$transaction` when the client has one, else
|
|
167
|
+
* against the client itself.
|
|
168
|
+
*
|
|
169
|
+
* @internal
|
|
170
|
+
*/
|
|
171
|
+
export declare function runSeedStep(prisma: SeedPrisma & SeedTransactionHost, step: SeedStep<never>, ctx: SeedStepContext): Promise<SeedStepResult>;
|
|
172
|
+
//# sourceMappingURL=steps.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"steps.d.ts","sourceRoot":"","sources":["../../src/seed/steps.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,iBAAiB,EAAE,UAAU,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEzF;;;;;;GAMG;AACH,MAAM,WAAW,cAAc;IAC7B,mFAAmF;IACnF,OAAO,EAAE,MAAM,CAAC;IAChB,gIAAgI;IAChI,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;;;GAIG;AACH,MAAM,WAAW,eAAe;IAC9B,uHAAuH;IACvH,GAAG,EAAE,UAAU,CAAC;IAChB,sGAAsG;IACtG,KAAK,EAAE,QAAQ,CAAC,iBAAiB,CAAC,CAAC;IACnC,4GAA4G;IAC5G,QAAQ,EAAE,QAAQ,CAAC,WAAW,CAAC,CAAC;CACjC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4BG;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H,wBAAsB,oBAAoB,CAAC,IAAI,EAAE,MAAM,OAAO,CAAC,WAAW,CAAC,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAY3G;AAED,6FAA6F;AAC7F,MAAM,WAAW,mBAAmB;IAClC,YAAY,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,KAAK,KAAK,OAAO,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,CAAC;CACjE;AAED;;;;;GAKG;AACH,wBAAsB,WAAW,CAC/B,MAAM,EAAE,UAAU,GAAG,mBAAmB,EACxC,IAAI,EAAE,QAAQ,CAAC,KAAK,CAAC,EACrB,GAAG,EAAE,eAAe,GACnB,OAAO,CAAC,cAAc,CAAC,CAWzB"}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// The seed hook (PP-14.27): app-owned steps that run after the platform's own,
|
|
3
|
+
// under one idempotence contract and one log. Types and helpers only; the
|
|
4
|
+
// loop lives in seed-platform.ts.
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.SeedStepError = exports.PLATFORM_SEED_STEP_IDS = void 0;
|
|
7
|
+
exports.assertSeedSteps = assertSeedSteps;
|
|
8
|
+
exports.readSeedStepResult = readSeedStepResult;
|
|
9
|
+
exports.assertSeedIdempotent = assertSeedIdempotent;
|
|
10
|
+
exports.runSeedStep = runSeedStep;
|
|
11
|
+
/**
|
|
12
|
+
* The ids the platform half logs under; an extra step may not reuse one.
|
|
13
|
+
*
|
|
14
|
+
* @stability experimental
|
|
15
|
+
*/
|
|
16
|
+
exports.PLATFORM_SEED_STEP_IDS = Object.freeze([
|
|
17
|
+
'roles',
|
|
18
|
+
'permissions',
|
|
19
|
+
'role-permissions',
|
|
20
|
+
'system-settings',
|
|
21
|
+
'allowlist',
|
|
22
|
+
'default-organization',
|
|
23
|
+
]);
|
|
24
|
+
const STEP_ID = /^[a-z][a-z0-9]*(?:[._-][a-z0-9]+)*$/;
|
|
25
|
+
/**
|
|
26
|
+
* A refusal of the seed's extra steps: a malformed or duplicate id, a step
|
|
27
|
+
* that returned a malformed result, or a step that threw.
|
|
28
|
+
*
|
|
29
|
+
* @stability experimental
|
|
30
|
+
*/
|
|
31
|
+
class SeedStepError extends Error {
|
|
32
|
+
/** The step the error is about. */
|
|
33
|
+
stepId;
|
|
34
|
+
/**
|
|
35
|
+
* @param stepId - The step.
|
|
36
|
+
* @param message - What is wrong.
|
|
37
|
+
* @param options - The thrown value, when a step threw.
|
|
38
|
+
*/
|
|
39
|
+
constructor(stepId, message, options) {
|
|
40
|
+
super(`seed step "${stepId}": ${message}`, options);
|
|
41
|
+
this.name = 'SeedStepError';
|
|
42
|
+
this.stepId = stepId;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.SeedStepError = SeedStepError;
|
|
46
|
+
/**
|
|
47
|
+
* Checks a run's extra steps before anything is written.
|
|
48
|
+
*
|
|
49
|
+
* @param extras - The steps.
|
|
50
|
+
* @throws SeedStepError for a malformed id, a reserved id, a duplicate id or a `run` that is not a function.
|
|
51
|
+
* @stability experimental
|
|
52
|
+
*/
|
|
53
|
+
function assertSeedSteps(extras) {
|
|
54
|
+
const seen = new Set();
|
|
55
|
+
for (const step of extras) {
|
|
56
|
+
if (typeof step.id !== 'string' || !STEP_ID.test(step.id)) {
|
|
57
|
+
throw new SeedStepError(String(step.id), 'the id must be lower-case words joined by ".", "-" or "_" (for example "coach.defaults")');
|
|
58
|
+
}
|
|
59
|
+
if (exports.PLATFORM_SEED_STEP_IDS.includes(step.id))
|
|
60
|
+
throw new SeedStepError(step.id, 'the id is one of the platform seed steps');
|
|
61
|
+
if (seen.has(step.id))
|
|
62
|
+
throw new SeedStepError(step.id, 'two steps share this id');
|
|
63
|
+
if (typeof step.run !== 'function')
|
|
64
|
+
throw new SeedStepError(step.id, 'run must be a function');
|
|
65
|
+
seen.add(step.id);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Validates what a step returned.
|
|
70
|
+
*
|
|
71
|
+
* @param id - The step.
|
|
72
|
+
* @param value - The value `run` resolved to.
|
|
73
|
+
* @returns The result, as `{ created, updated }`.
|
|
74
|
+
* @throws SeedStepError when a count is missing, negative, fractional or not a number.
|
|
75
|
+
* @stability experimental
|
|
76
|
+
*/
|
|
77
|
+
function readSeedStepResult(id, value) {
|
|
78
|
+
const result = value;
|
|
79
|
+
for (const field of ['created', 'updated']) {
|
|
80
|
+
const count = result?.[field];
|
|
81
|
+
if (typeof count !== 'number' || !Number.isInteger(count) || count < 0) {
|
|
82
|
+
throw new SeedStepError(id, `run must resolve to { created, updated } with non-negative integers; got ${field}=${String(count)}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return { created: result.created, updated: result.updated };
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The idempotence conformance helper: runs the seed twice over the same
|
|
89
|
+
* database and fails when the second run creates a row. Call it from the
|
|
90
|
+
* app's real-database test, with the same input and steps `seed.ts` uses.
|
|
91
|
+
* Jest- and Vitest-agnostic: it throws, it does not assert through a runner.
|
|
92
|
+
*
|
|
93
|
+
* @param seed - Runs the whole seed once and returns its summary: `() => seedPlatform(prisma, input, { extras })`.
|
|
94
|
+
* @returns Both summaries, for further assertions.
|
|
95
|
+
* @throws Error naming every step whose second run created rows (the platform's default organization included).
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* await assertSeedIdempotent(() => seedPlatform(prisma, platformSeedInputFrom(SEED_SNAPSHOT, {}), { extras: appSeedSteps }));
|
|
99
|
+
* ```
|
|
100
|
+
* @stability experimental
|
|
101
|
+
*/
|
|
102
|
+
async function assertSeedIdempotent(seed) {
|
|
103
|
+
const first = await seed();
|
|
104
|
+
const second = await seed();
|
|
105
|
+
const offenders = [];
|
|
106
|
+
if (second.defaultOrganizationCreated)
|
|
107
|
+
offenders.push('default-organization: created again');
|
|
108
|
+
for (const step of second.extras ?? []) {
|
|
109
|
+
if (step.created !== 0)
|
|
110
|
+
offenders.push(`${step.id}: created ${step.created} row(s) on the second run`);
|
|
111
|
+
}
|
|
112
|
+
if (offenders.length > 0) {
|
|
113
|
+
throw new Error(`the seed is not idempotent (a second run must create 0):\n${offenders.map((line) => ` ${line}`).join('\n')}`);
|
|
114
|
+
}
|
|
115
|
+
return { first, second };
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Runs one step: inside `prisma.$transaction` when the client has one, else
|
|
119
|
+
* against the client itself.
|
|
120
|
+
*
|
|
121
|
+
* @internal
|
|
122
|
+
*/
|
|
123
|
+
async function runSeedStep(prisma, step, ctx) {
|
|
124
|
+
try {
|
|
125
|
+
const raw = typeof prisma.$transaction === 'function'
|
|
126
|
+
? await prisma.$transaction((tx) => step.run(tx, ctx))
|
|
127
|
+
: await step.run(prisma, ctx);
|
|
128
|
+
return readSeedStepResult(step.id, raw);
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
if (error instanceof SeedStepError)
|
|
132
|
+
throw error;
|
|
133
|
+
throw new SeedStepError(step.id, error?.message ?? String(error), { cause: error });
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=steps.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"steps.js","sourceRoot":"","sources":["../../src/seed/steps.ts"],"names":[],"mappings":";AAAA,+EAA+E;AAC/E,0EAA0E;AAC1E,kCAAkC;;;AAgJlC,0CAWC;AAWD,gDASC;AA6BD,oDAYC;AAaD,kCAeC;AAjJD;;;;GAIG;AACU,QAAA,sBAAsB,GAA0B,MAAM,CAAC,MAAM,CAAC;IACzE,OAAO;IACP,aAAa;IACb,kBAAkB;IAClB,iBAAiB;IACjB,WAAW;IACX,sBAAsB;CACvB,CAAC,CAAC;AAEH,MAAM,OAAO,GAAG,qCAAqC,CAAC;AAEtD;;;;;GAKG;AACH,MAAa,aAAc,SAAQ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