Ghost/ghost/data-generator/lib/utils/JsonImporter.js
Sam Lord 8c3e5ece01 Added option to disable fast import for data generator
Data generator uses CSV imports for a massive speed increase, but
can't be used in some environments where SQL admin isn't
available. This allows us to set a flag to use the original
insert-based importer.
2024-05-09 15:22:15 +01:00

40 lines
1.0 KiB
JavaScript

const TableImporter = require('../importers/TableImporter');
class JsonImporter extends TableImporter {
constructor(knex, transaction) {
super();
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;