bigly-chat/db.js
2024-11-25 14:12:44 -05:00

32 lines
No EOL
1 KiB
JavaScript

import sqlite3 from 'sqlite3'
import { open } from 'sqlite'
let db;
const sql = `CREATE TABLE IF NOT EXISTS auth (username TEXT, password TEXT);
CREATE TABLE IF NOT EXISTS token (username TEXT, token TEXT);
CREATE TABLE IF NOT EXISTS video (id TEXT, title TEXT, desc TEXT, username TEXT, date REAL);
CREATE TABLE IF NOT EXISTS comment (username TEXT, targetType TEXT, targetId TEXT, date REAL, content TEXT, id TEXT);
CREATE TABLE IF NOT EXISTS follow (username TEXT, target TEXT);
CREATE TABLE IF NOT EXISTS user (username TEXT, bio TEXT);
CREATE TABLE IF NOT EXISTS captcha (key TEXT, solution TEXT);
CREATE TABLE IF NOT EXISTS message (username TEXT, targetType TEXT, targetId TEXT, date REAL, content TEXT, read TEXT);`
async function initDb() {
if (db) return db;
db = await open({
filename: `${process.cwd()}/db/main.db`,
driver: sqlite3.Database
});
let sqlCmds = sql.split('\n');
for (let cmd of sqlCmds) {
await db.run(cmd);
}
return db;
}
export default initDb;