mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-10-09 07:09:36 +08:00
started migration convertion
This commit is contained in:
parent
0cf495e127
commit
997a96ee1b
3 changed files with 14706 additions and 142 deletions
|
@ -1,9 +1,16 @@
|
|||
const { config } = require("dotenv");
|
||||
const path = require("path");
|
||||
config({ path: path.join(__dirname, ".env") });
|
||||
const { mongoDB } = require("./init/mongodb");
|
||||
const { connectDB } = require("./init/mongodb");
|
||||
|
||||
console.log(config());
|
||||
|
||||
const admin = require("firebase-admin");
|
||||
const mongoose = require("mongoose");
|
||||
const { User } = require("./models/user");
|
||||
const { Leaderboard } = require("./models/leaderboard");
|
||||
const { Stats } = require("./models/stats");
|
||||
const { BotCommand } = require("./models/bot-command");
|
||||
|
||||
// const { User } = require("./models/user");
|
||||
// const { Leaderboard } = require("./models/leaderboard");
|
||||
// const { BotCommand } = require("./models/bot-command");
|
||||
|
||||
const serviceAccount = require("../functions/serviceAccountKey.json");
|
||||
|
||||
|
@ -13,101 +20,119 @@ admin.initializeApp({
|
|||
|
||||
var db = admin.firestore();
|
||||
|
||||
const port = process.env.PORT || "5005";
|
||||
|
||||
mongoose.connect("mongodb://localhost:27017/monkeytype", {
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true,
|
||||
});
|
||||
|
||||
// Database should be completely clear before this is ran in order to prevent overlapping documents
|
||||
// Migrate users
|
||||
userCount = 1;
|
||||
db.collection("users")
|
||||
// .where("name","==","mio")
|
||||
.get()
|
||||
.then((querySnapshot) => {
|
||||
// console.log('start of foreach');
|
||||
querySnapshot.forEach( async (userDoc) => {
|
||||
let newUser;
|
||||
try{
|
||||
let data = userDoc.data();
|
||||
data._id = userDoc.id;
|
||||
newUser = new User(data);
|
||||
newUser.uid = userDoc.id;
|
||||
newUser.globalStats = {
|
||||
started: userDoc.data().startedTests,
|
||||
completed: userDoc.data().completedTests,
|
||||
time: userDoc.data().timeTyping,
|
||||
};
|
||||
let tagIdDict = {};
|
||||
let tagsSnapshot = await db.collection(`users/${userDoc.id}/tags`).get();
|
||||
tagsSnapshot.forEach((tagDoc) => {
|
||||
let formattedTag = tagDoc.data();
|
||||
formattedTag._id = mongoose.Types.ObjectId(); //generate new objectId
|
||||
tagIdDict[tagDoc.id] = formattedTag._id; //save pair of ids in memory to determine what to set new id as in result tags
|
||||
newUser.tags.push(formattedTag);
|
||||
console.log(`Tag ${tagDoc.id} saved for user ${userCount}`);
|
||||
});
|
||||
let resultsSnapshot = await db.collection(`users/${userDoc.id}/results`).get();
|
||||
let resCount = 1;
|
||||
resultsSnapshot.forEach((result) => {
|
||||
let formattedResult = result.data();
|
||||
if(formattedResult.tags != undefined){
|
||||
formattedResult.tags.forEach((tag, index) => {
|
||||
if (tagIdDict[tag])
|
||||
formattedResult.tags[index] = tagIdDict[tag];
|
||||
});
|
||||
}
|
||||
newUser.results.push(formattedResult);
|
||||
console.log(`Result ${resCount} saved for user ${userCount}`);
|
||||
resCount++;
|
||||
});
|
||||
newUser.results.sort((a, b) => {
|
||||
return a.timestamp - b.timestamp;
|
||||
});
|
||||
let presetsSnapshot = await db.collection(`users/${userDoc.id}/presets`).get();
|
||||
presetsSnapshot.forEach((preset) => {
|
||||
newUser.presets.push(preset.data());
|
||||
});
|
||||
await newUser.save();
|
||||
console.log(`User ${userCount} (${newUser.uid}) saved`);
|
||||
userCount++;
|
||||
}catch(e){
|
||||
// throw e;
|
||||
console.log(`User ${userCount} (${newUser.uid}) failed: ${e.message}`);
|
||||
userCount++;
|
||||
}
|
||||
async function migrateUsers() {
|
||||
await db
|
||||
.collection("users")
|
||||
.where("name", "==", "mio")
|
||||
.get()
|
||||
.then((querySnapshot) => {
|
||||
// console.log('start of foreach');
|
||||
querySnapshot.forEach(async (userDoc) => {
|
||||
let userData = userDoc.data();
|
||||
console.log(userData);
|
||||
// let newUser;
|
||||
// try{
|
||||
// let data = userDoc.data();
|
||||
// data._id = userDoc.id;
|
||||
// newUser = new User(data);
|
||||
// newUser.uid = userDoc.id;
|
||||
// newUser.globalStats = {
|
||||
// started: userDoc.data().startedTests,
|
||||
// completed: userDoc.data().completedTests,
|
||||
// time: userDoc.data().timeTyping,
|
||||
// };
|
||||
// let tagIdDict = {};
|
||||
// let tagsSnapshot = await db.collection(`users/${userDoc.id}/tags`).get();
|
||||
// tagsSnapshot.forEach((tagDoc) => {
|
||||
// let formattedTag = tagDoc.data();
|
||||
// formattedTag._id = mongoose.Types.ObjectId(); //generate new objectId
|
||||
// tagIdDict[tagDoc.id] = formattedTag._id; //save pair of ids in memory to determine what to set new id as in result tags
|
||||
// newUser.tags.push(formattedTag);
|
||||
// console.log(`Tag ${tagDoc.id} saved for user ${userCount}`);
|
||||
// });
|
||||
// let resultsSnapshot = await db.collection(`users/${userDoc.id}/results`).get();
|
||||
// let resCount = 1;
|
||||
// resultsSnapshot.forEach((result) => {
|
||||
// let formattedResult = result.data();
|
||||
// if(formattedResult.tags != undefined){
|
||||
// formattedResult.tags.forEach((tag, index) => {
|
||||
// if (tagIdDict[tag])
|
||||
// formattedResult.tags[index] = tagIdDict[tag];
|
||||
// });
|
||||
// }
|
||||
// newUser.results.push(formattedResult);
|
||||
// console.log(`Result ${resCount} saved for user ${userCount}`);
|
||||
// resCount++;
|
||||
// });
|
||||
// newUser.results.sort((a, b) => {
|
||||
// return a.timestamp - b.timestamp;
|
||||
// });
|
||||
// let presetsSnapshot = await db.collection(`users/${userDoc.id}/presets`).get();
|
||||
// presetsSnapshot.forEach((preset) => {
|
||||
// newUser.presets.push(preset.data());
|
||||
// });
|
||||
// await newUser.save();
|
||||
// console.log(`User ${userCount} (${newUser.uid}) saved`);
|
||||
// userCount++;
|
||||
// }catch(e){
|
||||
// // throw e;
|
||||
// console.log(`User ${userCount} (${newUser.uid}) failed: ${e.message}`);
|
||||
// userCount++;
|
||||
// }
|
||||
});
|
||||
// console.log('end of foreach');
|
||||
});
|
||||
// console.log('end of foreach');
|
||||
});
|
||||
}
|
||||
// //not tested because I can't get leaderboards to work on my fork for some reason
|
||||
// db.collection("leaderboards")
|
||||
// .get()
|
||||
// .then((leaderboardsSnapshot) => {
|
||||
// leaderboardsSnapshot.forEach((lbDoc) => {
|
||||
// let newLb = new Leaderboard(lbDoc.data());
|
||||
// newLb.save();
|
||||
// });
|
||||
// });
|
||||
|
||||
//not tested because I can't get leaderboards to work on my fork for some reason
|
||||
db.collection("leaderboards")
|
||||
.get()
|
||||
.then((leaderboardsSnapshot) => {
|
||||
leaderboardsSnapshot.forEach((lbDoc) => {
|
||||
let newLb = new Leaderboard(lbDoc.data());
|
||||
newLb.save();
|
||||
// //migrate bot-commands
|
||||
// db.collection("bot-commands")
|
||||
// .get()
|
||||
// .then((botCommandsSnapshot) => {
|
||||
// botCommandsSnapshot.forEach((bcDoc) => {
|
||||
// let newBotCommand = new BotCommand(bcDoc.data());
|
||||
// newBotCommand.save();
|
||||
// });
|
||||
// });
|
||||
|
||||
//migrate public stat
|
||||
async function migratePublicStats() {
|
||||
db.collection("public")
|
||||
.doc("stats")
|
||||
.get()
|
||||
.then((ret) => {
|
||||
let stats = ret.data();
|
||||
mongoDB()
|
||||
.collection("public")
|
||||
.updateOne(
|
||||
{ type: "stats" },
|
||||
{
|
||||
$set: {
|
||||
completedTests: stats.completedTests,
|
||||
startedTests: stats.startedTests,
|
||||
timeTyping: stats.timeTyping,
|
||||
},
|
||||
},
|
||||
{ upsert: true }
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
//migrate bot-commands
|
||||
db.collection("bot-commands")
|
||||
.get()
|
||||
.then((botCommandsSnapshot) => {
|
||||
botCommandsSnapshot.forEach((bcDoc) => {
|
||||
let newBotCommand = new BotCommand(bcDoc.data());
|
||||
newBotCommand.save();
|
||||
});
|
||||
});
|
||||
async function init() {
|
||||
await connectDB();
|
||||
// await migratePublicStats();
|
||||
await migrateUsers();
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
//migrate public stats
|
||||
db.collection("public")
|
||||
.doc("stats")
|
||||
.get()
|
||||
.then((ret) => {
|
||||
let stats = ret.data();
|
||||
let newStats = new Stats(stats);
|
||||
newStats.save();
|
||||
});
|
||||
init();
|
||||
|
|
14629
package-lock.json
generated
14629
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -54,7 +54,7 @@
|
|||
"cors": "^2.8.5",
|
||||
"dotenv": "^10.0.0",
|
||||
"express": "^4.17.1",
|
||||
"firebase-admin": "^9.9.0",
|
||||
"firebase-admin": "^9.11.0",
|
||||
"helmet": "^4.6.0",
|
||||
"howler": "^2.2.1",
|
||||
"mongodb": "^3.6.9",
|
||||
|
|
Loading…
Add table
Reference in a new issue