experiments in a post-browser web
at main 64 lines 2.1 kB view raw
1/** 2 * Schema Validator 3 * Generated by schema/codegen.js 4 * Schema version: 1 5 * DO NOT EDIT - regenerate with: yarn schema:codegen 6 */ 7 8/** 9 * Validate that a database has all required sync columns. 10 * Works with any SQLite wrapper that supports PRAGMA table_info. 11 * 12 * @param {Function} getColumns - Function that takes table name and returns column names array 13 * @returns {{ valid: boolean, missing: string[] }} Validation result 14 */ 15export function validateSyncSchema(getColumns) { 16 const required = { 17 items: ["id","type","content","syncId","syncSource","syncedAt","createdAt","updatedAt","deletedAt"], 18 tags: ["id","name","frequency","lastUsed","frecencyScore","createdAt","updatedAt"], 19 item_tags: ["itemId","tagId","createdAt"], 20 item_events: ["id","itemId","content","value","occurredAt","metadata","createdAt"], 21 }; 22 23 const missing = []; 24 25 for (const [table, cols] of Object.entries(required)) { 26 const actual = new Set(getColumns(table)); 27 for (const col of cols) { 28 if (!actual.has(col)) { 29 missing.push(`${table}.${col}`); 30 } 31 } 32 } 33 34 return { 35 valid: missing.length === 0, 36 missing, 37 }; 38} 39 40/** 41 * Validate schema and throw if invalid. 42 * @param {Function} getColumns - Function that takes table name and returns column names array 43 * @throws {Error} If required columns are missing 44 */ 45export function assertValidSyncSchema(getColumns) { 46 const result = validateSyncSchema(getColumns); 47 if (!result.valid) { 48 throw new Error( 49 `[schema] Required columns missing: ${result.missing.join(", ")}. ` + 50 `Database may need migration. See schema/v1.json for canonical schema.` 51 ); 52 } 53} 54 55/** Schema version */ 56export const SCHEMA_VERSION = 1; 57 58/** Required sync columns by table */ 59export const REQUIRED_SYNC_COLUMNS = { 60 items: ["id","type","content","syncId","syncSource","syncedAt","createdAt","updatedAt","deletedAt"], 61 tags: ["id","name","frequency","lastUsed","frecencyScore","createdAt","updatedAt"], 62 item_tags: ["itemId","tagId","createdAt"], 63 item_events: ["id","itemId","content","value","occurredAt","metadata","createdAt"], 64};