mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
Split data exporter's index file into separate module
refs https://github.com/TryGhost/Team/issues/610 - This is a next step removing bloat from export module's index.js file, which get's rid of eslisn error completely
This commit is contained in:
parent
79439bdac5
commit
459e8215b9
3 changed files with 103 additions and 98 deletions
34
core/server/data/exporter/export-filename.js
Normal file
34
core/server/data/exporter/export-filename.js
Normal file
|
@ -0,0 +1,34 @@
|
|||
const _ = require('lodash');
|
||||
const logging = require('../../../shared/logging');
|
||||
const errors = require('@tryghost/errors');
|
||||
const security = require('@tryghost/security');
|
||||
const models = require('../../models');
|
||||
|
||||
const modelOptions = {context: {internal: true}};
|
||||
|
||||
const exportFileName = async function exportFileName(options) {
|
||||
const datetime = require('moment')().format('YYYY-MM-DD-HH-mm-ss');
|
||||
let title = '';
|
||||
|
||||
options = options || {};
|
||||
|
||||
// custom filename
|
||||
if (options.filename) {
|
||||
return options.filename + '.json';
|
||||
}
|
||||
|
||||
try {
|
||||
const settingsTitle = await models.Settings.findOne({key: 'title'}, _.merge({}, modelOptions, _.pick(options, 'transacting')));
|
||||
|
||||
if (settingsTitle) {
|
||||
title = security.string.safe(settingsTitle.get('value')) + '.';
|
||||
}
|
||||
|
||||
return title + 'ghost.' + datetime + '.json';
|
||||
} catch (err) {
|
||||
logging.error(new errors.GhostError({err: err}));
|
||||
return 'ghost.' + datetime + '.json';
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = exportFileName;
|
66
core/server/data/exporter/exporter.js
Normal file
66
core/server/data/exporter/exporter.js
Normal file
|
@ -0,0 +1,66 @@
|
|||
const _ = require('lodash');
|
||||
const Promise = require('bluebird');
|
||||
const db = require('../../data/db');
|
||||
const commands = require('../schema').commands;
|
||||
const ghostVersion = require('../../lib/ghost-version');
|
||||
const i18n = require('../../../shared/i18n');
|
||||
const errors = require('@tryghost/errors');
|
||||
|
||||
const {
|
||||
TABLES_ALLOWLIST,
|
||||
SETTING_KEYS_BLOCKLIST
|
||||
} = require('./table-lists');
|
||||
|
||||
const exportTable = function exportTable(tableName, options) {
|
||||
if (TABLES_ALLOWLIST.includes(tableName) ||
|
||||
(options.include && _.isArray(options.include) && options.include.indexOf(tableName) !== -1)) {
|
||||
const query = (options.transacting || db.knex)(tableName);
|
||||
|
||||
return query.select();
|
||||
}
|
||||
};
|
||||
|
||||
const getSettingsTableData = function getSettingsTableData(settingsData) {
|
||||
return settingsData && settingsData.filter((setting) => {
|
||||
return !SETTING_KEYS_BLOCKLIST.includes(setting.key);
|
||||
});
|
||||
};
|
||||
|
||||
const doExport = async function doExport(options) {
|
||||
options = options || {include: []};
|
||||
|
||||
try {
|
||||
const tables = await commands.getTables(options.transacting);
|
||||
|
||||
const tableData = await Promise.mapSeries(tables, function (tableName) {
|
||||
return exportTable(tableName, options);
|
||||
});
|
||||
|
||||
const exportData = {
|
||||
meta: {
|
||||
exported_on: new Date().getTime(),
|
||||
version: ghostVersion.full
|
||||
},
|
||||
data: {
|
||||
// Filled below
|
||||
}
|
||||
};
|
||||
|
||||
tables.forEach((name, i) => {
|
||||
if (name === 'settings') {
|
||||
exportData.data[name] = getSettingsTableData(tableData[i]);
|
||||
} else {
|
||||
exportData.data[name] = tableData[i];
|
||||
}
|
||||
});
|
||||
|
||||
return exportData;
|
||||
} catch (err) {
|
||||
throw new errors.DataExportError({
|
||||
err: err,
|
||||
context: i18n.t('errors.data.export.errorExportingData')
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = doExport;
|
|
@ -1,100 +1,5 @@
|
|||
const _ = require('lodash');
|
||||
const Promise = require('bluebird');
|
||||
const db = require('../../data/db');
|
||||
const commands = require('../schema').commands;
|
||||
const ghostVersion = require('../../lib/ghost-version');
|
||||
const i18n = require('../../../shared/i18n');
|
||||
const logging = require('../../../shared/logging');
|
||||
const errors = require('@tryghost/errors');
|
||||
const security = require('@tryghost/security');
|
||||
const models = require('../../models');
|
||||
const {
|
||||
BACKUP_TABLES,
|
||||
TABLES_ALLOWLIST,
|
||||
SETTING_KEYS_BLOCKLIST
|
||||
} = require('./table-lists');
|
||||
|
||||
const modelOptions = {context: {internal: true}};
|
||||
|
||||
const exportFileName = async function exportFileName(options) {
|
||||
const datetime = require('moment')().format('YYYY-MM-DD-HH-mm-ss');
|
||||
let title = '';
|
||||
|
||||
options = options || {};
|
||||
|
||||
// custom filename
|
||||
if (options.filename) {
|
||||
return options.filename + '.json';
|
||||
}
|
||||
|
||||
try {
|
||||
const settingsTitle = await models.Settings.findOne({key: 'title'}, _.merge({}, modelOptions, _.pick(options, 'transacting')));
|
||||
|
||||
if (settingsTitle) {
|
||||
title = security.string.safe(settingsTitle.get('value')) + '.';
|
||||
}
|
||||
|
||||
return title + 'ghost.' + datetime + '.json';
|
||||
} catch (err) {
|
||||
logging.error(new errors.GhostError({err: err}));
|
||||
return 'ghost.' + datetime + '.json';
|
||||
}
|
||||
};
|
||||
|
||||
const exportTable = function exportTable(tableName, options) {
|
||||
if (TABLES_ALLOWLIST.includes(tableName) ||
|
||||
(options.include && _.isArray(options.include) && options.include.indexOf(tableName) !== -1)) {
|
||||
const query = (options.transacting || db.knex)(tableName);
|
||||
|
||||
return query.select();
|
||||
}
|
||||
};
|
||||
|
||||
const getSettingsTableData = function getSettingsTableData(settingsData) {
|
||||
return settingsData && settingsData.filter((setting) => {
|
||||
return !SETTING_KEYS_BLOCKLIST.includes(setting.key);
|
||||
});
|
||||
};
|
||||
|
||||
const doExport = async function doExport(options) {
|
||||
options = options || {include: []};
|
||||
|
||||
try {
|
||||
const tables = await commands.getTables(options.transacting);
|
||||
|
||||
const tableData = await Promise.mapSeries(tables, function (tableName) {
|
||||
return exportTable(tableName, options);
|
||||
});
|
||||
|
||||
const exportData = {
|
||||
meta: {
|
||||
exported_on: new Date().getTime(),
|
||||
version: ghostVersion.full
|
||||
},
|
||||
data: {
|
||||
// Filled below
|
||||
}
|
||||
};
|
||||
|
||||
tables.forEach((name, i) => {
|
||||
if (name === 'settings') {
|
||||
exportData.data[name] = getSettingsTableData(tableData[i]);
|
||||
} else {
|
||||
exportData.data[name] = tableData[i];
|
||||
}
|
||||
});
|
||||
|
||||
return exportData;
|
||||
} catch (err) {
|
||||
throw new errors.DataExportError({
|
||||
err: err,
|
||||
context: i18n.t('errors.data.export.errorExportingData')
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
doExport: doExport,
|
||||
fileName: exportFileName,
|
||||
BACKUP_TABLES: BACKUP_TABLES
|
||||
doExport: require('./exporter'),
|
||||
fileName: require('./export-filename'),
|
||||
BACKUP_TABLES: require('./table-lists').BACKUP_TABLES
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue