|
| 1 | +import { z } from 'zod'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Metadata Scope Enum |
| 5 | + * Defines the lifecycle and mutability of a metadata item. |
| 6 | + */ |
| 7 | +export const MetadataScopeSchema = z.enum([ |
| 8 | + 'system', // Defined in Code (Files). Read-only at runtime. Upgraded via deployment. |
| 9 | + 'platform', // Defined in DB (Global). admin-configured. Overrides system. |
| 10 | + 'user', // Defined in DB (Personal). User-configured. Overrides platform/system. |
| 11 | +]); |
| 12 | + |
| 13 | +/** |
| 14 | + * Metadata Lifecycle State |
| 15 | + */ |
| 16 | +export const MetadataStateSchema = z.enum([ |
| 17 | + 'draft', // Work in progress, not active |
| 18 | + 'active', // Live and running |
| 19 | + 'archived', // Soft deleted |
| 20 | + 'deprecated' // Running but flagged for removal |
| 21 | +]); |
| 22 | + |
| 23 | +/** |
| 24 | + * Unified Metadata Persistence Protocol |
| 25 | + * |
| 26 | + * Defines the standardized envelope for storing ANY metadata item (Object, View, Flow) |
| 27 | + * in the database (e.g. `_framework_metadata` or generic `metadata` table). |
| 28 | + * |
| 29 | + * This treats "Metadata as Data". |
| 30 | + */ |
| 31 | +export const MetadataRecordSchema = z.object({ |
| 32 | + /** Primary Key (UUID) */ |
| 33 | + _id: z.string(), |
| 34 | + |
| 35 | + /** |
| 36 | + * Machine Name |
| 37 | + * The unique identifier used in code references (e.g. "account_list_view"). |
| 38 | + */ |
| 39 | + name: z.string(), |
| 40 | + |
| 41 | + /** |
| 42 | + * Metadata Type |
| 43 | + * e.g. "object", "view", "permission_set", "flow" |
| 44 | + */ |
| 45 | + type: z.string(), |
| 46 | + |
| 47 | + /** |
| 48 | + * Namespace / Module |
| 49 | + * Groups metadata into packages (e.g. "crm", "finance", "core"). |
| 50 | + */ |
| 51 | + namespace: z.string().default('default'), |
| 52 | + |
| 53 | + /** |
| 54 | + * Ownership differentiation |
| 55 | + */ |
| 56 | + scope: MetadataScopeSchema.default('platform'), |
| 57 | + |
| 58 | + /** |
| 59 | + * The Payload |
| 60 | + * Stores the actual configuration JSON. |
| 61 | + * This field holds the value of `ViewSchema`, `ObjectSchema`, etc. |
| 62 | + */ |
| 63 | + metadata: z.record(z.string(), z.any()), |
| 64 | + |
| 65 | + /** |
| 66 | + * Extension / Merge Strategy |
| 67 | + * If this record overrides a system record, how should it be applied? |
| 68 | + */ |
| 69 | + extends: z.string().optional().describe('Name of the parent metadata to extend/override'), |
| 70 | + strategy: z.enum(['merge', 'replace']).default('merge'), |
| 71 | + |
| 72 | + /** Owner (for user-scope items) */ |
| 73 | + owner: z.string().optional(), |
| 74 | + |
| 75 | + /** State */ |
| 76 | + state: MetadataStateSchema.default('active'), |
| 77 | + |
| 78 | + /** Audit */ |
| 79 | + created_by: z.string().optional(), |
| 80 | + created_at: z.date().optional(), |
| 81 | + updated_by: z.string().optional(), |
| 82 | + updated_at: z.date().optional(), |
| 83 | +}); |
| 84 | + |
| 85 | +export type MetadataRecord = z.infer<typeof MetadataRecordSchema>; |
| 86 | +export type MetadataScope = z.infer<typeof MetadataScopeSchema>; |
0 commit comments