Started implementing sqlite2mongo...

This commit is contained in:
BurnyLlama 2021-09-26 18:06:45 +02:00
parent e7a1e768ee
commit 03a99e7046
3 changed files with 96 additions and 11 deletions

View File

@ -1,6 +1,6 @@
import express from 'express'
import databaseHandler from './libs/databaseHandler.js'
import dotenv from 'dotenv'
import sqlite2mongo from './libs/sqlite2mongo.js'
// Read the .env file
dotenv.config()
@ -17,6 +17,6 @@ Server.get('/', (req, res) =>
)
)
console.log(databaseHandler.getAllMaps("BurnyLlama"))
sqlite2mongo()
Server.listen(12345, () => console.log("Server has started!"))
Server.listen(process.env.PORT, () => console.log("Server has started!"))

View File

@ -2,14 +2,17 @@ import betterSqlite3 from 'better-sqlite3'
import mongoose from 'mongoose'
const sqlite = new betterSqlite3('ddnet.sqlite', { readonly: true })
mongoose.connect(
function initMongo() {
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 = ?')
@ -17,5 +20,6 @@ function getAllMaps(player) {
}
export default {
initMongo,
getAllMaps
}

81
libs/sqlite2mongo.js Normal file
View File

@ -0,0 +1,81 @@
import betterSqlite3 from 'better-sqlite3'
import { raw } from 'express'
import mongoose from 'mongoose'
import Finish from '../schemas/Finishes.js'
import Level from '../schemas/Level.js'
import Player from '../schemas/Player.js'
function log(string) {
console.log(`sqlite2mongo >>> ${string}`)
}
export default async function() {
log("Reconstructing the Database!")
await mongoose.connect(
process.env.MONGO_URI,
{
useNewUrlParser: true,
useUnifiedTopology: true
}
).then(async () => {
log("Connected to mongodb!")
const sqlite = new betterSqlite3('ddnet.sqlite')
log("Initialised sqlite!")
log("Adding levels (maps)...")
const latestMapAddition = (await Level.findOne({}).sort({ "release": "desc" })).release.toISOString().replace(/[TZ]/g, " ").replace(/\.[0-9\s]+/, "")
const allMaps = sqlite.prepare("SELECT Map, Server, Points, Stars, Mapper, Timestamp FROM maps WHERE Timestamp > ? ORDER BY Timestamp").iterate(`datetime('${latestMapAddition}')`)
const newMapAmount = sqlite.prepare("SELECT count(Map) FROM maps WHERE Timestamp > ? ORDER BY Timestamp").get(`datetime('${latestMapAddition}')`)['count(Map)']
let addedMapCount = 0
console.log({ allMaps, newMapAmount, addedMapCount })
for (let map of allMaps) {
log("OOMPA")
Level.create({
name: map.Map,
mapper: map.Mapper,
release: map.Timestamp === '0000-00-00 00:00:00' ? new Date('January 1, 1970 00:00:00 UTC') : new Date(map.Timestamp),
category: map.Server,
rating: map.Stars,
awardPoints: map.Points
}).then(() => {
++addedMapCount
log(`Adding map ${addedMapCount}/${newMapAmount} -> ${map.Map}`)
})
}
// log("Adding players...")
// let addedPlayerCount = 0
// const allPlayers = sqlite.prepare(`SELECT distinct Name FROM race LIMIT 10000`).iterate()
// for (const player of allPlayers) {
// Player.create({
// name: player.name
// }).then(() => {
// ++addedPlayerCount
// log(`Adding player ${addedPlayerCount}/10000 -> ${player.Name}`)
// })
// }
// log("Adding finishes (races)...")
// const rows = sqlite.prepare("SELECT count(Name) FROM race;").get()['count(Name)']
// let addedFinishCount = 0
// for (let i = 0; i < rows; i += 5000) {
// const allFinishes = sqlite.prepare(`SELECT Map, Name, Time, Timestamp, Server FROM race LIMIT 5000 OFFSET ${i + 1}`).all()
// for (let finish of allFinishes) {
// Level.findOne({ name: finish.Map }).then(map => {
// Player.findOne({ name: finish.Name }).then(async player => {
// await Finish.create({
// map: map._id,
// time: finish.Time,
// date: finish.Timestamp === '0000-00-00 00:00:00' ? new Date('January 1, 1970 00:00:00 UTC') : new Date(finish.Timestamp),
// players: player._id
// })
// ++addedFinishCount
// log(`Adding race ${addedFinishCount}/${rows} -> ${finish.Name} @ ${finish.Map}`)
// })
// })
// }
// }
})
}