import { Schema, SchemaFunc } from '../schema/Schema' import { Table, } from '../table/Table' import { wrapTableInDebugProxy } from '../table/TableDebugProxy' function getCallbackForSchemaFunc(func: SchemaFunc, schema: Schema, table: Table, sourceTable: Table, mapper: (item:any)=>any) { switch (func.funcName) { case 'each': return function*() { for (const item of sourceTable.each()) yield mapper(item); } case 'listAll': return () => { return sourceTable.listAll().map(mapper); } case 'listen': return () => { return (sourceTable.listen().map(mapper)); } } throw new Error(`Function is not supported by DerivedMapped table: ${func.funcName} (${func.publicName})`); } export interface Options { source: Table mapper: (item: SourceItemType) => ItemType schema: Schema> } export function createDerivedMappedTable({source, mapper, schema}: Options): Table { for (const schemaIndex of schema.indexes) { // ignore for now } const tableObject = { schema, } as Table; for (const func of schema.funcsByPublicName.values()) { if (func.funcName === 'insert' || func.funcName === 'preInsert') { // ignore continue; } tableObject[func.publicName] = getCallbackForSchemaFunc(func, schema, tableObject as Table, source, mapper); } return wrapTableInDebugProxy(tableObject); }