I am creating a CMS for a site. There is an about page that needs to have content from the CMS. There needs to be only one document acting as a config file for the about page. My proposed solution for this is:
Is there a better way to do this? Is there a way to do this in the save pre hook for my schema ?
Something like this could be done for a singleton model:
HomePageSchema.statics = {
getSingleton: function (cb) {
this.findOne()
.sort({updated: -1})
.limit(1)
.exec(function (error, model) {
if (error) {
cb(error, null);
} else if (model == null) {
cb(error, new HomePage());
} else {
cb(error, model);
}
});
},
};
and then
HomePage.getSingleton((err, homepage) => {
homepage.image = '/images/myImage.jpg';
homepage.headline = 'Homepage Headline';
homepage.save();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With