Document typedefs better. Fix tables deletion schema sync issue.
This commit is contained in:
parent
01b96d788e
commit
259a684136
2
dist/commands/schema.js
vendored
2
dist/commands/schema.js
vendored
@ -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")
|
||||
.description("Build DB From Schema")
|
||||
@ -33,6 +34,7 @@ export default function () {
|
||||
config,
|
||||
});
|
||||
}
|
||||
writeLiveSchema({ schema: finaldbSchema });
|
||||
console.log(`${chalk.bold(chalk.green(`DB Schema setup success!`))}`);
|
||||
process.exit();
|
||||
});
|
||||
|
||||
12
dist/data/grab-dir-names.d.ts
vendored
12
dist/data/grab-dir-names.d.ts
vendored
@ -1,3 +1,11 @@
|
||||
export default function grabDirNames(): {
|
||||
ROOT_DIR: string;
|
||||
import type { BunSQLiteConfig } from "../types";
|
||||
type Params = {
|
||||
config?: BunSQLiteConfig;
|
||||
};
|
||||
export default function grabDirNames(params?: Params): {
|
||||
ROOT_DIR: string;
|
||||
BUN_SQLITE_DIR: string;
|
||||
BUN_SQLITE_TEMP_DIR: string;
|
||||
BUN_SQLITE_LIVE_SCHEMA: string;
|
||||
};
|
||||
export {};
|
||||
|
||||
8
dist/data/grab-dir-names.js
vendored
8
dist/data/grab-dir-names.js
vendored
@ -1,7 +1,13 @@
|
||||
import path from "path";
|
||||
export default function grabDirNames() {
|
||||
export default function grabDirNames(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,
|
||||
};
|
||||
}
|
||||
|
||||
7
dist/functions/live-schema.d.ts
vendored
Normal file
7
dist/functions/live-schema.d.ts
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
import type { BUN_SQLITE_DatabaseSchemaType } from "../types";
|
||||
type Params = {
|
||||
schema: BUN_SQLITE_DatabaseSchemaType;
|
||||
};
|
||||
export declare function writeLiveSchema({ schema }: Params): void;
|
||||
export declare function readLiveSchema(): BUN_SQLITE_DatabaseSchemaType | undefined;
|
||||
export {};
|
||||
15
dist/functions/live-schema.js
vendored
Normal file
15
dist/functions/live-schema.js
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
||||
import grabDirNames from "../data/grab-dir-names";
|
||||
import path from "path";
|
||||
const { BUN_SQLITE_LIVE_SCHEMA } = grabDirNames();
|
||||
export function writeLiveSchema({ schema }) {
|
||||
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);
|
||||
}
|
||||
14
dist/lib/sqlite/db-schema-manager.js
vendored
14
dist/lib/sqlite/db-schema-manager.js
vendored
@ -3,6 +3,7 @@ import { Database } from "bun:sqlite";
|
||||
import _ from "lodash";
|
||||
import DbClient from ".";
|
||||
import { AppData } from "../../data/app-data";
|
||||
import { readLiveSchema } from "../../functions/live-schema";
|
||||
// Schema Manager Class
|
||||
class SQLiteSchemaManager {
|
||||
db;
|
||||
@ -60,8 +61,21 @@ class SQLiteSchemaManager {
|
||||
* Drop tables that are no longer in the schema
|
||||
*/
|
||||
async dropRemovedTables(existingTables, schemaTables) {
|
||||
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}"`);
|
||||
|
||||
403
dist/types/index.d.ts
vendored
403
dist/types/index.d.ts
vendored
File diff suppressed because it is too large
Load Diff
42
dist/types/index.js
vendored
42
dist/types/index.js
vendored
@ -1,3 +1,7 @@
|
||||
/**
|
||||
* User fields that should be omitted from general-purpose payloads and public
|
||||
* responses.
|
||||
*/
|
||||
export const UsersOmitedFields = [
|
||||
"password",
|
||||
"social_id",
|
||||
@ -9,10 +13,17 @@ export const UsersOmitedFields = [
|
||||
"date_updated_code",
|
||||
"date_updated_timestamp",
|
||||
];
|
||||
/**
|
||||
* Supported MariaDB collations that can be applied at the database or table
|
||||
* level.
|
||||
*/
|
||||
export const MariaDBCollations = [
|
||||
"utf8mb4_bin",
|
||||
"utf8mb4_unicode_520_ci",
|
||||
];
|
||||
/**
|
||||
* Supported editor/content modes for text fields.
|
||||
*/
|
||||
export const TextFieldTypesArray = [
|
||||
{ title: "Plain Text", value: "plain" },
|
||||
{ title: "Rich Text", value: "richText" },
|
||||
@ -25,13 +36,22 @@ export const TextFieldTypesArray = [
|
||||
{ title: "Shell", value: "shell" },
|
||||
{ title: "Code", value: "code" },
|
||||
];
|
||||
/**
|
||||
* Core SQLite column types supported by the schema builder.
|
||||
*/
|
||||
export const BUN_SQLITE_DATATYPES = [
|
||||
{ value: "TEXT" },
|
||||
{ value: "INTEGER" },
|
||||
{ value: "BLOB" },
|
||||
{ value: "REAL" },
|
||||
];
|
||||
/**
|
||||
* Supported logical operators for server-side query generation.
|
||||
*/
|
||||
export const ServerQueryOperators = ["AND", "OR"];
|
||||
/**
|
||||
* Supported comparison operators for server-side query generation.
|
||||
*/
|
||||
export const ServerQueryEqualities = [
|
||||
"EQUAL",
|
||||
"LIKE",
|
||||
@ -60,6 +80,9 @@ export const ServerQueryEqualities = [
|
||||
"MATCH",
|
||||
"MATCH_BOOLEAN",
|
||||
];
|
||||
/**
|
||||
* Uppercase HTTP methods supported by the CRUD helpers.
|
||||
*/
|
||||
export const DataCrudRequestMethods = [
|
||||
"GET",
|
||||
"POST",
|
||||
@ -68,6 +91,10 @@ export const DataCrudRequestMethods = [
|
||||
"DELETE",
|
||||
"OPTIONS",
|
||||
];
|
||||
/**
|
||||
* Lowercase variant of the supported HTTP methods, used where string casing
|
||||
* must match external APIs.
|
||||
*/
|
||||
export const DataCrudRequestMethodsLowerCase = [
|
||||
"get",
|
||||
"post",
|
||||
@ -76,7 +103,13 @@ export const DataCrudRequestMethodsLowerCase = [
|
||||
"delete",
|
||||
"options",
|
||||
];
|
||||
/**
|
||||
* High-level CRUD actions supported by the DSQL helpers.
|
||||
*/
|
||||
export const DsqlCrudActions = ["insert", "update", "delete", "get"];
|
||||
/**
|
||||
* Reserved query parameter names used throughout the API layer.
|
||||
*/
|
||||
export const QueryFields = [
|
||||
"duplicate",
|
||||
"user_id",
|
||||
@ -85,6 +118,9 @@ export const QueryFields = [
|
||||
"table_id",
|
||||
"db_slug",
|
||||
];
|
||||
/**
|
||||
* Supported Docker Compose service names used by the deployment helpers.
|
||||
*/
|
||||
export const DockerComposeServices = [
|
||||
"setup",
|
||||
"cron",
|
||||
@ -102,7 +138,13 @@ export const DockerComposeServices = [
|
||||
"db-cron",
|
||||
"web-app-post-db-setup",
|
||||
];
|
||||
/**
|
||||
* Supported index strategies for generated schemas.
|
||||
*/
|
||||
export const IndexTypes = ["regular", "full_text", "vector"];
|
||||
/**
|
||||
* Default fields automatically suggested for new tables.
|
||||
*/
|
||||
export const DefaultFields = [
|
||||
{
|
||||
fieldName: "id",
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@moduletrace/bun-sqlite",
|
||||
"version": "1.0.23",
|
||||
"version": "1.0.24",
|
||||
"description": "SQLite manager for Bun",
|
||||
"author": "Benjamin Toby",
|
||||
"main": "dist/index.js",
|
||||
|
||||
@ -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!`))}`,
|
||||
);
|
||||
|
||||
@ -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
src/functions/live-schema.ts
Normal file
24
src/functions/live-schema.ts
Normal 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;
|
||||
}
|
||||
@ -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}"`);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user