added types; added types, transactions to db; fixed dependencies

This commit is contained in:
Jan Kaufer
2025-04-02 16:56:39 +02:00
committed by ManInDark
parent d5187a86ee
commit 4c232e07c5
7 changed files with 49 additions and 1329 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
*.db *.db
node_modules node_modules
build build
pnpm-lock.yaml

View File

@@ -4,17 +4,19 @@
"description": "", "description": "",
"type": "commonjs", "type": "commonjs",
"scripts": { "scripts": {
"build": "esbuild src/main.ts --bundle --outfile=build/out.js --platform=node --external:better-sqlite3" "build": "esbuild src/main.ts --bundle --outfile=build/out.js --platform=node --external:better-sqlite3 --external:express",
"execute": "pnpm build && node build/out.js"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "devDependencies": {
"@types/better-sqlite3": "^7.6.12", "@types/better-sqlite3": "^7.6.12",
"@types/express": "^5.0.1", "@types/express": "^5.0.1",
"esbuild": "^0.25.2"
},
"dependencies": {
"better-sqlite3": "^11.9.1", "better-sqlite3": "^11.9.1",
"esbuild": "^0.25.2", "express": "^5.1.0"
"express": "^5.1.0",
"ts-node": "^10.9.2"
} }
} }

1312
pnpm-lock.yaml generated

File diff suppressed because it is too large Load Diff

5
src/database.ts Normal file
View File

@@ -0,0 +1,5 @@
import Database from "better-sqlite3";
export default function getDB() {
return new Database('store.db', { verbose: console.log })
}

View File

@@ -1,21 +1,31 @@
import express from 'express'; import express from 'express';
import Database from 'better-sqlite3';
import prepareDB from './prepare'; import prepareDB from './prepare';
import getDB from './database';
const app = express(); const app = express();
app.get("/tiers", (req, res) => {
const db = new Database('store.db', { verbose: console.log }); app.get("/tiers", (_, res) => {
res.status(200); const db = getDB();
res.set("Content-Type", "application/json"); res.send((db.prepare("SELECT * FROM tiers").all() as tiers[]).map(t => t.tier));
res.send((db.prepare("SELECT * FROM tiers").all() as { tier: number}[]).map(t => t.tier));
db.close(); db.close();
}); });
app.listen(2500, () => { app.get("/types", (_, res) => {
console.log("Server is running"); const db = getDB();
res.send((db.prepare("SELECT * FROM types").all() as type[]).map(t => t.type));
db.close();
});
app.get("/transactions", (_, res) => {
const db = getDB();
res.send((db.prepare("SELECT * FROM transactions").all() as transaction[]));
db.close();
}) })
const db = new Database('store.db', { verbose: console.log }); app.listen(2500, () => {
const db = getDB();
prepareDB(db); prepareDB(db);
db.close() db.close()
console.log("Server is running");
});

View File

@@ -1,11 +1,22 @@
import Database from "better-sqlite3"; import Database from "better-sqlite3";
export default function prepareDB(db: Database.Database) { export default function prepareDB(db: Database.Database) {
// setup tiers table
db.exec("CREATE TABLE IF NOT EXISTS tiers (tier INT PRIMARY KEY);"); db.exec("CREATE TABLE IF NOT EXISTS tiers (tier INT PRIMARY KEY);");
for (let i = 1; i <= 8; i++) { for (let i = 1; i <= 8; i++) {
if (!db.prepare(`SELECT tier FROM tiers WHERE tier = ${i};`).pluck().get()) { if (!db.prepare(`SELECT tier FROM tiers WHERE tier = ${i};`).pluck().get()) {
db.prepare("INSERT INTO tiers VALUES (?)").run(i); 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));");
} }

3
src/types.ts Normal file
View File

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