I'm using mongoose to seed a mongodb database based on my mongoose models. I'm particular interested in improving the seed code.
I have two models, an article and a category. The relationship between the models are as follows:
A category has many articles An article has a parent category
The model schemas I'm using are listed below:
The category schema
var mongoose = require('mongoose');
var categorySchema = mongoose.Schema({
title: String,
articles : [{ type: mongoose.Schema.Types.ObjectId, ref: 'Article' }]
});
mongoose.model('Category', categorySchema);
The article schema
var mongoose = require('mongoose');
var articleSchema = mongoose.Schema({
category: { type: mongoose.Schema.Types.ObjectId, ref: 'Category' },
title: String
});
mongoose.model('Article', articleSchema);
I'm using the following code to seed:
Seeding the category
var CategoryModel = require('mongoose').model('Category');
exports.seedCategories = function seedCategories() {
CategoryModel.find({}).exec(function (err, collection) {
if (collection.length === 0) {
CategoryModel.create({ title: 'Category One' });
CategoryModel.create({ title: 'Category Two' });
}
});
}
Seeding the article
var CategoryModel = require('mongoose').model('Category');
var ArticleModel = require('mongoose').model('Article');
exports.seedArticles = function seedArticles() {
ArticleModel.find({}).exec(function (err, collection) {
if (collection.length === 0) {
seedArticle('Category One', 'Article One');
seedArticle('Category One', 'Article Two');
seedArticle('Category Two', 'Article Three');
seedArticle('Category Two', 'Article Four');
}
});
function seedArticle(categoryTitle, articleTitle) {
var parentCategory;
CategoryModel.findOne({ title: categoryTitle }).exec()
.then(function (category) {
parentCategory = category;
return ArticleModel.create({ title: articleTitle, _category: parentCategory._id });
}).then(function (article) {
parentCategory.articles.push(article._id);
parentCategory.save(function (saveError) {
if (!saveError) {
console.log("Seeded article");
} else {
console.log(saveError);
}
});
});
}
}
I'm particularly interested in methods for preventing the Christmas tree of doom, and removing the obstruent error handling. Any recommendations are welcome, and thanks in advance for the help. I hope this code helps others.
Mongoose have just implemented promises returned from saves, so that should help. It will be available in mongoose 3.10.
parentCategory._id
of the article to the field_category
? The schema defines it ascategory
. Am I misunderstanding something? \$\endgroup\$