stripe-experiment-sync 1.0.3 → 1.0.7
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 +25 -4
- package/dist/chunk-3P5TZKWU.js +595 -0
- package/dist/chunk-7JWRDXNB.js +3264 -0
- package/dist/chunk-SX3HLE4H.js +406 -0
- package/dist/{chunk-SNGEJHKN.js → chunk-YXQZXR7S.js} +18 -6
- package/dist/cli/index.cjs +4376 -0
- package/dist/cli/index.d.cts +1 -0
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +55 -0
- package/dist/cli/lib.cjs +4359 -0
- package/dist/cli/lib.d.cts +70 -0
- package/dist/cli/lib.d.ts +70 -0
- package/dist/cli/lib.js +21 -0
- package/dist/index.cjs +48 -36
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +7 -3255
- package/dist/migrations/0057_rename_sync_tables.sql +57 -0
- package/dist/supabase/index.cjs +98 -50
- package/dist/supabase/index.d.cts +12 -4
- package/dist/supabase/index.d.ts +12 -4
- package/dist/supabase/index.js +18 -367
- package/package.json +18 -6
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
interface Config {
|
|
2
|
+
stripeApiKey: string;
|
|
3
|
+
ngrokAuthToken: string;
|
|
4
|
+
databaseUrl: string;
|
|
5
|
+
}
|
|
6
|
+
interface CliOptions {
|
|
7
|
+
stripeKey?: string;
|
|
8
|
+
ngrokToken?: string;
|
|
9
|
+
databaseUrl?: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Load configuration from .env file, environment variables, and interactive prompts.
|
|
13
|
+
* Values are masked with *** when prompting for sensitive information.
|
|
14
|
+
*/
|
|
15
|
+
declare function loadConfig(options: CliOptions): Promise<Config>;
|
|
16
|
+
|
|
17
|
+
interface DeployOptions {
|
|
18
|
+
supabaseAccessToken?: string;
|
|
19
|
+
supabaseProjectRef?: string;
|
|
20
|
+
stripeKey?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Backfill command - backfills a specific entity type from Stripe.
|
|
25
|
+
*/
|
|
26
|
+
declare function backfillCommand(options: CliOptions, entityName: string): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Migration command - runs database migrations only.
|
|
29
|
+
*/
|
|
30
|
+
declare function migrateCommand(options: CliOptions): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Main sync command - syncs Stripe data to PostgreSQL using webhooks for real-time updates.
|
|
33
|
+
* 1. Runs database migrations
|
|
34
|
+
* 2. Creates StripeSync instance
|
|
35
|
+
* 3. Creates ngrok tunnel and Stripe webhook endpoint
|
|
36
|
+
* 4. Runs initial backfill of all Stripe data
|
|
37
|
+
* 5. Keeps running to process live webhook events (Ctrl+C to stop)
|
|
38
|
+
*/
|
|
39
|
+
declare function syncCommand(options: CliOptions): Promise<void>;
|
|
40
|
+
/**
|
|
41
|
+
* Install command - installs Stripe sync Edge Functions to Supabase.
|
|
42
|
+
* 1. Validates Supabase project access
|
|
43
|
+
* 2. Deploys stripe-setup, stripe-webhook, and stripe-worker Edge Functions
|
|
44
|
+
* 3. Sets required secrets (STRIPE_SECRET_KEY)
|
|
45
|
+
* 4. Runs the setup function to create webhook and run migrations
|
|
46
|
+
*/
|
|
47
|
+
declare function installCommand(options: DeployOptions): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* Uninstall command - removes Stripe sync Edge Functions and resources from Supabase.
|
|
50
|
+
* 1. Validates Supabase project access
|
|
51
|
+
* 2. Deletes Stripe webhooks
|
|
52
|
+
* 3. Deletes Edge Functions (stripe-setup, stripe-webhook, stripe-worker)
|
|
53
|
+
* 4. Deletes secrets and pg_cron jobs
|
|
54
|
+
* 5. Drops the stripe schema
|
|
55
|
+
*/
|
|
56
|
+
declare function uninstallCommand(options: DeployOptions): Promise<void>;
|
|
57
|
+
|
|
58
|
+
interface NgrokTunnel {
|
|
59
|
+
url: string;
|
|
60
|
+
close: () => Promise<void>;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Create an ngrok tunnel to expose the local server to the internet.
|
|
64
|
+
* @param port - The local port to expose
|
|
65
|
+
* @param authToken - ngrok authentication token
|
|
66
|
+
* @returns The tunnel URL and a close function
|
|
67
|
+
*/
|
|
68
|
+
declare function createTunnel(port: number, authToken: string): Promise<NgrokTunnel>;
|
|
69
|
+
|
|
70
|
+
export { type CliOptions, type Config, type DeployOptions, type NgrokTunnel, backfillCommand, createTunnel, installCommand, loadConfig, migrateCommand, syncCommand, uninstallCommand };
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
interface Config {
|
|
2
|
+
stripeApiKey: string;
|
|
3
|
+
ngrokAuthToken: string;
|
|
4
|
+
databaseUrl: string;
|
|
5
|
+
}
|
|
6
|
+
interface CliOptions {
|
|
7
|
+
stripeKey?: string;
|
|
8
|
+
ngrokToken?: string;
|
|
9
|
+
databaseUrl?: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Load configuration from .env file, environment variables, and interactive prompts.
|
|
13
|
+
* Values are masked with *** when prompting for sensitive information.
|
|
14
|
+
*/
|
|
15
|
+
declare function loadConfig(options: CliOptions): Promise<Config>;
|
|
16
|
+
|
|
17
|
+
interface DeployOptions {
|
|
18
|
+
supabaseAccessToken?: string;
|
|
19
|
+
supabaseProjectRef?: string;
|
|
20
|
+
stripeKey?: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Backfill command - backfills a specific entity type from Stripe.
|
|
25
|
+
*/
|
|
26
|
+
declare function backfillCommand(options: CliOptions, entityName: string): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Migration command - runs database migrations only.
|
|
29
|
+
*/
|
|
30
|
+
declare function migrateCommand(options: CliOptions): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Main sync command - syncs Stripe data to PostgreSQL using webhooks for real-time updates.
|
|
33
|
+
* 1. Runs database migrations
|
|
34
|
+
* 2. Creates StripeSync instance
|
|
35
|
+
* 3. Creates ngrok tunnel and Stripe webhook endpoint
|
|
36
|
+
* 4. Runs initial backfill of all Stripe data
|
|
37
|
+
* 5. Keeps running to process live webhook events (Ctrl+C to stop)
|
|
38
|
+
*/
|
|
39
|
+
declare function syncCommand(options: CliOptions): Promise<void>;
|
|
40
|
+
/**
|
|
41
|
+
* Install command - installs Stripe sync Edge Functions to Supabase.
|
|
42
|
+
* 1. Validates Supabase project access
|
|
43
|
+
* 2. Deploys stripe-setup, stripe-webhook, and stripe-worker Edge Functions
|
|
44
|
+
* 3. Sets required secrets (STRIPE_SECRET_KEY)
|
|
45
|
+
* 4. Runs the setup function to create webhook and run migrations
|
|
46
|
+
*/
|
|
47
|
+
declare function installCommand(options: DeployOptions): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* Uninstall command - removes Stripe sync Edge Functions and resources from Supabase.
|
|
50
|
+
* 1. Validates Supabase project access
|
|
51
|
+
* 2. Deletes Stripe webhooks
|
|
52
|
+
* 3. Deletes Edge Functions (stripe-setup, stripe-webhook, stripe-worker)
|
|
53
|
+
* 4. Deletes secrets and pg_cron jobs
|
|
54
|
+
* 5. Drops the stripe schema
|
|
55
|
+
*/
|
|
56
|
+
declare function uninstallCommand(options: DeployOptions): Promise<void>;
|
|
57
|
+
|
|
58
|
+
interface NgrokTunnel {
|
|
59
|
+
url: string;
|
|
60
|
+
close: () => Promise<void>;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Create an ngrok tunnel to expose the local server to the internet.
|
|
64
|
+
* @param port - The local port to expose
|
|
65
|
+
* @param authToken - ngrok authentication token
|
|
66
|
+
* @returns The tunnel URL and a close function
|
|
67
|
+
*/
|
|
68
|
+
declare function createTunnel(port: number, authToken: string): Promise<NgrokTunnel>;
|
|
69
|
+
|
|
70
|
+
export { type CliOptions, type Config, type DeployOptions, type NgrokTunnel, backfillCommand, createTunnel, installCommand, loadConfig, migrateCommand, syncCommand, uninstallCommand };
|
package/dist/cli/lib.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import {
|
|
2
|
+
backfillCommand,
|
|
3
|
+
createTunnel,
|
|
4
|
+
installCommand,
|
|
5
|
+
loadConfig,
|
|
6
|
+
migrateCommand,
|
|
7
|
+
syncCommand,
|
|
8
|
+
uninstallCommand
|
|
9
|
+
} from "../chunk-3P5TZKWU.js";
|
|
10
|
+
import "../chunk-7JWRDXNB.js";
|
|
11
|
+
import "../chunk-SX3HLE4H.js";
|
|
12
|
+
import "../chunk-YXQZXR7S.js";
|
|
13
|
+
export {
|
|
14
|
+
backfillCommand,
|
|
15
|
+
createTunnel,
|
|
16
|
+
installCommand,
|
|
17
|
+
loadConfig,
|
|
18
|
+
migrateCommand,
|
|
19
|
+
syncCommand,
|
|
20
|
+
uninstallCommand
|
|
21
|
+
};
|
package/dist/index.cjs
CHANGED
|
@@ -38,19 +38,19 @@ __export(index_exports, {
|
|
|
38
38
|
});
|
|
39
39
|
module.exports = __toCommonJS(index_exports);
|
|
40
40
|
|
|
41
|
-
// ../../node_modules/.pnpm/tsup@8.5.0_postcss@8.5.6_tsx@4.
|
|
41
|
+
// ../../node_modules/.pnpm/tsup@8.5.0_postcss@8.5.6_tsx@4.21.0_typescript@5.9.3_yaml@2.8.1/node_modules/tsup/assets/cjs_shims.js
|
|
42
42
|
var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
|
|
43
43
|
var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
|
|
44
44
|
|
|
45
45
|
// package.json
|
|
46
46
|
var package_default = {
|
|
47
47
|
name: "stripe-experiment-sync",
|
|
48
|
-
version: "1.0.
|
|
48
|
+
version: "1.0.7",
|
|
49
49
|
private: false,
|
|
50
50
|
description: "Stripe Sync Engine to sync Stripe data to Postgres",
|
|
51
51
|
type: "module",
|
|
52
52
|
main: "./dist/index.cjs",
|
|
53
|
-
bin: "./dist/cli/index.
|
|
53
|
+
bin: "./dist/cli/index.js",
|
|
54
54
|
exports: {
|
|
55
55
|
".": {
|
|
56
56
|
types: "./dist/index.d.ts",
|
|
@@ -61,12 +61,17 @@ var package_default = {
|
|
|
61
61
|
types: "./dist/supabase/index.d.ts",
|
|
62
62
|
import: "./dist/supabase/index.js",
|
|
63
63
|
require: "./dist/supabase/index.cjs"
|
|
64
|
+
},
|
|
65
|
+
"./cli": {
|
|
66
|
+
types: "./dist/cli/lib.d.ts",
|
|
67
|
+
import: "./dist/cli/lib.js",
|
|
68
|
+
require: "./dist/cli/lib.cjs"
|
|
64
69
|
}
|
|
65
70
|
},
|
|
66
71
|
scripts: {
|
|
67
72
|
clean: "rimraf dist",
|
|
68
73
|
prebuild: "npm run clean",
|
|
69
|
-
build: "tsup src/index.ts src/supabase/index.ts --format esm,cjs --dts --shims && cp -r src/database/migrations dist/migrations",
|
|
74
|
+
build: "tsup src/index.ts src/supabase/index.ts src/cli/index.ts src/cli/lib.ts --format esm,cjs --dts --shims && cp -r src/database/migrations dist/migrations",
|
|
70
75
|
lint: "eslint src --ext .ts",
|
|
71
76
|
test: "vitest"
|
|
72
77
|
},
|
|
@@ -74,21 +79,28 @@ var package_default = {
|
|
|
74
79
|
"dist"
|
|
75
80
|
],
|
|
76
81
|
dependencies: {
|
|
82
|
+
"@ngrok/ngrok": "^1.4.1",
|
|
83
|
+
chalk: "^5.3.0",
|
|
84
|
+
commander: "^12.1.0",
|
|
85
|
+
dotenv: "^16.4.7",
|
|
86
|
+
express: "^4.18.2",
|
|
87
|
+
inquirer: "^12.3.0",
|
|
77
88
|
pg: "^8.16.3",
|
|
78
89
|
"pg-node-migrations": "0.0.8",
|
|
90
|
+
stripe: "^17.7.0",
|
|
79
91
|
"supabase-management-js": "^0.1.6",
|
|
80
92
|
ws: "^8.18.0",
|
|
81
93
|
yesql: "^7.0.0"
|
|
82
94
|
},
|
|
83
|
-
peerDependencies: {
|
|
84
|
-
stripe: "> 11"
|
|
85
|
-
},
|
|
86
95
|
devDependencies: {
|
|
96
|
+
"@types/express": "^4.17.21",
|
|
97
|
+
"@types/inquirer": "^9.0.7",
|
|
87
98
|
"@types/node": "^24.10.1",
|
|
88
99
|
"@types/pg": "^8.15.5",
|
|
89
100
|
"@types/ws": "^8.5.13",
|
|
90
101
|
"@types/yesql": "^4.1.4",
|
|
91
102
|
"@vitest/ui": "^4.0.9",
|
|
103
|
+
tsx: "^4.19.2",
|
|
92
104
|
vitest: "^3.2.4"
|
|
93
105
|
},
|
|
94
106
|
repository: {
|
|
@@ -143,9 +155,9 @@ var ORDERED_STRIPE_TABLES = [
|
|
|
143
155
|
"reviews",
|
|
144
156
|
"_managed_webhooks",
|
|
145
157
|
"customers",
|
|
146
|
-
"
|
|
147
|
-
// Must be deleted before
|
|
148
|
-
"
|
|
158
|
+
"_sync_obj_runs",
|
|
159
|
+
// Must be deleted before _sync_runs (foreign key)
|
|
160
|
+
"_sync_runs"
|
|
149
161
|
];
|
|
150
162
|
var TABLES_WITH_ACCOUNT_ID = /* @__PURE__ */ new Set(["_managed_webhooks"]);
|
|
151
163
|
var PostgresClient = class {
|
|
@@ -436,7 +448,7 @@ var PostgresClient = class {
|
|
|
436
448
|
// Observable Sync System Methods
|
|
437
449
|
// =============================================================================
|
|
438
450
|
// These methods support long-running syncs with full observability.
|
|
439
|
-
// Uses two tables:
|
|
451
|
+
// Uses two tables: _sync_runs (parent) and _sync_obj_runs (children)
|
|
440
452
|
// RunKey = (accountId, runStartedAt) - natural composite key
|
|
441
453
|
/**
|
|
442
454
|
* Cancel stale runs (running but no object updated in 5 minutes).
|
|
@@ -446,7 +458,7 @@ var PostgresClient = class {
|
|
|
446
458
|
*/
|
|
447
459
|
async cancelStaleRuns(accountId) {
|
|
448
460
|
await this.query(
|
|
449
|
-
`UPDATE "${this.config.schema}"."
|
|
461
|
+
`UPDATE "${this.config.schema}"."_sync_obj_runs" o
|
|
450
462
|
SET status = 'error',
|
|
451
463
|
error_message = 'Auto-cancelled: stale (no update in 5 min)',
|
|
452
464
|
completed_at = now()
|
|
@@ -456,17 +468,17 @@ var PostgresClient = class {
|
|
|
456
468
|
[accountId]
|
|
457
469
|
);
|
|
458
470
|
await this.query(
|
|
459
|
-
`UPDATE "${this.config.schema}"."
|
|
471
|
+
`UPDATE "${this.config.schema}"."_sync_runs" r
|
|
460
472
|
SET closed_at = now()
|
|
461
473
|
WHERE r."_account_id" = $1
|
|
462
474
|
AND r.closed_at IS NULL
|
|
463
475
|
AND EXISTS (
|
|
464
|
-
SELECT 1 FROM "${this.config.schema}"."
|
|
476
|
+
SELECT 1 FROM "${this.config.schema}"."_sync_obj_runs" o
|
|
465
477
|
WHERE o."_account_id" = r."_account_id"
|
|
466
478
|
AND o.run_started_at = r.started_at
|
|
467
479
|
)
|
|
468
480
|
AND NOT EXISTS (
|
|
469
|
-
SELECT 1 FROM "${this.config.schema}"."
|
|
481
|
+
SELECT 1 FROM "${this.config.schema}"."_sync_obj_runs" o
|
|
470
482
|
WHERE o."_account_id" = r."_account_id"
|
|
471
483
|
AND o.run_started_at = r.started_at
|
|
472
484
|
AND o.status IN ('pending', 'running')
|
|
@@ -484,7 +496,7 @@ var PostgresClient = class {
|
|
|
484
496
|
async getOrCreateSyncRun(accountId, triggeredBy) {
|
|
485
497
|
await this.cancelStaleRuns(accountId);
|
|
486
498
|
const existing = await this.query(
|
|
487
|
-
`SELECT "_account_id", started_at FROM "${this.config.schema}"."
|
|
499
|
+
`SELECT "_account_id", started_at FROM "${this.config.schema}"."_sync_runs"
|
|
488
500
|
WHERE "_account_id" = $1 AND closed_at IS NULL`,
|
|
489
501
|
[accountId]
|
|
490
502
|
);
|
|
@@ -494,7 +506,7 @@ var PostgresClient = class {
|
|
|
494
506
|
}
|
|
495
507
|
try {
|
|
496
508
|
const result = await this.query(
|
|
497
|
-
`INSERT INTO "${this.config.schema}"."
|
|
509
|
+
`INSERT INTO "${this.config.schema}"."_sync_runs" ("_account_id", triggered_by, started_at)
|
|
498
510
|
VALUES ($1, $2, date_trunc('milliseconds', now()))
|
|
499
511
|
RETURNING "_account_id", started_at`,
|
|
500
512
|
[accountId, triggeredBy ?? null]
|
|
@@ -513,7 +525,7 @@ var PostgresClient = class {
|
|
|
513
525
|
*/
|
|
514
526
|
async getActiveSyncRun(accountId) {
|
|
515
527
|
const result = await this.query(
|
|
516
|
-
`SELECT "_account_id", started_at FROM "${this.config.schema}"."
|
|
528
|
+
`SELECT "_account_id", started_at FROM "${this.config.schema}"."_sync_runs"
|
|
517
529
|
WHERE "_account_id" = $1 AND closed_at IS NULL`,
|
|
518
530
|
[accountId]
|
|
519
531
|
);
|
|
@@ -523,12 +535,12 @@ var PostgresClient = class {
|
|
|
523
535
|
}
|
|
524
536
|
/**
|
|
525
537
|
* Get sync run config (for concurrency control).
|
|
526
|
-
* Status is derived from
|
|
538
|
+
* Status is derived from sync_runs view.
|
|
527
539
|
*/
|
|
528
540
|
async getSyncRun(accountId, runStartedAt) {
|
|
529
541
|
const result = await this.query(
|
|
530
542
|
`SELECT "_account_id", started_at, max_concurrent, closed_at
|
|
531
|
-
FROM "${this.config.schema}"."
|
|
543
|
+
FROM "${this.config.schema}"."_sync_runs"
|
|
532
544
|
WHERE "_account_id" = $1 AND started_at = $2`,
|
|
533
545
|
[accountId, runStartedAt]
|
|
534
546
|
);
|
|
@@ -547,7 +559,7 @@ var PostgresClient = class {
|
|
|
547
559
|
*/
|
|
548
560
|
async closeSyncRun(accountId, runStartedAt) {
|
|
549
561
|
await this.query(
|
|
550
|
-
`UPDATE "${this.config.schema}"."
|
|
562
|
+
`UPDATE "${this.config.schema}"."_sync_runs"
|
|
551
563
|
SET closed_at = now()
|
|
552
564
|
WHERE "_account_id" = $1 AND started_at = $2 AND closed_at IS NULL`,
|
|
553
565
|
[accountId, runStartedAt]
|
|
@@ -561,7 +573,7 @@ var PostgresClient = class {
|
|
|
561
573
|
if (objects.length === 0) return;
|
|
562
574
|
const values = objects.map((_, i) => `($1, $2, $${i + 3})`).join(", ");
|
|
563
575
|
await this.query(
|
|
564
|
-
`INSERT INTO "${this.config.schema}"."
|
|
576
|
+
`INSERT INTO "${this.config.schema}"."_sync_obj_runs" ("_account_id", run_started_at, object)
|
|
565
577
|
VALUES ${values}
|
|
566
578
|
ON CONFLICT ("_account_id", run_started_at, object) DO NOTHING`,
|
|
567
579
|
[accountId, runStartedAt, ...objects]
|
|
@@ -580,7 +592,7 @@ var PostgresClient = class {
|
|
|
580
592
|
const runningCount = await this.countRunningObjects(accountId, runStartedAt);
|
|
581
593
|
if (runningCount >= run.maxConcurrent) return false;
|
|
582
594
|
const result = await this.query(
|
|
583
|
-
`UPDATE "${this.config.schema}"."
|
|
595
|
+
`UPDATE "${this.config.schema}"."_sync_obj_runs"
|
|
584
596
|
SET status = 'running', started_at = now(), updated_at = now()
|
|
585
597
|
WHERE "_account_id" = $1 AND run_started_at = $2 AND object = $3 AND status = 'pending'
|
|
586
598
|
RETURNING *`,
|
|
@@ -594,7 +606,7 @@ var PostgresClient = class {
|
|
|
594
606
|
async getObjectRun(accountId, runStartedAt, object) {
|
|
595
607
|
const result = await this.query(
|
|
596
608
|
`SELECT object, status, processed_count, cursor
|
|
597
|
-
FROM "${this.config.schema}"."
|
|
609
|
+
FROM "${this.config.schema}"."_sync_obj_runs"
|
|
598
610
|
WHERE "_account_id" = $1 AND run_started_at = $2 AND object = $3`,
|
|
599
611
|
[accountId, runStartedAt, object]
|
|
600
612
|
);
|
|
@@ -613,7 +625,7 @@ var PostgresClient = class {
|
|
|
613
625
|
*/
|
|
614
626
|
async incrementObjectProgress(accountId, runStartedAt, object, count) {
|
|
615
627
|
await this.query(
|
|
616
|
-
`UPDATE "${this.config.schema}"."
|
|
628
|
+
`UPDATE "${this.config.schema}"."_sync_obj_runs"
|
|
617
629
|
SET processed_count = processed_count + $4, updated_at = now()
|
|
618
630
|
WHERE "_account_id" = $1 AND run_started_at = $2 AND object = $3`,
|
|
619
631
|
[accountId, runStartedAt, object, count]
|
|
@@ -629,7 +641,7 @@ var PostgresClient = class {
|
|
|
629
641
|
const isNumeric = cursor !== null && /^\d+$/.test(cursor);
|
|
630
642
|
if (isNumeric) {
|
|
631
643
|
await this.query(
|
|
632
|
-
`UPDATE "${this.config.schema}"."
|
|
644
|
+
`UPDATE "${this.config.schema}"."_sync_obj_runs"
|
|
633
645
|
SET cursor = GREATEST(COALESCE(cursor::bigint, 0), $4::bigint)::text,
|
|
634
646
|
updated_at = now()
|
|
635
647
|
WHERE "_account_id" = $1 AND run_started_at = $2 AND object = $3`,
|
|
@@ -637,7 +649,7 @@ var PostgresClient = class {
|
|
|
637
649
|
);
|
|
638
650
|
} else {
|
|
639
651
|
await this.query(
|
|
640
|
-
`UPDATE "${this.config.schema}"."
|
|
652
|
+
`UPDATE "${this.config.schema}"."_sync_obj_runs"
|
|
641
653
|
SET cursor = $4, updated_at = now()
|
|
642
654
|
WHERE "_account_id" = $1 AND run_started_at = $2 AND object = $3`,
|
|
643
655
|
[accountId, runStartedAt, object, cursor]
|
|
@@ -653,7 +665,7 @@ var PostgresClient = class {
|
|
|
653
665
|
async getLastCompletedCursor(accountId, object) {
|
|
654
666
|
const result = await this.query(
|
|
655
667
|
`SELECT MAX(o.cursor::bigint)::text as cursor
|
|
656
|
-
FROM "${this.config.schema}"."
|
|
668
|
+
FROM "${this.config.schema}"."_sync_obj_runs" o
|
|
657
669
|
WHERE o."_account_id" = $1
|
|
658
670
|
AND o.object = $2
|
|
659
671
|
AND o.cursor IS NOT NULL`,
|
|
@@ -667,10 +679,10 @@ var PostgresClient = class {
|
|
|
667
679
|
*/
|
|
668
680
|
async deleteSyncRuns(accountId) {
|
|
669
681
|
await this.query(
|
|
670
|
-
`DELETE FROM "${this.config.schema}"."
|
|
682
|
+
`DELETE FROM "${this.config.schema}"."_sync_obj_runs" WHERE "_account_id" = $1`,
|
|
671
683
|
[accountId]
|
|
672
684
|
);
|
|
673
|
-
await this.query(`DELETE FROM "${this.config.schema}"."
|
|
685
|
+
await this.query(`DELETE FROM "${this.config.schema}"."_sync_runs" WHERE "_account_id" = $1`, [
|
|
674
686
|
accountId
|
|
675
687
|
]);
|
|
676
688
|
}
|
|
@@ -680,7 +692,7 @@ var PostgresClient = class {
|
|
|
680
692
|
*/
|
|
681
693
|
async completeObjectSync(accountId, runStartedAt, object) {
|
|
682
694
|
await this.query(
|
|
683
|
-
`UPDATE "${this.config.schema}"."
|
|
695
|
+
`UPDATE "${this.config.schema}"."_sync_obj_runs"
|
|
684
696
|
SET status = 'complete', completed_at = now()
|
|
685
697
|
WHERE "_account_id" = $1 AND run_started_at = $2 AND object = $3`,
|
|
686
698
|
[accountId, runStartedAt, object]
|
|
@@ -696,7 +708,7 @@ var PostgresClient = class {
|
|
|
696
708
|
*/
|
|
697
709
|
async failObjectSync(accountId, runStartedAt, object, errorMessage) {
|
|
698
710
|
await this.query(
|
|
699
|
-
`UPDATE "${this.config.schema}"."
|
|
711
|
+
`UPDATE "${this.config.schema}"."_sync_obj_runs"
|
|
700
712
|
SET status = 'error', error_message = $4, completed_at = now()
|
|
701
713
|
WHERE "_account_id" = $1 AND run_started_at = $2 AND object = $3`,
|
|
702
714
|
[accountId, runStartedAt, object, errorMessage]
|
|
@@ -711,7 +723,7 @@ var PostgresClient = class {
|
|
|
711
723
|
*/
|
|
712
724
|
async hasAnyObjectErrors(accountId, runStartedAt) {
|
|
713
725
|
const result = await this.query(
|
|
714
|
-
`SELECT COUNT(*) as count FROM "${this.config.schema}"."
|
|
726
|
+
`SELECT COUNT(*) as count FROM "${this.config.schema}"."_sync_obj_runs"
|
|
715
727
|
WHERE "_account_id" = $1 AND run_started_at = $2 AND status = 'error'`,
|
|
716
728
|
[accountId, runStartedAt]
|
|
717
729
|
);
|
|
@@ -722,7 +734,7 @@ var PostgresClient = class {
|
|
|
722
734
|
*/
|
|
723
735
|
async countRunningObjects(accountId, runStartedAt) {
|
|
724
736
|
const result = await this.query(
|
|
725
|
-
`SELECT COUNT(*) as count FROM "${this.config.schema}"."
|
|
737
|
+
`SELECT COUNT(*) as count FROM "${this.config.schema}"."_sync_obj_runs"
|
|
726
738
|
WHERE "_account_id" = $1 AND run_started_at = $2 AND status = 'running'`,
|
|
727
739
|
[accountId, runStartedAt]
|
|
728
740
|
);
|
|
@@ -738,7 +750,7 @@ var PostgresClient = class {
|
|
|
738
750
|
const runningCount = await this.countRunningObjects(accountId, runStartedAt);
|
|
739
751
|
if (runningCount >= run.maxConcurrent) return null;
|
|
740
752
|
const result = await this.query(
|
|
741
|
-
`SELECT object FROM "${this.config.schema}"."
|
|
753
|
+
`SELECT object FROM "${this.config.schema}"."_sync_obj_runs"
|
|
742
754
|
WHERE "_account_id" = $1 AND run_started_at = $2 AND status = 'pending'
|
|
743
755
|
ORDER BY object
|
|
744
756
|
LIMIT 1`,
|
|
@@ -751,7 +763,7 @@ var PostgresClient = class {
|
|
|
751
763
|
*/
|
|
752
764
|
async areAllObjectsComplete(accountId, runStartedAt) {
|
|
753
765
|
const result = await this.query(
|
|
754
|
-
`SELECT COUNT(*) as count FROM "${this.config.schema}"."
|
|
766
|
+
`SELECT COUNT(*) as count FROM "${this.config.schema}"."_sync_obj_runs"
|
|
755
767
|
WHERE "_account_id" = $1 AND run_started_at = $2 AND status IN ('pending', 'running')`,
|
|
756
768
|
[accountId, runStartedAt]
|
|
757
769
|
);
|
package/dist/index.d.cts
CHANGED
|
@@ -105,7 +105,7 @@ declare class PostgresClient {
|
|
|
105
105
|
} | null>;
|
|
106
106
|
/**
|
|
107
107
|
* Get sync run config (for concurrency control).
|
|
108
|
-
* Status is derived from
|
|
108
|
+
* Status is derived from sync_runs view.
|
|
109
109
|
*/
|
|
110
110
|
getSyncRun(accountId: string, runStartedAt: Date): Promise<{
|
|
111
111
|
accountId: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -105,7 +105,7 @@ declare class PostgresClient {
|
|
|
105
105
|
} | null>;
|
|
106
106
|
/**
|
|
107
107
|
* Get sync run config (for concurrency control).
|
|
108
|
-
* Status is derived from
|
|
108
|
+
* Status is derived from sync_runs view.
|
|
109
109
|
*/
|
|
110
110
|
getSyncRun(accountId: string, runStartedAt: Date): Promise<{
|
|
111
111
|
accountId: string;
|