2014-08-17 10:17:23 +04:00
|
|
|
var _ = require('lodash'),
|
|
|
|
Promise = require('bluebird'),
|
2014-08-14 01:58:12 +04:00
|
|
|
requireTree = require('../require-tree'),
|
2014-07-13 15:17:18 +04:00
|
|
|
models;
|
2013-06-01 18:47:41 +04:00
|
|
|
|
2014-07-13 15:17:18 +04:00
|
|
|
models = {
|
2014-08-14 01:58:12 +04:00
|
|
|
excludeFiles: ['_messages', 'basetoken.js', 'base.js', 'index.js'],
|
2013-11-24 18:29:36 +04:00
|
|
|
|
2014-08-14 01:58:12 +04:00
|
|
|
// ### 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}
|
2013-06-25 15:43:15 +04:00
|
|
|
init: function () {
|
2014-08-14 01:58:12 +04:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
// One off inclusion of Base file.
|
|
|
|
self.Base = require('./base');
|
|
|
|
|
|
|
|
// Require all files in this directory
|
2014-10-02 03:19:46 +04:00
|
|
|
return requireTree.readAll(__dirname, {followSymlinks: false}).then(function (modelFiles) {
|
2014-08-14 01:58:12 +04:00
|
|
|
// 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;
|
|
|
|
});
|
2013-08-03 19:11:16 +04:00
|
|
|
},
|
2013-12-25 04:05:20 +04:00
|
|
|
// ### deleteAllContent
|
|
|
|
// Delete all content from the database (posts, tags, tags_posts)
|
|
|
|
deleteAllContent: function () {
|
|
|
|
var self = this;
|
|
|
|
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
return self.Post.findAll().then(function (posts) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.all(_.map(posts.toJSON(), function (post) {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
return self.Post.destroy({id: post.id});
|
2014-03-24 17:49:23 +04:00
|
|
|
}));
|
2013-12-25 04:05:20 +04:00
|
|
|
}).then(function () {
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
return self.Tag.findAll().then(function (tags) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.all(_.map(tags.toJSON(), function (tag) {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
return self.Tag.destroy({id: tag.id});
|
2014-03-24 17:49:23 +04:00
|
|
|
}));
|
2013-12-25 04:05:20 +04:00
|
|
|
});
|
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
|
|
|
};
|
2014-07-13 15:17:18 +04:00
|
|
|
|
|
|
|
module.exports = models;
|