This commit is contained in:
2026-03-08 08:32:12 +01:00
parent 9e922be199
commit c7f2f22b32
38 changed files with 222 additions and 445 deletions
+4 -4
View File
@@ -1,4 +1,4 @@
#!/usr/bin/env bun
#!/usr/bin/env node
import { program } from "commander";
import schema from "./schema";
@@ -15,9 +15,9 @@ declare global {}
* # Describe Program
*/
program
.name(`bun-sqlite`)
.description(`SQLite manager for Bun`)
.version(`1.0.0`);
.name(`nsqlite`)
.description(`SQLite manager for Node JS`)
.version(`1.1.0`);
/**
* # Declare Commands
+1 -1
View File
@@ -1,5 +1,5 @@
export const AppData = {
ConfigFileName: "bun-sqlite.config.ts",
ConfigFileName: "nsqlite.config.ts",
MaxBackups: 10,
DefaultBackupDirName: ".backups",
} as const;
+6 -6
View File
@@ -3,12 +3,12 @@ import fs from "fs";
import { AppData } from "../data/app-data";
import grabDirNames from "../data/grab-dir-names";
import type {
BunSQLiteConfig,
BunSQLiteConfigReturn,
BUN_SQLITE_DatabaseSchemaType,
NSQLiteConfig,
NSQLiteConfigReturn,
NSQLITE_DatabaseSchemaType,
} from "../types";
export default function init(): BunSQLiteConfigReturn {
export default function init(): NSQLiteConfigReturn {
try {
const { ROOT_DIR } = grabDirNames();
const { ConfigFileName } = AppData;
@@ -25,7 +25,7 @@ export default function init(): BunSQLiteConfigReturn {
}
const ConfigImport = require(ConfigFilePath);
const Config = ConfigImport["default"] as BunSQLiteConfig;
const Config = ConfigImport["default"] as NSQLiteConfig;
if (!Config.db_name) {
console.error(`\`db_name\` is required in your config`);
@@ -51,7 +51,7 @@ export default function init(): BunSQLiteConfigReturn {
const DbSchemaImport = require(DBSchemaFilePath);
const DbSchema = DbSchemaImport[
"default"
] as BUN_SQLITE_DatabaseSchemaType;
] as NSQLITE_DatabaseSchemaType;
const backup_dir =
Config.db_backup_dir || AppData["DefaultBackupDirName"];
+6 -6
View File
@@ -1,11 +1,11 @@
import type {
BUN_SQLITE_FieldSchemaType,
BUN_SQLITE_TableSchemaType,
NSQLITE_FieldSchemaType,
NSQLITE_TableSchemaType,
} from "../../types";
type Param = {
paradigm: "JavaScript" | "TypeScript" | undefined;
table: BUN_SQLITE_TableSchemaType;
table: NSQLITE_TableSchemaType;
query?: any;
typeDefName?: string;
allValuesOptional?: boolean;
@@ -29,12 +29,12 @@ export default function generateTypeDefinition({
tdName = typeDefName
? typeDefName
: dbName
? `BUN_SQLITE_${dbName}_${table.tableName}`.toUpperCase()
: `BUN_SQLITE_${query.single}_${query.single_table}`.toUpperCase();
? `NSQLITE_${dbName}_${table.tableName}`.toUpperCase()
: `NSQLITE_${query.single}_${query.single_table}`.toUpperCase();
const fields = table.fields;
function typeMap(schemaType: BUN_SQLITE_FieldSchemaType) {
function typeMap(schemaType: NSQLITE_FieldSchemaType) {
if (schemaType.options && schemaType.options.length > 0) {
return schemaType.options
.map((opt) =>
+16 -26
View File
@@ -1,12 +1,12 @@
#!/usr/bin/env bun
#!/usr/bin/env node
import { type Database } from "better-sqlite3";
import _ from "lodash";
import DbClient from ".";
import type {
BUN_SQLITE_DatabaseSchemaType,
BUN_SQLITE_FieldSchemaType,
BUN_SQLITE_TableSchemaType,
NSQLITE_DatabaseSchemaType,
NSQLITE_FieldSchemaType,
NSQLITE_TableSchemaType,
} from "../../types";
// Schema Manager Class
@@ -14,13 +14,13 @@ class SQLiteSchemaManager {
private db: Database;
private db_manager_table_name: string;
private recreate_vector_table: boolean;
private db_schema: BUN_SQLITE_DatabaseSchemaType;
private db_schema: NSQLITE_DatabaseSchemaType;
constructor({
schema,
recreate_vector_table = false,
}: {
schema: BUN_SQLITE_DatabaseSchemaType;
schema: NSQLITE_DatabaseSchemaType;
recreate_vector_table?: boolean;
}) {
this.db = DbClient;
@@ -115,7 +115,7 @@ class SQLiteSchemaManager {
* Sync a single table (create or update)
*/
private async syncTable(
table: BUN_SQLITE_TableSchemaType,
table: NSQLITE_TableSchemaType,
existingTables: string[],
): Promise<void> {
let tableExists = existingTables.includes(table.tableName);
@@ -151,9 +151,7 @@ class SQLiteSchemaManager {
/**
* Create a new table
*/
private async createTable(
table: BUN_SQLITE_TableSchemaType,
): Promise<void> {
private async createTable(table: NSQLITE_TableSchemaType): Promise<void> {
console.log(`Creating table: ${table.tableName}`);
let new_table = _.cloneDeep(table);
@@ -219,9 +217,7 @@ class SQLiteSchemaManager {
/**
* Update an existing table
*/
private async updateTable(
table: BUN_SQLITE_TableSchemaType,
): Promise<void> {
private async updateTable(table: NSQLITE_TableSchemaType): Promise<void> {
console.log(`Updating table: ${table.tableName}`);
const existingColumns = this.getTableColumns(table.tableName);
@@ -278,7 +274,7 @@ class SQLiteSchemaManager {
*/
private async addColumn(
tableName: string,
field: BUN_SQLITE_FieldSchemaType,
field: NSQLITE_FieldSchemaType,
): Promise<void> {
console.log(`Adding column: ${tableName}.${field.fieldName}`);
@@ -298,9 +294,7 @@ class SQLiteSchemaManager {
/**
* Recreate table (for complex schema changes)
*/
private async recreateTable(
table: BUN_SQLITE_TableSchemaType,
): Promise<void> {
private async recreateTable(table: NSQLITE_TableSchemaType): Promise<void> {
if (table.isVector) {
if (!this.recreate_vector_table) {
return;
@@ -374,7 +368,7 @@ class SQLiteSchemaManager {
/**
* Build column definition SQL
*/
private buildColumnDefinition(field: BUN_SQLITE_FieldSchemaType): string {
private buildColumnDefinition(field: NSQLITE_FieldSchemaType): string {
if (!field.fieldName) {
throw new Error("Field name is required");
}
@@ -429,7 +423,7 @@ class SQLiteSchemaManager {
/**
* Map DSQL data types to SQLite types
*/
private mapDataType(field: BUN_SQLITE_FieldSchemaType): string {
private mapDataType(field: NSQLITE_FieldSchemaType): string {
const dataType = field.dataType?.toLowerCase() || "text";
const vectorSize = field.vectorSize || 1536;
@@ -481,9 +475,7 @@ class SQLiteSchemaManager {
/**
* Build foreign key constraint
*/
private buildForeignKeyConstraint(
field: BUN_SQLITE_FieldSchemaType,
): string {
private buildForeignKeyConstraint(field: NSQLITE_FieldSchemaType): string {
const fk = field.foreignKey!;
let constraint = `FOREIGN KEY ("${field.fieldName}") REFERENCES "${fk.destinationTableName}"("${fk.destinationTableColumnName}")`;
@@ -501,9 +493,7 @@ class SQLiteSchemaManager {
/**
* Sync indexes for a table
*/
private async syncIndexes(
table: BUN_SQLITE_TableSchemaType,
): Promise<void> {
private async syncIndexes(table: NSQLITE_TableSchemaType): Promise<void> {
if (!table.indexes || table.indexes.length === 0) {
return;
}
@@ -560,7 +550,7 @@ class SQLiteSchemaManager {
// Example usage
async function main() {
const schema: BUN_SQLITE_DatabaseSchemaType = {
const schema: NSQLITE_DatabaseSchemaType = {
dbName: "example_db",
tables: [
{
+5 -5
View File
@@ -1,9 +1,9 @@
import _ from "lodash";
import type { BUN_SQLITE_DatabaseSchemaType } from "../../types";
import type { NSQLITE_DatabaseSchemaType } from "../../types";
import generateTypeDefinition from "./db-generate-type-defs";
type Params = {
dbSchema?: BUN_SQLITE_DatabaseSchemaType;
dbSchema?: NSQLITE_DatabaseSchemaType;
};
export default function dbSchemaToType(params?: Params): string[] | undefined {
@@ -11,7 +11,7 @@ export default function dbSchemaToType(params?: Params): string[] | undefined {
if (!datasquirelSchema) return;
let tableNames = `export const BunSQLiteTables = [\n${datasquirelSchema.tables
let tableNames = `export const NSQLiteTables = [\n${datasquirelSchema.tables
.map((tbl) => ` "${tbl.tableName}",`)
.join("\n")}\n] as const`;
@@ -43,7 +43,7 @@ export default function dbSchemaToType(params?: Params): string[] | undefined {
const defObj = generateTypeDefinition({
paradigm: "TypeScript",
table: final_table,
typeDefName: `BUN_SQLITE_${defDbName}_${final_table.tableName.toUpperCase()}`,
typeDefName: `NSQLITE_${defDbName}_${final_table.tableName.toUpperCase()}`,
allValuesOptional: true,
addExport: true,
});
@@ -57,7 +57,7 @@ export default function dbSchemaToType(params?: Params): string[] | undefined {
.filter((schm) => typeof schm == "string");
const allTd = defNames?.[0]
? `export type BUN_SQLITE_${defDbName}_ALL_TYPEDEFS = ${defNames.join(` & `)}`
? `export type NSQLITE_${defDbName}_ALL_TYPEDEFS = ${defNames.join(` & `)}`
: ``;
return [tableNames, ...schemas, allTd];
+2 -2
View File
@@ -1,10 +1,10 @@
import path from "node:path";
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
import type { BUN_SQLITE_DatabaseSchemaType } from "../../types";
import type { NSQLITE_DatabaseSchemaType } from "../../types";
import dbSchemaToType from "./db-schema-to-typedef";
type Params = {
dbSchema: BUN_SQLITE_DatabaseSchemaType;
dbSchema: NSQLITE_DatabaseSchemaType;
dst_file: string;
};
+2 -2
View File
@@ -1,7 +1,7 @@
import _ from "lodash";
import type { BUN_SQLITE_DatabaseSchemaType } from "../../types";
import type { NSQLITE_DatabaseSchemaType } from "../../types";
export const DbSchema: BUN_SQLITE_DatabaseSchemaType = {
export const DbSchema: NSQLITE_DatabaseSchemaType = {
dbName: "travis-ai",
tables: [],
};
+39 -39
View File
@@ -1,6 +1,6 @@
import type { RequestOptions } from "https";
export type BUN_SQLITE_DatabaseFullName = string;
export type NSQLITE_DatabaseFullName = string;
export const UsersOmitedFields = [
"password",
@@ -14,22 +14,22 @@ export const UsersOmitedFields = [
"date_updated_timestamp",
] as const;
export interface BUN_SQLITE_DatabaseSchemaType {
export interface NSQLITE_DatabaseSchemaType {
id?: string | number;
dbName?: string;
dbSlug?: string;
dbFullName?: string;
dbDescription?: string;
dbImage?: string;
tables: BUN_SQLITE_TableSchemaType[];
childrenDatabases?: BUN_SQLITE_ChildrenDatabaseObject[];
tables: NSQLITE_TableSchemaType[];
childrenDatabases?: NSQLITE_ChildrenDatabaseObject[];
childDatabase?: boolean;
childDatabaseDbId?: string | number;
updateData?: boolean;
collation?: (typeof MariaDBCollations)[number];
}
export interface BUN_SQLITE_ChildrenDatabaseObject {
export interface NSQLITE_ChildrenDatabaseObject {
dbId?: string | number;
}
@@ -38,14 +38,14 @@ export const MariaDBCollations = [
"utf8mb4_unicode_520_ci",
] as const;
export interface BUN_SQLITE_TableSchemaType {
export interface NSQLITE_TableSchemaType {
id?: string | number;
tableName: string;
tableDescription?: string;
fields: BUN_SQLITE_FieldSchemaType[];
indexes?: BUN_SQLITE_IndexSchemaType[];
uniqueConstraints?: BUN_SQLITE_UniqueConstraintSchemaType[];
childrenTables?: BUN_SQLITE_ChildrenTablesType[];
fields: NSQLITE_FieldSchemaType[];
indexes?: NSQLITE_IndexSchemaType[];
uniqueConstraints?: NSQLITE_UniqueConstraintSchemaType[];
childrenTables?: NSQLITE_ChildrenTablesType[];
/**
* Whether this is a child table
*/
@@ -81,7 +81,7 @@ export interface BUN_SQLITE_TableSchemaType {
vectorType?: string;
}
export interface BUN_SQLITE_ChildrenTablesType {
export interface NSQLITE_ChildrenTablesType {
tableId?: string | number;
dbId?: string | number;
}
@@ -99,18 +99,18 @@ export const TextFieldTypesArray = [
{ title: "Code", value: "code" },
] as const;
export const BUN_SQLITE_DATATYPES = [
export const NSQLITE_DATATYPES = [
{ value: "TEXT" },
{ value: "INTEGER" },
] as const;
export type BUN_SQLITE_FieldSchemaType = {
export type NSQLITE_FieldSchemaType = {
id?: number | string;
fieldName?: string;
fieldDescription?: string;
originName?: string;
updatedField?: boolean;
dataType: (typeof BUN_SQLITE_DATATYPES)[number]["value"];
dataType: (typeof NSQLITE_DATATYPES)[number]["value"];
nullValue?: boolean;
notNullValue?: boolean;
primaryKey?: boolean;
@@ -118,7 +118,7 @@ export type BUN_SQLITE_FieldSchemaType = {
autoIncrement?: boolean;
defaultValue?: string | number;
defaultValueLiteral?: string;
foreignKey?: BUN_SQLITE_ForeignKeyType;
foreignKey?: NSQLITE_ForeignKeyType;
defaultField?: boolean;
plainText?: boolean;
unique?: boolean;
@@ -159,7 +159,7 @@ CREATE VIRTUAL TABLE documents USING vec0(
-- INSERTING (Notice: No '+' here)
INSERT INTO documents(embedding, title, raw_body)
VALUES (vec_f32(?), 'Bun Docs', 'Bun is a fast JavaScript runtime...');
VALUES (vec_f32(?), 'Node Docs', 'Node is a fast JavaScript runtime...');
-- QUERYING (Notice: No '+' here)
SELECT title, raw_body
@@ -172,7 +172,7 @@ WHERE embedding MATCH ? AND k = 1;
[key in (typeof TextFieldTypesArray)[number]["value"]]?: boolean;
};
export interface BUN_SQLITE_ForeignKeyType {
export interface NSQLITE_ForeignKeyType {
foreignKeyName?: string;
destinationTableName?: string;
destinationTableColumnName?: string;
@@ -181,32 +181,32 @@ export interface BUN_SQLITE_ForeignKeyType {
cascadeUpdate?: boolean;
}
export interface BUN_SQLITE_IndexSchemaType {
export interface NSQLITE_IndexSchemaType {
id?: string | number;
indexName?: string;
indexType?: (typeof IndexTypes)[number];
indexTableFields?: BUN_SQLITE_IndexTableFieldType[];
indexTableFields?: NSQLITE_IndexTableFieldType[];
alias?: string;
newTempIndex?: boolean;
}
export interface BUN_SQLITE_UniqueConstraintSchemaType {
export interface NSQLITE_UniqueConstraintSchemaType {
id?: string | number;
constraintName?: string;
alias?: string;
constraintTableFields?: BUN_SQLITE_UniqueConstraintFieldType[];
constraintTableFields?: NSQLITE_UniqueConstraintFieldType[];
}
export interface BUN_SQLITE_UniqueConstraintFieldType {
export interface NSQLITE_UniqueConstraintFieldType {
value: string;
}
export interface BUN_SQLITE_IndexTableFieldType {
export interface NSQLITE_IndexTableFieldType {
value: string;
dataType: string;
}
export interface BUN_SQLITE_MYSQL_SHOW_INDEXES_Type {
export interface NSQLITE_MYSQL_SHOW_INDEXES_Type {
Key_name: string;
Table: string;
Column_name: string;
@@ -217,7 +217,7 @@ export interface BUN_SQLITE_MYSQL_SHOW_INDEXES_Type {
Comment: string;
}
export interface BUN_SQLITE_MYSQL_SHOW_COLUMNS_Type {
export interface NSQLITE_MYSQL_SHOW_COLUMNS_Type {
Field: string;
Type: string;
Null: string;
@@ -226,7 +226,7 @@ export interface BUN_SQLITE_MYSQL_SHOW_COLUMNS_Type {
Extra: string;
}
export interface BUN_SQLITE_MARIADB_SHOW_INDEXES_TYPE {
export interface NSQLITE_MARIADB_SHOW_INDEXES_TYPE {
Table: string;
Non_unique: 0 | 1;
Key_name: string;
@@ -242,13 +242,13 @@ export interface BUN_SQLITE_MARIADB_SHOW_INDEXES_TYPE {
Ignored?: "YES" | "NO";
}
export interface BUN_SQLITE_MYSQL_FOREIGN_KEYS_Type {
export interface NSQLITE_MYSQL_FOREIGN_KEYS_Type {
CONSTRAINT_NAME: string;
CONSTRAINT_SCHEMA: string;
TABLE_NAME: string;
}
export interface BUN_SQLITE_MYSQL_user_databases_Type {
export interface NSQLITE_MYSQL_user_databases_Type {
id: number;
user_id: number;
db_full_name: string;
@@ -387,7 +387,7 @@ export interface GetReturn<R extends any = any> {
payload?: R;
msg?: string;
error?: string;
schema?: BUN_SQLITE_TableSchemaType;
schema?: NSQLITE_TableSchemaType;
finalQuery?: string;
}
@@ -411,7 +411,7 @@ export interface PostReturn {
payload?: Object[] | string | PostInsertReturn;
msg?: string;
error?: any;
schema?: BUN_SQLITE_TableSchemaType;
schema?: NSQLITE_TableSchemaType;
}
export interface PostDataPayload {
@@ -709,8 +709,8 @@ export type ServerQueryParamsJoinMatchSourceTargetObject = {
export type ApiConnectBody = {
url: string;
key: string;
database: BUN_SQLITE_MYSQL_user_databases_Type;
dbSchema: BUN_SQLITE_DatabaseSchemaType;
database: NSQLITE_MYSQL_user_databases_Type;
dbSchema: NSQLITE_DatabaseSchemaType;
type: "pull" | "push";
user_id?: string | number;
};
@@ -994,7 +994,7 @@ export type APIResponseObject<
countQueryObject?: ResponseQueryObject;
status?: number;
count?: number;
errors?: BUNSQLITEErrorObject[];
errors?: NSQLITEErrorObject[];
debug?: any;
batchPayload?: any[][] | null;
errorData?: any;
@@ -1115,7 +1115,7 @@ export type DefaultEntryType = {
export const IndexTypes = ["regular", "full_text", "vector"] as const;
export type BUNSQLITEErrorObject = {
export type NSQLITEErrorObject = {
sql?: string;
sqlValues?: any[];
error?: string;
@@ -1141,7 +1141,7 @@ export type SQLInsertGenParams = {
dbFullName?: string;
};
export type BunSQLiteConfig = {
export type NSQLiteConfig = {
db_name: string;
/**
* The Name of the Database Schema File. Eg `db_schema.ts`. This is
@@ -1164,12 +1164,12 @@ export type BunSQLiteConfig = {
typedef_file_path?: string;
};
export type BunSQLiteConfigReturn = {
config: BunSQLiteConfig;
dbSchema: BUN_SQLITE_DatabaseSchemaType;
export type NSQLiteConfigReturn = {
config: NSQLiteConfig;
dbSchema: NSQLITE_DatabaseSchemaType;
};
export const DefaultFields: BUN_SQLITE_FieldSchemaType[] = [
export const DefaultFields: NSQLITE_FieldSchemaType[] = [
{
fieldName: "id",
dataType: "INTEGER",
@@ -1,11 +1,11 @@
import _ from "lodash";
import { DefaultFields, type BUN_SQLITE_DatabaseSchemaType } from "../types";
import { DefaultFields, type NSQLITE_DatabaseSchemaType } from "../types";
type Params = {
dbSchema: BUN_SQLITE_DatabaseSchemaType;
dbSchema: NSQLITE_DatabaseSchemaType;
};
export default function ({ dbSchema }: Params): BUN_SQLITE_DatabaseSchemaType {
export default function ({ dbSchema }: Params): NSQLITE_DatabaseSchemaType {
const finaldbSchema = _.cloneDeep(dbSchema);
finaldbSchema.tables = finaldbSchema.tables.map((t) => {
const newTable = _.cloneDeep(t);
+2 -2
View File
@@ -1,7 +1,7 @@
import type { BunSQLiteConfig } from "../types";
import type { NSQLiteConfig } from "../types";
type Params = {
config: BunSQLiteConfig;
config: NSQLiteConfig;
};
export default function grabDBBackupFileName({ config }: Params) {
+2 -2
View File
@@ -1,10 +1,10 @@
import path from "path";
import grabDirNames from "../data/grab-dir-names";
import type { BunSQLiteConfig } from "../types";
import type { NSQLiteConfig } from "../types";
import { AppData } from "../data/app-data";
type Params = {
config: BunSQLiteConfig;
config: NSQLiteConfig;
};
export default function grabDBDir({ config }: Params) {
+2 -2
View File
@@ -1,9 +1,9 @@
import grabDBDir from "../utils/grab-db-dir";
import fs from "fs";
import type { BunSQLiteConfig } from "../types";
import type { NSQLiteConfig } from "../types";
type Params = {
config: BunSQLiteConfig;
config: NSQLiteConfig;
};
export default function grabSortedBackups({ config }: Params) {
+2 -2
View File
@@ -1,12 +1,12 @@
import grabDBDir from "../utils/grab-db-dir";
import fs from "fs";
import type { BunSQLiteConfig } from "../types";
import type { NSQLiteConfig } from "../types";
import grabSortedBackups from "./grab-sorted-backups";
import { AppData } from "../data/app-data";
import path from "path";
type Params = {
config: BunSQLiteConfig;
config: NSQLiteConfig;
};
export default function trimBackups({ config }: Params) {