Commit d9038368 authored by nanahira's avatar nanahira

add chunk saving

parent 62676f05
...@@ -92,13 +92,23 @@ export class CrudBase<T extends ValidCrudEntity<T>> { ...@@ -92,13 +92,23 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
let entsToSave = ents; let entsToSave = ents;
if (entsWithId.length) { if (entsWithId.length) {
const existingEnts = await repo.find({ const entIds = entsWithId.map((ent) => ent.id);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment const entIdChunks = _.chunk(entIds, 65535);
// @ts-ignore const existingEnts = (
where: { id: In<string | number>(entsWithId.map((ent) => ent.id)) }, await Promise.all(
select: ['id', 'deleteTime'], entIdChunks.map((chunk) =>
withDeleted: true, repo.find({
}); // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
where: {
id: In<string | number>(chunk),
},
select: ['id', 'deleteTime'],
withDeleted: true,
}),
),
)
).flat();
if (existingEnts.length) { if (existingEnts.length) {
const existingEntsWithoutDeleteTime = existingEnts.filter( const existingEntsWithoutDeleteTime = existingEnts.filter(
(ent) => ent.deleteTime == null, (ent) => ent.deleteTime == null,
...@@ -136,7 +146,17 @@ export class CrudBase<T extends ValidCrudEntity<T>> { ...@@ -136,7 +146,17 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
await beforeCreate(repo); await beforeCreate(repo);
} }
try { try {
const results = await repo.save(entsToSave as DeepPartial<T>[]); const entChunksToSave = _.chunk(
entsToSave,
Math.floor(
65535 / Math.max(1, Object.keys(entsToSave[0] || {}).length),
),
);
let results: T[] = [];
for (const entChunk of entChunksToSave) {
const savedChunk = await repo.save(entChunk);
results = results.concat(savedChunk);
}
return { return {
results, results,
skipped, skipped,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment