Made mongoose schemas.

This commit is contained in:
BurnyLlama 2021-09-25 17:47:05 +02:00
parent d82bc75ea9
commit 6654aac896
8 changed files with 73 additions and 2 deletions

3
.gitignore vendored
View File

@ -119,4 +119,5 @@ dist
package-lock.json
pnpm-lock.yaml
ddnet.sqlite
ddnet.sqlite
.env

1
dotenv-template Normal file
View File

@ -0,0 +1 @@
MONGO_URI = "A URI to the mongodb database."

View File

@ -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!"))

21
libs/databaseHandler.js Normal file
View File

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

View File

@ -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"
}
}

11
schemas/Finishes.js Normal file
View File

@ -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

14
schemas/Level.js Normal file
View File

@ -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

13
schemas/Player.js Normal file
View File

@ -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