Skip to content

Commit

Permalink
feat(apis): auto detect merge key before save ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Mar 16, 2020
1 parent 787acf6 commit 097056d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
12 changes: 12 additions & 0 deletions lib/helpers/montaineSaving.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@ exports.saving = (json, date) => {
}
throw new AppError('Saving failed', { code: 'HELPERS_ERROR' });
};

exports.getKeyForMerge = (json) => {
let result;
if (Array.isArray(json)) {
result = Object.keys(json[0]);
result = _.remove(result, (k) => k.charAt(0) === '@');
} if (typeof example === 'object' && typeof data === 'object') {
result = Object.keys(json);
result = _.remove(result, (k) => k.charAt(0) === '@');
}
return result;
};
2 changes: 1 addition & 1 deletion modules/apis/services/apis.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ exports.load = async (api) => {
// prepare for save
if (result.result) {
result.result = montaineSaving.saving(result.result, start);
await ApisRepository.import(_.camelCase(api.title), result.result, ['@id', '@date']);
await ApisRepository.import(_.camelCase(api.title), result.result, montaineSaving.getKeyForMerge(result.result));
}

const history = await HistoryRepository.create(montaineRequest.setScrapHistory(result.request, api, start));
Expand Down

0 comments on commit 097056d

Please sign in to comment.