Ghost/core/server/api/db.js
Katharina Irrgang d81bc91bd2 Error creation (#7477)
refs #7116, refs #2001

- Changes the way Ghost errors are implemented to benefit from proper inheritance
- Moves all error definitions into a single file
- Changes the error constructor to take an options object, rather than needing the arguments to be passed in the correct order.
- Provides a wrapper so that any errors that haven't already been converted to GhostErrors get converted before they are displayed.

Summary of changes:

* 🐛  set NODE_ENV in config handler
*   add GhostError implementation (core/server/errors.js)
  - register all errors in one file
  - inheritance from GhostError
  - option pattern
* 🔥  remove all error files
*   wrap all errors into GhostError in case of HTTP
* 🎨  adaptions
  - option pattern for errors
  - use GhostError when needed
* 🎨  revert debug deletion and add TODO for error id's
2016-10-06 13:27:35 +01:00

118 lines
3.1 KiB
JavaScript

// # DB API
// API for DB operations
var Promise = require('bluebird'),
exporter = require('../data/export'),
importer = require('../data/importer'),
backupDatabase = require('../data/migration').backupDatabase,
models = require('../models'),
errors = require('../errors'),
utils = require('./utils'),
pipeline = require('../utils/pipeline'),
api = {},
docName = 'db',
db;
api.settings = require('./settings');
/**
* ## DB API Methods
*
* **See:** [API Methods](index.js.html#api%20methods)
*/
db = {
/**
* ### Export Content
* Generate the JSON to export
*
* @public
* @param {{context}} options
* @returns {Promise} Ghost Export JSON format
*/
exportContent: function (options) {
var tasks = [];
options = options || {};
// Export data, otherwise send error 500
function exportContent() {
return exporter.doExport().then(function (exportedData) {
return {db: [exportedData]};
}).catch(function (err) {
return Promise.reject(new errors.GhostError({err: err}));
});
}
tasks = [
utils.handlePermissions(docName, 'exportContent'),
exportContent
];
return pipeline(tasks, options);
},
/**
* ### Import Content
* Import posts, tags etc from a JSON blob
*
* @public
* @param {{context}} options
* @returns {Promise} Success
*/
importContent: function (options) {
var tasks = [];
options = options || {};
function importContent(options) {
return importer.importFromFile(options)
.then(function () {
api.settings.updateSettingsCache();
})
.return({db: []});
}
tasks = [
utils.handlePermissions(docName, 'importContent'),
importContent
];
return pipeline(tasks, options);
},
/**
* ### Delete All Content
* Remove all posts and tags
*
* @public
* @param {{context}} options
* @returns {Promise} Success
*/
deleteAllContent: function (options) {
var tasks,
queryOpts = {columns: 'id', context: {internal: true}};
options = options || {};
function deleteContent() {
var collections = [
models.Post.findAll(queryOpts),
models.Tag.findAll(queryOpts)
];
return Promise.each(collections, function then(Collection) {
return Collection.invokeThen('destroy');
}).return({db: []})
.catch(function (err) {
throw new errors.GhostError({err: err});
});
}
tasks = [
utils.handlePermissions(docName, 'deleteAllContent'),
backupDatabase,
deleteContent
];
return pipeline(tasks, options);
}
};
module.exports = db;