File size: 677 Bytes
5ef6e9d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import { drizzle } from "drizzle-orm/better-sqlite3";
import Database from "better-sqlite3";
import * as schema from "./schema";
import path from "path";
import fs from "fs";

const DATABASE_URL = process.env.DATABASE_URL || "file:/data/sqlite.db";

// 解析 SQLite 檔案路徑
const dbPath = DATABASE_URL.replace("file:", "");

// 確保資料目錄存在
const dbDir = path.dirname(dbPath);
if (!fs.existsSync(dbDir)) {
  fs.mkdirSync(dbDir, { recursive: true });
}

// 建立 SQLite 連接
const sqlite = new Database(dbPath);
sqlite.pragma("journal_mode = WAL"); // 啟用 WAL 模式提升效能

export const db = drizzle(sqlite, { schema });

export * from "./schema";