From 03a99e7046cd89bf4fad2f865f70518d78d71ad0 Mon Sep 17 00:00:00 2001 From: BurnyLlama Date: Sun, 26 Sep 2021 18:06:45 +0200 Subject: [PATCH] Started implementing sqlite2mongo... --- index.js | 6 +-- libs/databaseHandler.js | 20 ++++++---- libs/sqlite2mongo.js | 81 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 11 deletions(-) create mode 100644 libs/sqlite2mongo.js diff --git a/index.js b/index.js index 2ce82d7..d68cef5 100644 --- a/index.js +++ b/index.js @@ -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!")) \ No newline at end of file +Server.listen(process.env.PORT, () => console.log("Server has started!")) \ No newline at end of file diff --git a/libs/databaseHandler.js b/libs/databaseHandler.js index 896a618..23a0353 100644 --- a/libs/databaseHandler.js +++ b/libs/databaseHandler.js @@ -2,14 +2,17 @@ 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 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 } \ No newline at end of file diff --git a/libs/sqlite2mongo.js b/libs/sqlite2mongo.js new file mode 100644 index 0000000..200e25c --- /dev/null +++ b/libs/sqlite2mongo.js @@ -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}`) +// }) +// }) +// } +// } + }) +} \ No newline at end of file