Files
backend/src/prepare.ts

22 lines
1.1 KiB
TypeScript

import Database from "better-sqlite3";
export default function prepareDB(db: Database.Database) {
// setup tiers table
db.exec("CREATE TABLE IF NOT EXISTS tiers (tier INT PRIMARY KEY);");
for (let i = 1; i <= 8; i++) {
if (!db.prepare(`SELECT tier FROM tiers WHERE tier = ${i};`).pluck().get()) {
db.prepare("INSERT INTO tiers VALUES (?)").run(i);
}
}
// setup item types table
db.exec("CREATE TABLE IF NOT EXISTS types (type VARCHAR PRIMARY KEY);")
for (const type of ["cloth", "hide", "fee_crafting", "fee_market"]) {
if(!db.prepare(`SELECT type FROM types WHERE type = '${type}'`).pluck().get()) {
db.prepare("INSERT INTO types VALUES (?)").run(type);
}
}
// create transactions table
db.exec("CREATE TABLE IF NOT EXISTS transactions (id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp INTEGER DEFAULT (unixepoch()), tier INT, type VARCHAR, cost INT, CONSTRAINT fk_tier FOREIGN KEY (tier) REFERENCES tiers(tier), CONSTRAINT fk_type FOREIGN KEY (type) REFERENCES types(type));");
}