This commit is contained in:
@@ -2,6 +2,7 @@ import { drizzle } from "drizzle-orm/libsql";
|
||||
import { createClient } from "@libsql/client";
|
||||
import * as schema from "./schema";
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
|
||||
// Define the database type based on the schema
|
||||
type Database = ReturnType<typeof drizzle<typeof schema>>;
|
||||
@@ -10,17 +11,16 @@ let _db: Database | null = null;
|
||||
|
||||
function initDb(): Database {
|
||||
if (!_db) {
|
||||
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 envRootDir = process.env.ROOT_DIR
|
||||
? process.env.ROOT_DIR
|
||||
: import.meta.env.ROOT_DIR;
|
||||
const rootDir = envRootDir || process.cwd();
|
||||
|
||||
if (envRootDir && !fs.existsSync(rootDir)) {
|
||||
fs.mkdirSync(rootDir, { recursive: true });
|
||||
}
|
||||
|
||||
const url = `file:${path.join(rootDir, "chronus.db")}`;
|
||||
const authToken = process.env.DATABASE_AUTH_TOKEN;
|
||||
|
||||
const client = createClient({
|
||||
|
||||
@@ -26,6 +26,7 @@ export const organizations = sqliteTable("organizations", {
|
||||
.primaryKey()
|
||||
.$defaultFn(() => nanoid()),
|
||||
name: text("name").notNull(),
|
||||
logoUrl: text("logo_url"),
|
||||
street: text("street"),
|
||||
city: text("city"),
|
||||
state: text("state"),
|
||||
@@ -68,6 +69,12 @@ export const clients = sqliteTable(
|
||||
organizationId: text("organization_id").notNull(),
|
||||
name: text("name").notNull(),
|
||||
email: text("email"),
|
||||
phone: text("phone"),
|
||||
street: text("street"),
|
||||
city: text("city"),
|
||||
state: text("state"),
|
||||
zip: text("zip"),
|
||||
country: text("country"),
|
||||
createdAt: integer("created_at", { mode: "timestamp" }).$defaultFn(
|
||||
() => new Date(),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user