- init() loads config, schema and the SQLite client once into globals - all CRUD queries go through lib/db-handler.ts (optional per-call config) - split db-schema-manager.ts into lib/schema/* with FK-aware table ordering - add bun:test tests for schema ordering, FK constraints and schema sync - config/schema files can be .ts/.js/.json/.yaml/.yml; db_schema_file_name is now optional (defaults to schema.* in db_dir) - add export/import commands (database snapshot + schema archives) - package exports map, type re-exports, hardened publish.sh - validate table name in delete Co-Authored-By: Claude Opus 5.5 <[email protected]>
40 lines
1017 B
TypeScript
40 lines
1017 B
TypeScript
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,
|
|
});
|
|
}
|