This commit is contained in:
Benjamin Toby 2026-03-11 05:40:37 +01:00
parent 406759306b
commit e7ee76f572
11 changed files with 40 additions and 23 deletions

View File

@ -30,6 +30,7 @@ export default function () {
dbSchemaToTypeDef({ dbSchemaToTypeDef({
dbSchema: finaldbSchema, dbSchema: finaldbSchema,
dst_file: out_file, dst_file: out_file,
config,
}); });
} }
console.log(`${chalk.bold(chalk.green(`DB Schema setup success!`))}`); console.log(`${chalk.bold(chalk.green(`DB Schema setup success!`))}`);

View File

@ -18,6 +18,7 @@ export default function () {
dbSchemaToTypeDef({ dbSchemaToTypeDef({
dbSchema: finaldbSchema, dbSchema: finaldbSchema,
dst_file: out_file, dst_file: out_file,
config,
}); });
} }
else { else {

View File

@ -1,6 +1,7 @@
import type { BUN_SQLITE_DatabaseSchemaType } from "../../types"; import type { BUN_SQLITE_DatabaseSchemaType, BunSQLiteConfig } from "../../types";
type Params = { type Params = {
dbSchema?: BUN_SQLITE_DatabaseSchemaType; dbSchema: BUN_SQLITE_DatabaseSchemaType;
config: BunSQLiteConfig;
}; };
export default function dbSchemaToType(params?: Params): string[] | undefined; export default function dbSchemaToType({ config, dbSchema, }: Params): string[] | undefined;
export {}; export {};

View File

@ -1,16 +1,14 @@
import _ from "lodash"; import _ from "lodash";
import generateTypeDefinition from "./db-generate-type-defs"; import generateTypeDefinition from "./db-generate-type-defs";
export default function dbSchemaToType(params) { export default function dbSchemaToType({ config, dbSchema, }) {
let datasquirelSchema = params?.dbSchema; let datasquirelSchema = dbSchema;
if (!datasquirelSchema) if (!datasquirelSchema)
return; return;
let tableNames = `export const BunSQLiteTables = [\n${datasquirelSchema.tables let tableNames = `export const BunSQLiteTables = [\n${datasquirelSchema.tables
.map((tbl) => ` "${tbl.tableName}",`) .map((tbl) => ` "${tbl.tableName}",`)
.join("\n")}\n] as const`; .join("\n")}\n] as const`;
const dbTablesSchemas = datasquirelSchema.tables; const dbTablesSchemas = datasquirelSchema.tables;
const defDbName = datasquirelSchema.dbName const defDbName = config.db_name?.toUpperCase().replace(/ |\-/g, "_");
?.toUpperCase()
.replace(/ |\-/g, "_");
const defNames = []; const defNames = [];
const schemas = dbTablesSchemas const schemas = dbTablesSchemas
.map((table) => { .map((table) => {

View File

@ -1,7 +1,8 @@
import type { BUN_SQLITE_DatabaseSchemaType } from "../../types"; import type { BUN_SQLITE_DatabaseSchemaType, BunSQLiteConfig } from "../../types";
type Params = { type Params = {
dbSchema: BUN_SQLITE_DatabaseSchemaType; dbSchema: BUN_SQLITE_DatabaseSchemaType;
dst_file: string; dst_file: string;
config: BunSQLiteConfig;
}; };
export default function dbSchemaToTypeDef({ dbSchema, dst_file }: Params): void; export default function dbSchemaToTypeDef({ dbSchema, dst_file, config, }: Params): void;
export {}; export {};

View File

@ -1,11 +1,11 @@
import path from "node:path"; import path from "node:path";
import { existsSync, mkdirSync, writeFileSync } from "node:fs"; import { existsSync, mkdirSync, writeFileSync } from "node:fs";
import dbSchemaToType from "./db-schema-to-typedef"; import dbSchemaToType from "./db-schema-to-typedef";
export default function dbSchemaToTypeDef({ dbSchema, dst_file }) { export default function dbSchemaToTypeDef({ dbSchema, dst_file, config, }) {
try { try {
if (!dbSchema) if (!dbSchema)
throw new Error("No schema found"); throw new Error("No schema found");
const definitions = dbSchemaToType({ dbSchema }); const definitions = dbSchemaToType({ dbSchema, config });
const ourfileDir = path.dirname(dst_file); const ourfileDir = path.dirname(dst_file);
if (!existsSync(ourfileDir)) { if (!existsSync(ourfileDir)) {
mkdirSync(ourfileDir, { recursive: true }); mkdirSync(ourfileDir, { recursive: true });

View File

@ -1,6 +1,6 @@
{ {
"name": "@moduletrace/bun-sqlite", "name": "@moduletrace/bun-sqlite",
"version": "1.0.9", "version": "1.0.10",
"description": "SQLite manager for Bun", "description": "SQLite manager for Bun",
"author": "Benjamin Toby", "author": "Benjamin Toby",
"main": "dist/index.js", "main": "dist/index.js",

View File

@ -44,6 +44,7 @@ export default function () {
dbSchemaToTypeDef({ dbSchemaToTypeDef({
dbSchema: finaldbSchema, dbSchema: finaldbSchema,
dst_file: out_file, dst_file: out_file,
config,
}); });
} }

View File

@ -25,6 +25,7 @@ export default function () {
dbSchemaToTypeDef({ dbSchemaToTypeDef({
dbSchema: finaldbSchema, dbSchema: finaldbSchema,
dst_file: out_file, dst_file: out_file,
config,
}); });
} else { } else {
console.error(``); console.error(``);

View File

@ -1,13 +1,20 @@
import _ from "lodash"; import _ from "lodash";
import type { BUN_SQLITE_DatabaseSchemaType } from "../../types"; import type {
BUN_SQLITE_DatabaseSchemaType,
BunSQLiteConfig,
} from "../../types";
import generateTypeDefinition from "./db-generate-type-defs"; import generateTypeDefinition from "./db-generate-type-defs";
type Params = { type Params = {
dbSchema?: BUN_SQLITE_DatabaseSchemaType; dbSchema: BUN_SQLITE_DatabaseSchemaType;
config: BunSQLiteConfig;
}; };
export default function dbSchemaToType(params?: Params): string[] | undefined { export default function dbSchemaToType({
let datasquirelSchema = params?.dbSchema; config,
dbSchema,
}: Params): string[] | undefined {
let datasquirelSchema = dbSchema;
if (!datasquirelSchema) return; if (!datasquirelSchema) return;
@ -17,9 +24,7 @@ export default function dbSchemaToType(params?: Params): string[] | undefined {
const dbTablesSchemas = datasquirelSchema.tables; const dbTablesSchemas = datasquirelSchema.tables;
const defDbName = datasquirelSchema.dbName const defDbName = config.db_name?.toUpperCase().replace(/ |\-/g, "_");
?.toUpperCase()
.replace(/ |\-/g, "_");
const defNames: string[] = []; const defNames: string[] = [];

View File

@ -1,18 +1,26 @@
import path from "node:path"; import path from "node:path";
import { existsSync, mkdirSync, writeFileSync } from "node:fs"; import { existsSync, mkdirSync, writeFileSync } from "node:fs";
import type { BUN_SQLITE_DatabaseSchemaType } from "../../types"; import type {
BUN_SQLITE_DatabaseSchemaType,
BunSQLiteConfig,
} from "../../types";
import dbSchemaToType from "./db-schema-to-typedef"; import dbSchemaToType from "./db-schema-to-typedef";
type Params = { type Params = {
dbSchema: BUN_SQLITE_DatabaseSchemaType; dbSchema: BUN_SQLITE_DatabaseSchemaType;
dst_file: string; dst_file: string;
config: BunSQLiteConfig;
}; };
export default function dbSchemaToTypeDef({ dbSchema, dst_file }: Params) { export default function dbSchemaToTypeDef({
dbSchema,
dst_file,
config,
}: Params) {
try { try {
if (!dbSchema) throw new Error("No schema found"); if (!dbSchema) throw new Error("No schema found");
const definitions = dbSchemaToType({ dbSchema }); const definitions = dbSchemaToType({ dbSchema, config });
const ourfileDir = path.dirname(dst_file); const ourfileDir = path.dirname(dst_file);