Skip to content

Commit 13936f6

Browse files
authored
feat: optional backfill of related entities (#91)
1 parent 3784b7d commit 13936f6

File tree

11 files changed

+161
-90
lines changed

11 files changed

+161
-90
lines changed

src/lib/charges.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ import { stripe } from '../utils/StripeClientManager'
99

1010
const config = getConfig()
1111

12-
export const upsertCharges = async (charges: Stripe.Charge[]): Promise<Stripe.Charge[]> => {
13-
await Promise.all([
14-
backfillCustomers(getUniqueIds(charges, 'customer')),
15-
backfillInvoices(getUniqueIds(charges, 'invoice')),
16-
])
12+
export const upsertCharges = async (
13+
charges: Stripe.Charge[],
14+
backfillRelatedEntities: boolean = true
15+
): Promise<Stripe.Charge[]> => {
16+
if (backfillRelatedEntities) {
17+
await Promise.all([
18+
backfillCustomers(getUniqueIds(charges, 'customer')),
19+
backfillInvoices(getUniqueIds(charges, 'invoice')),
20+
])
21+
}
1722

1823
return upsertMany(charges, () =>
1924
constructUpsertSql(config.SCHEMA || 'stripe', 'charges', chargeSchema)

src/lib/disputes.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ import { getUniqueIds, upsertMany } from './database_utils'
77

88
const config = getConfig()
99

10-
export const upsertDisputes = async (disputes: Stripe.Dispute[]): Promise<Stripe.Dispute[]> => {
11-
await backfillCharges(getUniqueIds(disputes, 'charge'))
10+
export const upsertDisputes = async (
11+
disputes: Stripe.Dispute[],
12+
backfillRelatedEntities: boolean = true
13+
): Promise<Stripe.Dispute[]> => {
14+
if (backfillRelatedEntities) {
15+
await backfillCharges(getUniqueIds(disputes, 'charge'))
16+
}
1217

1318
return upsertMany(disputes, () =>
1419
constructUpsertSql(config.SCHEMA || 'stripe', 'disputes', disputeSchema)

src/lib/invoices.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ import { stripe } from '../utils/StripeClientManager'
1010

1111
const config = getConfig()
1212

13-
export const upsertInvoices = async (invoices: Invoice.Invoice[]): Promise<Invoice.Invoice[]> => {
14-
await Promise.all([
15-
backfillCustomers(getUniqueIds(invoices, 'customer')),
16-
backfillSubscriptions(getUniqueIds(invoices, 'subscription')),
17-
])
13+
export const upsertInvoices = async (
14+
invoices: Invoice.Invoice[],
15+
backfillRelatedEntities: boolean = true
16+
): Promise<Invoice.Invoice[]> => {
17+
if (backfillRelatedEntities) {
18+
await Promise.all([
19+
backfillCustomers(getUniqueIds(invoices, 'customer')),
20+
backfillSubscriptions(getUniqueIds(invoices, 'subscription')),
21+
])
22+
}
1823

1924
return upsertMany(invoices, () =>
2025
constructUpsertSql(config.SCHEMA || 'stripe', 'invoices', invoiceSchema)

src/lib/payment_intents.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ import { paymentIntentSchema } from '../schemas/payment_intent'
99
const config = getConfig()
1010

1111
export const upsertPaymentIntents = async (
12-
paymentIntents: Stripe.PaymentIntent[]
12+
paymentIntents: Stripe.PaymentIntent[],
13+
backfillRelatedEntities: boolean = true
1314
): Promise<Stripe.PaymentIntent[]> => {
14-
await Promise.all([
15-
backfillCustomers(getUniqueIds(paymentIntents, 'customer')),
16-
backfillInvoices(getUniqueIds(paymentIntents, 'invoice')),
17-
])
15+
if (backfillRelatedEntities) {
16+
await Promise.all([
17+
backfillCustomers(getUniqueIds(paymentIntents, 'customer')),
18+
backfillInvoices(getUniqueIds(paymentIntents, 'invoice')),
19+
])
20+
}
1821

1922
return upsertMany(paymentIntents, () =>
2023
constructUpsertSql(config.SCHEMA || 'stripe', 'payment_intents', paymentIntentSchema)

src/lib/payment_methods.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import { getUniqueIds, upsertMany } from './database_utils'
88
const config = getConfig()
99

1010
export const upsertPaymentMethods = async (
11-
paymentMethods: Stripe.PaymentMethod[]
11+
paymentMethods: Stripe.PaymentMethod[],
12+
backfillRelatedEntities: boolean = true
1213
): Promise<Stripe.PaymentMethod[]> => {
13-
await backfillCustomers(getUniqueIds(paymentMethods, 'customer'))
14+
if (backfillRelatedEntities) {
15+
await backfillCustomers(getUniqueIds(paymentMethods, 'customer'))
16+
}
1417

1518
return upsertMany(paymentMethods, () =>
1619
constructUpsertSql(config.SCHEMA || 'stripe', 'payment_methods', paymentMethodsSchema)

src/lib/plans.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ import { planSchema } from '../schemas/plan'
99

1010
const config = getConfig()
1111

12-
export const upsertPlans = async (plans: Stripe.Plan[]): Promise<Stripe.Plan[]> => {
13-
await backfillProducts(getUniqueIds(plans, 'product'))
12+
export const upsertPlans = async (
13+
plans: Stripe.Plan[],
14+
backfillRelatedEntities: boolean = true
15+
): Promise<Stripe.Plan[]> => {
16+
if (backfillRelatedEntities) {
17+
await backfillProducts(getUniqueIds(plans, 'product'))
18+
}
1419

1520
return upsertMany(plans, () => constructUpsertSql(config.SCHEMA || 'stripe', 'plans', planSchema))
1621
}

src/lib/prices.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ import { getUniqueIds, upsertMany } from './database_utils'
99

1010
const config = getConfig()
1111

12-
export const upsertPrices = async (prices: Price.Price[]): Promise<Price.Price[]> => {
13-
await backfillProducts(getUniqueIds(prices, 'product'))
12+
export const upsertPrices = async (
13+
prices: Price.Price[],
14+
backfillRelatedEntities: boolean = true
15+
): Promise<Price.Price[]> => {
16+
if (backfillRelatedEntities) {
17+
await backfillProducts(getUniqueIds(prices, 'product'))
18+
}
1419

1520
return upsertMany(prices, () =>
1621
constructUpsertSql(config.SCHEMA || 'stripe', 'prices', priceSchema)

src/lib/setup_intents.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ import { getUniqueIds, upsertMany } from './database_utils'
88
const config = getConfig()
99

1010
export const upsertSetupIntents = async (
11-
setupIntents: Stripe.SetupIntent[]
11+
setupIntents: Stripe.SetupIntent[],
12+
backfillRelatedEntities: boolean = true
1213
): Promise<Stripe.SetupIntent[]> => {
13-
await backfillCustomers(getUniqueIds(setupIntents, 'customer'))
14+
if (backfillRelatedEntities) {
15+
await backfillCustomers(getUniqueIds(setupIntents, 'customer'))
16+
}
1417

1518
return upsertMany(setupIntents, () =>
1619
constructUpsertSql(config.SCHEMA || 'stripe', 'setup_intents', setupIntentsSchema)

src/lib/subscription_schedules.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ import { subscriptionScheduleSchema } from '../schemas/subscription_schedules'
1010
const config = getConfig()
1111

1212
export const upsertSubscriptionSchedules = async (
13-
subscriptionSchedules: Subscription.SubscriptionSchedule[]
13+
subscriptionSchedules: Subscription.SubscriptionSchedule[],
14+
backfillRelatedEntities: boolean = true
1415
): Promise<Subscription.SubscriptionSchedule[]> => {
15-
const customerIds = getUniqueIds(subscriptionSchedules, 'customer')
16+
if (backfillRelatedEntities) {
17+
const customerIds = getUniqueIds(subscriptionSchedules, 'customer')
1618

17-
await backfillCustomers(customerIds)
19+
await backfillCustomers(customerIds)
20+
}
1821

1922
// Run it
2023
const rows = await upsertMany(subscriptionSchedules, () =>

src/lib/subscriptions.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,14 @@ import Stripe from 'stripe'
1111
const config = getConfig()
1212

1313
export const upsertSubscriptions = async (
14-
subscriptions: Subscription.Subscription[]
14+
subscriptions: Subscription.Subscription[],
15+
backfillRelatedEntities: boolean = true
1516
): Promise<Subscription.Subscription[]> => {
16-
const customerIds = getUniqueIds(subscriptions, 'customer')
17+
if (backfillRelatedEntities) {
18+
const customerIds = getUniqueIds(subscriptions, 'customer')
1719

18-
await backfillCustomers(customerIds)
20+
await backfillCustomers(customerIds)
21+
}
1922

2023
// Run it
2124
const rows = await upsertMany(subscriptions, () =>

0 commit comments

Comments
 (0)