Ghost/core/server/models/index.js
Harry Wolff 5ff2a31ce1 Move Models module to have an init method that sets up all models
resolves #2170

- creates a models.init() function that requires all other model files
and caches them.  This is opposed to the previous functionality where
when you require('./models') it would immediately require all other models.
Now it's done when you want.

- Updates all tests to reflect the new structure of the model module
2014-08-18 15:54:10 -04:00

65 lines
2.0 KiB
JavaScript

var _ = require('lodash'),
when = require('when'),
requireTree = require('../require-tree'),
models;
models = {
excludeFiles: ['_messages', 'basetoken.js', 'base.js', 'index.js'],
// ### init
// Scan all files in this directory and then require each one and cache
// the objects exported onto this `models` object so that every other
// module can safely access models without fear of introducing circular
// dependency issues.
// @returns {Promise}
init: function () {
var self = this;
// One off inclusion of Base file.
self.Base = require('./base');
// Require all files in this directory
return requireTree.readAll(__dirname).then(function (modelFiles) {
// For each found file, excluding those we don't want,
// we will require it and cache it here.
_.each(modelFiles, function (path, fileName) {
// Return early if this fileName is one of the ones we want
// to exclude.
if (_.contains(self.excludeFiles, fileName)) {
return;
}
// Require the file.
var file = require(path);
// Cache its `export` object onto this object.
_.extend(self, file);
});
return;
});
},
// ### deleteAllContent
// Delete all content from the database (posts, tags, tags_posts)
deleteAllContent: function () {
var self = this;
return self.Post.findAll().then(function (posts) {
return when.all(_.map(posts.toJSON(), function (post) {
return self.Post.destroy({id: post.id});
}));
}).then(function () {
return self.Tag.findAll().then(function (tags) {
return when.all(_.map(tags.toJSON(), function (tag) {
return self.Tag.destroy({id: tag.id});
}));
});
});
}
};
module.exports = models;