Skip to content

Commit

Permalink
mongoose - use lean() for performance
Browse files Browse the repository at this point in the history
and set the default of arrays to undefined, to prevent unwanted behaviour
  • Loading branch information
Nico105 committed Aug 31, 2021
1 parent 282dc85 commit 35f61f4
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions examples/custom-databases/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ const giveawaySchema = new mongoose.Schema({
},
thumbnail: String,
hostedBy: String,
winnerIds: [String],
winnerIds: { type: [String], default: undefined },
reaction: mongoose.Mixed,
botsCanWin: Boolean,
embedColor: mongoose.Mixed,
embedColorEnd: mongoose.Mixed,
exemptPermissions: [],
exemptPermissions: { type: [], default: undefined },
exemptMembers: String,
bonusEntries: String,
extraData: mongoose.Mixed,
Expand All @@ -70,11 +70,11 @@ const giveawaySchema = new mongoose.Schema({
},
isDrop: Boolean,
allowedMentions: {
parse: [String],
users: [String],
roles: [String]
parse: { type: [String], default: undefined },
users: { type: [String], default: undefined },
roles: { type: [String], default: undefined }
}
});
}, { id: false });

// Create the model
const giveawayModel = mongoose.model('giveaways', giveawaySchema);
Expand All @@ -84,7 +84,7 @@ const GiveawayManagerWithOwnDatabase = class extends GiveawaysManager {
// This function is called when the manager needs to get all giveaways which are stored in the database.
async getAllGiveaways() {
// Get all giveaways from the database. We fetch all documents by passing an empty condition.
return await giveawayModel.find({});
return await giveawayModel.find({}).lean().exec();
}

// This function is called when a giveaway needs to be saved in the database.
Expand All @@ -98,15 +98,15 @@ const GiveawayManagerWithOwnDatabase = class extends GiveawaysManager {
// This function is called when a giveaway needs to be edited in the database.
async editGiveaway(messageId, giveawayData) {
// Find by messageId and update it
await giveawayModel.findOneAndUpdate({ messageId }, giveawayData, { omitUndefined: true }).exec();
await giveawayModel.findOneAndUpdate({ messageId }, giveawayData, { omitUndefined: true }).lean().exec();
// Don't forget to return something!
return true;
}

// This function is called when a giveaway needs to be deleted from the database.
async deleteGiveaway(messageId) {
// Find by messageId and delete it
await giveawayModel.findOneAndDelete({ messageId }).exec();
await giveawayModel.findOneAndDelete({ messageId }).lean().exec();
// Don't forget to return something!
return true;
}
Expand Down

0 comments on commit 35f61f4

Please sign in to comment.