Make use of ES6 arrow functions in our data importer

no issue

- reduces the usage of `self`
This commit is contained in:
kirrg001 2018-01-28 15:48:24 +01:00
parent 2a10c83d92
commit 39ee95cc07
5 changed files with 91 additions and 98 deletions

View File

@ -9,8 +9,6 @@ const debug = require('ghost-ignition').debug('importer:base'),
class Base {
constructor(allDataFromFile, options) {
let self = this;
this.options = options;
this.modelName = options.modelName;
@ -23,9 +21,9 @@ class Base {
};
this.legacyKeys = {};
this.legacyMapper = function legacyMapper(item) {
return _.mapKeys(item, function matchLegacyKey(value, key) {
return self.legacyKeys[key] || key;
this.legacyMapper = (item) => {
return _.mapKeys(item, (value, key) => {
return this.legacyKeys[key] || key;
});
};
@ -50,8 +48,8 @@ class Base {
* Never ever import these attributes!
*/
stripProperties(properties) {
_.each(this.dataToImport, function (obj) {
_.each(properties, function (property) {
_.each(this.dataToImport, (obj) => {
_.each(properties, (property) => {
delete obj[property];
});
});
@ -61,16 +59,14 @@ class Base {
* Clean invalid values.
*/
sanitizeValues() {
let self = this;
_.each(this.dataToImport, function (obj) {
_.each(_.pick(obj, ['updated_at', 'created_at', 'published_at']), function (value, key) {
_.each(this.dataToImport, (obj) => {
_.each(_.pick(obj, ['updated_at', 'created_at', 'published_at']), (value, key) => {
let temporaryDate = new Date(value);
if (isNaN(temporaryDate)) {
self.problems.push({
this.problems.push({
message: 'Date is in a wrong format and invalid. It was replaced with the current timestamp.',
help: self.modelName,
help: this.modelName,
context: JSON.stringify(obj)
});
@ -87,20 +83,20 @@ class Base {
}
handleError(errs, obj) {
let self = this, errorsToReject = [], problems = [];
let errorsToReject = [], problems = [];
// CASE: validation errors, see models/base/events.js onValidate
if (!_.isArray(errs)) {
errs = [errs];
}
_.each(errs, function (err) {
_.each(errs, (err) => {
if (err.code && err.message.toLowerCase().indexOf('unique') !== -1) {
if (self.errorConfig.allowDuplicates) {
if (self.errorConfig.returnDuplicates) {
if (this.errorConfig.allowDuplicates) {
if (this.errorConfig.returnDuplicates) {
problems.push({
message: 'Entry was not imported and ignored. Detected duplicated entry.',
help: self.modelName,
help: this.modelName,
context: JSON.stringify(obj),
err: err
});
@ -108,16 +104,16 @@ class Base {
} else {
errorsToReject.push(new common.errors.DataImportError({
message: 'Detected duplicated entry.',
help: self.modelName,
help: this.modelName,
context: JSON.stringify(obj),
err: err
}));
}
} else if (err instanceof common.errors.NotFoundError) {
if (self.showNotFoundWarning) {
if (this.errorConfig.showNotFoundWarning) {
problems.push({
message: 'Entry was not imported and ignored. Could not find entry.',
help: self.modelName,
help: this.modelName,
context: JSON.stringify(obj),
err: err
});
@ -127,7 +123,7 @@ class Base {
err = new common.errors.DataImportError({
message: err.message,
context: JSON.stringify(obj),
help: self.modelName,
help: this.modelName,
errorType: err.errorType,
err: err
});
@ -153,24 +149,24 @@ class Base {
doImport(options, importOptions) {
debug('doImport', this.modelName, this.dataToImport.length);
let self = this, ops = [];
let ops = [];
_.each(this.dataToImport, function forEachDataToImport(obj) {
ops.push(function addModel() {
return models[self.modelName].add(obj, options)
.then(function (importedModel) {
_.each(this.dataToImport, (obj) => {
ops.push(() => {
return models[this.modelName].add(obj, options)
.then((importedModel) => {
obj.model = {
id: importedModel.id
};
if (importOptions.returnImportedData) {
self.importedDataToReturn.push(importedModel.toJSON());
this.importedDataToReturn.push(importedModel.toJSON());
}
return importedModel;
})
.catch(function (err) {
return self.handleError(err, obj);
.catch((err) => {
return this.handleError(err, obj);
})
.reflect();
});
@ -195,30 +191,28 @@ class Base {
* - we update all fields after the import (!)
*/
afterImport(options) {
let self = this;
debug('afterImport', this.modelName);
return models.User.getOwnerUser(options)
.then(function (ownerUser) {
return Promise.each(self.dataToImport, function (obj) {
.then((ownerUser) => {
return Promise.each(this.dataToImport, (obj) => {
if (!obj.model) {
return;
}
return Promise.each(['author_id', 'published_by', 'created_by', 'updated_by'], function (key) {
return Promise.each(['author_id', 'published_by', 'created_by', 'updated_by'], (key) => {
// CASE: not all fields exist on each model, skip them if so
if (!obj[key]) {
return Promise.resolve();
}
let oldUser = _.find(self.requiredFromFile.users, {id: obj[key]});
let oldUser = _.find(this.requiredFromFile.users, {id: obj[key]});
if (!oldUser) {
self.problems.push({
this.problems.push({
message: 'Entry was imported, but we were not able to update user reference field: ' +
key + '. The user does not exist, fallback to owner user.',
help: self.modelName,
help: this.modelName,
context: JSON.stringify(obj)
});
@ -230,7 +224,7 @@ class Base {
return models.User.findOne({
email: oldUser.email,
status: 'all'
}, options).then(function (userModel) {
}, options).then((userModel) => {
// CASE: user could not be imported e.g. multiple roles attached
if (!userModel) {
userModel = {
@ -250,7 +244,7 @@ class Base {
context = {};
}
return models[self.modelName].edit(dataToEdit, _.merge({}, options, {id: obj.model.id}, context));
return models[this.modelName].edit(dataToEdit, _.merge({}, options, {id: obj.model.id}, context));
});
});
});

View File

@ -20,7 +20,7 @@ class PostsImporter extends BaseImporter {
}
sanitizeAttributes() {
_.each(this.dataToImport, function (obj) {
_.each(this.dataToImport, (obj) => {
if (!validation.validator.isUUID(obj.uuid || '')) {
obj.uuid = uuid.v4();
}
@ -35,14 +35,13 @@ class PostsImporter extends BaseImporter {
addTagsToPosts() {
let postTags = this.requiredFromFile.posts_tags,
postsWithTags = new Map(),
self = this,
duplicatedTagsPerPost = {},
tagsToAttach = [],
foundOriginalTag;
postTags = _.orderBy(postTags, ['post_id', 'sort_order'], ['asc', 'asc']);
_.each(postTags, function (postTag) {
_.each(postTags, (postTag) => {
if (!postsWithTags.get(postTag.post_id)) {
postsWithTags.set(postTag.post_id, []);
}
@ -58,11 +57,11 @@ class PostsImporter extends BaseImporter {
postsWithTags.get(postTag.post_id).push(postTag.tag_id);
});
postsWithTags.forEach(function (tagIds, postId) {
postsWithTags.forEach((tagIds, postId) => {
tagsToAttach = [];
_.each(tagIds, function (tagId) {
foundOriginalTag = _.find(self.requiredFromFile.tags, {id: tagId});
_.each(tagIds, (tagId) => {
foundOriginalTag = _.find(this.requiredFromFile.tags, {id: tagId});
if (!foundOriginalTag) {
return;
@ -71,21 +70,21 @@ class PostsImporter extends BaseImporter {
tagsToAttach.push(foundOriginalTag);
});
_.each(tagsToAttach, function (tag) {
_.each(self.dataToImport, function (obj) {
_.each(tagsToAttach, (tag) => {
_.each(this.dataToImport, (obj) => {
if (obj.id === postId) {
if (!_.isArray(obj.tags)) {
obj.tags = [];
}
if (duplicatedTagsPerPost.hasOwnProperty(postId) && duplicatedTagsPerPost[postId].length) {
self.problems.push({
this.problems.push({
message: 'Detected duplicated tags for: ' + obj.title || obj.slug,
help: self.modelName,
help: this.modelName,
context: JSON.stringify({
tags: _.map(_.filter(self.requiredFromFile.tags, function (tag) {
tags: _.map(_.filter(this.requiredFromFile.tags, (tag) => {
return _.indexOf(duplicatedTagsPerPost[postId], tag.id) !== -1;
}), function (value) {
}), (value) => {
return value.slug || value.name;
})
})
@ -103,21 +102,21 @@ class PostsImporter extends BaseImporter {
beforeImport() {
debug('beforeImport');
let mobileDocContent, self = this;
let mobileDocContent;
this.sanitizeAttributes();
this.addTagsToPosts();
// Remove legacy field language
this.dataToImport = _.filter(this.dataToImport, function (data) {
this.dataToImport = _.filter(this.dataToImport, (data) => {
return _.omit(data, 'language');
});
this.dataToImport = this.dataToImport.map(self.legacyMapper);
this.dataToImport = this.dataToImport.map(this.legacyMapper);
// For legacy imports/custom imports with only html we can parse the markdown or html into a mobile doc card
// For now we can hardcode the version
_.each(this.dataToImport, function (model) {
_.each(this.dataToImport, (model) => {
if (!model.mobiledoc) {
if (model.markdown && model.markdown.length > 0) {
mobileDocContent = model.markdown;
@ -150,11 +149,11 @@ class PostsImporter extends BaseImporter {
// NOTE: We only support removing duplicate posts within the file to import.
// For any further future duplication detection, see https://github.com/TryGhost/Ghost/issues/8717.
let slugs = [];
this.dataToImport = _.filter(this.dataToImport, function (post) {
this.dataToImport = _.filter(this.dataToImport, (post) => {
if (slugs.indexOf(post.slug) !== -1) {
self.problems.push({
this.problems.push({
message: 'Entry was not imported and ignored. Detected duplicated entry.',
help: self.modelName,
help: this.modelName,
context: JSON.stringify({
post: post
})

View File

@ -37,25 +37,24 @@ class SettingsImporter extends BaseImporter {
beforeImport() {
debug('beforeImport');
let self = this,
ltsActiveTheme = _.find(this.dataToImport, {key: 'activeTheme'});
let ltsActiveTheme = _.find(this.dataToImport, {key: 'activeTheme'});
// If there is an lts we want to warn user that theme is not imported
if (ltsActiveTheme) {
self.problems.push({
this.problems.push({
message: 'Theme not imported, please upload in Settings - Design',
help: self.modelName,
help: this.modelName,
context: JSON.stringify(ltsActiveTheme)
});
}
// Remove core and theme data types
this.dataToImport = _.filter(this.dataToImport, function (data) {
this.dataToImport = _.filter(this.dataToImport, (data) => {
return ['core', 'theme'].indexOf(data.type) === -1;
});
_.each(this.dataToImport, function (obj) {
obj.key = self.legacySettingsKeyValues[obj.key] || obj.key;
_.each(this.dataToImport, (obj) => {
obj.key = this.legacySettingsKeyValues[obj.key] || obj.key;
if (obj.key === 'labs' && obj.value) {
// Overwrite the labs setting with our current defaults
@ -70,13 +69,13 @@ class SettingsImporter extends BaseImporter {
doImport(options) {
debug('doImport', this.dataToImport.length);
let self = this, ops = [];
let ops = [];
_.each(this.dataToImport, function (model) {
_.each(this.dataToImport, (model) => {
ops.push(
models.Settings.edit(model, options)
.catch(function (err) {
return self.handleError(err, model);
.catch((err) => {
return this.handleError(err, model);
})
.reflect()
);

View File

@ -36,32 +36,33 @@ class TagsImporter extends BaseImporter {
doImport(options, importOptions) {
debug('doImport', this.modelName, this.dataToImport.length);
let self = this, ops = [];
let ops = [];
this.dataToImport = this.dataToImport.map(self.legacyMapper);
this.dataToImport = this.dataToImport.map(this.legacyMapper);
_.each(this.dataToImport, function (obj) {
ops.push(models[self.modelName].findOne({name: obj.name}, options).then(function (tag) {
if (tag) {
return Promise.resolve();
}
_.each(this.dataToImport, (obj) => {
ops.push(models[this.modelName].findOne({name: obj.name}, options)
.then((tag) => {
if (tag) {
return Promise.resolve();
}
return models[self.modelName].add(obj, options)
.then(function (importedModel) {
obj.model = {
id: importedModel.id
};
return models[this.modelName].add(obj, options)
.then((importedModel) => {
obj.model = {
id: importedModel.id
};
if (importOptions.returnImportedData) {
self.importedDataToReturn.push(importedModel.toJSON());
}
if (importOptions.returnImportedData) {
this.importedDataToReturn.push(importedModel.toJSON());
}
return importedModel;
})
.catch(function (err) {
return self.handleError(err, obj);
});
}).reflect());
return importedModel;
})
.catch((err) => {
return this.handleError(err, obj);
});
}).reflect());
});
return Promise.all(ops);

View File

@ -31,17 +31,17 @@ class UsersImporter extends BaseImporter {
beforeImport() {
debug('beforeImport');
let self = this, role, lookup = {};
let role, lookup = {};
// Remove legacy field language
this.dataToImport = _.filter(this.dataToImport, function (data) {
this.dataToImport = _.filter(this.dataToImport, (data) => {
return _.omit(data, 'language');
});
this.dataToImport = this.dataToImport.map(self.legacyMapper);
this.dataToImport = this.dataToImport.map(this.legacyMapper);
// NOTE: sort out duplicated roles based on incremental id
_.each(this.requiredFromFile.roles_users, function (attachedRole) {
_.each(this.requiredFromFile.roles_users, (attachedRole) => {
if (lookup.hasOwnProperty(attachedRole.user_id)) {
if (lookup[attachedRole.user_id].id < attachedRole.id) {
lookup[attachedRole.user_id] = attachedRole;
@ -53,8 +53,8 @@ class UsersImporter extends BaseImporter {
this.requiredFromFile.roles_users = _.toArray(lookup);
_.each(this.requiredFromFile.roles_users, function (attachedRole) {
role = _.find(self.requiredFromFile.roles, function (role) {
_.each(this.requiredFromFile.roles_users, (attachedRole) => {
role = _.find(this.requiredFromFile.roles, (role) => {
if (attachedRole.role_id === role.id) {
return role;
}
@ -65,7 +65,7 @@ class UsersImporter extends BaseImporter {
role = {name: 'Author'};
}
_.each(self.dataToImport, function (obj) {
_.each(this.dataToImport, (obj) => {
if (attachedRole.user_id === obj.id) {
if (!_.isArray(obj.roles)) {
obj.roles = [];