Document typedefs better. Fix tables deletion schema sync issue.

This commit is contained in:
2026-04-05 09:03:47 +01:00
parent 01b96d788e
commit 259a684136
14 changed files with 869 additions and 104 deletions
+3
View File
@@ -7,6 +7,7 @@ import dbSchemaToTypeDef from "../lib/sqlite/schema-to-typedef";
import _ from "lodash";
import appendDefaultFieldsToDbSchema from "../utils/append-default-fields-to-db-schema";
import chalk from "chalk";
import { writeLiveSchema } from "../functions/live-schema";
export default function () {
return new Command("schema")
@@ -48,6 +49,8 @@ export default function () {
});
}
writeLiveSchema({ schema: finaldbSchema });
console.log(
`${chalk.bold(chalk.green(`DB Schema setup success!`))}`,
);
+15 -1
View File
@@ -1,9 +1,23 @@
import path from "path";
import type { BunSQLiteConfig } from "../types";
export default function grabDirNames() {
type Params = {
config?: BunSQLiteConfig;
};
export default function grabDirNames(params?: Params) {
const ROOT_DIR = process.cwd();
const BUN_SQLITE_DIR = path.join(ROOT_DIR, ".bun-sqlite");
const BUN_SQLITE_TEMP_DIR = path.join(BUN_SQLITE_DIR, ".tmp");
const BUN_SQLITE_LIVE_SCHEMA = path.join(
BUN_SQLITE_DIR,
"live-schema.json",
);
return {
ROOT_DIR,
BUN_SQLITE_DIR,
BUN_SQLITE_TEMP_DIR,
BUN_SQLITE_LIVE_SCHEMA,
};
}
+24
View File
@@ -0,0 +1,24 @@
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
import grabDirNames from "../data/grab-dir-names";
import type { BUN_SQLITE_DatabaseSchemaType } from "../types";
import path from "path";
const { BUN_SQLITE_LIVE_SCHEMA } = grabDirNames();
type Params = {
schema: BUN_SQLITE_DatabaseSchemaType;
};
export function writeLiveSchema({ schema }: Params) {
mkdirSync(path.dirname(BUN_SQLITE_LIVE_SCHEMA), { recursive: true });
writeFileSync(BUN_SQLITE_LIVE_SCHEMA, JSON.stringify(schema));
}
export function readLiveSchema() {
if (!existsSync(BUN_SQLITE_LIVE_SCHEMA)) {
return undefined;
}
const live_schema = readFileSync(BUN_SQLITE_LIVE_SCHEMA, "utf-8");
return JSON.parse(live_schema) as BUN_SQLITE_DatabaseSchemaType;
}
+21
View File
@@ -9,6 +9,7 @@ import type {
BUN_SQLITE_TableSchemaType,
} from "../../types";
import { AppData } from "../../data/app-data";
import { readLiveSchema } from "../../functions/live-schema";
// Schema Manager Class
class SQLiteSchemaManager {
@@ -95,12 +96,32 @@ class SQLiteSchemaManager {
existingTables: string[],
schemaTables: string[],
): Promise<void> {
console.log(`Cleaning up tables ...`);
const tablesToDrop = existingTables.filter(
(t) =>
!schemaTables.includes(t) &&
!schemaTables.find((scT) => t.startsWith(scT + "_")),
);
const currentSchema = readLiveSchema();
if (currentSchema?.tables?.[0]) {
for (let i = 0; i < currentSchema.tables.length; i++) {
const table = currentSchema.tables[i];
if (!table?.tableName) continue;
const does_table_exist = schemaTables.find(
(t) => t == table.tableName,
);
if (!does_table_exist) {
tablesToDrop.push(table.tableName);
}
}
}
for (const tableName of tablesToDrop) {
console.log(`Dropping table: ${tableName}`);
this.db.run(`DROP TABLE IF EXISTS "${tableName}"`);
+354 -50
View File
File diff suppressed because it is too large Load Diff