mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-03 21:22:39 +00:00
Update Mongoose calls to not use callbacks.
This commit is contained in:
@@ -27,8 +27,8 @@ const disconnect = async ()=>{
|
|||||||
};
|
};
|
||||||
|
|
||||||
const connect = async (config)=>{
|
const connect = async (config)=>{
|
||||||
return await Mongoose.connect(getMongoDBURL(config),
|
return await Mongoose.connect(getMongoDBURL(config), { retryWrites: false })
|
||||||
{ retryWrites: false }, handleConnectionError);
|
.catch(error=>handleConnectionError(error));
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
@@ -40,30 +40,24 @@ HomebrewSchema.statics.increaseView = async function(query) {
|
|||||||
return brew;
|
return brew;
|
||||||
};
|
};
|
||||||
|
|
||||||
HomebrewSchema.statics.get = function(query, fields=null){
|
HomebrewSchema.statics.get = async function(query, fields=null){
|
||||||
return new Promise((resolve, reject)=>{
|
const brew = await Homebrew.findOne(query, fields).orFail()
|
||||||
Homebrew.find(query, fields, null, (err, brews)=>{
|
.catch(error=>{throw 'Can not find brew'});
|
||||||
if(err || !brews.length) return reject('Can not find brew');
|
if(!_.isNil(brew.textBin)) { // Uncompress zipped text field
|
||||||
if(!_.isNil(brews[0].textBin)) { // Uncompress zipped text field
|
unzipped = zlib.inflateRawSync(brew.textBin);
|
||||||
unzipped = zlib.inflateRawSync(brews[0].textBin);
|
brew.text = unzipped.toString();
|
||||||
brews[0].text = unzipped.toString();
|
}
|
||||||
}
|
return brew;
|
||||||
return resolve(brews[0]);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
HomebrewSchema.statics.getByUser = function(username, allowAccess=false, fields=null){
|
HomebrewSchema.statics.getByUser = async function(username, allowAccess=false, fields=null){
|
||||||
return new Promise((resolve, reject)=>{
|
const query = { authors: username, published: true };
|
||||||
const query = { authors: username, published: true };
|
if(allowAccess){
|
||||||
if(allowAccess){
|
delete query.published;
|
||||||
delete query.published;
|
}
|
||||||
}
|
const brews = await Homebrew.find(query, fields).lean().exec() //lean() converts results to JSObjects
|
||||||
Homebrew.find(query, fields).lean().exec((err, brews)=>{ //lean() converts results to JSObjects
|
.catch(error=>{throw 'Can not find brews'});
|
||||||
if(err) return reject('Can not find brew');
|
return brews;
|
||||||
return resolve(brews);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const Homebrew = mongoose.model('Homebrew', HomebrewSchema);
|
const Homebrew = mongoose.model('Homebrew', HomebrewSchema);
|
||||||
|
|||||||
Reference in New Issue
Block a user