mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-01 13:54:35 +03:00
4237446277
no issue - Consistent naming for postLookup - makes it easier to search and inspect the various usages - Cleanup unneeded code - Make res.render calls more consistent - add some consistency to the calls to res.render - Remove ancient reference to dataProvider - Let's call it models everywhere now... - Use consistent formatting across the API - we're no longer using alignment in vars - Misc other consistency changes in API - always refer to local utils as apiUtils - logical grouping of requires - dependencies, utils, "lib common" etc - use xAPI to refer to API endpoints, e.g. mailAPI, settingsAPI for clarity
142 lines
3.9 KiB
JavaScript
142 lines
3.9 KiB
JavaScript
// # DB API
|
|
// API for DB operations
|
|
var Promise = require('bluebird'),
|
|
path = require('path'),
|
|
fs = require('fs'),
|
|
pipeline = require('../utils/pipeline'),
|
|
apiUtils = require('./utils'),
|
|
exporter = require('../data/export'),
|
|
importer = require('../data/importer'),
|
|
backupDatabase = require('../data/db/backup'),
|
|
models = require('../models'),
|
|
config = require('../config'),
|
|
errors = require('../errors'),
|
|
utilsUrl = require('../utils/url'),
|
|
docName = 'db',
|
|
db;
|
|
|
|
/**
|
|
* ## DB API Methods
|
|
*
|
|
* **See:** [API Methods](index.js.html#api%20methods)
|
|
*/
|
|
db = {
|
|
/**
|
|
* ### Archive Content
|
|
* Generate the JSON to export - for Moya only
|
|
*
|
|
* @public
|
|
* @returns {Promise} Ghost Export JSON format
|
|
*/
|
|
backupContent: function () {
|
|
var props = {
|
|
data: exporter.doExport(),
|
|
filename: exporter.fileName()
|
|
};
|
|
|
|
return Promise.props(props)
|
|
.then(function successMessage(exportResult) {
|
|
var filename = path.resolve(utilsUrl.urlJoin(config.get('paths').contentPath, 'data', exportResult.filename));
|
|
|
|
return Promise.promisify(fs.writeFile)(filename, JSON.stringify(exportResult.data))
|
|
.then(function () {
|
|
return filename;
|
|
});
|
|
});
|
|
},
|
|
/**
|
|
* ### Export Content
|
|
* Generate the JSON to export
|
|
*
|
|
* @public
|
|
* @param {{context}} options
|
|
* @returns {Promise} Ghost Export JSON format
|
|
*/
|
|
exportContent: function exportContent(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 = [
|
|
apiUtils.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 importContent(options) {
|
|
var tasks;
|
|
options = options || {};
|
|
|
|
function importContent(options) {
|
|
return importer.importFromFile(options)
|
|
.then(function (response) {
|
|
// NOTE: response can contain 2 objects if images are imported
|
|
return {db: [], problems: response.length === 2 ? response[1].problems : response[0].problems};
|
|
});
|
|
}
|
|
|
|
tasks = [
|
|
apiUtils.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 deleteAllContent(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', queryOpts);
|
|
}).return({db: []})
|
|
.catch(function (err) {
|
|
throw new errors.GhostError({err: err});
|
|
});
|
|
}
|
|
|
|
tasks = [
|
|
apiUtils.handlePermissions(docName, 'deleteAllContent'),
|
|
backupDatabase,
|
|
deleteContent
|
|
];
|
|
|
|
return pipeline(tasks, options);
|
|
}
|
|
};
|
|
|
|
module.exports = db;
|