Ghost/ghost/data-generator/lib/importers/EmailBatchesImporter.js
Sam Lord 4ff467794f Entirely rewrote data generator to simplify codebase
refs: https://github.com/TryGhost/DevOps/issues/11

This is a pretty huge commit, but the relevant points are:
* Each importer no longer needs to be passed a set of data, it just gets the data it needs
* Each importer specifies its dependencies, so that the order of import can be determined at runtime using a topological sort
* The main data generator function can just tell each importer to import the data it has

This makes working on the data generator much easier.

Some other benefits are:
* Batched importing, massively speeding up the whole process
* `--tables` to set the exact tables you want to import, and specify the quantity of each
2023-08-04 13:36:09 +01:00

37 lines
1.4 KiB
JavaScript

const TableImporter = require('./TableImporter');
const {faker} = require('@faker-js/faker');
const dateToDatabaseString = require('../utils/database-date');
class EmailBatchesImporter extends TableImporter {
static table = 'email_batches';
static dependencies = ['emails'];
constructor(knex, transaction) {
super(EmailBatchesImporter.table, knex, transaction);
}
async import(quantity) {
const emails = await this.transaction.select('id', 'created_at').from('emails');
// TODO: Generate >1 batch per email
await this.importForEach(emails, quantity ?? emails.length);
}
generate() {
const emailSentDate = new Date(this.model.created_at);
const latestUpdatedDate = new Date(this.model.created_at);
latestUpdatedDate.setHours(latestUpdatedDate.getHours() + 1);
return {
id: faker.database.mongodbObjectId(),
email_id: this.model.id,
provider_id: `${new Date().toISOString().split('.')[0].replace(/[^0-9]/g, '')}.${faker.datatype.hexadecimal({length: 16, prefix: '', case: 'lower'})}@m.example.com`,
status: 'submitted', // TODO: introduce failures
created_at: this.model.created_at,
updated_at: dateToDatabaseString(faker.date.between(emailSentDate, latestUpdatedDate))
};
}
}
module.exports = EmailBatchesImporter;