stripe-experiment-sync 0.0.0

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.
Files changed (47) hide show
  1. package/README.md +100 -0
  2. package/dist/index.cjs +2370 -0
  3. package/dist/index.d.cts +222 -0
  4. package/dist/index.d.ts +222 -0
  5. package/dist/index.js +2328 -0
  6. package/dist/migrations/0000_initial_migration.sql +1 -0
  7. package/dist/migrations/0001_products.sql +17 -0
  8. package/dist/migrations/0002_customers.sql +23 -0
  9. package/dist/migrations/0003_prices.sql +34 -0
  10. package/dist/migrations/0004_subscriptions.sql +56 -0
  11. package/dist/migrations/0005_invoices.sql +77 -0
  12. package/dist/migrations/0006_charges.sql +43 -0
  13. package/dist/migrations/0007_coupons.sql +19 -0
  14. package/dist/migrations/0008_disputes.sql +17 -0
  15. package/dist/migrations/0009_events.sql +12 -0
  16. package/dist/migrations/0010_payouts.sql +30 -0
  17. package/dist/migrations/0011_plans.sql +25 -0
  18. package/dist/migrations/0012_add_updated_at.sql +108 -0
  19. package/dist/migrations/0013_add_subscription_items.sql +12 -0
  20. package/dist/migrations/0014_migrate_subscription_items.sql +26 -0
  21. package/dist/migrations/0015_add_customer_deleted.sql +2 -0
  22. package/dist/migrations/0016_add_invoice_indexes.sql +2 -0
  23. package/dist/migrations/0017_drop_charges_unavailable_columns.sql +6 -0
  24. package/dist/migrations/0018_setup_intents.sql +17 -0
  25. package/dist/migrations/0019_payment_methods.sql +12 -0
  26. package/dist/migrations/0020_disputes_payment_intent_created_idx.sql +3 -0
  27. package/dist/migrations/0021_payment_intent.sql +42 -0
  28. package/dist/migrations/0022_adjust_plans.sql +5 -0
  29. package/dist/migrations/0023_invoice_deleted.sql +1 -0
  30. package/dist/migrations/0024_subscription_schedules.sql +29 -0
  31. package/dist/migrations/0025_tax_ids.sql +14 -0
  32. package/dist/migrations/0026_credit_notes.sql +36 -0
  33. package/dist/migrations/0027_add_marketing_features_to_products.sql +2 -0
  34. package/dist/migrations/0028_early_fraud_warning.sql +22 -0
  35. package/dist/migrations/0029_reviews.sql +28 -0
  36. package/dist/migrations/0030_refunds.sql +29 -0
  37. package/dist/migrations/0031_add_default_price.sql +2 -0
  38. package/dist/migrations/0032_update_subscription_items.sql +3 -0
  39. package/dist/migrations/0033_add_last_synced_at.sql +85 -0
  40. package/dist/migrations/0034_remove_foreign_keys.sql +13 -0
  41. package/dist/migrations/0035_checkout_sessions.sql +77 -0
  42. package/dist/migrations/0036_checkout_session_line_items.sql +24 -0
  43. package/dist/migrations/0037_add_features.sql +18 -0
  44. package/dist/migrations/0038_active_entitlement.sql +20 -0
  45. package/dist/migrations/0039_add_paused_to_subscription_status.sql +1 -0
  46. package/dist/migrations/0040_managed_webhooks.sql +28 -0
  47. package/package.json +60 -0
package/README.md ADDED
@@ -0,0 +1,100 @@
1
+ # Stripe Sync Engine
2
+
3
+ ![GitHub License](https://img.shields.io/github/license/supabase/stripe-sync-engine)
4
+ ![NPM Version](https://img.shields.io/npm/v/%40supabase%2Fstripe-sync-engine)
5
+
6
+ A TypeScript library to synchronize Stripe data into a PostgreSQL database, designed for use in Node.js backends and serverless environments.
7
+
8
+ ## Features
9
+
10
+ - Sync Stripe objects (customers, invoices, products, etc.) to your PostgreSQL database.
11
+ - Handles Stripe webhooks for real-time updates.
12
+ - Supports backfilling and entity revalidation.
13
+
14
+ ## Installation
15
+
16
+ ```sh
17
+ npm install @supabase/stripe-sync-engine stripe
18
+ # or
19
+ pnpm add @supabase/stripe-sync-engine stripe
20
+ # or
21
+ yarn add @supabase/stripe-sync-engine stripe
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ ```ts
27
+ import { StripeSync } from '@supabase/stripe-sync-engine'
28
+
29
+ const sync = new StripeSync({
30
+ poolConfig: {
31
+ connectionString: 'postgres://user:pass@host:port/db',
32
+ max: 10, // Maximum number of connections
33
+ },
34
+ stripeSecretKey: 'sk_test_...',
35
+ stripeWebhookSecret: 'whsec_...',
36
+ // logger: <a pino logger>
37
+ })
38
+
39
+ // Example: process a Stripe webhook
40
+ await sync.processWebhook(payload, signature)
41
+ ```
42
+
43
+ ## Configuration
44
+
45
+ | Option | Type | Description |
46
+ | ------------------------------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
47
+ | `databaseUrl` | string | **Deprecated:** Use `poolConfig` with a connection string instead. |
48
+ | `schema` | string | Database schema name (default: `stripe`) |
49
+ | `stripeSecretKey` | string | Stripe secret key |
50
+ | `stripeWebhookSecret` | string | Stripe webhook signing secret |
51
+ | `stripeApiVersion` | string | Stripe API version (default: `2020-08-27`) |
52
+ | `autoExpandLists` | boolean | Fetch all list items from Stripe (not just the default 10) |
53
+ | `backfillRelatedEntities` | boolean | Ensure related entities are present for foreign key integrity |
54
+ | `revalidateObjectsViaStripeApi` | Array | Always fetch latest entity from Stripe instead of trusting webhook payload, possible values: charge, credit_note, customer, dispute, invoice, payment_intent, payment_method, plan, price, product, refund, review, radar.early_fraud_warning, setup_intent, subscription, subscription_schedule, tax_id |
55
+ | `poolConfig` | object | Configuration for PostgreSQL connection pooling. Supports options like `connectionString`, `max`, and `keepAlive`. For more details, refer to the [Node-Postgres Pool API documentation](https://node-postgres.com/apis/pool). |
56
+ | `maxPostgresConnections` | number | **Deprecated:** Use `poolConfig.max` instead to configure the maximum number of PostgreSQL connections. |
57
+ | `logger` | Logger | Logger instance (pino) |
58
+
59
+ ## Database Schema
60
+
61
+ The library will create and manage a `stripe` schema in your PostgreSQL database, with tables for all supported Stripe objects (products, customers, invoices, etc.).
62
+
63
+ ### Migrations
64
+
65
+ Migrations are included in the `db/migrations` directory. You can run them using the provided `runMigrations` function:
66
+
67
+ ```ts
68
+ import { runMigrations } from '@supabase/stripe-sync-engine'
69
+
70
+ await runMigrations({ databaseUrl: 'postgres://...' })
71
+ ```
72
+
73
+ ## Backfilling and Syncing Data
74
+
75
+ ### Syncing a Single Entity
76
+
77
+ You can sync or update a single Stripe entity by its ID using the `syncSingleEntity` method:
78
+
79
+ ```ts
80
+ await sync.syncSingleEntity('cus_12345')
81
+ ```
82
+
83
+ The entity type is detected automatically based on the Stripe ID prefix (e.g., `cus_` for customer, `prod_` for product). `ent_` is not supported at the moment.
84
+
85
+ ### Backfilling Data
86
+
87
+ To backfill Stripe data (e.g., all products created after a certain date), use the `syncBackfill` method:
88
+
89
+ ```ts
90
+ await sync.syncBackfill({
91
+ object: 'product',
92
+ created: { gte: 1643872333 }, // Unix timestamp
93
+ })
94
+ ```
95
+
96
+ - `object` can be one of: `all`, `charge`, `customer`, `dispute`, `invoice`, `payment_method`, `payment_intent`, `plan`, `price`, `product`, `setup_intent`, `subscription`.
97
+ - `created` is a Stripe RangeQueryParam and supports `gt`, `gte`, `lt`, `lte`.
98
+
99
+ > **Note:**
100
+ > For large Stripe accounts (more than 10,000 objects), it is recommended to write a script that loops through each day and sets the `created` date filters to the start and end of day. This avoids timeouts and memory issues when syncing large datasets.