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

33 lines
1001 B
JavaScript

const {faker} = require('@faker-js/faker');
const TableImporter = require('./TableImporter');
class PostsAuthorsImporter extends TableImporter {
static table = 'posts_authors';
static dependencies = ['posts', 'users'];
constructor(knex, transaction) {
super(PostsAuthorsImporter.table, knex, transaction);
this.sortOrder = 0;
}
async import(quantity) {
const posts = await this.transaction.select('id').from('posts');
this.users = await this.transaction.select('id').from('users');
await this.importForEach(posts, quantity ? quantity / posts.length : 1);
}
generate() {
const sortOrder = this.sortOrder;
this.sortOrder = this.sortOrder + 1;
return {
id: this.fastFakeObjectId(),
post_id: this.model.id,
author_id: this.users[faker.datatype.number(this.users.length - 1)].id,
sort_order: sortOrder
};
}
}
module.exports = PostsAuthorsImporter;