Merge branch 'mongo' of https://github.com/lukew3/monkeytype into mongo

This commit is contained in:
lukew3 2021-06-05 16:02:42 -04:00
commit d2a6d6b49b
5 changed files with 62 additions and 49 deletions

View file

@ -24,53 +24,60 @@ mongoose.connect("mongodb://localhost:27017/monkeytype", {
// Migrate users
userCount = 1;
db.collection("users")
// .where("name","==","mio")
.get()
.then((querySnapshot) => {
// console.log('start of foreach');
querySnapshot.forEach((userDoc) => {
let newUser = new User(userDoc.data());
newUser.uid = userDoc.id;
newUser.globalStats = {
started: userDoc.data().startedTests,
completed: userDoc.data().completedTests,
time: userDoc.data().timeTyping,
};
let tagIdDict = {};
db.collection(`users/${userDoc.id}/tags`)
.get()
.then((tagsSnapshot) => {
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);
});
db.collection(`users/${userDoc.id}/results`)
.get()
.then((resultsSnapshot) => {
resultsSnapshot.forEach((result) => {
let formattedResult = result.data();
formattedResult.tags.forEach((tag, index) => {
if (tagIdDict[tag])
formattedResult.tags[index] = tagIdDict[tag];
});
newUser.results.push(formattedResult);
});
newUser.results.sort((a, b) => {
return a.timestamp - b.timestamp;
});
db.collection(`users/${userDoc.id}/presets`)
.get()
.then((presetsSnapshot) => {
presetsSnapshot.forEach((preset) => {
newUser.presets.push(preset.data());
});
newUser.save();
console.log("User", userCount, "saved");
userCount++;
});
let newUser = new User(userDoc.data());
newUser.uid = userDoc.id;
newUser.globalStats = {
started: userDoc.data().startedTests,
completed: userDoc.data().completedTests,
time: userDoc.data().timeTyping,
};
let tagIdDict = {};
db.collection(`users/${userDoc.id}/tags`)
.get()
.then((tagsSnapshot) => {
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}`);
});
});
db.collection(`users/${userDoc.id}/results`)
.get()
.then((resultsSnapshot) => {
let resCount = 1;
resultsSnapshot.forEach((result) => {
let formattedResult = result.data();
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;
});
db.collection(`users/${userDoc.id}/presets`)
.get()
.then((presetsSnapshot) => {
presetsSnapshot.forEach((preset) => {
newUser.presets.push(preset.data());
});
newUser.save();
console.log(`User ${userCount} (${newUser.uid}) saved`);
userCount++;
});
});
});
});
// console.log('end of foreach');
});
//not tested because I can't get leaderboards to work on my fork for some reason
@ -78,7 +85,7 @@ db.collection("leaderboards")
.get()
.then((leaderboardsSnapshot) => {
leaderboardsSnapshot.forEach((lbDoc) => {
let newLb = new Leaderboard(lbDoc);
let newLb = new Leaderboard(lbDoc.data());
newLb.save();
});
});
@ -88,7 +95,7 @@ db.collection("bot-commands")
.get()
.then((botCommandsSnapshot) => {
botCommandsSnapshot.forEach((bcDoc) => {
let newBotCommand = new BotCommand(botCommandDoc);
let newBotCommand = new BotCommand(bcDoc.data());
newBotCommand.save();
});
});

View file

@ -6,7 +6,7 @@ const leaderboardEntrySchema = new Schema({
wpm: { type: Number },
raw: { type: Number },
acc: { type: Number },
consistency: { type: Number }, //can be null
consistency: { type: {} }, //can be null
mode: { type: String }, //not sure why mode and mode2 are needed
mode2: { type: Number },
timestamp: { type: Date },

View file

@ -9,8 +9,8 @@ const resultSchema = new Schema({
allChars: { type: Number, required: true },
acc: { type: Number, required: true },
mode: { type: String, required: true }, //is this always string type?
mode2: { type: Number, required: true }, //is this always number type?
quoteLength: { type: Number, required: true },
mode2: { type: String, required: true }, //is this always number type? not always
quoteLength: { type: Number, required: false },
timestamp: { type: Number, required: true }, //can this be removed if timestamps are added to mongoose
language: { type: String, default: "english" },
restartCount: { type: Number, required: true },

View file

@ -1500,6 +1500,8 @@
"dependencies": {
"@firebase/database": "^0.10.0",
"@firebase/database-types": "^0.7.2",
"@google-cloud/firestore": "^4.5.0",
"@google-cloud/storage": "^5.3.0",
"@types/node": ">=12.12.47",
"dicer": "^0.3.0",
"jsonwebtoken": "^8.5.1",

8
package-lock.json generated
View file

@ -5,7 +5,6 @@
"requires": true,
"packages": {
"": {
"name": "monkeytype",
"version": "1.5.6",
"hasInstallScript": true,
"license": "GPL-3.0",
@ -3355,6 +3354,7 @@
"dependencies": {
"anymatch": "~3.1.1",
"braces": "~3.0.2",
"fsevents": "~2.3.1",
"glob-parent": "~5.1.0",
"is-binary-path": "~2.1.0",
"is-glob": "~4.0.1",
@ -5710,6 +5710,8 @@
"dependencies": {
"@firebase/database": "^0.10.0",
"@firebase/database-types": "^0.7.2",
"@google-cloud/firestore": "^4.5.0",
"@google-cloud/storage": "^5.3.0",
"@types/node": ">=12.12.47",
"dicer": "^0.3.0",
"jsonwebtoken": "^8.5.1",
@ -6344,6 +6346,7 @@
"anymatch": "^2.0.0",
"async-each": "^1.0.1",
"braces": "^2.3.2",
"fsevents": "^1.2.7",
"glob-parent": "^3.1.0",
"inherits": "^2.0.3",
"is-binary-path": "^1.0.0",
@ -9341,7 +9344,8 @@
"bson": "^1.1.4",
"denque": "^1.4.1",
"optional-require": "^1.0.3",
"safe-buffer": "^5.1.2"
"safe-buffer": "^5.1.2",
"saslprep": "^1.0.0"
},
"engines": {
"node": ">=4"