2019-11-06 01:47:10 +00:00
|
|
|
'use strict';
|
|
|
|
|
2019-12-31 02:12:52 +00:00
|
|
|
const knex = require('../knex');
|
2020-02-07 18:53:16 +00:00
|
|
|
const logger = require('./logger')(__filename);
|
2019-12-31 02:12:52 +00:00
|
|
|
|
|
|
|
async function upsert(table, items, identifier = ['id'], _knex) {
|
|
|
|
const identifiers = Array.isArray(identifier) ? identifier : [identifier];
|
|
|
|
|
|
|
|
const duplicates = await knex(table).whereIn(identifiers, items.map(item => identifiers.map(identifierX => item[identifierX])));
|
|
|
|
const duplicatesByIdentifiers = duplicates.reduce((acc, duplicate) => {
|
|
|
|
const duplicateIdentifier = identifiers.map(identifierX => duplicate[identifierX]).toString();
|
|
|
|
|
|
|
|
return { ...acc, [duplicateIdentifier]: duplicate };
|
|
|
|
}, {});
|
2019-12-19 01:35:07 +00:00
|
|
|
|
2019-11-06 01:47:10 +00:00
|
|
|
const { insert, update } = items.reduce((acc, item) => {
|
2019-12-31 02:12:52 +00:00
|
|
|
const itemIdentifier = identifiers.map(identifierX => item[identifierX]).toString();
|
|
|
|
|
|
|
|
if (duplicatesByIdentifiers[itemIdentifier]) {
|
2019-12-19 01:35:07 +00:00
|
|
|
acc.update.push(item);
|
|
|
|
return acc;
|
2019-11-06 01:47:10 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 01:35:07 +00:00
|
|
|
acc.insert.push(item);
|
|
|
|
return acc;
|
|
|
|
}, {
|
2019-11-06 01:47:10 +00:00
|
|
|
insert: [],
|
|
|
|
update: [],
|
|
|
|
});
|
|
|
|
|
|
|
|
if (knex) {
|
2020-01-10 01:43:04 +00:00
|
|
|
logger.debug(`${table}: Inserting ${insert.length}`);
|
|
|
|
logger.debug(`${table}: Updating ${update.length}`);
|
2019-11-06 01:47:10 +00:00
|
|
|
|
2019-12-19 01:35:07 +00:00
|
|
|
const [inserted, updated] = await Promise.all([
|
|
|
|
knex(table).returning('*').insert(insert),
|
2019-12-31 02:12:52 +00:00
|
|
|
knex.transaction(async trx => Promise.all(update.map((item) => {
|
|
|
|
const clause = identifiers.reduce((acc, identifierX) => ({ ...acc, [identifierX]: item[identifierX] }), {});
|
|
|
|
|
|
|
|
return trx
|
|
|
|
.where(clause)
|
|
|
|
.update(item)
|
|
|
|
.into(table)
|
|
|
|
.returning('*');
|
|
|
|
}))),
|
2019-11-06 01:47:10 +00:00
|
|
|
]);
|
2019-12-19 01:35:07 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
inserted: Array.isArray(inserted) ? inserted : [],
|
|
|
|
updated: updated.reduce((acc, updatedItems) => acc.concat(updatedItems), []),
|
|
|
|
};
|
2019-11-06 01:47:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return { insert, update };
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = upsert;
|