2023-01-23 15:47:10 +03:00
|
|
|
const tables = require('./tables');
|
2023-02-16 15:11:00 +03:00
|
|
|
// Order here does not matter
|
2022-10-26 19:55:08 +03:00
|
|
|
const {
|
|
|
|
NewslettersImporter,
|
2023-02-16 15:11:00 +03:00
|
|
|
PostsImporter,
|
2022-10-26 19:55:08 +03:00
|
|
|
UsersImporter,
|
|
|
|
TagsImporter,
|
|
|
|
ProductsImporter,
|
|
|
|
MembersImporter,
|
|
|
|
BenefitsImporter,
|
2023-02-16 15:11:00 +03:00
|
|
|
MentionsImporter,
|
|
|
|
PostsAuthorsImporter,
|
|
|
|
PostsTagsImporter,
|
2022-10-26 19:55:08 +03:00
|
|
|
ProductsBenefitsImporter,
|
|
|
|
MembersProductsImporter,
|
|
|
|
PostsProductsImporter,
|
|
|
|
MembersNewslettersImporter,
|
|
|
|
StripeProductsImporter,
|
|
|
|
StripePricesImporter,
|
|
|
|
SubscriptionsImporter,
|
2023-02-16 15:11:00 +03:00
|
|
|
EmailsImporter,
|
|
|
|
MembersCreatedEventsImporter,
|
|
|
|
MembersLoginEventsImporter,
|
|
|
|
MembersStatusEventsImporter,
|
|
|
|
MembersSubscribeEventsImporter,
|
|
|
|
MembersSubscriptionCreatedEventsImporter,
|
2022-10-26 19:55:08 +03:00
|
|
|
MembersStripeCustomersImporter,
|
|
|
|
MembersStripeCustomersSubscriptionsImporter,
|
2023-02-17 17:03:52 +03:00
|
|
|
MembersPaidSubscriptionEventsImporter,
|
|
|
|
EmailBatchesImporter,
|
|
|
|
EmailRecipientsImporter,
|
|
|
|
RedirectsImporter,
|
|
|
|
MembersClickEventsImporter
|
2023-01-23 15:47:10 +03:00
|
|
|
} = tables;
|
2022-11-03 19:48:25 +03:00
|
|
|
const path = require('path');
|
2022-11-03 17:54:17 +03:00
|
|
|
const fs = require('fs/promises');
|
2022-10-26 19:55:08 +03:00
|
|
|
const {faker} = require('@faker-js/faker');
|
2022-11-03 17:54:17 +03:00
|
|
|
const JsonImporter = require('./utils/json-importer');
|
2022-11-03 19:48:25 +03:00
|
|
|
const {getProcessRoot} = require('@tryghost/root-utils');
|
2022-10-26 19:55:08 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} DataGeneratorOptions
|
2022-11-03 17:54:17 +03:00
|
|
|
* @property {string} baseDataPack
|
2022-10-26 19:55:08 +03:00
|
|
|
* @property {import('knex/types').Knex} knex
|
|
|
|
* @property {Object} schema
|
|
|
|
* @property {Object} logger
|
|
|
|
* @property {Object} modelQuantities
|
2023-01-23 15:47:10 +03:00
|
|
|
* @property {string} baseUrl
|
2023-02-16 15:11:00 +03:00
|
|
|
* @property {boolean} clearDatabase
|
2022-10-26 19:55:08 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
const defaultQuantities = {
|
|
|
|
members: () => faker.datatype.number({
|
|
|
|
min: 7000,
|
|
|
|
max: 8000
|
|
|
|
}),
|
2023-02-16 23:54:14 +03:00
|
|
|
// This will generate n * <members> events, is not worth being very high
|
|
|
|
membersLoginEvents: 5,
|
2022-10-26 19:55:08 +03:00
|
|
|
posts: () => faker.datatype.number({
|
|
|
|
min: 80,
|
|
|
|
max: 120
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
class DataGenerator {
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {DataGeneratorOptions} options
|
|
|
|
*/
|
|
|
|
constructor({
|
2022-11-03 17:54:17 +03:00
|
|
|
baseDataPack = '',
|
2022-10-26 19:55:08 +03:00
|
|
|
knex,
|
|
|
|
schema,
|
|
|
|
logger,
|
2023-01-23 15:47:10 +03:00
|
|
|
modelQuantities = {},
|
2023-02-16 15:11:00 +03:00
|
|
|
baseUrl,
|
|
|
|
clearDatabase
|
2022-10-26 19:55:08 +03:00
|
|
|
}) {
|
2022-11-03 17:54:17 +03:00
|
|
|
this.useBaseData = baseDataPack !== '';
|
|
|
|
this.baseDataPack = baseDataPack;
|
2022-10-26 19:55:08 +03:00
|
|
|
this.knex = knex;
|
|
|
|
this.schema = schema;
|
|
|
|
this.logger = logger;
|
|
|
|
this.modelQuantities = Object.assign({}, defaultQuantities, modelQuantities);
|
2023-01-23 15:47:10 +03:00
|
|
|
this.baseUrl = baseUrl;
|
2023-02-16 15:11:00 +03:00
|
|
|
this.clearDatabase = clearDatabase;
|
2022-10-26 19:55:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async importData() {
|
|
|
|
const transaction = await this.knex.transaction();
|
2023-02-16 15:11:00 +03:00
|
|
|
|
|
|
|
if (this.clearDatabase) {
|
|
|
|
this.logger.info('Clearing existing database');
|
|
|
|
|
|
|
|
// List of tables ordered to avoid dependencies when deleting
|
|
|
|
const tableNames = Object.values(tables).map(importer => importer.table).reverse();
|
|
|
|
for (const table of tableNames) {
|
|
|
|
this.logger.debug(`Clearing table ${table}`);
|
|
|
|
if (table === 'users') {
|
|
|
|
// Avoid deleting the admin user
|
|
|
|
await transaction(table).del().whereNot('id', '1');
|
|
|
|
continue;
|
|
|
|
}
|
2023-02-16 23:54:14 +03:00
|
|
|
await transaction(table).del();
|
2023-02-16 15:11:00 +03:00
|
|
|
}
|
|
|
|
this.logger.info('Finished clearing database');
|
|
|
|
}
|
|
|
|
|
2022-11-07 19:11:11 +03:00
|
|
|
this.logger.info('Starting import process, this has two parts: base data and member data. It can take a while...');
|
2022-10-26 19:55:08 +03:00
|
|
|
|
2022-10-27 18:09:04 +03:00
|
|
|
const usersImporter = new UsersImporter(transaction);
|
|
|
|
const users = await usersImporter.import({amount: 8});
|
2022-10-26 19:55:08 +03:00
|
|
|
|
2022-10-27 18:09:04 +03:00
|
|
|
let newsletters;
|
|
|
|
let posts;
|
|
|
|
let tags;
|
|
|
|
let products;
|
|
|
|
let stripeProducts;
|
|
|
|
let stripePrices;
|
2022-11-03 17:54:17 +03:00
|
|
|
let benefits;
|
2022-10-27 18:09:04 +03:00
|
|
|
|
|
|
|
// Use an existant set of data for a more realisitic looking site
|
|
|
|
if (this.useBaseData) {
|
2022-11-03 19:48:25 +03:00
|
|
|
let baseDataPack = this.baseDataPack;
|
|
|
|
if (!path.isAbsolute(this.baseDataPack)) {
|
|
|
|
baseDataPack = path.join(getProcessRoot(), baseDataPack);
|
|
|
|
}
|
|
|
|
let baseData = {};
|
|
|
|
try {
|
|
|
|
baseData = JSON.parse(await (await fs.readFile(baseDataPack)).toString());
|
2022-11-07 19:11:11 +03:00
|
|
|
this.logger.info('Read base data pack');
|
2022-11-03 19:48:25 +03:00
|
|
|
} catch (error) {
|
2022-11-07 19:11:11 +03:00
|
|
|
this.logger.error('Failed to read data pack: ', error);
|
2022-11-03 19:48:25 +03:00
|
|
|
throw error;
|
|
|
|
}
|
2022-11-07 19:11:11 +03:00
|
|
|
|
|
|
|
this.logger.info('Starting base data import');
|
2022-11-03 17:54:17 +03:00
|
|
|
const jsonImporter = new JsonImporter(transaction);
|
|
|
|
|
2022-10-27 18:09:04 +03:00
|
|
|
// Must have at least 2 in base data set
|
2022-11-03 17:54:17 +03:00
|
|
|
newsletters = await jsonImporter.import({
|
|
|
|
name: 'newsletters',
|
2022-11-03 19:48:25 +03:00
|
|
|
data: baseData.newsletters,
|
2023-02-09 01:12:19 +03:00
|
|
|
rows: ['sort_order']
|
2022-11-03 17:54:17 +03:00
|
|
|
});
|
2022-11-03 19:48:25 +03:00
|
|
|
newsletters.sort((a, b) => a.sort_order - b.sort_order);
|
2022-11-03 17:54:17 +03:00
|
|
|
|
2022-10-27 18:09:04 +03:00
|
|
|
const postsImporter = new PostsImporter(transaction, {
|
|
|
|
newsletters
|
|
|
|
});
|
2022-11-03 17:54:17 +03:00
|
|
|
posts = await jsonImporter.import({
|
|
|
|
name: 'posts',
|
|
|
|
data: baseData.posts
|
|
|
|
});
|
2022-11-03 19:48:25 +03:00
|
|
|
await postsImporter.addNewsletters({posts});
|
2023-02-06 13:40:18 +03:00
|
|
|
posts = await transaction.select('id', 'newsletter_id', 'published_at', 'slug').from('posts');
|
2022-10-27 18:09:04 +03:00
|
|
|
|
2022-11-03 17:54:17 +03:00
|
|
|
tags = await jsonImporter.import({
|
|
|
|
name: 'tags',
|
|
|
|
data: baseData.tags
|
|
|
|
});
|
2022-10-27 18:09:04 +03:00
|
|
|
|
2022-11-03 17:54:17 +03:00
|
|
|
products = await jsonImporter.import({
|
|
|
|
name: 'products',
|
|
|
|
data: baseData.products,
|
|
|
|
rows: ['name', 'monthly_price', 'yearly_price']
|
|
|
|
});
|
|
|
|
|
|
|
|
benefits = await jsonImporter.import({
|
|
|
|
name: 'benefits',
|
|
|
|
data: baseData.benefits
|
|
|
|
});
|
|
|
|
await jsonImporter.import({
|
|
|
|
name: 'products_benefits',
|
|
|
|
data: baseData.products_benefits
|
|
|
|
});
|
|
|
|
|
|
|
|
stripeProducts = await jsonImporter.import({
|
|
|
|
name: 'stripe_products',
|
|
|
|
data: baseData.stripe_products,
|
|
|
|
rows: ['product_id', 'stripe_product_id']
|
|
|
|
});
|
|
|
|
stripePrices = await jsonImporter.import({
|
|
|
|
name: 'stripe_prices',
|
|
|
|
data: baseData.stripe_prices,
|
|
|
|
rows: ['stripe_price_id', 'interval', 'stripe_product_id', 'currency', 'amount', 'nickname']
|
|
|
|
});
|
|
|
|
|
|
|
|
// Import settings
|
2022-11-07 19:51:01 +03:00
|
|
|
await transaction('settings').delete();
|
2022-11-03 17:54:17 +03:00
|
|
|
await jsonImporter.import({
|
|
|
|
name: 'settings',
|
|
|
|
data: baseData.settings
|
|
|
|
});
|
|
|
|
await jsonImporter.import({
|
|
|
|
name: 'custom_theme_settings',
|
|
|
|
data: baseData.custom_theme_settings
|
|
|
|
});
|
2022-11-03 19:48:25 +03:00
|
|
|
|
2022-11-07 19:11:11 +03:00
|
|
|
this.logger.info('Completed base data import');
|
2022-10-26 19:55:08 +03:00
|
|
|
} else {
|
2022-11-07 19:11:11 +03:00
|
|
|
this.logger.info('No base data pack specified, starting random base data generation');
|
2022-10-27 18:09:04 +03:00
|
|
|
const newslettersImporter = new NewslettersImporter(transaction);
|
2023-02-09 01:12:19 +03:00
|
|
|
// First newsletter is paid, second is free
|
|
|
|
newsletters = await newslettersImporter.import({amount: 2, rows: ['sort_order']});
|
2022-11-03 19:48:25 +03:00
|
|
|
newsletters.sort((a, b) => a.sort_order - b.sort_order);
|
2022-10-27 18:09:04 +03:00
|
|
|
|
|
|
|
const postsImporter = new PostsImporter(transaction, {
|
|
|
|
newsletters
|
|
|
|
});
|
2022-10-26 19:55:08 +03:00
|
|
|
posts = await postsImporter.import({
|
|
|
|
amount: this.modelQuantities.posts,
|
2023-01-23 15:47:10 +03:00
|
|
|
rows: ['newsletter_id', 'published_at', 'slug']
|
2022-10-26 19:55:08 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const tagsImporter = new TagsImporter(transaction, {
|
|
|
|
users
|
|
|
|
});
|
|
|
|
tags = await tagsImporter.import({amount: faker.datatype.number({
|
|
|
|
min: 16,
|
|
|
|
max: 24
|
|
|
|
})});
|
2022-10-27 18:09:04 +03:00
|
|
|
|
|
|
|
const productsImporter = new ProductsImporter(transaction);
|
|
|
|
products = await productsImporter.import({amount: 4, rows: ['name', 'monthly_price', 'yearly_price']});
|
|
|
|
|
|
|
|
const stripeProductsImporter = new StripeProductsImporter(transaction);
|
2022-11-03 19:48:25 +03:00
|
|
|
stripeProducts = await stripeProductsImporter.importForEach(products.filter(product => product.name !== 'Free'), {
|
2022-10-27 18:09:04 +03:00
|
|
|
amount: 1,
|
|
|
|
rows: ['product_id', 'stripe_product_id']
|
|
|
|
});
|
|
|
|
|
|
|
|
const stripePricesImporter = new StripePricesImporter(transaction, {products});
|
|
|
|
stripePrices = await stripePricesImporter.importForEach(stripeProducts, {
|
|
|
|
amount: 2,
|
|
|
|
rows: ['stripe_price_id', 'interval', 'stripe_product_id', 'currency', 'amount', 'nickname']
|
|
|
|
});
|
|
|
|
|
|
|
|
await productsImporter.addStripePrices({
|
|
|
|
products,
|
|
|
|
stripeProducts,
|
|
|
|
stripePrices
|
|
|
|
});
|
|
|
|
|
|
|
|
const benefitsImporter = new BenefitsImporter(transaction);
|
2022-11-03 17:54:17 +03:00
|
|
|
benefits = await benefitsImporter.import({amount: 5});
|
2022-10-27 18:09:04 +03:00
|
|
|
|
|
|
|
const productsBenefitsImporter = new ProductsBenefitsImporter(transaction, {benefits});
|
|
|
|
// Up to 5 benefits for each product
|
|
|
|
await productsBenefitsImporter.importForEach(products, {amount: 5});
|
2022-11-07 19:11:11 +03:00
|
|
|
|
|
|
|
this.logger.info('Completed random base data generation');
|
2022-10-26 19:55:08 +03:00
|
|
|
}
|
|
|
|
|
2022-11-07 19:11:11 +03:00
|
|
|
this.logger.info('Started member data generation');
|
|
|
|
|
2022-10-26 19:55:08 +03:00
|
|
|
const postsTagsImporter = new PostsTagsImporter(transaction, {
|
|
|
|
tags
|
|
|
|
});
|
|
|
|
await postsTagsImporter.importForEach(posts, {
|
|
|
|
amount: () => faker.datatype.number({
|
|
|
|
min: 0,
|
|
|
|
max: 3
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
const membersImporter = new MembersImporter(transaction);
|
2023-02-17 17:03:52 +03:00
|
|
|
const members = await membersImporter.import({amount: this.modelQuantities.members, rows: ['status', 'created_at', 'name', 'email', 'uuid']});
|
2022-10-26 19:55:08 +03:00
|
|
|
|
2022-10-27 18:09:04 +03:00
|
|
|
const postsAuthorsImporter = new PostsAuthorsImporter(transaction, {
|
|
|
|
users
|
|
|
|
});
|
|
|
|
await postsAuthorsImporter.importForEach(posts, {amount: 1});
|
2022-10-26 19:55:08 +03:00
|
|
|
|
|
|
|
// TODO: Use subscriptions to generate members_products table?
|
2022-11-03 17:54:17 +03:00
|
|
|
const membersProductsImporter = new MembersProductsImporter(transaction, {products: products.filter(product => product.name !== 'Free')});
|
2022-10-26 19:55:08 +03:00
|
|
|
const membersProducts = await membersProductsImporter.importForEach(members.filter(member => member.status !== 'free'), {
|
|
|
|
amount: 1,
|
|
|
|
rows: ['product_id', 'member_id']
|
|
|
|
});
|
2022-11-03 17:54:17 +03:00
|
|
|
const membersFreeProductsImporter = new MembersProductsImporter(transaction, {products: products.filter(product => product.name === 'Free')});
|
2022-10-26 19:55:08 +03:00
|
|
|
await membersFreeProductsImporter.importForEach(members.filter(member => member.status === 'free'), {
|
|
|
|
amount: 1,
|
|
|
|
rows: ['product_id', 'member_id']
|
|
|
|
});
|
|
|
|
|
2022-10-27 18:09:04 +03:00
|
|
|
const postsProductsImporter = new PostsProductsImporter(transaction, {products: products.slice(1)});
|
2022-10-26 19:55:08 +03:00
|
|
|
// Paid newsletters
|
2023-02-09 01:12:19 +03:00
|
|
|
await postsProductsImporter.importForEach(posts.filter(post => newsletters.findIndex(newsletter => newsletter.id === post.newsletter_id) === 0), {
|
2022-10-27 18:09:04 +03:00
|
|
|
// Each post is available on all 3 premium products
|
2022-10-26 19:55:08 +03:00
|
|
|
amount: 3
|
|
|
|
});
|
|
|
|
|
|
|
|
const membersCreatedEventsImporter = new MembersCreatedEventsImporter(transaction);
|
|
|
|
await membersCreatedEventsImporter.importForEach(members, {amount: 1});
|
|
|
|
|
|
|
|
const membersLoginEventsImporter = new MembersLoginEventsImporter(transaction);
|
|
|
|
// Will create roughly 1 login event for every 3 days, up to a maximum of 100.
|
|
|
|
await membersLoginEventsImporter.importForEach(members, {amount: this.modelQuantities.membersLoginEvents});
|
|
|
|
|
|
|
|
const membersStatusEventsImporter = new MembersStatusEventsImporter(transaction);
|
|
|
|
// Up to 2 events per member - 1 from null -> free, 1 from free -> {paid, comped}
|
|
|
|
await membersStatusEventsImporter.importForEach(members, {amount: 2});
|
|
|
|
|
|
|
|
const subscriptionsImporter = new SubscriptionsImporter(transaction, {members, stripeProducts, stripePrices});
|
|
|
|
const subscriptions = await subscriptionsImporter.importForEach(membersProducts, {
|
|
|
|
amount: 1,
|
|
|
|
rows: ['cadence', 'tier_id', 'expires_at', 'created_at', 'member_id', 'currency']
|
|
|
|
});
|
|
|
|
|
|
|
|
const membersStripeCustomersImporter = new MembersStripeCustomersImporter(transaction);
|
|
|
|
const membersStripeCustomers = await membersStripeCustomersImporter.importForEach(members, {
|
|
|
|
amount: 1,
|
|
|
|
rows: ['customer_id', 'member_id']
|
|
|
|
});
|
|
|
|
|
|
|
|
const membersStripeCustomersSubscriptionsImporter = new MembersStripeCustomersSubscriptionsImporter(transaction, {
|
|
|
|
membersStripeCustomers,
|
|
|
|
products,
|
|
|
|
stripeProducts,
|
|
|
|
stripePrices
|
|
|
|
});
|
|
|
|
const membersStripeCustomersSubscriptions = await membersStripeCustomersSubscriptionsImporter.importForEach(subscriptions, {
|
|
|
|
amount: 1,
|
|
|
|
rows: ['mrr', 'plan_id', 'subscription_id']
|
|
|
|
});
|
|
|
|
|
|
|
|
const membersSubscribeEventsImporter = new MembersSubscribeEventsImporter(transaction, {newsletters, subscriptions});
|
|
|
|
const membersSubscribeEvents = await membersSubscribeEventsImporter.importForEach(members, {
|
|
|
|
amount: 2,
|
2023-01-20 19:24:22 +03:00
|
|
|
rows: ['member_id', 'newsletter_id', 'created_at']
|
2022-10-26 19:55:08 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const membersNewslettersImporter = new MembersNewslettersImporter(transaction);
|
|
|
|
await membersNewslettersImporter.importForEach(membersSubscribeEvents, {amount: 1});
|
|
|
|
|
|
|
|
const membersPaidSubscriptionEventsImporter = new MembersPaidSubscriptionEventsImporter(transaction, {
|
|
|
|
membersStripeCustomersSubscriptions
|
|
|
|
});
|
|
|
|
await membersPaidSubscriptionEventsImporter.importForEach(subscriptions, {amount: 1});
|
|
|
|
|
|
|
|
const membersSubscriptionCreatedEventsImporter = new MembersSubscriptionCreatedEventsImporter(transaction, {subscriptions});
|
|
|
|
await membersSubscriptionCreatedEventsImporter.importForEach(membersStripeCustomersSubscriptions, {amount: 1});
|
|
|
|
|
2023-01-23 15:47:10 +03:00
|
|
|
const mentionsImporter = new MentionsImporter(transaction, {baseUrl: this.baseUrl});
|
|
|
|
// Generate up to 4 webmentions per post
|
|
|
|
await mentionsImporter.importForEach(posts, {amount: 4});
|
|
|
|
|
2023-01-20 19:24:22 +03:00
|
|
|
const emailsImporter = new EmailsImporter(transaction, {newsletters, members, membersSubscribeEvents});
|
2023-02-17 17:03:52 +03:00
|
|
|
const emails = await emailsImporter.importForEach(posts, {
|
|
|
|
amount: 1,
|
|
|
|
rows: ['created_at', 'email_count', 'delivered_count', 'opened_count', 'failed_count', 'newsletter_id', 'post_id']
|
|
|
|
});
|
|
|
|
|
|
|
|
const emailBatchesImporter = new EmailBatchesImporter(transaction);
|
|
|
|
const emailBatches = await emailBatchesImporter.importForEach(emails, {
|
|
|
|
amount: 1,
|
|
|
|
rows: ['email_id', 'updated_at']
|
|
|
|
});
|
|
|
|
|
|
|
|
const emailRecipientsImporter = new EmailRecipientsImporter(transaction, {emailBatches, members, membersSubscribeEvents});
|
|
|
|
const emailRecipients = await emailRecipientsImporter.importForEach(emails, {
|
|
|
|
amount: this.modelQuantities.members,
|
|
|
|
rows: ['opened_at', 'email_id', 'member_id']
|
|
|
|
});
|
|
|
|
|
|
|
|
const redirectsImporter = new RedirectsImporter(transaction);
|
|
|
|
const redirects = await redirectsImporter.importForEach(posts, {
|
|
|
|
amount: 10,
|
|
|
|
rows: ['post_id']
|
|
|
|
});
|
|
|
|
|
|
|
|
const membersClickEventsImporter = new MembersClickEventsImporter(transaction, {redirects, emails});
|
|
|
|
await membersClickEventsImporter.importForEach(emailRecipients, {amount: 2});
|
2022-10-26 19:55:08 +03:00
|
|
|
|
|
|
|
// TODO: Email clicks - redirect, members_click_events (relies on emails)
|
|
|
|
|
|
|
|
// TODO: Feedback - members_feedback (relies on members and posts)
|
|
|
|
|
|
|
|
await transaction.commit();
|
2022-11-03 19:48:25 +03:00
|
|
|
|
2022-11-07 19:11:11 +03:00
|
|
|
this.logger.info('Completed member data generation');
|
|
|
|
this.logger.ok('Completed import process.');
|
2022-10-26 19:55:08 +03:00
|
|
|
}
|
2023-02-06 13:40:18 +03:00
|
|
|
|
|
|
|
async importSingleTable(tableName, quantity) {
|
|
|
|
this.logger.info('Importing a single table');
|
|
|
|
const transaction = await this.knex.transaction();
|
|
|
|
|
|
|
|
const importMembers = async () => {
|
|
|
|
this.logger.info(`Importing ${quantity ?? this.modelQuantities.members} members`);
|
|
|
|
const membersImporter = new MembersImporter(transaction);
|
|
|
|
await membersImporter.import({amount: quantity ?? this.modelQuantities.members});
|
|
|
|
};
|
|
|
|
|
|
|
|
const importMentions = async () => {
|
|
|
|
const posts = await transaction.select('id', 'newsletter_id', 'published_at', 'slug').from('posts');
|
|
|
|
this.logger.info(`Importing up to ${posts.length * 4} mentions`);
|
|
|
|
|
|
|
|
const mentionsImporter = new MentionsImporter(transaction, {baseUrl: this.baseUrl});
|
|
|
|
await mentionsImporter.importForEach(posts, {amount: 4});
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (tableName) {
|
|
|
|
case 'members':
|
|
|
|
await importMembers();
|
|
|
|
break;
|
|
|
|
case 'mentions':
|
|
|
|
await importMentions();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
this.logger.warn(`Cannot import single table '${tableName}'`);
|
|
|
|
await transaction.commit(); // no-op, just close the transaction
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.logger.ok('Completed import process.');
|
|
|
|
await transaction.commit();
|
|
|
|
}
|
2022-10-26 19:55:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = DataGenerator;
|
2023-01-23 15:47:10 +03:00
|
|
|
module.exports.tables = tables;
|