build collections
This commit is contained in:
@@ -10,23 +10,31 @@ fs.mkdirSync(DB_DIR, { recursive: true });
|
||||
|
||||
const db = new Database(DB_PATH);
|
||||
|
||||
// Enable WAL mode for better concurrent read performance
|
||||
db.pragma('journal_mode = WAL');
|
||||
db.pragma('foreign_keys = ON');
|
||||
|
||||
// Core tables
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS collections (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT UNIQUE NOT NULL,
|
||||
is_default INTEGER NOT NULL DEFAULT 0,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS memes (
|
||||
id TEXT PRIMARY KEY,
|
||||
title TEXT NOT NULL,
|
||||
description TEXT,
|
||||
file_path TEXT NOT NULL,
|
||||
file_name TEXT NOT NULL,
|
||||
file_size INTEGER NOT NULL,
|
||||
mime_type TEXT NOT NULL,
|
||||
width INTEGER NOT NULL,
|
||||
height INTEGER NOT NULL,
|
||||
parent_id TEXT REFERENCES memes(id) ON DELETE CASCADE,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
id TEXT PRIMARY KEY,
|
||||
title TEXT NOT NULL,
|
||||
description TEXT,
|
||||
file_path TEXT NOT NULL,
|
||||
file_name TEXT NOT NULL,
|
||||
file_size INTEGER NOT NULL,
|
||||
mime_type TEXT NOT NULL,
|
||||
width INTEGER NOT NULL,
|
||||
height INTEGER NOT NULL,
|
||||
parent_id TEXT REFERENCES memes(id) ON DELETE CASCADE,
|
||||
collection_id INTEGER REFERENCES collections(id) ON DELETE SET NULL,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS tags (
|
||||
@@ -42,8 +50,34 @@ db.exec(`
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_memes_parent_id ON memes(parent_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_memes_created_at ON memes(created_at DESC);
|
||||
CREATE INDEX IF NOT EXISTS idx_memes_collection_id ON memes(collection_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_meme_tags_meme_id ON meme_tags(meme_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_meme_tags_tag_id ON meme_tags(tag_id);
|
||||
`);
|
||||
|
||||
// Migration: add collection_id column if upgrading from earlier schema
|
||||
const memesCols = db.prepare('PRAGMA table_info(memes)').all() as { name: string }[];
|
||||
if (!memesCols.find((c) => c.name === 'collection_id')) {
|
||||
db.exec('ALTER TABLE memes ADD COLUMN collection_id INTEGER REFERENCES collections(id) ON DELETE SET NULL');
|
||||
db.exec('CREATE INDEX IF NOT EXISTS idx_memes_collection_id ON memes(collection_id)');
|
||||
}
|
||||
|
||||
// Seed the default UNSORTED collection
|
||||
const defaultCollection = db
|
||||
.prepare('SELECT id FROM collections WHERE is_default = 1')
|
||||
.get() as { id: number } | undefined;
|
||||
|
||||
if (!defaultCollection) {
|
||||
db.prepare("INSERT INTO collections (name, is_default) VALUES ('Unsorted', 1)").run();
|
||||
}
|
||||
|
||||
const unsorted = db
|
||||
.prepare('SELECT id FROM collections WHERE is_default = 1')
|
||||
.get() as { id: number };
|
||||
|
||||
// Assign any existing memes with no collection to UNSORTED
|
||||
db.prepare('UPDATE memes SET collection_id = ? WHERE collection_id IS NULL').run(unsorted.id);
|
||||
|
||||
export const UNSORTED_ID = unsorted.id;
|
||||
|
||||
export default db;
|
||||
|
||||
Reference in New Issue
Block a user