ddstats-server/libs/sqlite2mongo.js

81 lines
3.4 KiB
JavaScript

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}`)
// })
// })
// }
// }
})
}