Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: optional backfill of related entities #91

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/lib/charges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ import { stripe } from '../utils/StripeClientManager'

const config = getConfig()

export const upsertCharges = async (charges: Stripe.Charge[]): Promise<Stripe.Charge[]> => {
await Promise.all([
backfillCustomers(getUniqueIds(charges, 'customer')),
backfillInvoices(getUniqueIds(charges, 'invoice')),
])
export const upsertCharges = async (
charges: Stripe.Charge[],
backfillRelatedEntities: boolean = true
): Promise<Stripe.Charge[]> => {
if (backfillRelatedEntities) {
await Promise.all([
backfillCustomers(getUniqueIds(charges, 'customer')),
backfillInvoices(getUniqueIds(charges, 'invoice')),
])
}

return upsertMany(charges, () =>
constructUpsertSql(config.SCHEMA || 'stripe', 'charges', chargeSchema)
Expand Down
9 changes: 7 additions & 2 deletions src/lib/disputes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import { getUniqueIds, upsertMany } from './database_utils'

const config = getConfig()

export const upsertDisputes = async (disputes: Stripe.Dispute[]): Promise<Stripe.Dispute[]> => {
await backfillCharges(getUniqueIds(disputes, 'charge'))
export const upsertDisputes = async (
disputes: Stripe.Dispute[],
backfillRelatedEntities: boolean = true
): Promise<Stripe.Dispute[]> => {
if (backfillRelatedEntities) {
await backfillCharges(getUniqueIds(disputes, 'charge'))
}

return upsertMany(disputes, () =>
constructUpsertSql(config.SCHEMA || 'stripe', 'disputes', disputeSchema)
Expand Down
15 changes: 10 additions & 5 deletions src/lib/invoices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ import { stripe } from '../utils/StripeClientManager'

const config = getConfig()

export const upsertInvoices = async (invoices: Invoice.Invoice[]): Promise<Invoice.Invoice[]> => {
await Promise.all([
backfillCustomers(getUniqueIds(invoices, 'customer')),
backfillSubscriptions(getUniqueIds(invoices, 'subscription')),
])
export const upsertInvoices = async (
invoices: Invoice.Invoice[],
backfillRelatedEntities: boolean = true
): Promise<Invoice.Invoice[]> => {
if (backfillRelatedEntities) {
await Promise.all([
backfillCustomers(getUniqueIds(invoices, 'customer')),
backfillSubscriptions(getUniqueIds(invoices, 'subscription')),
])
}

return upsertMany(invoices, () =>
constructUpsertSql(config.SCHEMA || 'stripe', 'invoices', invoiceSchema)
Expand Down
13 changes: 8 additions & 5 deletions src/lib/payment_intents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import { paymentIntentSchema } from '../schemas/payment_intent'
const config = getConfig()

export const upsertPaymentIntents = async (
paymentIntents: Stripe.PaymentIntent[]
paymentIntents: Stripe.PaymentIntent[],
backfillRelatedEntities: boolean = true
): Promise<Stripe.PaymentIntent[]> => {
await Promise.all([
backfillCustomers(getUniqueIds(paymentIntents, 'customer')),
backfillInvoices(getUniqueIds(paymentIntents, 'invoice')),
])
if (backfillRelatedEntities) {
await Promise.all([
backfillCustomers(getUniqueIds(paymentIntents, 'customer')),
backfillInvoices(getUniqueIds(paymentIntents, 'invoice')),
])
}

return upsertMany(paymentIntents, () =>
constructUpsertSql(config.SCHEMA || 'stripe', 'payment_intents', paymentIntentSchema)
Expand Down
7 changes: 5 additions & 2 deletions src/lib/payment_methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import { getUniqueIds, upsertMany } from './database_utils'
const config = getConfig()

export const upsertPaymentMethods = async (
paymentMethods: Stripe.PaymentMethod[]
paymentMethods: Stripe.PaymentMethod[],
backfillRelatedEntities: boolean = true
): Promise<Stripe.PaymentMethod[]> => {
await backfillCustomers(getUniqueIds(paymentMethods, 'customer'))
if (backfillRelatedEntities) {
await backfillCustomers(getUniqueIds(paymentMethods, 'customer'))
}

return upsertMany(paymentMethods, () =>
constructUpsertSql(config.SCHEMA || 'stripe', 'payment_methods', paymentMethodsSchema)
Expand Down
9 changes: 7 additions & 2 deletions src/lib/plans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ import { planSchema } from '../schemas/plan'

const config = getConfig()

export const upsertPlans = async (plans: Stripe.Plan[]): Promise<Stripe.Plan[]> => {
await backfillProducts(getUniqueIds(plans, 'product'))
export const upsertPlans = async (
plans: Stripe.Plan[],
backfillRelatedEntities: boolean = true
): Promise<Stripe.Plan[]> => {
if (backfillRelatedEntities) {
await backfillProducts(getUniqueIds(plans, 'product'))
}

return upsertMany(plans, () => constructUpsertSql(config.SCHEMA || 'stripe', 'plans', planSchema))
}
Expand Down
9 changes: 7 additions & 2 deletions src/lib/prices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ import { getUniqueIds, upsertMany } from './database_utils'

const config = getConfig()

export const upsertPrices = async (prices: Price.Price[]): Promise<Price.Price[]> => {
await backfillProducts(getUniqueIds(prices, 'product'))
export const upsertPrices = async (
prices: Price.Price[],
backfillRelatedEntities: boolean = true
): Promise<Price.Price[]> => {
if (backfillRelatedEntities) {
await backfillProducts(getUniqueIds(prices, 'product'))
}

return upsertMany(prices, () =>
constructUpsertSql(config.SCHEMA || 'stripe', 'prices', priceSchema)
Expand Down
7 changes: 5 additions & 2 deletions src/lib/setup_intents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import { getUniqueIds, upsertMany } from './database_utils'
const config = getConfig()

export const upsertSetupIntents = async (
setupIntents: Stripe.SetupIntent[]
setupIntents: Stripe.SetupIntent[],
backfillRelatedEntities: boolean = true
): Promise<Stripe.SetupIntent[]> => {
await backfillCustomers(getUniqueIds(setupIntents, 'customer'))
if (backfillRelatedEntities) {
await backfillCustomers(getUniqueIds(setupIntents, 'customer'))
}

return upsertMany(setupIntents, () =>
constructUpsertSql(config.SCHEMA || 'stripe', 'setup_intents', setupIntentsSchema)
Expand Down
9 changes: 6 additions & 3 deletions src/lib/subscription_schedules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import { subscriptionScheduleSchema } from '../schemas/subscription_schedules'
const config = getConfig()

export const upsertSubscriptionSchedules = async (
subscriptionSchedules: Subscription.SubscriptionSchedule[]
subscriptionSchedules: Subscription.SubscriptionSchedule[],
backfillRelatedEntities: boolean = true
): Promise<Subscription.SubscriptionSchedule[]> => {
const customerIds = getUniqueIds(subscriptionSchedules, 'customer')
if (backfillRelatedEntities) {
const customerIds = getUniqueIds(subscriptionSchedules, 'customer')

await backfillCustomers(customerIds)
await backfillCustomers(customerIds)
}

// Run it
const rows = await upsertMany(subscriptionSchedules, () =>
Expand Down
9 changes: 6 additions & 3 deletions src/lib/subscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ import Stripe from 'stripe'
const config = getConfig()

export const upsertSubscriptions = async (
subscriptions: Subscription.Subscription[]
subscriptions: Subscription.Subscription[],
backfillRelatedEntities: boolean = true
): Promise<Subscription.Subscription[]> => {
const customerIds = getUniqueIds(subscriptions, 'customer')
if (backfillRelatedEntities) {
const customerIds = getUniqueIds(subscriptions, 'customer')

await backfillCustomers(customerIds)
await backfillCustomers(customerIds)
}

// Run it
const rows = await upsertMany(subscriptions, () =>
Expand Down
Loading