Skip to content

Commit cc8ff40

Browse files
authored
fix: write to database more frequently during manual sync (#20)
1 parent cbe9442 commit cc8ff40

6 files changed

Lines changed: 78 additions & 30 deletions

File tree

packages/orb-sync-lib/src/database/postgres.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pg from 'pg';
22
import { pg as sql } from 'yesql';
33
import type { JsonSchema } from '../schemas/types';
4+
import { randomUUID } from 'node:crypto';
45

56
type PostgresConfig = {
67
databaseUrl: string;
@@ -25,6 +26,8 @@ export class PostgresClient {
2526
const chunkSize = 5;
2627
const results: pg.QueryResult<T>[] = [];
2728

29+
const timerLoggingLabel = `upsert-many-${randomUUID()}`;
30+
console.time(timerLoggingLabel);
2831
for (let i = 0; i < entries.length; i += chunkSize) {
2932
const chunk = entries.slice(i, i + chunkSize);
3033

@@ -43,6 +46,7 @@ export class PostgresClient {
4346

4447
results.push(...(await Promise.all(queries)));
4548
}
49+
console.timeEnd(timerLoggingLabel);
4650

4751
return results.flatMap((it) => it.rows);
4852
}

packages/orb-sync-lib/src/sync/credit_notes.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type Orb from 'orb-billing';
55
import { CreditNotesFetchParams } from '../types';
66

77
const TABLE = 'credit_notes';
8+
const TIMER_LOGGING_LABEL = 'fetch-credit-notes';
89

910
export async function syncCreditNotes(postgresClient: PostgresClient, creditNotes: CreditNote[]) {
1011
return postgresClient.upsertMany(
@@ -22,19 +23,27 @@ export async function fetchAndSyncCreditNotes(
2223
orbClient: Orb,
2324
params: CreditNotesFetchParams
2425
): Promise<number> {
25-
const creditNotes = [];
26+
let numberOfCreditNotes = 0;
2627

28+
console.time(TIMER_LOGGING_LABEL);
2729
let creditNotesPage = await orbClient.creditNotes.list({ limit: params.limit || 100 });
28-
creditNotes.push(...creditNotesPage.data);
30+
console.timeEnd(TIMER_LOGGING_LABEL);
31+
32+
numberOfCreditNotes += creditNotesPage.data.length;
33+
34+
await syncCreditNotes(postgresClient, creditNotesPage.data);
2935

3036
while (creditNotesPage.hasNextPage()) {
37+
console.time(TIMER_LOGGING_LABEL);
3138
creditNotesPage = await creditNotesPage.getNextPage();
32-
creditNotes.push(...creditNotesPage.data);
33-
}
39+
console.timeEnd(TIMER_LOGGING_LABEL);
3440

35-
await syncCreditNotes(postgresClient, creditNotes);
41+
numberOfCreditNotes += creditNotesPage.data.length;
42+
43+
await syncCreditNotes(postgresClient, creditNotesPage.data);
44+
}
3645

37-
return creditNotes.length;
46+
return numberOfCreditNotes;
3847
}
3948

4049
export async function fetchAndSyncCreditNote(postgresClient: PostgresClient, orbClient: Orb, creditNoteId: string) {

packages/orb-sync-lib/src/sync/customers.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { customerSchema } from '../schemas/customer';
55
import { CustomersFetchParams } from '../types';
66

77
const TABLE = 'customers';
8+
const TIMER_LOGGING_LABEL = 'fetch-customers';
89

910
export async function syncCustomers(postgresClient: PostgresClient, customers: Customer[]) {
1011
return postgresClient.upsertMany(customers, TABLE, customerSchema);
@@ -15,25 +16,33 @@ export async function fetchAndSyncCustomers(
1516
orbClient: Orb,
1617
params: CustomersFetchParams
1718
): Promise<number> {
18-
const customers = [];
19+
let numberOfCustomers = 0;
1920

21+
console.time(TIMER_LOGGING_LABEL);
2022
let customersPage = await orbClient.customers.list({
2123
limit: params.limit || 100,
2224
'created_at[gt]': params.createdAtGt,
2325
'created_at[gte]': params.createdAtGte,
2426
'created_at[lt]': params.createdAtLt,
2527
'created_at[lte]': params.createdAtLte,
2628
});
27-
customers.push(...customersPage.data);
29+
console.timeEnd(TIMER_LOGGING_LABEL);
30+
31+
numberOfCustomers += customersPage.data.length;
32+
33+
await syncCustomers(postgresClient, customersPage.data);
2834

2935
while (customersPage.hasNextPage()) {
36+
console.time(TIMER_LOGGING_LABEL);
3037
customersPage = await customersPage.getNextPage();
31-
customers.push(...customersPage.data);
32-
}
38+
console.timeEnd(TIMER_LOGGING_LABEL);
3339

34-
await syncCustomers(postgresClient, customers);
40+
numberOfCustomers += customersPage.data.length;
41+
42+
await syncCustomers(postgresClient, customersPage.data);
43+
}
3544

36-
return customers.length;
45+
return numberOfCustomers;
3746
}
3847

3948
export async function fetchAndSyncCustomer(postgresClient: PostgresClient, orbClient: Orb, customerId: string) {

packages/orb-sync-lib/src/sync/invoices.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { invoiceSchema } from '../schemas/invoice';
55
import { InvoicesFetchParams } from '../types';
66

77
const TABLE = 'invoices';
8+
const TIMER_LOGGING_LABEL = 'fetch-invoices';
89

910
export async function syncInvoices(postgresClient: PostgresClient, invoices: Invoice[]) {
1011
return postgresClient.upsertMany(
@@ -23,25 +24,33 @@ export async function fetchAndSyncInvoices(
2324
orbClient: Orb,
2425
params: InvoicesFetchParams
2526
): Promise<number> {
26-
const invoices = [];
27+
let numberOfInvoices = 0;
2728

29+
console.time(TIMER_LOGGING_LABEL);
2830
let invoicesPage = await orbClient.invoices.list({
2931
limit: params.limit || 100,
3032
'invoice_date[gt]': params.createdAtGt,
3133
'invoice_date[gte]': params.createdAtGte,
3234
'invoice_date[lt]': params.createdAtLt,
3335
'invoice_date[lte]': params.createdAtLte,
3436
});
35-
invoices.push(...invoicesPage.data);
37+
console.timeEnd(TIMER_LOGGING_LABEL);
38+
39+
numberOfInvoices += invoicesPage.data.length;
40+
41+
await syncInvoices(postgresClient, invoicesPage.data);
3642

3743
while (invoicesPage.hasNextPage()) {
44+
console.time(TIMER_LOGGING_LABEL);
3845
invoicesPage = await invoicesPage.getNextPage();
39-
invoices.push(...invoicesPage.data);
40-
}
46+
console.timeEnd(TIMER_LOGGING_LABEL);
4147

42-
await syncInvoices(postgresClient, invoices);
48+
numberOfInvoices += invoicesPage.data.length;
49+
50+
await syncInvoices(postgresClient, invoicesPage.data);
51+
}
4352

44-
return invoices.length;
53+
return numberOfInvoices;
4554
}
4655

4756
export async function fetchAndSyncInvoice(postgresClient: PostgresClient, orbClient: Orb, invoiceId: string) {

packages/orb-sync-lib/src/sync/plans.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { PlansFetchParams } from '../types';
55
import { planSchema } from '../schemas/plan';
66

77
const TABLE = 'plans';
8+
const TIMER_LOGGING_LABEL = 'fetch-plans';
89

910
export async function syncPlans(postgresClient: PostgresClient, plans: Plan[]) {
1011
return postgresClient.upsertMany(plans, TABLE, planSchema);
@@ -15,26 +16,33 @@ export async function fetchAndSyncPlans(
1516
orbClient: Orb,
1617
params: PlansFetchParams
1718
): Promise<number> {
18-
const plans = [];
19+
let numberOfPlans = 0;
1920

21+
console.time(TIMER_LOGGING_LABEL);
2022
let plansPage = await orbClient.plans.list({
2123
limit: params.limit || 100,
2224
'created_at[gt]': params.createdAtGt,
2325
'created_at[gte]': params.createdAtGte,
2426
'created_at[lt]': params.createdAtLt,
2527
'created_at[lte]': params.createdAtLte,
2628
});
29+
console.timeEnd(TIMER_LOGGING_LABEL);
2730

28-
plans.push(...plansPage.data);
31+
numberOfPlans += plansPage.data.length;
32+
33+
await syncPlans(postgresClient, plansPage.data);
2934

3035
while (plansPage.hasNextPage()) {
36+
console.time(TIMER_LOGGING_LABEL);
3137
plansPage = await plansPage.getNextPage();
32-
plans.push(...plansPage.data);
33-
}
38+
console.timeEnd(TIMER_LOGGING_LABEL);
3439

35-
await syncPlans(postgresClient, plans);
40+
numberOfPlans += plansPage.data.length;
41+
42+
await syncPlans(postgresClient, plansPage.data);
43+
}
3644

37-
return plans.length;
45+
return numberOfPlans;
3846
}
3947

4048
export async function fetchAndSyncPlan(postgresClient: PostgresClient, orbClient: Orb, planId: string) {

packages/orb-sync-lib/src/sync/subscriptions.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { subscriptionSchema } from '../schemas/subscription';
55
import { SubscriptionsFetchParams } from '../types';
66

77
const TABLE = 'subscriptions';
8+
const TIMER_LOGGING_LABEL = 'fetch-subscriptions';
89

910
export async function syncSubscriptions(postgresClient: PostgresClient, subscriptions: Subscription[]) {
1011
return postgresClient.upsertMany(
@@ -23,25 +24,33 @@ export async function fetchAndSyncSubscriptions(
2324
orbClient: Orb,
2425
params: SubscriptionsFetchParams
2526
): Promise<number> {
26-
const subscriptions = [];
27+
let numberOfSubscriptions = 0;
2728

29+
console.time(TIMER_LOGGING_LABEL);
2830
let subscriptionsPage = await orbClient.subscriptions.list({
2931
limit: params.limit || 100,
3032
'created_at[gt]': params.createdAtGt,
3133
'created_at[gte]': params.createdAtGte,
3234
'created_at[lt]': params.createdAtLt,
3335
'created_at[lte]': params.createdAtLte,
3436
});
35-
subscriptions.push(...subscriptionsPage.data);
37+
console.timeEnd(TIMER_LOGGING_LABEL);
38+
39+
numberOfSubscriptions += subscriptionsPage.data.length;
40+
41+
await syncSubscriptions(postgresClient, subscriptionsPage.data);
3642

3743
while (subscriptionsPage.hasNextPage()) {
44+
console.time(TIMER_LOGGING_LABEL);
3845
subscriptionsPage = await subscriptionsPage.getNextPage();
39-
subscriptions.push(...subscriptionsPage.data);
40-
}
46+
console.timeEnd(TIMER_LOGGING_LABEL);
4147

42-
await syncSubscriptions(postgresClient, subscriptions);
48+
numberOfSubscriptions += subscriptionsPage.data.length;
49+
50+
await syncSubscriptions(postgresClient, subscriptionsPage.data);
51+
}
4352

44-
return subscriptions.length;
53+
return numberOfSubscriptions;
4554
}
4655

4756
export async function fetchAndSyncSubscription(postgresClient: PostgresClient, orbClient: Orb, subscriptionId: string) {

0 commit comments

Comments
 (0)