Normalized database. Updated seed files. Simplified seed upsert.
This commit is contained in:
@@ -1,29 +1,18 @@
|
||||
'use strict';
|
||||
|
||||
async function upsert(table, items, duplicatesById, identifier = 'id', knex) {
|
||||
async function upsert(table, items, identifier = 'id', knex) {
|
||||
const duplicates = await knex(table).whereIn(identifier, items.map(item => item[identifier]));
|
||||
const duplicatesByIdentifier = duplicates.reduce((acc, item) => ({ ...acc, [item[identifier]]: item }), {});
|
||||
|
||||
const { insert, update } = items.reduce((acc, item) => {
|
||||
if (duplicatesById[item[identifier]]) {
|
||||
return {
|
||||
...acc,
|
||||
update: [
|
||||
...acc.update,
|
||||
{
|
||||
...duplicatesById[item[identifier]],
|
||||
...item,
|
||||
},
|
||||
],
|
||||
};
|
||||
if (duplicatesByIdentifier[item[identifier]]) {
|
||||
acc.update.push(item);
|
||||
return acc;
|
||||
}
|
||||
|
||||
return {
|
||||
...acc,
|
||||
insert: [
|
||||
...acc.insert,
|
||||
item,
|
||||
],
|
||||
};
|
||||
},
|
||||
{
|
||||
acc.insert.push(item);
|
||||
return acc;
|
||||
}, {
|
||||
insert: [],
|
||||
update: [],
|
||||
});
|
||||
@@ -32,13 +21,19 @@ async function upsert(table, items, duplicatesById, identifier = 'id', knex) {
|
||||
console.log(`${table}: Inserting ${insert.length}`);
|
||||
console.log(`${table}: Updating ${update.length}`);
|
||||
|
||||
return Promise.all([
|
||||
knex(table).insert(insert),
|
||||
const [inserted, updated] = await Promise.all([
|
||||
knex(table).returning('*').insert(insert),
|
||||
knex.transaction(async trx => Promise.all(update.map(item => trx
|
||||
.where({ [identifier]: item[identifier] })
|
||||
.update(item)
|
||||
.into(table)))),
|
||||
.into(table)
|
||||
.returning('*')))),
|
||||
]);
|
||||
|
||||
return {
|
||||
inserted: Array.isArray(inserted) ? inserted : [],
|
||||
updated: updated.reduce((acc, updatedItems) => acc.concat(updatedItems), []),
|
||||
};
|
||||
}
|
||||
|
||||
return { insert, update };
|
||||
|
||||
Reference in New Issue
Block a user