import { ModuleContext, tablesToSchema, type TablesToSchema, type UntypedSchemaDef, } from '../lib/schema.ts'; import type { UntypedTableSchema } from '../lib/table_schema.ts'; class Tables { constructor(readonly schemaType: S) {} } /** * Creates a schema from table definitions * @param handles - Array of table handles created by table() function * @returns ColumnBuilder representing the complete database schema * @example * ```ts * const spacetimedb = schema({ * user: table({}, userType), * post: table({}, postType) * }); * ``` */ export function schema>( tables: H ): Tables> { const ctx = new ModuleContext(); return new Tables(tablesToSchema(ctx, tables)); } type HasAccessor = { accessorName: PropertyKey }; export type ConvertToAccessorMap = { [Tbl in TableDefs[number] as Tbl['accessorName']]: Tbl; }; export function convertToAccessorMap( arr: T ): ConvertToAccessorMap { return Object.fromEntries( arr.map(v => [v.accessorName, v]) ) as ConvertToAccessorMap; }