diff --git a/src/main.ts b/src/main.ts index 5a2b570..5490a78 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,6 +1,7 @@ import express from 'express'; import prepareDB from './prepare'; import getDB from './database'; +import { tiers, type, transaction } from './types'; const app = express(); diff --git a/src/prepare.ts b/src/prepare.ts index b215359..7c74838 100644 --- a/src/prepare.ts +++ b/src/prepare.ts @@ -2,21 +2,19 @@ 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); - } - } + db.exec("CREATE TABLE IF NOT EXISTS tiers (tier INTEGER, enchantment INTEGER, PRIMARY KEY(tier, enchantment));"); + db.exec(`INSERT OR IGNORE INTO tiers (tier, enchantment) VALUES (1,0), (2,0), (3,0), + (4,0), (4,1), (4,2), (4,3), (4,4), + (5,0), (5,1), (5,2), (5,3), (5,4), + (6,0), (6,1), (6,2), (6,3), (6,4), + (7,0), (7,1), (7,2), (7,3), (7,4), + (8,0), (8,1), (8,2), (8,3), (8,4)` + ); // 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); - } - } - + db.exec("CREATE TABLE IF NOT EXISTS types (type TEXT PRIMARY KEY);"); + db.exec("INSERT OR IGNORE INTO types (type) VALUES ('cloth'), ('leather'), ('bar'), ('block'), ('plank'), ('fee_crafting'), ('fee_market');"); + // 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));"); + db.exec("CREATE TABLE IF NOT EXISTS transactions (id INTEGER PRIMARY KEY AUTOINCREMENT, timestamp INTEGER DEFAULT (unixepoch()), tier INT, type TEXT, cost INT, CONSTRAINT fk_tier FOREIGN KEY (tier) REFERENCES tiers(tier), CONSTRAINT fk_type FOREIGN KEY (type) REFERENCES types(type));"); } \ No newline at end of file diff --git a/src/types.ts b/src/types.ts index 5afeda3..681406a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,3 +1,5 @@ type type = { type: string }; type tiers = { tier: number }; type transaction = { id: number; tier: number; type: string; cost: number }; + +export type { type, tiers, transaction }; \ No newline at end of file