Ghost/core/server/api/v0.1/db.js

169 lines
5.1 KiB
JavaScript
Raw Normal View History

// # DB API
// API for DB operations
const Promise = require('bluebird'),
_ = require('lodash'),
pipeline = require('../../lib/promise/pipeline'),
localUtils = require('./utils'),
exporter = require('../../data/exporter'),
importer = require('../../data/importer'),
backupDatabase = require('../../data/db/backup'),
models = require('../../models'),
common = require('../../lib/common'),
docName = 'db';
let db;
/**
* ## DB API Methods
*
* **See:** [API Methods](constants.js.html#api%20methods)
*/
db = {
/**
* ### Archive Content
* Generate the JSON to export
*
* @public
* @returns {Promise} Ghost Export JSON format
*/
backupContent(options) {
let tasks;
options = options || {};
function jsonResponse(filename) {
return {db: [{filename: filename}]};
}
tasks = [
localUtils.convertOptions(exporter.EXCLUDED_TABLES, null, {forModel: false}),
backupDatabase,
jsonResponse
];
return pipeline(tasks, options);
},
/**
* ### Export Content
* Generate the JSON to export
*
* @public
* @param {{context}} options
* @returns {Promise} Ghost Export JSON format
*/
exportContent(options) {
let tasks;
options = options || {};
// Export data, otherwise send error 500
function exportContent(options) {
return exporter.doExport({include: options.include}).then((exportedData) => {
return {
db: [exportedData]
};
}).catch((err) => {
return Promise.reject(new common.errors.GhostError({err: err}));
});
}
tasks = [
localUtils.handlePermissions(docName, 'exportContent'),
localUtils.convertOptions(exporter.EXCLUDED_TABLES, null, {forModel: false}),
exportContent
];
return pipeline(tasks, options);
},
/**
* ### Import Content
* Import posts, tags etc from a JSON blob
*
* @public
* @param {{context}} options
* @returns {Promise} Success
*/
importContent(options) {
let tasks;
options = options || {};
function importContent(options) {
return importer.importFromFile(_.omit(options, 'include'), {include: options.include})
// NOTE: response can contain 2 objects if images are imported
.then((response) => {
return {
db: [],
problems: response.length === 2 ? response[1].problems : response[0].problems
};
🎨 refactor the importer (#8473) refs #5422 - we can support null titles after this PR if we want - user model: fix getAuthorRole - user model: support adding roles by name - we support this for roles as well, this makes it easier when importing related user roles (because usually roles already exists in the database and the related id's are wrong e.g. roles_users) - base model: support for null created_at or updated_at values - post or tag slugs are always safe strings - enable an import of a null slug, no need to crash or to cover this on import layer - add new DataImporter logic - uses a class inheritance mechanism to achieve an easier readability and maintenance - schema validation (happens on model layer) was ignored - allow to import unknown user id's (see https://github.com/TryGhost/Ghost/issues/8365) - most of the duplication handling happens on model layer (we can use the power of unique fields and errors from the database) - the import is splitted into three steps: - beforeImport --> prepares the data to import, sorts out relations (roles, tags), detects fields (for LTS) - doImport --> does the actual import - afterImport --> updates the data after successful import e.g. update all user reference fields e.g. published_by (compares the imported data with the current state of the database) - import images: markdown can be null - show error message when json handler can't parse file - do not request gravatar if email is null - return problems/warnings after successful import - optimise warnings in importer - do not return warnings for role duplications, no helpful information - error handler: return context information of error - we show the affected json entries as one line in the UI - show warning for: detected duplicated tag - schema validation: fix valueMustBeBoolean translation - remove context property from json parse error
2017-05-23 19:18:13 +03:00
});
}
tasks = [
localUtils.handlePermissions(docName, 'importContent'),
localUtils.convertOptions(exporter.EXCLUDED_TABLES, null, {forModel: false}),
importContent
];
return pipeline(tasks, options);
},
/**
* ### Delete All Content
* Remove all posts and tags
*
* @public
* @param {{context}} options
* @returns {Promise} Success
*/
deleteAllContent(options) {
let tasks;
const queryOpts = {columns: 'id', context: {internal: true}, destroyAll: true};
options = options || {};
/**
* @NOTE:
* We fetch all posts with `columns:id` to increase the speed of this endpoint.
* And if you trigger `post.destroy(..)`, this will trigger bookshelf and model events.
* But we only have to `id` available in the model. This won't work, because:
* - model layer can't trigger event e.g. `post.page` to trigger `post|page.unpublished`.
* - `onDestroyed` or `onDestroying` can contain custom logic
*/
function deleteContent() {
return models.Base.transaction((transacting) => {
queryOpts.transacting = transacting;
return models.Post.findAll(queryOpts)
.then((response) => {
return Promise.map(response.models, (post) => {
return models.Post.destroy(Object.assign({id: post.id}, queryOpts));
}, {concurrency: 100});
})
.then(() => {
return models.Tag.findAll(queryOpts);
})
.then((response) => {
return Promise.map(response.models, (tag) => {
return models.Tag.destroy(Object.assign({id: tag.id}, queryOpts));
}, {concurrency: 100});
})
.return({db: []})
.catch((err) => {
throw new common.errors.GhostError({
err: err
});
});
});
}
tasks = [
localUtils.handlePermissions(docName, 'deleteAllContent'),
backupDatabase,
deleteContent
];
return pipeline(tasks, options);
2013-11-01 16:12:01 +04:00
}
};
module.exports = db;