/** * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal */ import { Table } from './table'; /** A collection of tables */ type Database = { readonly _name: string, readonly _tableNames: ReadonlyArray, readonly _schema: Schema } & Database.Tables namespace Database { export type Tables = { [T in keyof S]: Table } export type Schema = { [table: string]: Table.Schema } export function ofTables(name: string, schema: Schema, tables: Tables) { const keys = Object.keys(tables); const ret = Object.create(null); const tableNames: string[] = []; ret._name = name; ret._tableNames = tableNames; ret._schema = schema; for (const k of keys) { if (!Table.is(tables[k])) continue; ret[k] = tables[k]; tableNames[tableNames.length] = k; } return ret; } export function getTablesAsRows(database: Database) { const ret: { [k: string]: Table.Row[] } = {}; for (const k of database._tableNames) { ret[k] = Table.getRows(database[k]); } return ret; } } export { Database };