first Commit

This commit is contained in:
Benjamin Toby 2026-03-02 07:56:05 +01:00
commit 5555a8e917
19 changed files with 1398 additions and 0 deletions

34
.gitignore vendored Normal file
View File

@ -0,0 +1,34 @@
# dependencies (bun install)
node_modules
# output
out
dist
*.tgz
# code coverage
coverage
*.lcov
# logs
logs
_.log
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# caches
.eslintcache
.cache
*.tsbuildinfo
# IntelliJ based IDEs
.idea
# Finder (MacOS) folder config
.DS_Store

3
.gitignore copy Normal file
View File

@ -0,0 +1,3 @@
.env
.data
node_modules

1
.npmrc Normal file
View File

@ -0,0 +1 @@
@moduletrace:registry=https://git.tben.me/api/packages/moduletrace/npm/

106
CLAUDE.md Normal file
View File

@ -0,0 +1,106 @@
Default to using Bun instead of Node.js.
- Use `bun <file>` instead of `node <file>` or `ts-node <file>`
- Use `bun test` instead of `jest` or `vitest`
- Use `bun build <file.html|file.ts|file.css>` instead of `webpack` or `esbuild`
- Use `bun install` instead of `npm install` or `yarn install` or `pnpm install`
- Use `bun run <script>` instead of `npm run <script>` or `yarn run <script>` or `pnpm run <script>`
- Bun automatically loads .env, so don't use dotenv.
## APIs
- `Bun.serve()` supports WebSockets, HTTPS, and routes. Don't use `express`.
- `bun:sqlite` for SQLite. Don't use `better-sqlite3`.
- `Bun.redis` for Redis. Don't use `ioredis`.
- `Bun.sql` for Postgres. Don't use `pg` or `postgres.js`.
- `WebSocket` is built-in. Don't use `ws`.
- Prefer `Bun.file` over `node:fs`'s readFile/writeFile
- Bun.$`ls` instead of execa.
## Testing
Use `bun test` to run tests.
```ts#index.test.ts
import { test, expect } from "bun:test";
test("hello world", () => {
expect(1).toBe(1);
});
```
## Frontend
Use HTML imports with `Bun.serve()`. Don't use `vite`. HTML imports fully support React, CSS, Tailwind.
Server:
```ts#index.ts
import index from "./index.html"
Bun.serve({
routes: {
"/": index,
"/api/users/:id": {
GET: (req) => {
return new Response(JSON.stringify({ id: req.params.id }));
},
},
},
// optional websocket support
websocket: {
open: (ws) => {
ws.send("Hello, world!");
},
message: (ws, message) => {
ws.send(message);
},
close: (ws) => {
// handle close
}
},
development: {
hmr: true,
console: true,
}
})
```
HTML files can import .tsx, .jsx or .js files directly and Bun's bundler will transpile & bundle automatically. `<link>` tags can point to stylesheets and Bun's CSS bundler will bundle.
```html#index.html
<html>
<body>
<h1>Hello, world!</h1>
<script type="module" src="./frontend.tsx"></script>
</body>
</html>
```
With the following `frontend.tsx`:
```tsx#frontend.tsx
import React from "react";
// import .css files directly and it works
import './index.css';
import { createRoot } from "react-dom/client";
const root = createRoot(document.body);
export default function Frontend() {
return <h1>Hello, world!</h1>;
}
root.render(<Frontend />);
```
Then, run index.ts
```sh
bun --hot ./index.ts
```
For more information, read the Bun API docs in `node_modules/bun-types/docs/**.md`.

15
README.md Normal file
View File

@ -0,0 +1,15 @@
# bun-sqlite
To install dependencies:
```bash
bun install
```
To run:
```bash
bun run index.ts
```
This project was created using `bun init` in bun v1.3.0. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.

25
bun.lock Normal file
View File

@ -0,0 +1,25 @@
{
"lockfileVersion": 1,
"workspaces": {
"": {
"name": "bun-sqlite",
"devDependencies": {
"@types/bun": "latest",
},
"peerDependencies": {
"typescript": "^5",
},
},
},
"packages": {
"@types/bun": ["@types/bun@1.3.9", "", { "dependencies": { "bun-types": "1.3.9" } }, "sha512-KQ571yULOdWJiMH+RIWIOZ7B2RXQGpL1YQrBtLIV3FqDcCu6FsbFUBwhdKUlCKUpS3PJDsHlJ1QKlpxoVR+xtw=="],
"@types/node": ["@types/node@25.3.3", "", { "dependencies": { "undici-types": "~7.18.0" } }, "sha512-DpzbrH7wIcBaJibpKo9nnSQL0MTRdnWttGyE5haGwK86xgMOkFLp7vEyfQPGLOJh5wNYiJ3V9PmUMDhV9u8kkQ=="],
"bun-types": ["bun-types@1.3.9", "", { "dependencies": { "@types/node": "*" } }, "sha512-+UBWWOakIP4Tswh0Bt0QD0alpTY8cb5hvgiYeWCMet9YukHbzuruIEeXC2D7nMJPB12kbh8C7XJykSexEqGKJg=="],
"typescript": ["typescript@5.9.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw=="],
"undici-types": ["undici-types@7.18.2", "", {}, "sha512-AsuCzffGHJybSaRrmr5eHr81mwJU3kjw6M+uprWvCXiNeN9SOGwQ3Jn8jb8m3Z6izVgknn1R0FTCEAP2QrLY/w=="],
}
}

1
index.ts Normal file
View File

@ -0,0 +1 @@
console.log("Hello via Bun!");

12
package.json Normal file
View File

@ -0,0 +1,12 @@
{
"name": "bun-sqlite",
"module": "index.ts",
"type": "module",
"private": true,
"devDependencies": {
"@types/bun": "latest"
},
"peerDependencies": {
"typescript": "^5"
}
}

View File

@ -0,0 +1,70 @@
import type { DSQL_TRAVIS_AI_ALL_TYPEDEFS, DsqlTables } from "@/types/db";
import datasquirel from "@moduletrace/datasquirel";
import type {
APIResponseObject,
ServerQueryParam,
} from "@moduletrace/datasquirel/dist/package-shared/types";
import DbClient from ".";
import _ from "lodash";
type Params<T extends { [k: string]: any } = DSQL_TRAVIS_AI_ALL_TYPEDEFS> = {
table: (typeof DsqlTables)[number];
query?: ServerQueryParam<T>;
targetId?: number | string;
};
export default async function DbDelete<
T extends { [k: string]: any } = DSQL_TRAVIS_AI_ALL_TYPEDEFS,
>({ table, query, targetId }: Params<T>): Promise<APIResponseObject> {
try {
let finalQuery = query || {};
if (targetId) {
finalQuery = _.merge<ServerQueryParam<any>, ServerQueryParam<any>>(
finalQuery,
{
query: {
id: {
value: String(targetId),
},
},
},
);
}
const sqlQueryObj = datasquirel.sql.sqlGenerator({
tableName: table,
genObject: finalQuery,
});
const whereClause = sqlQueryObj.string.match(/WHERE .*/)?.[0];
if (whereClause) {
let sql = `DELETE FROM ${table} ${whereClause}`;
const res = DbClient.run(sql, sqlQueryObj.values);
return {
success: Boolean(res.changes),
postInsertReturn: {
affectedRows: res.changes,
insertId: Number(res.lastInsertRowid),
},
debug: {
sql,
values: sqlQueryObj.values,
},
};
} else {
return {
success: false,
msg: `No WHERE clause`,
};
}
} catch (error: any) {
return {
success: false,
error: error.message,
};
}
}

View File

@ -0,0 +1,112 @@
import type {
DSQL_FieldSchemaType,
DSQL_TableSchemaType,
} from "@moduletrace/datasquirel/dist/package-shared/types";
type Param = {
paradigm: "JavaScript" | "TypeScript" | undefined;
table: DSQL_TableSchemaType;
query?: any;
typeDefName?: string;
allValuesOptional?: boolean;
addExport?: boolean;
dbName?: string;
};
export default function generateTypeDefinition({
paradigm,
table,
query,
typeDefName,
allValuesOptional,
addExport,
dbName,
}: Param) {
let typeDefinition: string | null = ``;
let tdName: string | null = ``;
try {
tdName = typeDefName
? typeDefName
: dbName
? `DSQL_${dbName}_${table.tableName}`.toUpperCase()
: `DSQL_${query.single}_${query.single_table}`.toUpperCase();
const fields = table.fields;
function typeMap(schemaType: DSQL_FieldSchemaType) {
if (schemaType.options && schemaType.options.length > 0) {
return schemaType.options
.map((opt) =>
schemaType.dataType?.match(/int/i) ||
typeof opt == "number"
? `${opt}`
: `"${opt}"`,
)
.join(" | ");
}
if (schemaType.dataType?.match(/int|double|decimal/i)) {
return "number";
}
if (schemaType.dataType?.match(/text|varchar|timestamp/i)) {
return "string";
}
if (schemaType.dataType?.match(/boolean/i)) {
return "0 | 1";
}
return "string";
}
const typesArrayTypeScript = [];
const typesArrayJavascript = [];
typesArrayTypeScript.push(
`${addExport ? "export " : ""}type ${tdName} = {`,
);
typesArrayJavascript.push(`/**\n * @typedef {object} ${tdName}`);
fields.forEach((field) => {
if (field.fieldDescription) {
typesArrayTypeScript.push(
` /** \n * ${field.fieldDescription}\n */`,
);
}
const nullValue = allValuesOptional
? "?"
: field.notNullValue
? ""
: "?";
typesArrayTypeScript.push(
` ${field.fieldName}${nullValue}: ${typeMap(field)};`,
);
typesArrayJavascript.push(
` * @property {${typeMap(field)}${nullValue}} ${
field.fieldName
}`,
);
});
typesArrayTypeScript.push(`}`);
typesArrayJavascript.push(` */`);
if (paradigm?.match(/javascript/i)) {
typeDefinition = typesArrayJavascript.join("\n");
}
if (paradigm?.match(/typescript/i)) {
typeDefinition = typesArrayTypeScript.join("\n");
}
} catch (error: any) {
console.log(error.message);
typeDefinition = null;
}
return { typeDefinition, tdName };
}

View File

@ -0,0 +1,45 @@
import type { DSQL_TRAVIS_AI_ALL_TYPEDEFS, DsqlTables } from "@/types/db";
import datasquirel from "@moduletrace/datasquirel";
import type { APIResponseObject } from "@moduletrace/datasquirel/dist/package-shared/types";
import DbClient from ".";
import type { DBChanges } from "@/types/general";
type Params<T extends { [k: string]: any } = DSQL_TRAVIS_AI_ALL_TYPEDEFS> = {
table: (typeof DsqlTables)[number];
data: T[];
};
export default async function DbInsert<
T extends { [k: string]: any } = DSQL_TRAVIS_AI_ALL_TYPEDEFS,
>({ table, data }: Params<T>): Promise<APIResponseObject<DBChanges>> {
try {
const finalData: DSQL_TRAVIS_AI_ALL_TYPEDEFS[] = data.map((d) => ({
...d,
created_at: Date.now(),
updated_at: Date.now(),
}));
const sqlObj = datasquirel.sql.sqlInsertGenerator({
tableName: table,
data: finalData as any[],
});
const res = DbClient.run(sqlObj?.query || "", sqlObj?.values || []);
return {
success: Boolean(Number(res.lastInsertRowid)),
postInsertReturn: {
affectedRows: res.changes,
insertId: Number(res.lastInsertRowid),
},
debug: {
sqlObj,
},
};
} catch (error: any) {
return {
success: false,
error: error.message,
};
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,67 @@
import type {
DSQL_DatabaseSchemaType,
DSQL_TableSchemaType,
} from "@moduletrace/datasquirel/dist/package-shared/types";
import _ from "lodash";
import generateTypeDefinition from "./generate-type-definitions";
type Params = {
dbSchema?: DSQL_DatabaseSchemaType;
};
export default function dbSchemaToType(params?: Params): string[] | undefined {
let datasquirelSchema = params?.dbSchema;
if (!datasquirelSchema) return;
let tableNames = `export const DsqlTables = [\n${datasquirelSchema.tables
.map((tbl) => ` "${tbl.tableName}",`)
.join("\n")}\n] as const`;
const dbTablesSchemas = datasquirelSchema.tables;
const defDbName = datasquirelSchema.dbName
?.toUpperCase()
.replace(/ |\-/g, "_");
const defNames: string[] = [];
const schemas = dbTablesSchemas
.map((table) => {
let final_table = _.cloneDeep(table);
if (final_table.parentTableName) {
const parent_table = dbTablesSchemas.find(
(t) => t.tableName === final_table.parentTableName,
);
if (parent_table) {
final_table = _.merge(parent_table, {
tableName: final_table.tableName,
tableDescription: final_table.tableDescription,
});
}
}
const defObj = generateTypeDefinition({
paradigm: "TypeScript",
table: final_table,
typeDefName: `DSQL_${defDbName}_${final_table.tableName.toUpperCase()}`,
allValuesOptional: true,
addExport: true,
});
if (defObj.tdName?.match(/./)) {
defNames.push(defObj.tdName);
}
return defObj.typeDefinition;
})
.filter((schm) => typeof schm == "string");
const allTd = defNames?.[0]
? `export type DSQL_${defDbName}_ALL_TYPEDEFS = ${defNames.join(` & `)}`
: ``;
return [tableNames, ...schemas, allTd];
}

View File

@ -0,0 +1,65 @@
import mysql from "mysql";
import type { DSQL_TRAVIS_AI_ALL_TYPEDEFS, DsqlTables } from "@/types/db";
import datasquirel from "@moduletrace/datasquirel";
import type {
APIResponseObject,
ServerQueryParam,
} from "@moduletrace/datasquirel/dist/package-shared/types";
import DbClient from ".";
import _ from "lodash";
type Params<
T extends DSQL_TRAVIS_AI_ALL_TYPEDEFS = DSQL_TRAVIS_AI_ALL_TYPEDEFS,
> = {
query?: ServerQueryParam<T>;
table: (typeof DsqlTables)[number];
count?: boolean;
targetId?: number | string;
};
export default async function DbSelect<
T extends DSQL_TRAVIS_AI_ALL_TYPEDEFS = DSQL_TRAVIS_AI_ALL_TYPEDEFS,
>({ table, query, count, targetId }: Params<T>): Promise<APIResponseObject<T>> {
try {
let finalQuery = query || {};
if (targetId) {
finalQuery = _.merge<ServerQueryParam<any>, ServerQueryParam<any>>(
finalQuery,
{
query: {
id: {
value: String(targetId),
},
},
},
);
}
const sqlObj = datasquirel.sql.sqlGenerator({
tableName: table,
genObject: finalQuery,
count,
});
const sql = mysql.format(sqlObj.string, sqlObj.values);
const res = DbClient.query<T, T[]>(sql);
const batchRes = res.all();
return {
success: true,
payload: batchRes,
singleRes: batchRes[0],
debug: {
sqlObj,
sql,
},
};
} catch (error: any) {
return {
success: false,
error: error.message,
};
}
}

100
src/lib/sqlite/db-update.ts Normal file
View File

@ -0,0 +1,100 @@
import mysql from "mysql";
import type { DSQL_TRAVIS_AI_ALL_TYPEDEFS, DsqlTables } from "@/types/db";
import datasquirel from "@moduletrace/datasquirel";
import type {
APIResponseObject,
ServerQueryParam,
} from "@moduletrace/datasquirel/dist/package-shared/types";
import DbClient from ".";
import _ from "lodash";
type Params<T extends { [k: string]: any } = DSQL_TRAVIS_AI_ALL_TYPEDEFS> = {
table: (typeof DsqlTables)[number];
data: T;
query?: ServerQueryParam<T>;
targetId?: number | string;
};
export default async function DbUpdate<
T extends { [k: string]: any } = DSQL_TRAVIS_AI_ALL_TYPEDEFS,
>({ table, data, query, targetId }: Params<T>): Promise<APIResponseObject> {
try {
let finalQuery = query || {};
if (targetId) {
finalQuery = _.merge<ServerQueryParam<any>, ServerQueryParam<any>>(
finalQuery,
{
query: {
id: {
value: String(targetId),
},
},
},
);
}
const sqlQueryObj = datasquirel.sql.sqlGenerator({
tableName: table,
genObject: finalQuery,
});
let values: (string | number)[] = [];
const whereClause = sqlQueryObj.string.match(/WHERE .*/)?.[0];
if (whereClause) {
let sql = `UPDATE ${table} SET`;
const finalData: DSQL_TRAVIS_AI_ALL_TYPEDEFS = {
...data,
updated_at: Date.now(),
};
const keys = Object.keys(finalData);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (!key) continue;
const isLast = i == keys.length - 1;
sql += ` ${key}=?`;
values.push(
String(finalData[key as keyof DSQL_TRAVIS_AI_ALL_TYPEDEFS]),
);
if (!isLast) {
sql += `,`;
}
}
sql += ` ${whereClause}`;
values = [...values, ...sqlQueryObj.values];
const res = DbClient.run(sql, values);
return {
success: Boolean(res.changes),
postInsertReturn: {
affectedRows: res.changes,
insertId: Number(res.lastInsertRowid),
},
debug: {
sql,
values,
},
};
} else {
return {
success: false,
msg: `No WHERE clause`,
};
}
} catch (error: any) {
return {
success: false,
error: error.message,
};
}
}

26
src/lib/sqlite/index.ts Normal file
View File

@ -0,0 +1,26 @@
import AppData from "@/data/app-data";
import grabDirNames from "@/utils/grab-dir-names";
import { Database } from "bun:sqlite";
import path from "node:path";
import * as sqliteVec from "sqlite-vec";
const { ROOT_DIR } = grabDirNames();
const DBFilePath = path.join(ROOT_DIR, AppData["DbName"]);
const DBVecPluginFilePath = path.join(ROOT_DIR, AppData["DbVecPluginName"]);
const DbClient = new Database(DBFilePath, {
create: true,
});
// DbClient.loadExtension(DBVecPluginFilePath);
sqliteVec.load(DbClient);
// Test if it's working
// const { vec_version } = DbClient.prepare(
// "select vec_version() as vec_version",
// ).get();
// console.log(`sqlite-vec version: ${vec_version}`);
export default DbClient;

View File

@ -0,0 +1,28 @@
import type { DSQL_DatabaseSchemaType } from "@moduletrace/datasquirel/dist/package-shared/types";
import dbSchemaToType from "./db-schema-to-type";
import path from "node:path";
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
type Params = {
dbSchema: DSQL_DatabaseSchemaType;
};
export default function dbSchemaToTypeDef({ dbSchema }: Params) {
try {
if (!dbSchema) throw new Error("No schema found");
const definitions = dbSchemaToType({ dbSchema });
const finalOutfile = path.resolve(__dirname, "../types/db/index.ts");
const ourfileDir = path.dirname(finalOutfile);
if (!existsSync(ourfileDir)) {
mkdirSync(ourfileDir, { recursive: true });
}
writeFileSync(finalOutfile, definitions?.join("\n\n") || "", "utf-8");
} catch (error: any) {
console.log(`Schema to Typedef Error =>`, error.message);
}
}

35
src/lib/sqlite/schema.ts Normal file
View File

@ -0,0 +1,35 @@
import type {
DSQL_DatabaseSchemaType,
DSQL_FieldSchemaType,
} from "@moduletrace/datasquirel/dist/package-shared/types";
import _ from "lodash";
const DefaultFields: DSQL_FieldSchemaType[] = [
{
fieldName: "id",
dataType: "INTEGER",
primaryKey: true,
autoIncrement: true,
notNullValue: true,
fieldDescription: "The unique identifier of the record.",
},
{
fieldName: "created_at",
dataType: "INTEGER",
notNullValue: true,
fieldDescription:
"The time when the record was created. (Unix Timestamp)",
},
{
fieldName: "updated_at",
dataType: "INTEGER",
notNullValue: true,
fieldDescription:
"The time when the record was updated. (Unix Timestamp)",
},
];
export const DbSchema: DSQL_DatabaseSchemaType = {
dbName: "travis-ai",
tables: [],
};

29
tsconfig.json Normal file
View File

@ -0,0 +1,29 @@
{
"compilerOptions": {
// Environment setup & latest features
"lib": ["ESNext"],
"target": "ESNext",
"module": "Preserve",
"moduleDetection": "force",
"jsx": "react-jsx",
"allowJs": true,
// Bundler mode
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"noEmit": true,
// Best practices
"strict": true,
"skipLibCheck": true,
"noFallthroughCasesInSwitch": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
// Some stricter flags (disabled by default)
"noUnusedLocals": false,
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false
}
}