You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
787 B
TypeScript
28 lines
787 B
TypeScript
import { Database } from 'bun:sqlite';
|
|
import path from 'path';
|
|
import fs from 'fs';
|
|
|
|
// Ensure the database directory exists
|
|
const dbDir = path.join(__dirname, '../../data');
|
|
if (!fs.existsSync(dbDir)) {
|
|
fs.mkdirSync(dbDir, { recursive: true });
|
|
}
|
|
|
|
// Initialize the database
|
|
const dbPath = path.join(dbDir, 'bot.db');
|
|
export const db = new Database(dbPath);
|
|
|
|
// Run the schema script
|
|
const schema = fs.readFileSync(path.join(__dirname, 'schema.sql'), 'utf8');
|
|
db.exec(schema);
|
|
|
|
// Helper function to query the database
|
|
export function query(sql: string, params: any[] = []) {
|
|
return db.prepare(sql).all(params);
|
|
}
|
|
|
|
// Helper function to execute a statement (insert, update, delete)
|
|
export function execute(sql: string, params: any[] = []) {
|
|
return db.prepare(sql).run(params);
|
|
}
|