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

This commit is contained in:
2025-04-03 13:06:33 +02:00
parent d5187a86ee
commit 7c27684ab5
7 changed files with 49 additions and 1329 deletions

1
.gitignore vendored
View File

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

View File

@@ -4,17 +4,19 @@
"description": "",
"type": "commonjs",
"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": [],
"author": "",
"license": "ISC",
"dependencies": {
"devDependencies": {
"@types/better-sqlite3": "^7.6.12",
"@types/express": "^5.0.1",
"esbuild": "^0.25.2"
},
"dependencies": {
"better-sqlite3": "^11.9.1",
"esbuild": "^0.25.2",
"express": "^5.1.0",
"ts-node": "^10.9.2"
"express": "^5.1.0"
}
}

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

View File

@@ -1,11 +1,22 @@
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));");
}

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 };