2022-10-26 19:55:08 +03:00
|
|
|
const {
|
|
|
|
PostsImporter,
|
|
|
|
NewslettersImporter,
|
|
|
|
UsersImporter,
|
|
|
|
PostsAuthorsImporter,
|
|
|
|
TagsImporter,
|
|
|
|
PostsTagsImporter,
|
|
|
|
ProductsImporter,
|
|
|
|
MembersImporter,
|
|
|
|
BenefitsImporter,
|
|
|
|
ProductsBenefitsImporter,
|
|
|
|
MembersProductsImporter,
|
|
|
|
PostsProductsImporter,
|
|
|
|
MembersNewslettersImporter,
|
|
|
|
MembersCreatedEventsImporter,
|
|
|
|
MembersLoginEventsImporter,
|
|
|
|
MembersStatusEventsImporter,
|
|
|
|
StripeProductsImporter,
|
|
|
|
StripePricesImporter,
|
|
|
|
SubscriptionsImporter,
|
|
|
|
MembersStripeCustomersImporter,
|
|
|
|
MembersStripeCustomersSubscriptionsImporter,
|
|
|
|
MembersPaidSubscriptionEventsImporter,
|
|
|
|
MembersSubscriptionCreatedEventsImporter,
|
|
|
|
MembersSubscribeEventsImporter
|
|
|
|
} = require('./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
|
|
|
|
*/
|
|
|
|
|
|
|
|
const defaultQuantities = {
|
|
|
|
members: () => faker.datatype.number({
|
|
|
|
min: 7000,
|
|
|
|
max: 8000
|
|
|
|
}),
|
|
|
|
membersLoginEvents: 100,
|
|
|
|
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,
|
|
|
|
modelQuantities = {}
|
|
|
|
}) {
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
async importData() {
|
|
|
|
const transaction = await this.knex.transaction();
|
|
|
|
|
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());
|
|
|
|
this.logger.info('Loaded data pack');
|
|
|
|
} catch (error) {
|
|
|
|
this.logger.error('Failed to load data pack: ', error);
|
|
|
|
throw error;
|
|
|
|
}
|
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
|
|
|
await transaction('newsletters').delete();
|
|
|
|
newsletters = await jsonImporter.import({
|
|
|
|
name: 'newsletters',
|
2022-11-03 19:48:25 +03:00
|
|
|
data: baseData.newsletters,
|
|
|
|
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
|
|
|
|
|
|
|
await transaction('posts_authors').delete();
|
|
|
|
await transaction('posts_tags').delete();
|
2022-10-27 18:09:04 +03:00
|
|
|
|
2022-11-03 17:54:17 +03:00
|
|
|
await transaction('posts').delete();
|
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});
|
2022-10-26 19:55:08 +03:00
|
|
|
posts = await transaction.select('id', 'newsletter_id').from('posts');
|
2022-10-27 18:09:04 +03:00
|
|
|
|
2022-11-03 17:54:17 +03:00
|
|
|
await transaction('tags').delete();
|
|
|
|
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
|
|
|
await transaction('products').delete();
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
this.logger.info('Completed JSON import');
|
2022-10-26 19:55:08 +03:00
|
|
|
} else {
|
2022-10-27 18:09:04 +03:00
|
|
|
const newslettersImporter = new NewslettersImporter(transaction);
|
|
|
|
// First newsletter is free, second is paid
|
2022-11-03 19:48:25 +03:00
|
|
|
newsletters = await newslettersImporter.import({amount: 2, rows: ['sort_order']});
|
|
|
|
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,
|
|
|
|
rows: ['newsletter_id']
|
|
|
|
});
|
|
|
|
|
|
|
|
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-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);
|
|
|
|
const members = await membersImporter.import({amount: this.modelQuantities.members, rows: ['status', 'created_at', 'name', 'email']});
|
|
|
|
|
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
|
|
|
|
await postsProductsImporter.importForEach(posts.filter(post => newsletters.findIndex(newsletter => newsletter.id === post.newsletter_id) === 1), {
|
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,
|
|
|
|
rows: ['member_id', 'newsletter_id']
|
|
|
|
});
|
|
|
|
|
|
|
|
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});
|
|
|
|
|
|
|
|
// TODO: Emails! (relies on posts & newsletters)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
this.logger.info('Completed random import');
|
2022-10-26 19:55:08 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = DataGenerator;
|