Updated userSchema to store flair by reference

This commit is contained in:
rainbow napkin 2024-12-03 06:21:43 -05:00
parent 108290de2d
commit 279640a7e7
7 changed files with 73 additions and 54 deletions

View file

@ -39,27 +39,34 @@ const flairSchema = new mongoose.Schema({
});
flairSchema.statics.loadDefaults = async function(){
//Make sure registerFlair function is happy
const _this = this;
//Ensure default comes first (.bind(this) doesn't seem to work here...)
await registerFlair(defaultFlair.default);
//For each entry in the defaultFlair.json file
defaultFlair.forEach(async (flair) => {
defaultFlair.array.forEach(registerFlair);
async function registerFlair(flair){
try{
//Look for flair matching the one from our file
//Look for flair matching the one from our file
const foundFlair = await _this.findOne({name: flair.name});
const foundFlair = await this.findOne({name: flair.name});
//if the flair doesn't exist
if(!foundFlair){
const flairDB = await this.create(flair);
console.log(`Loading default flair '${flair.name} into DB from defaultFlair.json`);
}
//if the flair doesn't exist
if(!foundFlair){
const flairDB = await _this.create(flair);
console.log(`Loading default flair '${flair.name} into DB from defaultFlair.json`);
}
}catch(err){
if(flair != null){
console.log(err);
console.log(`Error loading flair '${flair.name}':`);
}else{
console.log("Error, null flair:");
}
}
});
}
}
module.exports = mongoose.model("flair", flairSchema);