create-substrat 0.16.1 → 0.17.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-substrat",
3
- "version": "0.16.1",
3
+ "version": "0.17.0",
4
4
  "description": "Scaffold a Substrat vertical — `npm create substrat <dir>`.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
package/project-files.js CHANGED
@@ -30,12 +30,12 @@ import { DEV_SERVERS } from './dev-servers.js';
30
30
  // The runtime packages release together off one version line (the changesets `fixed`
31
31
  // group), so one constant is right for all of them. Engines do NOT share a line —
32
32
  // each versions on its own, so one pin per engine, deliberately.
33
- const SUBSTRAT = '^0.192.0';
34
- const ENGINE_WORKORDER = '^0.13.17';
35
- const ENGINE_INVOICING = '^0.11.76';
33
+ const SUBSTRAT = '^0.194.0';
34
+ const ENGINE_WORKORDER = '^0.13.19';
35
+ const ENGINE_INVOICING = '^0.11.78';
36
36
  const BOUNDARY_LINT = '^0.9.1';
37
- const DEV_ISSUER = '^0.6.0';
38
- const ATTACHMENT_EXTRACTORS = '^0.2.53';
37
+ const DEV_ISSUER = '^0.7.0';
38
+ const ATTACHMENT_EXTRACTORS = '^0.2.55';
39
39
 
40
40
  export const TSCONFIG = `${JSON.stringify(
41
41
  {
@@ -8,6 +8,9 @@ import type { DevPersona } from '@substrat-run/dev-issuer';
8
8
  * password. `linkDevPersonas` reads the same array and binds each `sub` to a principal in
9
9
  * the identity directory. Neither side holds a copy of the other's list.
10
10
  *
11
+ * `role` is the role key each person holds; `elsewhere` puts them in the other shop. A seeded
12
+ * preview of this app offers the same people (`src/world.ts`), except anyone `elsewhere`.
13
+ *
11
14
  * Nothing here is a bypass, and that is the point. The issuer asserts a subject and the
12
15
  * directory maps it to a principal — the same two steps a deployed instance performs against
13
16
  * a real issuer, so the login you exercise all day is the login your users will run. What
@@ -19,12 +22,14 @@ import type { DevPersona } from '@substrat-run/dev-issuer';
19
22
  * readable: they end up in an identity directory that outlives a restart.
20
23
  */
21
24
  export const PERSONAS: DevPersona[] = [
22
- { sub: 'dev|greta', name: 'Greta', email: 'greta@kedja.test', note: 'workshop-admin' },
23
- { sub: 'dev|mans', name: 'Måns', email: 'mans@kedja.test', note: 'mechanic' },
25
+ { sub: 'dev|greta', name: 'Greta', email: 'greta@kedja.test', note: 'workshop-admin', role: 'workshop-admin' },
26
+ { sub: 'dev|mans', name: 'Måns', email: 'mans@kedja.test', note: 'mechanic', role: 'mechanic' },
27
+ // Portal customers hold no role: their access is a grant on their own customer record,
28
+ // which `src/world.ts` gives once that record exists.
24
29
  { sub: 'dev|lisbeth', name: 'Lisbeth', email: 'lisbeth@example.test', note: 'portal customer' },
25
30
  { sub: 'dev|otto', name: 'Otto', email: 'otto@example.test', note: 'portal customer' },
26
31
  // The other shop, so the cross-tenant beat has someone to be turned away as.
27
- { sub: 'dev|rutger', name: 'Rutger', email: 'rutger@trampolin.test', note: 'admin at a DIFFERENT shop' },
32
+ { sub: 'dev|rutger', name: 'Rutger', email: 'rutger@trampolin.test', note: 'admin at a DIFFERENT shop', role: 'workshop-admin', elsewhere: true },
28
33
  ];
29
34
 
30
35
  /**
@@ -9,16 +9,17 @@ import {
9
9
  type ScopeId,
10
10
  type TenantId,
11
11
  } from '@substrat-run/contracts';
12
- import { ulid } from '@substrat-run/kernel';
12
+ import { seatWorld, ulid, type WorldSeat } from '@substrat-run/kernel';
13
13
  import { SqliteScopeHost } from '@substrat-run/adapter-sqlite';
14
14
  import { defaultAttachmentExtractors } from '@substrat-run/attachment-extractors';
15
- import { ENTITLEMENT_KEYS, ENTITY_GRANTS, MODULES, OWNER_ROLE_KEY, permissions, portalPerms, ROLES } from './provision.js';
15
+ import { ENTITLEMENT_KEYS, ENTITY_GRANTS, MODULES, permissions, portalPerms, ROLES } from './provision.js';
16
16
 
17
17
  // The provisioning surface (modules, roles, grant shapes) lives in
18
18
  // provision.ts — node-free so the worker bundles it and `substrat push` reads
19
19
  // it. Re-exported here for callers that treat seed.ts as the world's front door.
20
20
  export { ENTITY_GRANTS, MODULES, permissions, ROLES } from './provision.js';
21
- import { DEV_PROVIDER, PERSONAS } from './personas.js';
21
+ import { DEV_PROVIDER } from './personas.js';
22
+ import { buildShop, WORLD, type ShopRecords } from './world.js';
22
23
 
23
24
  // ============================================================================
24
25
  // The seeded world. TWO tenants on purpose: the first is the shop the scenario
@@ -50,13 +51,13 @@ export function buildBikeShopHost(dir: string): SqliteScopeHost {
50
51
  }
51
52
 
52
53
  /**
53
- * Provision ONE tenant of this vertical: tenant, entitlements for every module
54
- * it runs, an active scope, the role table, and the owner holding workshop-admin.
55
- * This is what an instantiate button would call — no demo cast, no fixtures.
54
+ * Provision ONE tenant of this vertical: tenant, entitlements for every module it runs, an
55
+ * active scope, and the role table. This is what an instantiate button would call — no demo
56
+ * cast, no fixtures. The people come after, from `src/world.ts`.
56
57
  */
57
58
  async function provisionShop(
58
59
  host: SqliteScopeHost,
59
- input: { tenantId: TenantId; scopeId: ScopeId; owner: PrincipalId; slug: string; name: string },
60
+ input: { tenantId: TenantId; scopeId: ScopeId; slug: string; name: string },
60
61
  ): Promise<void> {
61
62
  const staff = platformActorId.parse(ulid());
62
63
  await host.admin.createTenant(staff, { id: input.tenantId, slug: input.slug, name: input.name });
@@ -75,11 +76,6 @@ async function provisionShop(
75
76
  // confirmation is immediate).
76
77
  await host.admin.activateScope(staff, input.tenantId, input.scopeId);
77
78
  for (const role of ROLES) await host.admin.defineRole(staff, input.tenantId, role);
78
- await host.admin.assignRole(staff, {
79
- principalId: input.owner,
80
- roleKey: OWNER_ROLE_KEY,
81
- node: { tenantId: input.tenantId, scopeId: null },
82
- });
83
79
  }
84
80
 
85
81
  /**
@@ -112,100 +108,52 @@ export async function seedBikeShop(host: SqliteScopeHost, dir: string): Promise<
112
108
  return world;
113
109
  }
114
110
 
111
+ const homes = {
112
+ home: { tenantId: tenantId.parse(ulid()), scopeId: scopeId.parse(ulid()) },
113
+ elsewhere: { tenantId: tenantId.parse(ulid()), scopeId: scopeId.parse(ulid()) },
114
+ } as const;
115
+ // The shop under test, and a second unrelated shop whose admin will attack it.
116
+ await provisionShop(host, { ...homes.home, slug: 'kedja-kugghjul', name: 'Kedja & Kugghjul Cykelverkstad' });
117
+ await provisionShop(host, { ...homes.elsewhere, slug: 'trampolin', name: 'Trampolin Cykel' });
118
+
119
+ // The cast and the records they start with: `src/world.ts`, seated by the kernel in each
120
+ // shop — a principal per persona, its role, its identity link — the same way a seeded
121
+ // preview of the deployed app seats it. Its records are kept for `cast.json`.
115
122
  const staff = platformActorId.parse(ulid());
123
+ await host.admin.registerIdentityPool(staff, { provider: DEV_PROVIDER, topology: 'central', tenantId: null });
124
+ let records: ShopRecords | null = null;
125
+ const seats = new Map<string, WorldSeat>();
126
+ for (const seat of ['home', 'elsewhere'] as const) {
127
+ const seated = await seatWorld(host, {
128
+ actor: staff,
129
+ node: homes[seat],
130
+ provider: DEV_PROVIDER,
131
+ world: { ...WORLD, build: async (w) => void (records ??= await buildShop(w)) },
132
+ seat,
133
+ build: true,
134
+ });
135
+ for (const [sub, at] of seated) seats.set(sub, at);
136
+ }
137
+ const principalOf = (sub: string): PrincipalId => {
138
+ const at = seats.get(sub);
139
+ if (!at) throw new Error(`seed: persona ${sub} was not seated`);
140
+ return at.principal;
141
+ };
142
+ if (!records) throw new Error('seed: the shop under test was built with no records');
116
143
  const world: BikeShopWorld = {
117
- t1: tenantId.parse(ulid()),
118
- s1: scopeId.parse(ulid()),
119
- t2: tenantId.parse(ulid()),
120
- s2: scopeId.parse(ulid()),
121
- greta: principalId.parse(ulid()),
122
- mans: principalId.parse(ulid()),
123
- lisbeth: principalId.parse(ulid()),
124
- otto: principalId.parse(ulid()),
125
- rutger: principalId.parse(ulid()),
126
- lisbethId: '',
127
- ottoId: '',
128
- crescentId: '',
129
- bianchiId: '',
144
+ t1: homes.home.tenantId,
145
+ s1: homes.home.scopeId,
146
+ t2: homes.elsewhere.tenantId,
147
+ s2: homes.elsewhere.scopeId,
148
+ greta: principalOf('dev|greta'),
149
+ mans: principalOf('dev|mans'),
150
+ lisbeth: principalOf('dev|lisbeth'),
151
+ otto: principalOf('dev|otto'),
152
+ rutger: principalOf('dev|rutger'),
153
+ ...(records as ShopRecords),
130
154
  };
131
-
132
- // The shop under test, and a second unrelated shop whose admin will attack it.
133
- await provisionShop(host, {
134
- tenantId: world.t1,
135
- scopeId: world.s1,
136
- owner: world.greta,
137
- slug: 'kedja-kugghjul',
138
- name: 'Kedja & Kugghjul Cykelverkstad',
139
- });
140
- await provisionShop(host, {
141
- tenantId: world.t2,
142
- scopeId: world.s2,
143
- owner: world.rutger,
144
- slug: 'trampolin',
145
- name: 'Trampolin Cykel',
146
- });
147
-
148
- // The rest of t1's cast.
149
- await host.admin.assignRole(staff, {
150
- principalId: world.mans,
151
- roleKey: 'mechanic',
152
- node: { tenantId: world.t1, scopeId: world.s1 },
153
- });
154
-
155
- // Seed entities go through the operations (NEVER raw SQL): the seed exercises
156
- // the same permission checks and event spine the app does.
157
- const greta = await host.getScope(world.greta, world.t1, world.s1);
158
- const lisbethCustomer = await greta.invoke<{ id: string }>('shop/create-customer', {
159
- number: '2001',
160
- name: 'Lisbeth Sandell',
161
- phone: '070-123 45 67',
162
- });
163
- const ottoCustomer = await greta.invoke<{ id: string }>('shop/create-customer', {
164
- number: '2002',
165
- name: 'Otto Vinge',
166
- });
167
- const crescent = await greta.invoke<{ id: string }>('shop/register-bike', {
168
- customerId: lisbethCustomer.id,
169
- label: 'Crescent Elina 3-vxl',
170
- frameNo: 'CR-88412',
171
- });
172
- const bianchi = await greta.invoke<{ id: string }>('shop/register-bike', {
173
- customerId: ottoCustomer.id,
174
- label: 'Bianchi Oltre XR3',
175
- });
176
- await greta.invoke('shop/upsert-price', {
177
- article: 'labor',
178
- description: 'Mechanic time',
179
- unit: 'hour',
180
- priceAmount: '495',
181
- minQty: '0.5',
182
- });
183
- await greta.invoke('shop/upsert-price', {
184
- article: 'shop-supplies',
185
- description: 'Workshop supplies',
186
- unit: 'ea',
187
- priceAmount: '15',
188
- internal: true,
189
- });
190
- await greta.invoke('shop/upsert-price', {
191
- article: 'tube-28',
192
- description: 'Inner tube 28"',
193
- unit: 'ea',
194
- priceAmount: '89',
195
- });
196
- await greta.invoke('shop/upsert-price', {
197
- article: 'chain-9s',
198
- description: 'Chain, 9-speed',
199
- unit: 'ea',
200
- priceAmount: '249',
201
- });
202
-
203
- world.lisbethId = lisbethCustomer.id;
204
- world.ottoId = ottoCustomer.id;
205
- world.crescentId = crescent.id;
206
- world.bianchiId = bianchi.id;
207
-
208
- await grantPortals(host, world);
155
+ // `buildShop` gave the portal grants; the reconcile tops every holder up to the shape as it is.
156
+ await host.admin.reconcileEntityGrantShapes(staff, homes.home, ENTITY_GRANTS);
209
157
 
210
158
  writeFileSync(castPath, JSON.stringify(world, null, 2));
211
159
  return world;
@@ -240,43 +188,23 @@ async function grantPortals(host: SqliteScopeHost, world: BikeShopWorld): Promis
240
188
  }
241
189
 
242
190
  /**
243
- * Bind each dev persona's OIDC `sub` to its principal in the identity directory.
244
- *
245
- * This is the ordinary production seam, not a dev one: a deployed instance builds the same
246
- * rows when someone claims the owner seat or accepts an invite. All that differs locally is
247
- * that the subjects are known up front, so nobody has to click through a first-run claim
248
- * after every data wipe.
191
+ * Seat the cast again on every boot, because `seedBikeShop` returns early once `cast.json`
192
+ * exists. Seating is idempotent — a persona already linked keeps its principal — so this costs
193
+ * nothing, a wiped identity link heals itself, and a persona added to `src/personas.ts` after
194
+ * the first boot gets a principal, its role and a link (but no records: `build` runs only on a
195
+ * fresh world).
249
196
  *
250
- * Run on EVERY boot rather than only on a fresh seed: `seedBikeShop` returns early once
251
- * `cast.json` exists, and `linkIdentity` is idempotent for an unchanged binding, so
252
- * re-running costs nothing and a wiped `.data` heals itself.
197
+ * This is the ordinary production seam, not a dev one: a deployed instance builds the same rows
198
+ * when someone claims the owner seat or accepts an invite, and a seeded preview runs this very
199
+ * seating. All that differs locally is that the subjects are known up front.
253
200
  */
254
201
  export async function linkDevPersonas(host: SqliteScopeHost, world: BikeShopWorld): Promise<void> {
255
202
  const staff = platformActorId.parse(ulid());
256
- await host.admin.registerIdentityPool(staff, {
257
- provider: DEV_PROVIDER,
258
- topology: 'central',
259
- tenantId: null,
260
- });
261
- // `scopeId` on the link is what carries a persona to their own node — Rutger into the
262
- // other shop entirely, which is why the cross-tenant beat still runs with no persona
263
- // table anywhere in the server.
264
- const homes: Record<string, { principal: PrincipalId; tenantId: TenantId; scopeId: ScopeId }> = {
265
- 'dev|greta': { principal: world.greta, tenantId: world.t1, scopeId: world.s1 },
266
- 'dev|mans': { principal: world.mans, tenantId: world.t1, scopeId: world.s1 },
267
- 'dev|lisbeth': { principal: world.lisbeth, tenantId: world.t1, scopeId: world.s1 },
268
- 'dev|otto': { principal: world.otto, tenantId: world.t1, scopeId: world.s1 },
269
- 'dev|rutger': { principal: world.rutger, tenantId: world.t2, scopeId: world.s2 },
270
- };
271
- for (const persona of PERSONAS) {
272
- const home = homes[persona.sub];
273
- if (!home) continue;
274
- await host.admin.linkIdentity(staff, {
275
- provider: DEV_PROVIDER,
276
- externalId: persona.sub,
277
- principal: home.principal,
278
- tenantId: home.tenantId,
279
- scopeId: home.scopeId,
280
- });
203
+ await host.admin.registerIdentityPool(staff, { provider: DEV_PROVIDER, topology: 'central', tenantId: null });
204
+ for (const [seat, node] of [
205
+ ['home', { tenantId: world.t1, scopeId: world.s1 }],
206
+ ['elsewhere', { tenantId: world.t2, scopeId: world.s2 }],
207
+ ] as const) {
208
+ await seatWorld(host, { actor: staff, node, provider: DEV_PROVIDER, world: WORLD, seat });
281
209
  }
282
210
  }
@@ -42,6 +42,7 @@ import { IdentityDO, workerDoor } from '@substrat-run/vertical-host/worker-door'
42
42
  // provision.ts exports these five, and needs no edit beyond a `join`.
43
43
  import { MODULES, OWNER_ROLE_KEY, ROLES, envSpec, permissions } from './provision.js';
44
44
  import { mountApi } from './routes.js';
45
+ import { WORLD } from './world.js';
45
46
 
46
47
  /** The scope-DO class = the app binary: kernel + engines + this vertical, bundled. */
47
48
  export const ScopeDO = defineScopeDO(MODULES, {}, { permissions });
@@ -163,6 +164,9 @@ const door = workerDoor<Env>({
163
164
  nodeFor,
164
165
  hostFor,
165
166
  envSpec,
167
+ // The sample world (src/world.ts) a seeded preview of this app holds: its people sign in by
168
+ // picking a persona, as they do locally. Seated only in a clean-room preview, never an install.
169
+ world: WORLD,
166
170
  });
167
171
 
168
172
  /** The caller's scope stub. 401 if nobody. */
@@ -0,0 +1,65 @@
1
+ /**
2
+ * The shop's sample world: who is in it (`src/personas.ts`), and what they start with. Node-free,
3
+ * because it is not only the dev server's. `seatWorld` runs it there, and a seeded preview of
4
+ * the deployed app runs it in the worker (`workerDoor({ world: WORLD })`), so the two hold the
5
+ * same people and the same records. The platform does its half itself: a principal per persona,
6
+ * its `role`, and the identity link its `sub` signs in by.
7
+ *
8
+ * What is left here is the vertical's: records are made through the operations, never raw SQL,
9
+ * so the seed runs the same checks and emits the same events the app does; and each portal
10
+ * customer is given the `customer` shape on their own record, the grant everything they can see
11
+ * hangs off.
12
+ */
13
+ import type { SeedWorld, WorldBuilder } from '@substrat-run/kernel';
14
+ import { portalPerms } from './provision.js';
15
+ import { PERSONAS } from './personas.js';
16
+
17
+ /** The records the shop starts with, which the dev seed remembers in `cast.json`. */
18
+ export interface ShopRecords {
19
+ lisbethId: string;
20
+ ottoId: string;
21
+ crescentId: string;
22
+ bianchiId: string;
23
+ }
24
+
25
+ /** Build the shop's records. The other shop (`elsewhere`) starts empty, so it answers null. */
26
+ export async function buildShop(world: WorldBuilder): Promise<ShopRecords | null> {
27
+ if (!world.personas.some((p) => p.sub === 'dev|greta')) return null;
28
+ const greta = world.as('dev|greta');
29
+ const lisbeth = await greta.invoke<{ id: string }>('shop/create-customer', {
30
+ number: '2001',
31
+ name: 'Lisbeth Sandell',
32
+ phone: '070-123 45 67',
33
+ });
34
+ const otto = await greta.invoke<{ id: string }>('shop/create-customer', { number: '2002', name: 'Otto Vinge' });
35
+ const crescent = await greta.invoke<{ id: string }>('shop/register-bike', {
36
+ customerId: lisbeth.id,
37
+ label: 'Crescent Elina 3-vxl',
38
+ frameNo: 'CR-88412',
39
+ });
40
+ const bianchi = await greta.invoke<{ id: string }>('shop/register-bike', { customerId: otto.id, label: 'Bianchi Oltre XR3' });
41
+ for (const price of [
42
+ { article: 'labor', description: 'Mechanic time', unit: 'hour', priceAmount: '495', minQty: '0.5' },
43
+ { article: 'shop-supplies', description: 'Workshop supplies', unit: 'ea', priceAmount: '15', internal: true },
44
+ { article: 'tube-28', description: 'Inner tube 28"', unit: 'ea', priceAmount: '89' },
45
+ { article: 'chain-9s', description: 'Chain, 9-speed', unit: 'ea', priceAmount: '249' },
46
+ ]) {
47
+ await greta.invoke('shop/upsert-price', price);
48
+ }
49
+ // Each portal customer holds the `customer` shape on their OWN record only; the walk
50
+ // workorder → bike → customer does the rest.
51
+ for (const [sub, customerId] of [
52
+ ['dev|lisbeth', lisbeth.id],
53
+ ['dev|otto', otto.id],
54
+ ] as const) {
55
+ await world.grantShape({ to: sub, entity: { entityType: 'customer', entityId: customerId }, permissions: portalPerms, grantedBy: 'dev|greta' });
56
+ }
57
+ return { lisbethId: lisbeth.id, ottoId: otto.id, crescentId: crescent.id, bianchiId: bianchi.id };
58
+ }
59
+
60
+ export const WORLD: SeedWorld = {
61
+ personas: PERSONAS,
62
+ async build(world) {
63
+ await buildShop(world);
64
+ },
65
+ };