fix: refactored database preparation & types

This commit is contained in:
2025-05-07 16:21:14 +02:00
parent e3c7f3c18f
commit 314955efcf
3 changed files with 15 additions and 14 deletions

View File

@@ -1,6 +1,7 @@
import express from 'express'; import express from 'express';
import prepareDB from './prepare'; import prepareDB from './prepare';
import getDB from './database'; import getDB from './database';
import { tiers, type, transaction } from './types';
const app = express(); const app = express();

View File

@@ -2,21 +2,19 @@ import Database from "better-sqlite3";
export default function prepareDB(db: Database.Database) { export default function prepareDB(db: Database.Database) {
// setup tiers table // setup tiers table
db.exec("CREATE TABLE IF NOT EXISTS tiers (tier INT PRIMARY KEY);"); db.exec("CREATE TABLE IF NOT EXISTS tiers (tier INTEGER, enchantment INTEGER, PRIMARY KEY(tier, enchantment));");
for (let i = 1; i <= 8; i++) { db.exec(`INSERT OR IGNORE INTO tiers (tier, enchantment) VALUES (1,0), (2,0), (3,0),
if (!db.prepare(`SELECT tier FROM tiers WHERE tier = ${i};`).pluck().get()) { (4,0), (4,1), (4,2), (4,3), (4,4),
db.prepare("INSERT INTO tiers VALUES (?)").run(i); (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 // setup item types table
db.exec("CREATE TABLE IF NOT EXISTS types (type VARCHAR PRIMARY KEY);") db.exec("CREATE TABLE IF NOT EXISTS types (type TEXT PRIMARY KEY);");
for (const type of ["cloth", "hide", "fee_crafting", "fee_market"]) { db.exec("INSERT OR IGNORE INTO types (type) VALUES ('cloth'), ('leather'), ('bar'), ('block'), ('plank'), ('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 // 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));");
} }

View File

@@ -1,3 +1,5 @@
type type = { type: string }; type type = { type: string };
type tiers = { tier: number }; type tiers = { tier: number };
type transaction = { id: number; tier: number; type: string; cost: number }; type transaction = { id: number; tier: number; type: string; cost: number };
export type { type, tiers, transaction };