0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-24 18:32:41 +00:00

Update Mongoose calls to not use callbacks.

This commit is contained in:
Trevor Buckner
2023-03-21 16:06:43 -04:00
parent 9dda58991f
commit 1ec08bb1fa
2 changed files with 18 additions and 24 deletions

View File

@@ -40,30 +40,24 @@ HomebrewSchema.statics.increaseView = async function(query) {
return brew;
};
HomebrewSchema.statics.get = function(query, fields=null){
return new Promise((resolve, reject)=>{
Homebrew.find(query, fields, null, (err, brews)=>{
if(err || !brews.length) return reject('Can not find brew');
if(!_.isNil(brews[0].textBin)) { // Uncompress zipped text field
unzipped = zlib.inflateRawSync(brews[0].textBin);
brews[0].text = unzipped.toString();
}
return resolve(brews[0]);
});
});
HomebrewSchema.statics.get = async function(query, fields=null){
const brew = await Homebrew.findOne(query, fields).orFail()
.catch(error=>{throw 'Can not find brew'});
if(!_.isNil(brew.textBin)) { // Uncompress zipped text field
unzipped = zlib.inflateRawSync(brew.textBin);
brew.text = unzipped.toString();
}
return brew;
};
HomebrewSchema.statics.getByUser = function(username, allowAccess=false, fields=null){
return new Promise((resolve, reject)=>{
const query = { authors: username, published: true };
if(allowAccess){
delete query.published;
}
Homebrew.find(query, fields).lean().exec((err, brews)=>{ //lean() converts results to JSObjects
if(err) return reject('Can not find brew');
return resolve(brews);
});
});
HomebrewSchema.statics.getByUser = async function(username, allowAccess=false, fields=null){
const query = { authors: username, published: true };
if(allowAccess){
delete query.published;
}
const brews = await Homebrew.find(query, fields).lean().exec() //lean() converts results to JSObjects
.catch(error=>{throw 'Can not find brews'});
return brews;
};
const Homebrew = mongoose.model('Homebrew', HomebrewSchema);