- 移除Prisma中不支持SQLite的类型(Json、枚举) - 使用String类型替代枚举值 - 更新Prisma schema适配SQLite - 添加数据库初始化脚本scripts/init-db.js - 更新数据库路径配置 - 添加sql.js依赖 - 删除旧的prisma.ts使用新的db.ts Co-Authored-By: Claude <noreply@anthropic.com>
99 lines
2.9 KiB
JavaScript
99 lines
2.9 KiB
JavaScript
import { createClient } from '@libsql/client';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
// Navigate from scripts/ to server/ root, then to prisma/
|
|
const dbPath = path.join(__dirname, '..', 'prisma', 'dev.db');
|
|
const prismaDir = path.dirname(dbPath);
|
|
|
|
// Ensure prisma directory exists
|
|
if (!fs.existsSync(prismaDir)) {
|
|
fs.mkdirSync(prismaDir, { recursive: true });
|
|
console.log('Created prisma directory');
|
|
}
|
|
|
|
console.log('Database path:', dbPath);
|
|
|
|
// Ensure file exists
|
|
if (!fs.existsSync(dbPath)) {
|
|
fs.writeFileSync(dbPath, '');
|
|
console.log('Created empty database file');
|
|
}
|
|
|
|
const db = createClient({
|
|
url: `file:${dbPath}`,
|
|
});
|
|
|
|
console.log('LibSQL client created');
|
|
|
|
// Create tables
|
|
await db.execute(`
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
id TEXT PRIMARY KEY,
|
|
email TEXT UNIQUE NOT NULL,
|
|
password_hash TEXT NOT NULL,
|
|
nickname TEXT,
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
`);
|
|
console.log('Created users table');
|
|
|
|
await db.execute(`
|
|
CREATE TABLE IF NOT EXISTS events (
|
|
id TEXT PRIMARY KEY,
|
|
user_id TEXT NOT NULL,
|
|
type TEXT NOT NULL,
|
|
title TEXT NOT NULL,
|
|
content TEXT,
|
|
date DATETIME NOT NULL,
|
|
is_lunar INTEGER DEFAULT 0,
|
|
repeat_type TEXT DEFAULT 'none',
|
|
is_holiday INTEGER DEFAULT 0,
|
|
is_completed INTEGER DEFAULT 0,
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
|
)
|
|
`);
|
|
console.log('Created events table');
|
|
|
|
await db.execute(`CREATE INDEX IF NOT EXISTS idx_events_user_id ON events(user_id)`);
|
|
await db.execute(`CREATE INDEX IF NOT EXISTS idx_events_type ON events(type)`);
|
|
await db.execute(`CREATE INDEX IF NOT EXISTS idx_events_date ON events(date)`);
|
|
|
|
await db.execute(`
|
|
CREATE TABLE IF NOT EXISTS notes (
|
|
id TEXT PRIMARY KEY,
|
|
user_id TEXT UNIQUE NOT NULL,
|
|
content TEXT,
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
|
)
|
|
`);
|
|
console.log('Created notes table');
|
|
|
|
await db.execute(`
|
|
CREATE TABLE IF NOT EXISTS ai_coach_conversations (
|
|
id TEXT PRIMARY KEY,
|
|
user_id TEXT NOT NULL,
|
|
message TEXT NOT NULL,
|
|
response TEXT NOT NULL,
|
|
parsed_data TEXT,
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
|
|
)
|
|
`);
|
|
console.log('Created ai_coach_conversations table');
|
|
|
|
await db.execute(`CREATE INDEX IF NOT EXISTS idx_ai_conversations_user_id ON ai_coach_conversations(user_id)`);
|
|
|
|
// Verify
|
|
const tables = await db.execute("SELECT name FROM sqlite_master WHERE type='table'");
|
|
console.log('Tables:', tables.rows.map(r => r.name).join(', '));
|
|
|
|
console.log('\nDatabase initialized successfully!');
|