From 6654aac896c1feb0bb43c986dc539d792e4ce79b Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Sat, 25 Sep 2021 17:47:05 +0200 Subject: [PATCH] Made mongoose schemas. --- .gitignore | 3 ++- dotenv-template | 1 + index.js | 7 +++++++ libs/databaseHandler.js | 21 +++++++++++++++++++++ package.json | 5 ++++- schemas/Finishes.js | 11 +++++++++++ schemas/Level.js | 14 ++++++++++++++ schemas/Player.js | 13 +++++++++++++ 8 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 dotenv-template create mode 100644 libs/databaseHandler.js create mode 100644 schemas/Finishes.js create mode 100644 schemas/Level.js create mode 100644 schemas/Player.js diff --git a/.gitignore b/.gitignore index 9ace535..44683e3 100644 --- a/.gitignore +++ b/.gitignore @@ -119,4 +119,5 @@ dist package-lock.json pnpm-lock.yaml -ddnet.sqlite \ No newline at end of file +ddnet.sqlite +.env \ No newline at end of file diff --git a/dotenv-template b/dotenv-template new file mode 100644 index 0000000..cab0778 --- /dev/null +++ b/dotenv-template @@ -0,0 +1 @@ +MONGO_URI = "A URI to the mongodb database." \ No newline at end of file diff --git a/index.js b/index.js index 6aaa3ec..2ce82d7 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,9 @@ import express from 'express' +import databaseHandler from './libs/databaseHandler.js' +import dotenv from 'dotenv' + +// Read the .env file +dotenv.config() const Server = express() @@ -12,4 +17,6 @@ Server.get('/', (req, res) => ) ) +console.log(databaseHandler.getAllMaps("BurnyLlama")) + Server.listen(12345, () => console.log("Server has started!")) \ No newline at end of file diff --git a/libs/databaseHandler.js b/libs/databaseHandler.js new file mode 100644 index 0000000..896a618 --- /dev/null +++ b/libs/databaseHandler.js @@ -0,0 +1,21 @@ +import betterSqlite3 from 'better-sqlite3' +import mongoose from 'mongoose' + +const sqlite = new betterSqlite3('ddnet.sqlite', { readonly: true }) +mongoose.connect( + process.env.MONGO_URI, + { + useNewUrlParser: true, + useUnifiedTopology: true + }, + () => "Connected to mongodb!" +) + +function getAllMaps(player) { + const stmt = sqlite.prepare('SELECT Name, Map, Time, Time, Timestamp, Server FROM race WHERE Name = ?') + return stmt.all(player) +} + +export default { + getAllMaps +} \ No newline at end of file diff --git a/package.json b/package.json index 62006ae..88ec613 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,9 @@ "author": "BurnyLlama, furo", "license": "WTFPL", "dependencies": { - "express": "^4.17.1" + "better-sqlite3": "^7.4.3", + "dotenv": "^10.0.0", + "express": "^4.17.1", + "mongoose": "^6.0.7" } } diff --git a/schemas/Finishes.js b/schemas/Finishes.js new file mode 100644 index 0000000..23dbcd3 --- /dev/null +++ b/schemas/Finishes.js @@ -0,0 +1,11 @@ +import { Schema } from 'mongoose' +import Level from './Level.js' +import Player from './Player.js' + +const Finish = new Schema({ + map: Level, + time: Date, + players: [Player] +}) + +export default Finish \ No newline at end of file diff --git a/schemas/Level.js b/schemas/Level.js new file mode 100644 index 0000000..2cbfa7f --- /dev/null +++ b/schemas/Level.js @@ -0,0 +1,14 @@ +import { Schema } from 'mongoose' +import Finish from './Finishes.js' + +const Level = new Schema({ + name: String, + mapper: String, + release: Date, + category: String, + rating: Number, + awardPoints: Number, + finishes: [Finish] +}) + +export default Level \ No newline at end of file diff --git a/schemas/Player.js b/schemas/Player.js new file mode 100644 index 0000000..9aa7ff5 --- /dev/null +++ b/schemas/Player.js @@ -0,0 +1,13 @@ +import { Schema } from 'mongoose' +import Level from './Level.js' + +const Player = new Schema({ + name: String, + points: Number, + rankPoints: Number, + teamPoints: Number, + finishedMaps: [Level], + favouritePartners: [Player] +}) + +export default Player \ No newline at end of file