import _ from "lodash"; import type { BUN_SQLITE_DatabaseSchemaType, BUN_SQLITE_TableSchemaType, } from "../../types"; type Params = { table: BUN_SQLITE_TableSchemaType; db_schema: BUN_SQLITE_DatabaseSchemaType; }; /** * Resolve a table's effective definition. Tables with `parentTableName` * take their structure from the parent. Always returns a copy: the schema * objects are never mutated. */ export default function resolveTable({ table, db_schema, }: Params): BUN_SQLITE_TableSchemaType { if (!table.parentTableName) { return _.cloneDeep(table); } const parent_table = db_schema.tables.find( (t) => t.tableName === table.parentTableName, ); if (!parent_table) { throw new Error( `Parent table \`${table.parentTableName}\` not found for \`${table.tableName}\``, ); } return _.merge(_.cloneDeep(parent_table), { tableName: table.tableName, tableDescription: table.tableDescription, }); }