Changed DB driver
All checks were successful
Docker Deploy / build-and-push (push) Successful in 3m42s

This commit is contained in:
2026-01-16 18:45:28 -07:00
parent 85750a5c79
commit 7026435cd3
7 changed files with 352 additions and 1179 deletions

View File

@@ -1,22 +1,41 @@
import Database from 'better-sqlite3';
import { drizzle } from 'drizzle-orm/better-sqlite3';
import * as schema from './schema';
import path from 'path';
import { drizzle } from "drizzle-orm/libsql";
import { createClient } from "@libsql/client";
import * as schema from "./schema";
import path from "path";
let _db: ReturnType<typeof drizzle> | null = null;
// Define the database type based on the schema
type Database = ReturnType<typeof drizzle<typeof schema>>;
function initDb() {
let _db: Database | null = null;
function initDb(): Database {
if (!_db) {
const dbUrl = process.env.DATABASE_URL || path.resolve(process.cwd(), 'chronus.db');
const sqlite = new Database(dbUrl, { readonly: false });
_db = drizzle(sqlite, { schema });
let url = process.env.DATABASE_URL;
if (!url) {
url = `file:${path.resolve(process.cwd(), "chronus.db")}`;
} else if (
!url.startsWith("file:") &&
!url.startsWith("libsql:") &&
!url.startsWith("http:") &&
!url.startsWith("https:")
) {
url = `file:${url}`;
}
const authToken = process.env.DATABASE_AUTH_TOKEN;
const client = createClient({
url,
authToken,
});
_db = drizzle(client, { schema });
}
return _db;
}
export const db = new Proxy({} as ReturnType<typeof drizzle>, {
export const db = new Proxy({} as Database, {
get(_target, prop) {
const database = initDb();
return database[prop as keyof typeof database];
}
return database[prop as keyof Database];
},
});