Ghost/core/server/data/exporter/index.js
Hannah Wolfe 22e13acd65 Updated var declarations to const/let and no lists
- All var declarations are now const or let as per ES6
- All comma-separated lists / chained declarations are now one declaration per line
- This is for clarity/readability but also made running the var-to-const/let switch smoother
- ESLint rules updated to match

How this was done:

- npm install -g jscodeshift
- git clone https://github.com/cpojer/js-codemod.git
- git clone git@github.com:TryGhost/Ghost.git shallow-ghost
- cd shallow-ghost
- jscodeshift -t ../js-codemod/transforms/unchain-variables.js . -v=2
- jscodeshift -t ../js-codemod/transforms/no-vars.js . -v=2
- yarn
- yarn test
- yarn lint / fix various lint errors (almost all indent) by opening files and saving in vscode
- grunt test-regression
- sorted!
2020-04-29 16:51:13 +01:00

120 lines
3.3 KiB
JavaScript

const _ = require('lodash');
const Promise = require('bluebird');
const db = require('../../data/db');
const commands = require('../schema').commands;
const ghostVersion = require('../../lib/ghost-version');
const common = require('../../lib/common');
const security = require('../../lib/security');
const models = require('../../models');
const EXCLUDED_TABLES = ['sessions', 'mobiledoc_revisions'];
const EXCLUDED_FIELDS_CONDITIONS = {
settings: [{
operator: 'whereNot',
key: 'key',
value: 'permalinks'
}]
};
const modelOptions = {context: {internal: true}};
// private
let getVersionAndTables;
let exportTable;
// public
let doExport;
let exportFileName;
exportFileName = function exportFileName(options) {
const datetime = require('moment')().format('YYYY-MM-DD-HH-mm-ss');
let title = '';
options = options || {};
// custom filename
if (options.filename) {
return Promise.resolve(options.filename + '.json');
}
return models.Settings.findOne({key: 'title'}, _.merge({}, modelOptions, _.pick(options, 'transacting'))).then(function (result) {
if (result) {
title = security.string.safe(result.get('value')) + '.';
}
return title + 'ghost.' + datetime + '.json';
}).catch(function (err) {
common.logging.error(new common.errors.GhostError({err: err}));
return 'ghost.' + datetime + '.json';
});
};
getVersionAndTables = function getVersionAndTables(options) {
const props = {
version: ghostVersion.full,
tables: commands.getTables(options.transacting)
};
return Promise.props(props);
};
exportTable = function exportTable(tableName, options) {
if (EXCLUDED_TABLES.indexOf(tableName) < 0 ||
(options.include && _.isArray(options.include) && options.include.indexOf(tableName) !== -1)) {
const query = (options.transacting || db.knex)(tableName);
if (EXCLUDED_FIELDS_CONDITIONS[tableName]) {
EXCLUDED_FIELDS_CONDITIONS[tableName].forEach((condition) => {
query[condition.operator](condition.key, condition.value);
});
}
return query.select();
}
};
doExport = function doExport(options) {
options = options || {include: []};
let tables;
let version;
return getVersionAndTables(options).then(function exportAllTables(result) {
tables = result.tables;
version = result.version;
return Promise.mapSeries(tables, function (tableName) {
return exportTable(tableName, options);
});
}).then(function formatData(tableData) {
const exportData = {
meta: {
exported_on: new Date().getTime(),
version: version
},
data: {
// Filled below
}
};
_.each(tables, function (name, i) {
exportData.data[name] = tableData[i];
});
return exportData;
}).catch(function (err) {
return Promise.reject(new common.errors.DataExportError({
err: err,
context: common.i18n.t('errors.data.export.errorExportingData')
}));
});
};
module.exports = {
doExport: doExport,
fileName: exportFileName,
EXCLUDED_TABLES: EXCLUDED_TABLES
};