Update seeder to chunk

This commit is contained in:
rijkvanzanten
2020-07-29 15:03:04 -04:00
parent 4509f34457
commit 5894f5dbd4
2 changed files with 28 additions and 4 deletions

View File

@@ -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;
},
[[]]
);
}

View File

@@ -62,6 +62,8 @@ async function create(directory) {
await execa('npx', ['directus', 'init'], {
cwd: rootPath,
stdin: 'ignore',
stdio: 'inherit'
});
process.exit(1);
}