Ghost/ghost/data-generator/lib/utils/JsonImporter.js
Simon Backx d2cb23c3fa
Wired up Docker setup script and increased data generation performance (#19420)
ref PROD-233

- Stored whether Docker is used in the config files
- When running `yarn setup`, any existing Docker container will be
reset. Run with `-y` to skip the confirmation step.
- `yarn setup` will always init the database and generate fake data
- Increased amount of default generated data to 100,000 members + 500
posts.
- Made lots of performance improvements in the data generator so we can
generate the default data in ±170s
2024-01-05 13:42:30 +00:00

37 lines
946 B
JavaScript

class JsonImporter {
constructor(knex, transaction) {
this.knex = knex;
this.transaction = transaction;
}
/**
* @typedef {Object} JsonImportOptions
* @property {string} name Name of the table to import
* @property {Object} data Models without ids to be imported
* @property {Array<string>} [rows] Set of rows to be returned
*/
/**
* Import a dataset to the database
* @param {JsonImportOptions} options
* @returns {Promise}
*/
async import({
name,
data,
rows = []
}) {
for (const obj of data) {
if (!('id' in obj)) {
obj.id = this.fastFakeObjectId();
}
}
if (rows.findIndex(row => row === 'id') === -1) {
rows.unshift('id');
}
await this.knex.batchInsert(name, data, 500).transacting(this.transaction);
}
}
module.exports = JsonImporter;