2014-06-03 17:05:25 +04:00
|
|
|
// # DB API
|
|
|
|
// API for DB operations
|
2018-07-30 13:28:05 +03:00
|
|
|
const Promise = require('bluebird'),
|
2017-12-14 00:20:02 +03:00
|
|
|
pipeline = require('../lib/promise/pipeline'),
|
2017-12-14 00:14:19 +03:00
|
|
|
localUtils = require('./utils'),
|
2017-09-12 18:31:14 +03:00
|
|
|
exporter = require('../data/export'),
|
|
|
|
importer = require('../data/importer'),
|
|
|
|
backupDatabase = require('../data/db/backup'),
|
|
|
|
models = require('../models'),
|
2017-12-12 00:47:46 +03:00
|
|
|
common = require('../lib/common'),
|
2018-07-30 13:28:05 +03:00
|
|
|
docName = 'db';
|
|
|
|
|
|
|
|
let db;
|
2013-10-09 12:52:58 +04:00
|
|
|
|
2014-06-03 17:05:25 +04:00
|
|
|
/**
|
|
|
|
* ## DB API Methods
|
|
|
|
*
|
2017-12-14 16:13:40 +03:00
|
|
|
* **See:** [API Methods](constants.js.html#api%20methods)
|
2014-06-03 17:05:25 +04:00
|
|
|
*/
|
2013-10-09 12:52:58 +04:00
|
|
|
db = {
|
2017-08-22 13:15:40 +03:00
|
|
|
/**
|
|
|
|
* ### Archive Content
|
2018-01-11 18:03:21 +03:00
|
|
|
* Generate the JSON to export
|
2017-08-22 13:15:40 +03:00
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @returns {Promise} Ghost Export JSON format
|
|
|
|
*/
|
2018-01-11 18:03:21 +03:00
|
|
|
backupContent: function (options) {
|
2018-07-30 13:28:05 +03:00
|
|
|
let tasks;
|
2017-08-22 13:15:40 +03:00
|
|
|
|
2018-01-11 18:03:21 +03:00
|
|
|
options = options || {};
|
2017-08-22 13:15:40 +03:00
|
|
|
|
2018-01-11 18:03:21 +03:00
|
|
|
function jsonResponse(filename) {
|
|
|
|
return {db: [{filename: filename}]};
|
|
|
|
}
|
|
|
|
|
|
|
|
tasks = [
|
|
|
|
backupDatabase,
|
|
|
|
jsonResponse
|
|
|
|
];
|
|
|
|
|
|
|
|
return pipeline(tasks, options);
|
2017-08-22 13:15:40 +03:00
|
|
|
},
|
2014-06-03 17:05:25 +04:00
|
|
|
/**
|
|
|
|
* ### Export Content
|
|
|
|
* Generate the JSON to export
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @param {{context}} options
|
|
|
|
* @returns {Promise} Ghost Export JSON format
|
|
|
|
*/
|
2017-03-03 01:00:01 +03:00
|
|
|
exportContent: function exportContent(options) {
|
2018-07-30 13:28:05 +03:00
|
|
|
let tasks;
|
2015-10-20 15:11:49 +03:00
|
|
|
|
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
|
|
|
options = options || {};
|
2014-04-08 17:40:33 +04:00
|
|
|
|
2014-02-27 19:48:38 +04:00
|
|
|
// Export data, otherwise send error 500
|
2018-07-30 18:21:52 +03:00
|
|
|
function exportContent(options) {
|
|
|
|
return exporter.doExport({include: options.include}).then((exportedData) => {
|
2018-07-30 13:28:05 +03:00
|
|
|
return {
|
|
|
|
db: [exportedData]
|
|
|
|
};
|
|
|
|
}).catch((err) => {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.GhostError({err: err}));
|
2014-04-08 17:40:33 +04:00
|
|
|
});
|
2015-10-20 15:11:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tasks = [
|
2017-12-14 00:14:19 +03:00
|
|
|
localUtils.handlePermissions(docName, 'exportContent'),
|
2018-07-30 18:21:52 +03:00
|
|
|
localUtils.convertOptions(exporter.EXCLUDED_TABLES, null, {forModel: false}),
|
2015-10-20 15:11:49 +03:00
|
|
|
exportContent
|
|
|
|
];
|
|
|
|
|
|
|
|
return pipeline(tasks, options);
|
2013-10-09 12:52:58 +04:00
|
|
|
},
|
2014-06-03 17:05:25 +04:00
|
|
|
/**
|
|
|
|
* ### Import Content
|
|
|
|
* Import posts, tags etc from a JSON blob
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @param {{context}} options
|
|
|
|
* @returns {Promise} Success
|
|
|
|
*/
|
2017-03-03 01:00:01 +03:00
|
|
|
importContent: function importContent(options) {
|
2018-07-30 13:28:05 +03:00
|
|
|
let tasks;
|
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
|
|
|
options = options || {};
|
2013-10-09 12:52:58 +04:00
|
|
|
|
2015-10-20 15:11:49 +03:00
|
|
|
function importContent(options) {
|
2016-03-30 06:31:31 +03:00
|
|
|
return importer.importFromFile(options)
|
2018-07-30 13:28:05 +03:00
|
|
|
// NOTE: response can contain 2 objects if images are imported
|
|
|
|
.then((response) => {
|
|
|
|
return {
|
|
|
|
db: [],
|
|
|
|
problems: response.length === 2 ? response[1].problems : response[0].problems
|
|
|
|
};
|
2017-05-23 19:18:13 +03:00
|
|
|
});
|
2015-10-20 15:11:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tasks = [
|
2017-12-14 00:14:19 +03:00
|
|
|
localUtils.handlePermissions(docName, 'importContent'),
|
2015-10-20 15:11:49 +03:00
|
|
|
importContent
|
|
|
|
];
|
|
|
|
|
|
|
|
return pipeline(tasks, options);
|
2013-12-25 04:05:20 +04:00
|
|
|
},
|
2014-06-03 17:05:25 +04:00
|
|
|
/**
|
|
|
|
* ### Delete All Content
|
|
|
|
* Remove all posts and tags
|
|
|
|
*
|
|
|
|
* @public
|
|
|
|
* @param {{context}} options
|
|
|
|
* @returns {Promise} Success
|
|
|
|
*/
|
2017-03-03 01:00:01 +03:00
|
|
|
deleteAllContent: function deleteAllContent(options) {
|
2018-07-30 13:28:05 +03:00
|
|
|
let tasks;
|
|
|
|
const queryOpts = {columns: 'id', context: {internal: true}, destroyAll: true};
|
2015-10-20 15:11:49 +03:00
|
|
|
|
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
|
|
|
options = options || {};
|
2014-04-08 17:40:33 +04:00
|
|
|
|
2018-04-06 15:34:07 +03:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2015-10-20 15:11:49 +03:00
|
|
|
function deleteContent() {
|
2018-07-30 13:28:05 +03:00
|
|
|
return models.Base.transaction((transacting) => {
|
2018-04-06 15:34:07 +03:00
|
|
|
queryOpts.transacting = transacting;
|
|
|
|
|
|
|
|
return models.Post.findAll(queryOpts)
|
|
|
|
.then((response) => {
|
|
|
|
return Promise.map(response.models, (post) => {
|
2018-07-30 13:28:05 +03:00
|
|
|
return models.Post.destroy(Object.assign({id: post.id}, queryOpts));
|
2018-04-06 15:34:07 +03:00
|
|
|
}, {concurrency: 100});
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
return models.Tag.findAll(queryOpts);
|
|
|
|
})
|
|
|
|
.then((response) => {
|
|
|
|
return Promise.map(response.models, (tag) => {
|
2018-07-30 13:28:05 +03:00
|
|
|
return models.Tag.destroy(Object.assign({id: tag.id}, queryOpts));
|
2018-04-06 15:34:07 +03:00
|
|
|
}, {concurrency: 100});
|
|
|
|
})
|
|
|
|
.return({db: []})
|
|
|
|
.catch((err) => {
|
|
|
|
throw new common.errors.GhostError({
|
|
|
|
err: err
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-10-20 15:11:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
tasks = [
|
2017-12-14 00:14:19 +03:00
|
|
|
localUtils.handlePermissions(docName, 'deleteAllContent'),
|
2016-02-14 12:04:43 +03:00
|
|
|
backupDatabase,
|
2015-10-20 15:11:49 +03:00
|
|
|
deleteContent
|
|
|
|
];
|
|
|
|
|
|
|
|
return pipeline(tasks, options);
|
2013-11-01 16:12:01 +04:00
|
|
|
}
|
2013-10-09 12:52:58 +04:00
|
|
|
};
|
|
|
|
|
2014-01-29 08:18:00 +04:00
|
|
|
module.exports = db;
|