- 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]>
51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
import type {
|
|
BUN_SQLITE_TableSchemaType,
|
|
CreateDBSchemaParams,
|
|
} from "../../types";
|
|
import { sqlQuoteIdent as q } from "../../utils/sql-safe";
|
|
import createTable from "./create-table";
|
|
import insertDBManagerTable from "./insert-db-manager-table";
|
|
import removeDBManagerTable from "./remove-db-manager-table";
|
|
import syncIndexes from "./sync-indexes";
|
|
import updateTable from "./update-table";
|
|
|
|
type Params = CreateDBSchemaParams & {
|
|
table: BUN_SQLITE_TableSchemaType;
|
|
existing_tables: string[];
|
|
};
|
|
|
|
/**
|
|
* Sync a single table (rename, create or update, then indexes)
|
|
*/
|
|
export default function handleDBSchemaTable(params: Params) {
|
|
const { db, table, existing_tables } = params;
|
|
|
|
let tableExists = existing_tables.includes(table.tableName);
|
|
|
|
// Handle table rename
|
|
if (
|
|
table.tableNameOld &&
|
|
table.tableNameOld !== table.tableName &&
|
|
existing_tables.includes(table.tableNameOld)
|
|
) {
|
|
console.log(
|
|
`Renaming table: ${table.tableNameOld} -> ${table.tableName}`,
|
|
);
|
|
db.run(
|
|
`ALTER TABLE ${q(table.tableNameOld)} RENAME TO ${q(table.tableName)}`,
|
|
);
|
|
insertDBManagerTable({ db, table_name: table.tableName });
|
|
removeDBManagerTable({ db, table_name: table.tableNameOld });
|
|
tableExists = true;
|
|
}
|
|
|
|
if (!tableExists) {
|
|
createTable(params);
|
|
insertDBManagerTable({ db, table_name: table.tableName });
|
|
} else {
|
|
updateTable(params);
|
|
}
|
|
|
|
syncIndexes({ db, table });
|
|
}
|