// Boot-lifecycle seed — a couple of authors + their books so the catalog has // data the moment `voltro dev` boots. Idempotent via `upsertByUnique`, so // re-running on every boot is safe (the fingerprint ledger also dedupes). // // NOTE: the seed store is the RAW store — it has no authenticated subject, so // the `tenant()` auto-fill doesn't fire. We pass `tenantId` explicitly here // (and seed the demo tenant row first). The dev AuthMiddleware default resolves // the `acme` tenant from the `x-tenant` header, so seeding under `acme` lines // up with what queries see out of the box. // // `bio` is the `.encrypted()` column — we pass plaintext; the store middleware // encrypts it on write (requires governancePlugin({ fieldEncryption: true })). // `tags` is the `array(text())` column — pass a plain JS array. `slug` is the // DB-computed `.generatedAs(...)` column — DON'T pass it; the engine fills it. import { defineSeed } from '@voltro/database' const TENANT_ID = 'acme' export default defineSeed({ id: 'catalog', name: 'Library catalog demo data', lifecycle: 'boot', steps: ({ step }) => [ step('tenant', async ({ upsertByUnique }) => { await upsertByUnique( 'tenants', { id: TENANT_ID }, { id: TENANT_ID, name: 'Acme Library', createdAt: new Date() }, ) return { rowsTouched: 1 } }), step('authors-and-books', async ({ upsertByUnique }) => { let touched = 0 const ursula = await upsertByUnique( 'authors', { id: 'author_leguin' }, { id: 'author_leguin', name: 'Ursula K. Le Guin', bio: 'American author best known for works of speculative fiction.', tenantId: TENANT_ID, }, ) touched += 1 const carl = await upsertByUnique( 'authors', { id: 'author_sagan' }, { id: 'author_sagan', name: 'Carl Sagan', bio: 'Astronomer and science communicator.', tenantId: TENANT_ID, }, ) touched += 1 const books = [ { id: 'book_leftHand', authorId: ursula.row['id'] as string, title: 'The Left Hand of Darkness', summary: 'A lone envoy on a frozen world where inhabitants have no fixed sex.', genre: 'sciFi', tags: ['classic', 'gender', 'winter'], tenantId: TENANT_ID, }, { id: 'book_earthsea', authorId: ursula.row['id'] as string, title: 'A Wizard of Earthsea', summary: 'A young mage learns the true cost of power and names.', genre: 'fantasy', tags: ['magic', 'coming-of-age'], tenantId: TENANT_ID, }, { id: 'book_cosmos', authorId: carl.row['id'] as string, title: 'Cosmos', summary: 'A sweeping tour of the universe and our place in it.', genre: 'nonfiction', tags: ['astronomy', 'science'], tenantId: TENANT_ID, }, ] for (const book of books) { // `slug` is omitted — the `.generatedAs(...)` STORED column is filled // by the DB engine from `title`. await upsertByUnique('books', { id: book.id }, book) touched += 1 } return { rowsTouched: touched } }), ], })