Ghost/core/server/data/export/index.js
Harry Wolff be37070fb6 This aims to speed up both the ghost application and tests by
migration from usage of config() to just an object of config.

no relevant issue

- Change 'loadConfig' task to 'ensureConfig' to more accurately reflect
what it is actually doing.  Its sole purpose is to make sure a `config.js`
 file exists, and as such the name now reflects that purpose.

- Update config/index.js to export the ghostConfig object directly
so that it can be accessed from other modules

- Update all references of config(). to config.
This was a blind global find all and replace, treat it as such.

- Fixes to tests to support new config access method

- Allow each test to still work when invoked invidually
2014-07-22 22:37:44 -04:00

64 lines
2.0 KiB
JavaScript

var _ = require('lodash'),
when = require('when'),
versioning = require('../versioning'),
config = require('../../config'),
utils = require('../utils'),
serverUtils = require('../../utils'),
errors = require('../../errors'),
settings = require('../../api/settings'),
excludedTables = ['accesstokens', 'refreshtokens', 'clients'],
exporter,
exportFileName;
exportFileName = function () {
var datetime = (new Date()).toJSON().substring(0, 10),
title = '';
return settings.read({key: 'title', context: {internal: true}}).then(function (result) {
if (result) {
title = serverUtils.safeString(result.settings[0].value) + '.';
}
return title + 'ghost.' + datetime + '.json';
}).catch(function (err) {
errors.logError(err);
return 'ghost.' + datetime + '.json';
});
};
exporter = function () {
return when.join(versioning.getDatabaseVersion(), utils.getTables()).then(function (results) {
var version = results[0],
tables = results[1],
selectOps = _.map(tables, function (name) {
if (excludedTables.indexOf(name) < 0) {
return config.database.knex(name).select();
}
});
return when.all(selectOps).then(function (tableData) {
var exportData = {
meta: {
exported_on: new Date().getTime(),
version: version
},
data: {
// Filled below
}
};
_.each(tables, function (name, i) {
exportData.data[name] = tableData[i];
});
return when.resolve(exportData);
}).catch(function (err) {
errors.logAndThrowError(err, 'Error exporting data', '');
});
});
};
module.exports = exporter;
module.exports.fileName = exportFileName;