|
| 1 | +import { z } from 'zod'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Data Import Strategy |
| 5 | + * Defines how the engine handles existing records. |
| 6 | + */ |
| 7 | +export const DatasetMode = z.enum([ |
| 8 | + 'insert', // Try to insert, fail on duplicate |
| 9 | + 'update', // Only update found records, ignore new |
| 10 | + 'upsert', // Create new or Update existing (Standard) |
| 11 | + 'replace', // Delete ALL records in object then insert (Dangerous - use for cache tables) |
| 12 | + 'ignore' // Try to insert, silently skip duplicates |
| 13 | +]); |
| 14 | + |
| 15 | +/** |
| 16 | + * Dataset Schema (Seed Data / Fixtures) |
| 17 | + * |
| 18 | + * Standardized format for transporting data. |
| 19 | + * Used for: |
| 20 | + * 1. System Bootstrapping (Admin accounts, Standard Roles) |
| 21 | + * 2. Reference Data (Countries, Currencies) |
| 22 | + * 3. Demo/Test Data |
| 23 | + */ |
| 24 | +export const DatasetSchema = z.object({ |
| 25 | + /** |
| 26 | + * Target Object |
| 27 | + * The machine name of the object to populate. |
| 28 | + */ |
| 29 | + object: z.string().regex(/^[a-z_][a-z0-9_]*$/).describe('Target Object Name'), |
| 30 | + |
| 31 | + /** |
| 32 | + * Idempotency Key (The "Upsert" Key) |
| 33 | + * The field used to check if a record already exists. |
| 34 | + * Best Practice: Use a natural key like 'code', 'slug', 'username' or 'external_id'. |
| 35 | + * Standard: '_id' (internal ID) is rarely used for portable seed data. |
| 36 | + */ |
| 37 | + externalId: z.string().default('name').describe('Field match for uniqueness check'), |
| 38 | + |
| 39 | + /** |
| 40 | + * Import Strategy |
| 41 | + */ |
| 42 | + mode: DatasetMode.default('upsert').describe('Conflict resolution strategy'), |
| 43 | + |
| 44 | + /** |
| 45 | + * Environment Scope |
| 46 | + * - 'all': Always load |
| 47 | + * - 'dev': Only for development/demo |
| 48 | + * - 'test': Only for CI/CD tests |
| 49 | + */ |
| 50 | + env: z.array(z.enum(['prod', 'dev', 'test'])).default(['prod', 'dev', 'test']).describe('Applicable environments'), |
| 51 | + |
| 52 | + /** |
| 53 | + * The Payload |
| 54 | + * Array of raw JSON objects matching the Object Schema. |
| 55 | + */ |
| 56 | + records: z.array(z.record(z.any())).describe('Data records'), |
| 57 | +}); |
| 58 | + |
| 59 | +export type Dataset = z.infer<typeof DatasetSchema>; |
| 60 | +export type DatasetImportMode = z.infer<typeof DatasetMode>; |
0 commit comments