UNPKG

convex

Version:

Client for the Convex Cloud

113 lines (112 loc) 3.01 kB
"use strict"; export class SchemaType { constructor(referencedTableNames = /* @__PURE__ */ new Set()) { this.referencedTableNames = referencedTableNames; } } export const s = { id(tableName) { return new SchemaType(/* @__PURE__ */ new Set([tableName])); }, null() { return new SchemaType(); }, number() { return new SchemaType(); }, bigint() { return new SchemaType(); }, boolean() { return new SchemaType(); }, string() { return new SchemaType(); }, bytes() { return new SchemaType(); }, literal(literal) { return new SchemaType(); }, array(values) { return new SchemaType(values.referencedTableNames); }, set(values) { return new SchemaType(values.referencedTableNames); }, map(keys, values) { return new SchemaType( /* @__PURE__ */ new Set([...keys.referencedTableNames, ...values.referencedTableNames]) ); }, object(schema) { const referencedTableNames = /* @__PURE__ */ new Set(); for (const schemaType of Object.values(schema)) { for (const tableName of schemaType.referencedTableNames) { referencedTableNames.add(tableName); } } return new SchemaType(referencedTableNames); }, union(...schemaTypes) { const referencedTableNames = /* @__PURE__ */ new Set(); for (const schemaType of schemaTypes) { for (const tableName of schemaType.referencedTableNames) { referencedTableNames.add(tableName); } } return new SchemaType(referencedTableNames); } }; export class TableDefinition { constructor(documentType) { this.indexes = []; this.documentType = documentType; } index(name, fields) { this.indexes.push({ indexDescriptor: name, fields }); return this; } export() { return { indexes: this.indexes, referencedTableNames: this.documentType.referencedTableNames }; } } export function defineTable(documentSchema) { if (documentSchema instanceof SchemaType) { return new TableDefinition(documentSchema); } else { return new TableDefinition(s.object(documentSchema)); } } export class SchemaDefinition { constructor(tables) { this.tables = tables; } export() { const tableNames = new Set(Object.keys(this.tables)); return JSON.stringify({ tables: Object.entries(this.tables).map(([tableName, definition]) => { const { indexes, referencedTableNames } = definition.export(); for (const referencedTableName of referencedTableNames) { if (!tableNames.has(referencedTableName)) { throw new Error( `SchemaValidationError: Table ${tableName} has a \`s.id\` expression that references table ${referencedTableName} which isn't defined in the schema.` ); } } return { tableName, indexes }; }) }); } } export function defineSchema(schema) { return new SchemaDefinition(schema); } //# sourceMappingURL=index.js.map