stripe-experiment-sync 1.0.18 → 1.0.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/{chunk-HV64EIZU.js → chunk-DFIHCPGH.js} +95 -28
- package/dist/chunk-HHDPFCIA.js +48329 -0
- package/dist/{chunk-XO57OJUE.js → chunk-NM6L6EPQ.js} +4 -2
- package/dist/{chunk-M5TTM27L.js → chunk-UPJNFZXA.js} +88 -17
- package/dist/cli/index.cjs +44782 -365
- package/dist/cli/index.js +16 -10
- package/dist/cli/lib.cjs +44770 -359
- package/dist/cli/lib.d.cts +1 -0
- package/dist/cli/lib.d.ts +1 -0
- package/dist/cli/lib.js +4 -4
- package/dist/index.cjs +44622 -347
- package/dist/index.d.cts +92 -8
- package/dist/index.d.ts +92 -8
- package/dist/index.js +2 -2
- package/dist/migrations/0061_add_page_cursor.sql +3 -0
- package/dist/migrations/0062_sigma_query_runs.sql +8 -0
- package/dist/supabase/index.cjs +92 -18
- package/dist/supabase/index.d.cts +13 -6
- package/dist/supabase/index.d.ts +13 -6
- package/dist/supabase/index.js +4 -2
- package/package.json +4 -2
- package/dist/chunk-K4JQHI7Y.js +0 -4055
package/dist/index.d.cts
CHANGED
|
@@ -33,7 +33,7 @@ declare class PostgresClient {
|
|
|
33
33
|
}>(entries: T[], table: string): Promise<T[]>;
|
|
34
34
|
upsertManyWithTimestampProtection<T extends {
|
|
35
35
|
[Key: string]: any;
|
|
36
|
-
}>(entries: T[], table: string, accountId: string, syncTimestamp?: string, upsertOptions?: RawJsonUpsertOptions): Promise<T[]>;
|
|
36
|
+
}>(entries: T[], table: string, accountId: string, syncTimestamp?: string, upsertOptions?: RawJsonUpsertOptions, schemaOverride?: string): Promise<T[]>;
|
|
37
37
|
private cleanseArrayField;
|
|
38
38
|
findMissingEntries(table: string, ids: string[]): Promise<string[]>;
|
|
39
39
|
upsertAccount(accountData: {
|
|
@@ -102,9 +102,10 @@ declare class PostgresClient {
|
|
|
102
102
|
cancelStaleRuns(accountId: string): Promise<void>;
|
|
103
103
|
/**
|
|
104
104
|
* Get or create a sync run for this account.
|
|
105
|
-
* Returns existing run if one is active, otherwise creates new one.
|
|
105
|
+
* Returns existing run if one is active for the given triggeredBy, otherwise creates new one.
|
|
106
106
|
* Auto-cancels stale runs before checking.
|
|
107
107
|
*
|
|
108
|
+
* @param triggeredBy - Worker type (e.g., 'worker', 'sigma-worker'). Runs are isolated per triggeredBy.
|
|
108
109
|
* @returns RunKey with isNew flag, or null if constraint violation (race condition)
|
|
109
110
|
*/
|
|
110
111
|
getOrCreateSyncRun(accountId: string, triggeredBy?: string): Promise<{
|
|
@@ -114,8 +115,9 @@ declare class PostgresClient {
|
|
|
114
115
|
} | null>;
|
|
115
116
|
/**
|
|
116
117
|
* Get the active sync run for an account (if any).
|
|
118
|
+
* @param triggeredBy - If provided, only returns run matching this triggeredBy value
|
|
117
119
|
*/
|
|
118
|
-
getActiveSyncRun(accountId: string): Promise<{
|
|
120
|
+
getActiveSyncRun(accountId: string, triggeredBy?: string): Promise<{
|
|
119
121
|
accountId: string;
|
|
120
122
|
runStartedAt: Date;
|
|
121
123
|
} | null>;
|
|
@@ -129,6 +131,10 @@ declare class PostgresClient {
|
|
|
129
131
|
maxConcurrent: number;
|
|
130
132
|
closedAt: Date | null;
|
|
131
133
|
} | null>;
|
|
134
|
+
/**
|
|
135
|
+
* Ensure a sync run has at least the requested max_concurrent value.
|
|
136
|
+
*/
|
|
137
|
+
ensureSyncRunMaxConcurrent(accountId: string, runStartedAt: Date, maxConcurrent: number): Promise<void>;
|
|
132
138
|
/**
|
|
133
139
|
* Close a sync run (mark as done).
|
|
134
140
|
* Status (complete/error) is derived from object run states.
|
|
@@ -157,12 +163,21 @@ declare class PostgresClient {
|
|
|
157
163
|
status: string;
|
|
158
164
|
processedCount: number;
|
|
159
165
|
cursor: string | null;
|
|
166
|
+
pageCursor: string | null;
|
|
160
167
|
} | null>;
|
|
161
168
|
/**
|
|
162
169
|
* Update progress for an object sync.
|
|
163
170
|
* Also touches updated_at for stale detection.
|
|
164
171
|
*/
|
|
165
172
|
incrementObjectProgress(accountId: string, runStartedAt: Date, object: string, count: number): Promise<void>;
|
|
173
|
+
/**
|
|
174
|
+
* Update the pagination page_cursor used for backfills using Stripe list calls.
|
|
175
|
+
*/
|
|
176
|
+
updateObjectPageCursor(accountId: string, runStartedAt: Date, object: string, pageCursor: string | null): Promise<void>;
|
|
177
|
+
/**
|
|
178
|
+
* Clear the pagination page_cursor for an object sync.
|
|
179
|
+
*/
|
|
180
|
+
clearObjectPageCursor(accountId: string, runStartedAt: Date, object: string): Promise<void>;
|
|
166
181
|
/**
|
|
167
182
|
* Update the cursor for an object sync.
|
|
168
183
|
* Only updates if the new cursor is higher than the existing one (cursors should never decrease).
|
|
@@ -170,17 +185,32 @@ declare class PostgresClient {
|
|
|
170
185
|
* For non-numeric cursors, just sets the value directly.
|
|
171
186
|
*/
|
|
172
187
|
updateObjectCursor(accountId: string, runStartedAt: Date, object: string, cursor: string | null): Promise<void>;
|
|
188
|
+
setObjectCursor(accountId: string, runStartedAt: Date, object: string, cursor: string | null): Promise<void>;
|
|
189
|
+
/**
|
|
190
|
+
* List object names for a run by status, optionally filtered to a subset.
|
|
191
|
+
*/
|
|
192
|
+
listObjectsByStatus(accountId: string, runStartedAt: Date, status: string, objectFilter?: string[]): Promise<string[]>;
|
|
173
193
|
/**
|
|
174
194
|
* Get the highest cursor from previous syncs for an object type.
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
195
|
+
* Uses only completed object runs.
|
|
196
|
+
* - During the initial backfill we page through history, but we also update the cursor as we go.
|
|
197
|
+
* If we crash mid-backfill and reuse that cursor, we can accidentally switch into incremental mode
|
|
198
|
+
* too early and only ever fetch the newest page (breaking the historical backfill).
|
|
178
199
|
*
|
|
179
200
|
* Handles two cursor formats:
|
|
180
201
|
* - Numeric: compared as bigint for correct ordering
|
|
181
202
|
* - Composite cursors: compared as strings with COLLATE "C"
|
|
182
203
|
*/
|
|
183
204
|
getLastCompletedCursor(accountId: string, object: string): Promise<string | null>;
|
|
205
|
+
/**
|
|
206
|
+
* Get the highest cursor from previous syncs for an object type, excluding the current run.
|
|
207
|
+
*/
|
|
208
|
+
getLastCursorBeforeRun(accountId: string, object: string, runStartedAt: Date): Promise<string | null>;
|
|
209
|
+
/**
|
|
210
|
+
* Get the most recent cursor for an object run before the given run.
|
|
211
|
+
* This returns the raw cursor value without interpretation.
|
|
212
|
+
*/
|
|
213
|
+
getLastObjectCursorBeforeRun(accountId: string, object: string, runStartedAt: Date): Promise<string | null>;
|
|
184
214
|
/**
|
|
185
215
|
* Delete all sync runs and object runs for an account.
|
|
186
216
|
* Useful for testing or resetting sync state.
|
|
@@ -229,6 +259,12 @@ type SigmaCursorSpec = {
|
|
|
229
259
|
version: 1;
|
|
230
260
|
columns: SigmaCursorColumnSpec[];
|
|
231
261
|
};
|
|
262
|
+
type SigmaColumnSpec = {
|
|
263
|
+
name: string;
|
|
264
|
+
sigmaType: string;
|
|
265
|
+
pgType: string;
|
|
266
|
+
primaryKey: boolean;
|
|
267
|
+
};
|
|
232
268
|
type SigmaIngestionConfig = {
|
|
233
269
|
/**
|
|
234
270
|
* The Sigma table name to query (no quoting, no schema).
|
|
@@ -244,6 +280,11 @@ type SigmaIngestionConfig = {
|
|
|
244
280
|
* Defines the ordering and cursor semantics. The columns must form a total order (i.e. be unique together) or pagination can be incorrect.
|
|
245
281
|
*/
|
|
246
282
|
cursor: SigmaCursorSpec;
|
|
283
|
+
/**
|
|
284
|
+
* Column metadata for the destination table.
|
|
285
|
+
* If provided, used to dynamically create/reconcile the table.
|
|
286
|
+
*/
|
|
287
|
+
columns?: SigmaColumnSpec[];
|
|
247
288
|
/** Optional additional WHERE clause appended with AND (must not include leading WHERE). */
|
|
248
289
|
additionalWhere?: string;
|
|
249
290
|
/** Columns to SELECT (defaults to `*`). */
|
|
@@ -272,6 +313,16 @@ type StripeSyncConfig = {
|
|
|
272
313
|
* Default: false (opt-in, so workers don't enqueue Sigma jobs unexpectedly).
|
|
273
314
|
*/
|
|
274
315
|
enableSigma?: boolean;
|
|
316
|
+
/**
|
|
317
|
+
* Optional override for Sigma page size (per query).
|
|
318
|
+
* Useful for edge function CPU limits; lower values reduce per-invocation work.
|
|
319
|
+
*/
|
|
320
|
+
sigmaPageSizeOverride?: number;
|
|
321
|
+
/**
|
|
322
|
+
* Postgres schema name for Sigma tables.
|
|
323
|
+
* Default: "sigma"
|
|
324
|
+
*/
|
|
325
|
+
sigmaSchemaName?: string;
|
|
275
326
|
/** Stripe account ID. If not provided, will be retrieved from Stripe API. Used as fallback option. */
|
|
276
327
|
stripeAccountId?: string;
|
|
277
328
|
/** Stripe webhook signing secret for validating webhook signatures. Required if not using managed webhooks. */
|
|
@@ -326,7 +377,7 @@ type StripeSyncConfig = {
|
|
|
326
377
|
*/
|
|
327
378
|
maxConcurrentCustomers?: number;
|
|
328
379
|
};
|
|
329
|
-
type SyncObject = 'all' | 'customer' | 'customer_with_entitlements' | 'invoice' | 'price' | 'product' | 'subscription' | 'subscription_schedules' | 'setup_intent' | 'payment_method' | 'dispute' | 'charge' | 'payment_intent' | 'plan' | 'tax_id' | 'credit_note' | 'early_fraud_warning' | 'refund' | 'checkout_sessions'
|
|
380
|
+
type SyncObject = 'all' | 'customer' | 'customer_with_entitlements' | 'invoice' | 'price' | 'product' | 'subscription' | 'subscription_schedules' | 'setup_intent' | 'payment_method' | 'dispute' | 'charge' | 'payment_intent' | 'plan' | 'tax_id' | 'credit_note' | 'early_fraud_warning' | 'refund' | 'checkout_sessions';
|
|
330
381
|
interface Sync {
|
|
331
382
|
synced: number;
|
|
332
383
|
}
|
|
@@ -350,6 +401,8 @@ interface SyncBackfill {
|
|
|
350
401
|
checkoutSessions?: Sync;
|
|
351
402
|
subscriptionItemChangeEventsV2Beta?: Sync;
|
|
352
403
|
exchangeRatesFromUsd?: Sync;
|
|
404
|
+
/** Sigma-backed results by table name (e.g. subscription_item_change_events_v2_beta). */
|
|
405
|
+
sigma?: Record<string, Sync>;
|
|
353
406
|
}
|
|
354
407
|
interface SyncParams {
|
|
355
408
|
created?: {
|
|
@@ -392,6 +445,8 @@ interface ProcessNextResult {
|
|
|
392
445
|
hasMore: boolean;
|
|
393
446
|
/** The sync run this processing belongs to */
|
|
394
447
|
runStartedAt: Date;
|
|
448
|
+
/** Sigma-only: whether this step started a new Sigma query run */
|
|
449
|
+
startedQuery?: boolean;
|
|
395
450
|
}
|
|
396
451
|
/**
|
|
397
452
|
* Parameters for processNext() including optional run context
|
|
@@ -482,7 +537,12 @@ declare class StripeSync {
|
|
|
482
537
|
private config;
|
|
483
538
|
stripe: Stripe;
|
|
484
539
|
postgresClient: PostgresClient;
|
|
540
|
+
private readonly resourceRegistry;
|
|
541
|
+
private get sigmaSchemaName();
|
|
485
542
|
constructor(config: StripeSyncConfig);
|
|
543
|
+
private buildResourceRegistry;
|
|
544
|
+
private isSigmaResource;
|
|
545
|
+
private sigmaResultKey;
|
|
486
546
|
/**
|
|
487
547
|
* Get the Stripe account ID. Delegates to getCurrentAccount() for the actual lookup.
|
|
488
548
|
*/
|
|
@@ -525,7 +585,6 @@ declare class StripeSync {
|
|
|
525
585
|
}>;
|
|
526
586
|
processWebhook(payload: Buffer | string, signature: string | undefined): Promise<void>;
|
|
527
587
|
private readonly eventHandlers;
|
|
528
|
-
private readonly resourceRegistry;
|
|
529
588
|
processEvent(event: Stripe.Event): Promise<void>;
|
|
530
589
|
/**
|
|
531
590
|
* Returns an array of all webhook event types that this sync engine can handle.
|
|
@@ -538,6 +597,13 @@ declare class StripeSync {
|
|
|
538
597
|
* Order is determined by the `order` field in resourceRegistry.
|
|
539
598
|
*/
|
|
540
599
|
getSupportedSyncObjects(): Exclude<SyncObject, 'all' | 'customer_with_entitlements'>[];
|
|
600
|
+
/**
|
|
601
|
+
* Get the list of Sigma-backed object types that can be synced.
|
|
602
|
+
* Only returns sigma objects when enableSigma is true.
|
|
603
|
+
*
|
|
604
|
+
* @returns Array of sigma object names (e.g. 'subscription_item_change_events_v2_beta')
|
|
605
|
+
*/
|
|
606
|
+
getSupportedSigmaObjects(): string[];
|
|
541
607
|
private handleChargeEvent;
|
|
542
608
|
private handleCustomerDeletedEvent;
|
|
543
609
|
private handleCustomerEvent;
|
|
@@ -587,6 +653,7 @@ declare class StripeSync {
|
|
|
587
653
|
* ```
|
|
588
654
|
*/
|
|
589
655
|
processNext(object: Exclude<SyncObject, 'all' | 'customer_with_entitlements'>, params?: ProcessNextParams): Promise<ProcessNextResult>;
|
|
656
|
+
private appendMigrationHint;
|
|
590
657
|
/**
|
|
591
658
|
* Get the database resource name for a SyncObject type
|
|
592
659
|
*/
|
|
@@ -630,6 +697,22 @@ declare class StripeSync {
|
|
|
630
697
|
runKey: RunKey;
|
|
631
698
|
objects: Exclude<SyncObject, 'all' | 'customer_with_entitlements'>[];
|
|
632
699
|
}>;
|
|
700
|
+
private applySyncBackfillResult;
|
|
701
|
+
processUntilDoneParallel(params?: SyncParams & {
|
|
702
|
+
maxParallel?: number;
|
|
703
|
+
triggeredBy?: string;
|
|
704
|
+
continueOnError?: boolean;
|
|
705
|
+
skipInaccessibleSigmaTables?: boolean;
|
|
706
|
+
}): Promise<{
|
|
707
|
+
results: SyncBackfill;
|
|
708
|
+
totals: Record<string, number>;
|
|
709
|
+
totalSynced: number;
|
|
710
|
+
skipped: string[];
|
|
711
|
+
errors: Array<{
|
|
712
|
+
object: string;
|
|
713
|
+
message: string;
|
|
714
|
+
}>;
|
|
715
|
+
}>;
|
|
633
716
|
processUntilDone(params?: SyncParams): Promise<SyncBackfill>;
|
|
634
717
|
/**
|
|
635
718
|
* Internal implementation of processUntilDone with an existing run.
|
|
@@ -748,6 +831,7 @@ type MigrationConfig = {
|
|
|
748
831
|
databaseUrl: string;
|
|
749
832
|
ssl?: ConnectionOptions;
|
|
750
833
|
logger?: Logger;
|
|
834
|
+
enableSigma?: boolean;
|
|
751
835
|
};
|
|
752
836
|
declare function runMigrations(config: MigrationConfig): Promise<void>;
|
|
753
837
|
|
package/dist/index.d.ts
CHANGED
|
@@ -33,7 +33,7 @@ declare class PostgresClient {
|
|
|
33
33
|
}>(entries: T[], table: string): Promise<T[]>;
|
|
34
34
|
upsertManyWithTimestampProtection<T extends {
|
|
35
35
|
[Key: string]: any;
|
|
36
|
-
}>(entries: T[], table: string, accountId: string, syncTimestamp?: string, upsertOptions?: RawJsonUpsertOptions): Promise<T[]>;
|
|
36
|
+
}>(entries: T[], table: string, accountId: string, syncTimestamp?: string, upsertOptions?: RawJsonUpsertOptions, schemaOverride?: string): Promise<T[]>;
|
|
37
37
|
private cleanseArrayField;
|
|
38
38
|
findMissingEntries(table: string, ids: string[]): Promise<string[]>;
|
|
39
39
|
upsertAccount(accountData: {
|
|
@@ -102,9 +102,10 @@ declare class PostgresClient {
|
|
|
102
102
|
cancelStaleRuns(accountId: string): Promise<void>;
|
|
103
103
|
/**
|
|
104
104
|
* Get or create a sync run for this account.
|
|
105
|
-
* Returns existing run if one is active, otherwise creates new one.
|
|
105
|
+
* Returns existing run if one is active for the given triggeredBy, otherwise creates new one.
|
|
106
106
|
* Auto-cancels stale runs before checking.
|
|
107
107
|
*
|
|
108
|
+
* @param triggeredBy - Worker type (e.g., 'worker', 'sigma-worker'). Runs are isolated per triggeredBy.
|
|
108
109
|
* @returns RunKey with isNew flag, or null if constraint violation (race condition)
|
|
109
110
|
*/
|
|
110
111
|
getOrCreateSyncRun(accountId: string, triggeredBy?: string): Promise<{
|
|
@@ -114,8 +115,9 @@ declare class PostgresClient {
|
|
|
114
115
|
} | null>;
|
|
115
116
|
/**
|
|
116
117
|
* Get the active sync run for an account (if any).
|
|
118
|
+
* @param triggeredBy - If provided, only returns run matching this triggeredBy value
|
|
117
119
|
*/
|
|
118
|
-
getActiveSyncRun(accountId: string): Promise<{
|
|
120
|
+
getActiveSyncRun(accountId: string, triggeredBy?: string): Promise<{
|
|
119
121
|
accountId: string;
|
|
120
122
|
runStartedAt: Date;
|
|
121
123
|
} | null>;
|
|
@@ -129,6 +131,10 @@ declare class PostgresClient {
|
|
|
129
131
|
maxConcurrent: number;
|
|
130
132
|
closedAt: Date | null;
|
|
131
133
|
} | null>;
|
|
134
|
+
/**
|
|
135
|
+
* Ensure a sync run has at least the requested max_concurrent value.
|
|
136
|
+
*/
|
|
137
|
+
ensureSyncRunMaxConcurrent(accountId: string, runStartedAt: Date, maxConcurrent: number): Promise<void>;
|
|
132
138
|
/**
|
|
133
139
|
* Close a sync run (mark as done).
|
|
134
140
|
* Status (complete/error) is derived from object run states.
|
|
@@ -157,12 +163,21 @@ declare class PostgresClient {
|
|
|
157
163
|
status: string;
|
|
158
164
|
processedCount: number;
|
|
159
165
|
cursor: string | null;
|
|
166
|
+
pageCursor: string | null;
|
|
160
167
|
} | null>;
|
|
161
168
|
/**
|
|
162
169
|
* Update progress for an object sync.
|
|
163
170
|
* Also touches updated_at for stale detection.
|
|
164
171
|
*/
|
|
165
172
|
incrementObjectProgress(accountId: string, runStartedAt: Date, object: string, count: number): Promise<void>;
|
|
173
|
+
/**
|
|
174
|
+
* Update the pagination page_cursor used for backfills using Stripe list calls.
|
|
175
|
+
*/
|
|
176
|
+
updateObjectPageCursor(accountId: string, runStartedAt: Date, object: string, pageCursor: string | null): Promise<void>;
|
|
177
|
+
/**
|
|
178
|
+
* Clear the pagination page_cursor for an object sync.
|
|
179
|
+
*/
|
|
180
|
+
clearObjectPageCursor(accountId: string, runStartedAt: Date, object: string): Promise<void>;
|
|
166
181
|
/**
|
|
167
182
|
* Update the cursor for an object sync.
|
|
168
183
|
* Only updates if the new cursor is higher than the existing one (cursors should never decrease).
|
|
@@ -170,17 +185,32 @@ declare class PostgresClient {
|
|
|
170
185
|
* For non-numeric cursors, just sets the value directly.
|
|
171
186
|
*/
|
|
172
187
|
updateObjectCursor(accountId: string, runStartedAt: Date, object: string, cursor: string | null): Promise<void>;
|
|
188
|
+
setObjectCursor(accountId: string, runStartedAt: Date, object: string, cursor: string | null): Promise<void>;
|
|
189
|
+
/**
|
|
190
|
+
* List object names for a run by status, optionally filtered to a subset.
|
|
191
|
+
*/
|
|
192
|
+
listObjectsByStatus(accountId: string, runStartedAt: Date, status: string, objectFilter?: string[]): Promise<string[]>;
|
|
173
193
|
/**
|
|
174
194
|
* Get the highest cursor from previous syncs for an object type.
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
*
|
|
195
|
+
* Uses only completed object runs.
|
|
196
|
+
* - During the initial backfill we page through history, but we also update the cursor as we go.
|
|
197
|
+
* If we crash mid-backfill and reuse that cursor, we can accidentally switch into incremental mode
|
|
198
|
+
* too early and only ever fetch the newest page (breaking the historical backfill).
|
|
178
199
|
*
|
|
179
200
|
* Handles two cursor formats:
|
|
180
201
|
* - Numeric: compared as bigint for correct ordering
|
|
181
202
|
* - Composite cursors: compared as strings with COLLATE "C"
|
|
182
203
|
*/
|
|
183
204
|
getLastCompletedCursor(accountId: string, object: string): Promise<string | null>;
|
|
205
|
+
/**
|
|
206
|
+
* Get the highest cursor from previous syncs for an object type, excluding the current run.
|
|
207
|
+
*/
|
|
208
|
+
getLastCursorBeforeRun(accountId: string, object: string, runStartedAt: Date): Promise<string | null>;
|
|
209
|
+
/**
|
|
210
|
+
* Get the most recent cursor for an object run before the given run.
|
|
211
|
+
* This returns the raw cursor value without interpretation.
|
|
212
|
+
*/
|
|
213
|
+
getLastObjectCursorBeforeRun(accountId: string, object: string, runStartedAt: Date): Promise<string | null>;
|
|
184
214
|
/**
|
|
185
215
|
* Delete all sync runs and object runs for an account.
|
|
186
216
|
* Useful for testing or resetting sync state.
|
|
@@ -229,6 +259,12 @@ type SigmaCursorSpec = {
|
|
|
229
259
|
version: 1;
|
|
230
260
|
columns: SigmaCursorColumnSpec[];
|
|
231
261
|
};
|
|
262
|
+
type SigmaColumnSpec = {
|
|
263
|
+
name: string;
|
|
264
|
+
sigmaType: string;
|
|
265
|
+
pgType: string;
|
|
266
|
+
primaryKey: boolean;
|
|
267
|
+
};
|
|
232
268
|
type SigmaIngestionConfig = {
|
|
233
269
|
/**
|
|
234
270
|
* The Sigma table name to query (no quoting, no schema).
|
|
@@ -244,6 +280,11 @@ type SigmaIngestionConfig = {
|
|
|
244
280
|
* Defines the ordering and cursor semantics. The columns must form a total order (i.e. be unique together) or pagination can be incorrect.
|
|
245
281
|
*/
|
|
246
282
|
cursor: SigmaCursorSpec;
|
|
283
|
+
/**
|
|
284
|
+
* Column metadata for the destination table.
|
|
285
|
+
* If provided, used to dynamically create/reconcile the table.
|
|
286
|
+
*/
|
|
287
|
+
columns?: SigmaColumnSpec[];
|
|
247
288
|
/** Optional additional WHERE clause appended with AND (must not include leading WHERE). */
|
|
248
289
|
additionalWhere?: string;
|
|
249
290
|
/** Columns to SELECT (defaults to `*`). */
|
|
@@ -272,6 +313,16 @@ type StripeSyncConfig = {
|
|
|
272
313
|
* Default: false (opt-in, so workers don't enqueue Sigma jobs unexpectedly).
|
|
273
314
|
*/
|
|
274
315
|
enableSigma?: boolean;
|
|
316
|
+
/**
|
|
317
|
+
* Optional override for Sigma page size (per query).
|
|
318
|
+
* Useful for edge function CPU limits; lower values reduce per-invocation work.
|
|
319
|
+
*/
|
|
320
|
+
sigmaPageSizeOverride?: number;
|
|
321
|
+
/**
|
|
322
|
+
* Postgres schema name for Sigma tables.
|
|
323
|
+
* Default: "sigma"
|
|
324
|
+
*/
|
|
325
|
+
sigmaSchemaName?: string;
|
|
275
326
|
/** Stripe account ID. If not provided, will be retrieved from Stripe API. Used as fallback option. */
|
|
276
327
|
stripeAccountId?: string;
|
|
277
328
|
/** Stripe webhook signing secret for validating webhook signatures. Required if not using managed webhooks. */
|
|
@@ -326,7 +377,7 @@ type StripeSyncConfig = {
|
|
|
326
377
|
*/
|
|
327
378
|
maxConcurrentCustomers?: number;
|
|
328
379
|
};
|
|
329
|
-
type SyncObject = 'all' | 'customer' | 'customer_with_entitlements' | 'invoice' | 'price' | 'product' | 'subscription' | 'subscription_schedules' | 'setup_intent' | 'payment_method' | 'dispute' | 'charge' | 'payment_intent' | 'plan' | 'tax_id' | 'credit_note' | 'early_fraud_warning' | 'refund' | 'checkout_sessions'
|
|
380
|
+
type SyncObject = 'all' | 'customer' | 'customer_with_entitlements' | 'invoice' | 'price' | 'product' | 'subscription' | 'subscription_schedules' | 'setup_intent' | 'payment_method' | 'dispute' | 'charge' | 'payment_intent' | 'plan' | 'tax_id' | 'credit_note' | 'early_fraud_warning' | 'refund' | 'checkout_sessions';
|
|
330
381
|
interface Sync {
|
|
331
382
|
synced: number;
|
|
332
383
|
}
|
|
@@ -350,6 +401,8 @@ interface SyncBackfill {
|
|
|
350
401
|
checkoutSessions?: Sync;
|
|
351
402
|
subscriptionItemChangeEventsV2Beta?: Sync;
|
|
352
403
|
exchangeRatesFromUsd?: Sync;
|
|
404
|
+
/** Sigma-backed results by table name (e.g. subscription_item_change_events_v2_beta). */
|
|
405
|
+
sigma?: Record<string, Sync>;
|
|
353
406
|
}
|
|
354
407
|
interface SyncParams {
|
|
355
408
|
created?: {
|
|
@@ -392,6 +445,8 @@ interface ProcessNextResult {
|
|
|
392
445
|
hasMore: boolean;
|
|
393
446
|
/** The sync run this processing belongs to */
|
|
394
447
|
runStartedAt: Date;
|
|
448
|
+
/** Sigma-only: whether this step started a new Sigma query run */
|
|
449
|
+
startedQuery?: boolean;
|
|
395
450
|
}
|
|
396
451
|
/**
|
|
397
452
|
* Parameters for processNext() including optional run context
|
|
@@ -482,7 +537,12 @@ declare class StripeSync {
|
|
|
482
537
|
private config;
|
|
483
538
|
stripe: Stripe;
|
|
484
539
|
postgresClient: PostgresClient;
|
|
540
|
+
private readonly resourceRegistry;
|
|
541
|
+
private get sigmaSchemaName();
|
|
485
542
|
constructor(config: StripeSyncConfig);
|
|
543
|
+
private buildResourceRegistry;
|
|
544
|
+
private isSigmaResource;
|
|
545
|
+
private sigmaResultKey;
|
|
486
546
|
/**
|
|
487
547
|
* Get the Stripe account ID. Delegates to getCurrentAccount() for the actual lookup.
|
|
488
548
|
*/
|
|
@@ -525,7 +585,6 @@ declare class StripeSync {
|
|
|
525
585
|
}>;
|
|
526
586
|
processWebhook(payload: Buffer | string, signature: string | undefined): Promise<void>;
|
|
527
587
|
private readonly eventHandlers;
|
|
528
|
-
private readonly resourceRegistry;
|
|
529
588
|
processEvent(event: Stripe.Event): Promise<void>;
|
|
530
589
|
/**
|
|
531
590
|
* Returns an array of all webhook event types that this sync engine can handle.
|
|
@@ -538,6 +597,13 @@ declare class StripeSync {
|
|
|
538
597
|
* Order is determined by the `order` field in resourceRegistry.
|
|
539
598
|
*/
|
|
540
599
|
getSupportedSyncObjects(): Exclude<SyncObject, 'all' | 'customer_with_entitlements'>[];
|
|
600
|
+
/**
|
|
601
|
+
* Get the list of Sigma-backed object types that can be synced.
|
|
602
|
+
* Only returns sigma objects when enableSigma is true.
|
|
603
|
+
*
|
|
604
|
+
* @returns Array of sigma object names (e.g. 'subscription_item_change_events_v2_beta')
|
|
605
|
+
*/
|
|
606
|
+
getSupportedSigmaObjects(): string[];
|
|
541
607
|
private handleChargeEvent;
|
|
542
608
|
private handleCustomerDeletedEvent;
|
|
543
609
|
private handleCustomerEvent;
|
|
@@ -587,6 +653,7 @@ declare class StripeSync {
|
|
|
587
653
|
* ```
|
|
588
654
|
*/
|
|
589
655
|
processNext(object: Exclude<SyncObject, 'all' | 'customer_with_entitlements'>, params?: ProcessNextParams): Promise<ProcessNextResult>;
|
|
656
|
+
private appendMigrationHint;
|
|
590
657
|
/**
|
|
591
658
|
* Get the database resource name for a SyncObject type
|
|
592
659
|
*/
|
|
@@ -630,6 +697,22 @@ declare class StripeSync {
|
|
|
630
697
|
runKey: RunKey;
|
|
631
698
|
objects: Exclude<SyncObject, 'all' | 'customer_with_entitlements'>[];
|
|
632
699
|
}>;
|
|
700
|
+
private applySyncBackfillResult;
|
|
701
|
+
processUntilDoneParallel(params?: SyncParams & {
|
|
702
|
+
maxParallel?: number;
|
|
703
|
+
triggeredBy?: string;
|
|
704
|
+
continueOnError?: boolean;
|
|
705
|
+
skipInaccessibleSigmaTables?: boolean;
|
|
706
|
+
}): Promise<{
|
|
707
|
+
results: SyncBackfill;
|
|
708
|
+
totals: Record<string, number>;
|
|
709
|
+
totalSynced: number;
|
|
710
|
+
skipped: string[];
|
|
711
|
+
errors: Array<{
|
|
712
|
+
object: string;
|
|
713
|
+
message: string;
|
|
714
|
+
}>;
|
|
715
|
+
}>;
|
|
633
716
|
processUntilDone(params?: SyncParams): Promise<SyncBackfill>;
|
|
634
717
|
/**
|
|
635
718
|
* Internal implementation of processUntilDone with an existing run.
|
|
@@ -748,6 +831,7 @@ type MigrationConfig = {
|
|
|
748
831
|
databaseUrl: string;
|
|
749
832
|
ssl?: ConnectionOptions;
|
|
750
833
|
logger?: Logger;
|
|
834
|
+
enableSigma?: boolean;
|
|
751
835
|
};
|
|
752
836
|
declare function runMigrations(config: MigrationConfig): Promise<void>;
|
|
753
837
|
|
package/dist/index.js
CHANGED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
-- Allow parallel sync runs per triggered_by (sigma-worker vs stripe-worker)
|
|
2
|
+
ALTER TABLE "stripe"."_sync_runs" DROP CONSTRAINT IF EXISTS one_active_run_per_account;
|
|
3
|
+
ALTER TABLE "stripe"."_sync_runs"
|
|
4
|
+
ADD CONSTRAINT one_active_run_per_account_triggered_by
|
|
5
|
+
EXCLUDE (
|
|
6
|
+
"_account_id" WITH =,
|
|
7
|
+
COALESCE(triggered_by, 'default') WITH =
|
|
8
|
+
) WHERE (closed_at IS NULL);
|