Ghost/ghost/data-generator/lib/importers/MembersStatusEventsImporter.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

48 lines
1.4 KiB
JavaScript

const TableImporter = require('./TableImporter');
const {faker} = require('@faker-js/faker');
const dateToDatabaseString = require('../utils/database-date');
class MembersStatusEventsImporter extends TableImporter {
static table = 'members_status_events';
static dependencies = ['members'];
constructor(knex, transaction) {
super(MembersStatusEventsImporter.table, knex, transaction);
}
async import(quantity) {
const members = await this.transaction.select('id', 'created_at', 'status').from('members');
await this.importForEach(members, quantity ? quantity / members.length : 2);
}
setReferencedModel(model) {
this.events = [{
id: faker.database.mongodbObjectId(),
member_id: model.id,
from_status: null,
to_status: 'free',
created_at: model.created_at
}];
if (model.status !== 'free') {
this.events.push({
id: faker.database.mongodbObjectId(),
member_id: model.id,
from_status: 'free',
to_status: model.status,
created_at: dateToDatabaseString(faker.date.between(new Date(model.created_at), new Date()))
});
}
}
generate() {
const event = this.events.shift();
if (!event) {
return null;
}
return event;
}
}
module.exports = MembersStatusEventsImporter;