Improved error handling for batch inserted records

no issue

- Similar to 3a594ce22e this adds error handling to batch operations done outside models
This commit is contained in:
Nazar Gargol 2020-08-20 19:01:17 +12:00
parent f8adb4a672
commit 8418c829de

View File

@ -3,21 +3,38 @@ const db = require('../../../data/db');
const CHUNK_SIZE = 100;
async function insertChunkSequential(table, chunk, result) {
for (const record of chunk) {
try {
await db.knex(table).insert(record);
result.successful += 1;
} catch (err) {
result.errors.push(err);
result.unsuccessfulIds.push(record.id);
result.unsuccessful += 1;
}
}
}
async function insertChunk(table, chunk, result) {
try {
await db.knex(table).insert(chunk);
result.successful += chunk.length;
} catch (err) {
await insertChunkSequential(table, chunk, result);
}
}
async function insert(table, data) {
const result = {
successful: 0,
unsuccessful: 0,
unsuccessfulIds: [],
errors: []
};
for (const chunk of _.chunk(data, CHUNK_SIZE)) {
try {
await db.knex(table).insert(chunk);
result.successful += chunk.length;
} catch (error) {
result.unsuccessful += chunk.length;
result.errors.push(error);
}
await insertChunk(table, chunk, result);
}
return result;