2022-10-26 19:55:08 +03:00
|
|
|
const {faker} = require('@faker-js/faker');
|
2023-08-02 16:43:26 +03:00
|
|
|
const TableImporter = require('./TableImporter');
|
2022-10-26 19:55:08 +03:00
|
|
|
|
|
|
|
class PostsAuthorsImporter extends TableImporter {
|
2023-02-16 15:11:00 +03:00
|
|
|
static table = 'posts_authors';
|
2023-08-02 16:43:26 +03:00
|
|
|
static dependencies = ['posts', 'users'];
|
2023-02-16 15:11:00 +03:00
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
constructor(knex, transaction) {
|
|
|
|
super(PostsAuthorsImporter.table, knex, transaction);
|
2022-10-26 19:55:08 +03:00
|
|
|
this.sortOrder = 0;
|
|
|
|
}
|
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
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);
|
2022-10-26 19:55:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
generate() {
|
|
|
|
const sortOrder = this.sortOrder;
|
|
|
|
this.sortOrder = this.sortOrder + 1;
|
|
|
|
return {
|
2024-01-05 16:42:30 +03:00
|
|
|
id: this.fastFakeObjectId(),
|
2022-10-26 19:55:08 +03:00
|
|
|
post_id: this.model.id,
|
|
|
|
author_id: this.users[faker.datatype.number(this.users.length - 1)].id,
|
|
|
|
sort_order: sortOrder
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PostsAuthorsImporter;
|