Ghost/core/server/models/index.js
Delgermurun a501711e71 Remove findAll from models that has findPage
closes #4577
- removed findAll from Post and User
- refactored deleteAllContent and data importer
2015-10-02 18:01:35 +08:00

62 lines
2.1 KiB
JavaScript

var _ = require('lodash'),
Promise = require('bluebird'),
requireTree = require('../require-tree'),
models;
models = {
excludeFiles: ['_messages', 'base', '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 init() {
var self = this;
// One off inclusion of Base file.
self.Base = require('./base');
// Require all files in this directory
return requireTree.readAll(__dirname, {followSymlinks: false}).then(function then(modelFiles) {
// For each found file, excluding those we don't want,
// we will require it and cache it here.
_.each(modelFiles, function each(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 deleteAllContent() {
var self = this;
return self.Post.findPage({limit: 'all'}).then(function then(data) {
return Promise.all(_.map(data.posts, function mapper(post) {
return self.Post.destroy({id: post.id});
}));
}).then(function () {
return self.Tag.findPage({limit: 'all'}).then(function then(data) {
return Promise.all(_.map(data.tags, function mapper(tag) {
return self.Tag.destroy({id: tag.id});
}));
});
});
}
};
module.exports = models;