Moved batching logic inside the members importer module

no issue

- This way importer is more self contained and controller logic doesn't have to know about batch sizes and other unecessary variables
This commit is contained in:
Nazar Gargol 2020-08-13 20:31:11 +12:00 committed by naz
parent 39aab8ae28
commit a6ab9a6db2

View File

@ -124,11 +124,19 @@ const doImport = async ({membersBatch: members, allLabelModels, importSetLabels,
try {
// TODO: below inserts most likely need to be wrapped into transaction
// to avoid creating orphaned member_labels connections
await db.knex('members')
.insert(mappedMemberBatchData);
const CHUNK_SIZE = 5000;
const chunkedMembers = _.chunk(mappedMemberBatchData, CHUNK_SIZE);
for (const data of chunkedMembers) {
await db.knex('members')
.insert(data);
}
await db.knex('members_labels')
.insert(mappedMembersLabelsBatchAssociations);
const chunkedLebelAssociations = _.chunk(mappedMembersLabelsBatchAssociations, CHUNK_SIZE);
for (const data of chunkedLebelAssociations) {
await db.knex('members_labels')
.insert(data);
}
imported.count += mappedMemberBatchData.length;
} catch (error) {