mirror of
https://github.com/directus/directus.git
synced 2026-02-13 02:25:13 -05: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;
|
||||
},
|
||||
[[]]
|
||||
);
|
||||
}
|
||||
|
||||
@@ -62,6 +62,8 @@ async function create(directory) {
|
||||
|
||||
await execa('npx', ['directus', 'init'], {
|
||||
cwd: rootPath,
|
||||
stdin: 'ignore',
|
||||
stdio: 'inherit'
|
||||
});
|
||||
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user