mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-03 08:25:06 +03:00
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:
parent
f8adb4a672
commit
8418c829de
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user