mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Update seeder to chunk
This commit is contained in:
@@ -2,8 +2,8 @@ import Knex, { ColumnBuilder } from 'knex';
|
||||
import fse from 'fs-extra';
|
||||
import path from 'path';
|
||||
import yaml from 'js-yaml';
|
||||
import { types } from '../types';
|
||||
import { isObject, merge } from 'lodash';
|
||||
import { types, Item } from '../types';
|
||||
import { isObject, merge, reduce } from 'lodash';
|
||||
|
||||
type SeedData = {
|
||||
tables?: {
|
||||
@@ -106,8 +106,30 @@ export default async function runSeed(knex: Knex, seed: string) {
|
||||
return merge({}, defaults, row);
|
||||
});
|
||||
|
||||
await transaction(table).insert(dataWithDefaults);
|
||||
const chunks = chunkArray(dataWithDefaults, 25);
|
||||
|
||||
for (const chunk of chunks) {
|
||||
await transaction(table).insert(chunk);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function chunkArray(array: Item[], chunkSize: number) {
|
||||
return reduce(
|
||||
array,
|
||||
(result: Item[][], value) => {
|
||||
let lastChunk: Item[] = result[result.length - 1];
|
||||
|
||||
if (lastChunk.length < chunkSize) {
|
||||
lastChunk.push(value);
|
||||
} else {
|
||||
result.push([value]);
|
||||
}
|
||||
|
||||
return result;
|
||||
},
|
||||
[[]]
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user