stripe-experiment-sync 1.0.0 → 1.0.2

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.
@@ -0,0 +1,72 @@
1
+ -- Fix generated columns: must drop and recreate with ::bigint cast
2
+ -- Money columns that can overflow PostgreSQL integer max (~2.1 billion)
3
+
4
+ -- checkout_session_line_items
5
+ ALTER TABLE "stripe"."checkout_session_line_items" DROP COLUMN "amount_discount";
6
+ ALTER TABLE "stripe"."checkout_session_line_items" ADD COLUMN "amount_discount" bigint GENERATED ALWAYS AS ((_raw_data->>'amount_discount')::bigint) STORED;
7
+ ALTER TABLE "stripe"."checkout_session_line_items" DROP COLUMN "amount_subtotal";
8
+ ALTER TABLE "stripe"."checkout_session_line_items" ADD COLUMN "amount_subtotal" bigint GENERATED ALWAYS AS ((_raw_data->>'amount_subtotal')::bigint) STORED;
9
+ ALTER TABLE "stripe"."checkout_session_line_items" DROP COLUMN "amount_tax";
10
+ ALTER TABLE "stripe"."checkout_session_line_items" ADD COLUMN "amount_tax" bigint GENERATED ALWAYS AS ((_raw_data->>'amount_tax')::bigint) STORED;
11
+ ALTER TABLE "stripe"."checkout_session_line_items" DROP COLUMN "amount_total";
12
+ ALTER TABLE "stripe"."checkout_session_line_items" ADD COLUMN "amount_total" bigint GENERATED ALWAYS AS ((_raw_data->>'amount_total')::bigint) STORED;
13
+
14
+ -- checkout_sessions
15
+ ALTER TABLE "stripe"."checkout_sessions" DROP COLUMN "amount_subtotal";
16
+ ALTER TABLE "stripe"."checkout_sessions" ADD COLUMN "amount_subtotal" bigint GENERATED ALWAYS AS ((_raw_data->>'amount_subtotal')::bigint) STORED;
17
+ ALTER TABLE "stripe"."checkout_sessions" DROP COLUMN "amount_total";
18
+ ALTER TABLE "stripe"."checkout_sessions" ADD COLUMN "amount_total" bigint GENERATED ALWAYS AS ((_raw_data->>'amount_total')::bigint) STORED;
19
+
20
+ -- credit_notes
21
+ ALTER TABLE "stripe"."credit_notes" DROP COLUMN "amount";
22
+ ALTER TABLE "stripe"."credit_notes" ADD COLUMN "amount" bigint GENERATED ALWAYS AS ((_raw_data->>'amount')::bigint) STORED;
23
+ ALTER TABLE "stripe"."credit_notes" DROP COLUMN "amount_shipping";
24
+ ALTER TABLE "stripe"."credit_notes" ADD COLUMN "amount_shipping" bigint GENERATED ALWAYS AS ((_raw_data->>'amount_shipping')::bigint) STORED;
25
+ ALTER TABLE "stripe"."credit_notes" DROP COLUMN "discount_amount";
26
+ ALTER TABLE "stripe"."credit_notes" ADD COLUMN "discount_amount" bigint GENERATED ALWAYS AS ((_raw_data->>'discount_amount')::bigint) STORED;
27
+ ALTER TABLE "stripe"."credit_notes" DROP COLUMN "out_of_band_amount";
28
+ ALTER TABLE "stripe"."credit_notes" ADD COLUMN "out_of_band_amount" bigint GENERATED ALWAYS AS ((_raw_data->>'out_of_band_amount')::bigint) STORED;
29
+ ALTER TABLE "stripe"."credit_notes" DROP COLUMN "subtotal";
30
+ ALTER TABLE "stripe"."credit_notes" ADD COLUMN "subtotal" bigint GENERATED ALWAYS AS ((_raw_data->>'subtotal')::bigint) STORED;
31
+ ALTER TABLE "stripe"."credit_notes" DROP COLUMN "subtotal_excluding_tax";
32
+ ALTER TABLE "stripe"."credit_notes" ADD COLUMN "subtotal_excluding_tax" bigint GENERATED ALWAYS AS ((_raw_data->>'subtotal_excluding_tax')::bigint) STORED;
33
+ ALTER TABLE "stripe"."credit_notes" DROP COLUMN "total";
34
+ ALTER TABLE "stripe"."credit_notes" ADD COLUMN "total" bigint GENERATED ALWAYS AS ((_raw_data->>'total')::bigint) STORED;
35
+ ALTER TABLE "stripe"."credit_notes" DROP COLUMN "total_excluding_tax";
36
+ ALTER TABLE "stripe"."credit_notes" ADD COLUMN "total_excluding_tax" bigint GENERATED ALWAYS AS ((_raw_data->>'total_excluding_tax')::bigint) STORED;
37
+
38
+ -- customers
39
+ ALTER TABLE "stripe"."customers" DROP COLUMN "balance";
40
+ ALTER TABLE "stripe"."customers" ADD COLUMN "balance" bigint GENERATED ALWAYS AS ((_raw_data->>'balance')::bigint) STORED;
41
+
42
+ -- invoices
43
+ ALTER TABLE "stripe"."invoices" DROP COLUMN "ending_balance";
44
+ ALTER TABLE "stripe"."invoices" ADD COLUMN "ending_balance" bigint GENERATED ALWAYS AS ((_raw_data->>'ending_balance')::bigint) STORED;
45
+ ALTER TABLE "stripe"."invoices" DROP COLUMN "starting_balance";
46
+ ALTER TABLE "stripe"."invoices" ADD COLUMN "starting_balance" bigint GENERATED ALWAYS AS ((_raw_data->>'starting_balance')::bigint) STORED;
47
+ ALTER TABLE "stripe"."invoices" DROP COLUMN "subtotal";
48
+ ALTER TABLE "stripe"."invoices" ADD COLUMN "subtotal" bigint GENERATED ALWAYS AS ((_raw_data->>'subtotal')::bigint) STORED;
49
+ ALTER TABLE "stripe"."invoices" DROP COLUMN "tax";
50
+ ALTER TABLE "stripe"."invoices" ADD COLUMN "tax" bigint GENERATED ALWAYS AS ((_raw_data->>'tax')::bigint) STORED;
51
+ ALTER TABLE "stripe"."invoices" DROP COLUMN "post_payment_credit_notes_amount";
52
+ ALTER TABLE "stripe"."invoices" ADD COLUMN "post_payment_credit_notes_amount" bigint GENERATED ALWAYS AS ((_raw_data->>'post_payment_credit_notes_amount')::bigint) STORED;
53
+ ALTER TABLE "stripe"."invoices" DROP COLUMN "pre_payment_credit_notes_amount";
54
+ ALTER TABLE "stripe"."invoices" ADD COLUMN "pre_payment_credit_notes_amount" bigint GENERATED ALWAYS AS ((_raw_data->>'pre_payment_credit_notes_amount')::bigint) STORED;
55
+
56
+ -- payment_intents
57
+ ALTER TABLE "stripe"."payment_intents" DROP COLUMN "amount";
58
+ ALTER TABLE "stripe"."payment_intents" ADD COLUMN "amount" bigint GENERATED ALWAYS AS ((_raw_data->>'amount')::bigint) STORED;
59
+ ALTER TABLE "stripe"."payment_intents" DROP COLUMN "amount_capturable";
60
+ ALTER TABLE "stripe"."payment_intents" ADD COLUMN "amount_capturable" bigint GENERATED ALWAYS AS ((_raw_data->>'amount_capturable')::bigint) STORED;
61
+ ALTER TABLE "stripe"."payment_intents" DROP COLUMN "amount_received";
62
+ ALTER TABLE "stripe"."payment_intents" ADD COLUMN "amount_received" bigint GENERATED ALWAYS AS ((_raw_data->>'amount_received')::bigint) STORED;
63
+ ALTER TABLE "stripe"."payment_intents" DROP COLUMN "application_fee_amount";
64
+ ALTER TABLE "stripe"."payment_intents" ADD COLUMN "application_fee_amount" bigint GENERATED ALWAYS AS ((_raw_data->>'application_fee_amount')::bigint) STORED;
65
+
66
+ -- prices
67
+ ALTER TABLE "stripe"."prices" DROP COLUMN "unit_amount";
68
+ ALTER TABLE "stripe"."prices" ADD COLUMN "unit_amount" bigint GENERATED ALWAYS AS ((_raw_data->>'unit_amount')::bigint) STORED;
69
+
70
+ -- refunds
71
+ ALTER TABLE "stripe"."refunds" DROP COLUMN "amount";
72
+ ALTER TABLE "stripe"."refunds" ADD COLUMN "amount" bigint GENERATED ALWAYS AS ((_raw_data->>'amount')::bigint) STORED;
@@ -0,0 +1,53 @@
1
+ -- Add closed_at column to _sync_run
2
+ -- closed_at IS NULL means the run is still active
3
+ -- Status is derived from object states when closed_at IS NOT NULL
4
+
5
+ -- Step 1: Drop dependent view first
6
+ DROP VIEW IF EXISTS "stripe"."sync_dashboard";
7
+
8
+ -- Step 2: Drop the old constraint, status column, and completed_at column
9
+ ALTER TABLE "stripe"."_sync_run" DROP CONSTRAINT IF EXISTS one_active_run_per_account;
10
+ ALTER TABLE "stripe"."_sync_run" DROP COLUMN IF EXISTS status;
11
+ ALTER TABLE "stripe"."_sync_run" DROP COLUMN IF EXISTS completed_at;
12
+
13
+ -- Step 3: Add closed_at column
14
+ ALTER TABLE "stripe"."_sync_run" ADD COLUMN IF NOT EXISTS closed_at TIMESTAMPTZ;
15
+
16
+ -- Step 4: Create exclusion constraint (only one active run per account)
17
+ ALTER TABLE "stripe"."_sync_run"
18
+ ADD CONSTRAINT one_active_run_per_account
19
+ EXCLUDE ("_account_id" WITH =) WHERE (closed_at IS NULL);
20
+
21
+ -- Step 5: Recreate sync_dashboard view (run-level only, one row per run)
22
+ -- Base table: _sync_run (parent sync operation)
23
+ -- Child table: _sync_obj_run (individual object syncs)
24
+ CREATE OR REPLACE VIEW "stripe"."sync_dashboard" AS
25
+ SELECT
26
+ run."_account_id" as account_id,
27
+ run.started_at,
28
+ run.closed_at,
29
+ run.max_concurrent,
30
+ run.triggered_by,
31
+ run.updated_at,
32
+ -- Derived status from object states
33
+ CASE
34
+ WHEN run.closed_at IS NULL THEN 'running'
35
+ WHEN EXISTS (
36
+ SELECT 1 FROM "stripe"."_sync_obj_run" obj
37
+ WHERE obj."_account_id" = run."_account_id"
38
+ AND obj.run_started_at = run.started_at
39
+ AND obj.status = 'error'
40
+ ) THEN 'error'
41
+ ELSE 'complete'
42
+ END as status,
43
+ -- First error message from failed objects
44
+ (SELECT obj.error_message FROM "stripe"."_sync_obj_run" obj
45
+ WHERE obj."_account_id" = run."_account_id"
46
+ AND obj.run_started_at = run.started_at
47
+ AND obj.status = 'error'
48
+ ORDER BY obj.object LIMIT 1) as error_message,
49
+ -- Total processed count across all objects
50
+ COALESCE((SELECT SUM(obj.processed_count) FROM "stripe"."_sync_obj_run" obj
51
+ WHERE obj."_account_id" = run."_account_id"
52
+ AND obj.run_started_at = run.started_at), 0) as processed_count
53
+ FROM "stripe"."_sync_run" run;
package/package.json CHANGED
@@ -1,46 +1,24 @@
1
1
  {
2
2
  "name": "stripe-experiment-sync",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "private": false,
5
5
  "description": "Stripe Sync Engine to sync Stripe data to Postgres",
6
6
  "type": "module",
7
7
  "main": "./dist/index.cjs",
8
8
  "exports": {
9
- ".": {
10
- "import": {
11
- "types": "./dist/index.d.ts",
12
- "default": "./dist/index.js"
13
- },
14
- "require": {
15
- "types": "./dist/index.d.cts",
16
- "default": "./dist/index.cjs"
17
- }
9
+ "import": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
18
12
  },
19
- "./pg": {
20
- "import": {
21
- "types": "./dist/pg.d.ts",
22
- "default": "./dist/pg.js"
23
- },
24
- "require": {
25
- "types": "./dist/pg.d.cts",
26
- "default": "./dist/pg.cjs"
27
- }
28
- },
29
- "./postgres-js": {
30
- "import": {
31
- "types": "./dist/postgres-js.d.ts",
32
- "default": "./dist/postgres-js.js"
33
- },
34
- "require": {
35
- "types": "./dist/postgres-js.d.cts",
36
- "default": "./dist/postgres-js.cjs"
37
- }
13
+ "require": {
14
+ "types": "./dist/index.d.cts",
15
+ "require": "./dist/index.cjs"
38
16
  }
39
17
  },
40
18
  "scripts": {
41
19
  "clean": "rimraf dist",
42
20
  "prebuild": "npm run clean",
43
- "build": "tsup src/index.ts src/pg.ts src/postgres-js.ts --format esm,cjs --dts --shims && cp -r src/database/migrations dist/migrations",
21
+ "build": "tsup src/index.ts --format esm,cjs --dts --shims && cp -r src/database/migrations dist/migrations",
44
22
  "lint": "eslint src --ext .ts",
45
23
  "test": "vitest"
46
24
  },
@@ -50,7 +28,6 @@
50
28
  "dependencies": {
51
29
  "pg": "^8.16.3",
52
30
  "pg-node-migrations": "0.0.8",
53
- "postgres": "^3.4.7",
54
31
  "ws": "^8.18.0",
55
32
  "yesql": "^7.0.0"
56
33
  },
@@ -63,7 +40,6 @@
63
40
  "@types/ws": "^8.5.13",
64
41
  "@types/yesql": "^4.1.4",
65
42
  "@vitest/ui": "^4.0.9",
66
- "stripe": "^20.0.0",
67
43
  "vitest": "^3.2.4"
68
44
  },
69
45
  "repository": {
@@ -1,51 +0,0 @@
1
- /**
2
- * pg-compatible client interface for use with pg-node-migrations.
3
- * This is the minimal interface required by the migration library.
4
- */
5
- interface PgCompatibleClient {
6
- query(sql: string | {
7
- text: string;
8
- values?: unknown[];
9
- }): Promise<{
10
- rows: unknown[];
11
- rowCount: number;
12
- }>;
13
- }
14
- /**
15
- * Database adapter interface for abstracting database operations.
16
- * This allows sync-engine to work with different database clients:
17
- * - pg (Node.js) - for CLI, tests, existing deployments
18
- * - postgres.js (Node.js + Deno) - for Supabase Edge Functions
19
- */
20
- interface DatabaseAdapter {
21
- /**
22
- * Execute a SQL query with optional parameters.
23
- * @param sql - The SQL query string with $1, $2, etc. placeholders
24
- * @param params - Optional array of parameter values
25
- * @returns Query result with rows and rowCount
26
- */
27
- query<T = Record<string, unknown>>(sql: string, params?: unknown[]): Promise<{
28
- rows: T[];
29
- rowCount: number;
30
- }>;
31
- /**
32
- * Close all connections and clean up resources.
33
- */
34
- end(): Promise<void>;
35
- /**
36
- * Execute a function while holding a PostgreSQL advisory lock.
37
- * Adapters that don't support locking should just execute fn() directly.
38
- *
39
- * @param lockId - Integer lock ID (use hashToInt32 to convert string keys)
40
- * @param fn - Function to execute while holding the lock
41
- * @returns Result of the function
42
- */
43
- withAdvisoryLock<T>(lockId: number, fn: () => Promise<T>): Promise<T>;
44
- /**
45
- * Returns a pg-compatible client for use with libraries that expect a pg.Client interface.
46
- * Used by pg-node-migrations to run database migrations.
47
- */
48
- toPgClient(): PgCompatibleClient;
49
- }
50
-
51
- export type { DatabaseAdapter as D, PgCompatibleClient as P };
@@ -1,51 +0,0 @@
1
- /**
2
- * pg-compatible client interface for use with pg-node-migrations.
3
- * This is the minimal interface required by the migration library.
4
- */
5
- interface PgCompatibleClient {
6
- query(sql: string | {
7
- text: string;
8
- values?: unknown[];
9
- }): Promise<{
10
- rows: unknown[];
11
- rowCount: number;
12
- }>;
13
- }
14
- /**
15
- * Database adapter interface for abstracting database operations.
16
- * This allows sync-engine to work with different database clients:
17
- * - pg (Node.js) - for CLI, tests, existing deployments
18
- * - postgres.js (Node.js + Deno) - for Supabase Edge Functions
19
- */
20
- interface DatabaseAdapter {
21
- /**
22
- * Execute a SQL query with optional parameters.
23
- * @param sql - The SQL query string with $1, $2, etc. placeholders
24
- * @param params - Optional array of parameter values
25
- * @returns Query result with rows and rowCount
26
- */
27
- query<T = Record<string, unknown>>(sql: string, params?: unknown[]): Promise<{
28
- rows: T[];
29
- rowCount: number;
30
- }>;
31
- /**
32
- * Close all connections and clean up resources.
33
- */
34
- end(): Promise<void>;
35
- /**
36
- * Execute a function while holding a PostgreSQL advisory lock.
37
- * Adapters that don't support locking should just execute fn() directly.
38
- *
39
- * @param lockId - Integer lock ID (use hashToInt32 to convert string keys)
40
- * @param fn - Function to execute while holding the lock
41
- * @returns Result of the function
42
- */
43
- withAdvisoryLock<T>(lockId: number, fn: () => Promise<T>): Promise<T>;
44
- /**
45
- * Returns a pg-compatible client for use with libraries that expect a pg.Client interface.
46
- * Used by pg-node-migrations to run database migrations.
47
- */
48
- toPgClient(): PgCompatibleClient;
49
- }
50
-
51
- export type { DatabaseAdapter as D, PgCompatibleClient as P };
package/dist/pg.cjs DELETED
@@ -1,87 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/pg.ts
31
- var pg_exports = {};
32
- __export(pg_exports, {
33
- PgAdapter: () => PgAdapter
34
- });
35
- module.exports = __toCommonJS(pg_exports);
36
-
37
- // src/database/pg-adapter.ts
38
- var import_pg = __toESM(require("pg"), 1);
39
- var PgAdapter = class {
40
- pool;
41
- constructor(config) {
42
- this.pool = new import_pg.default.Pool(config);
43
- }
44
- async query(sql, params) {
45
- const result = await this.pool.query(sql, params);
46
- return {
47
- rows: result.rows,
48
- rowCount: result.rowCount ?? 0
49
- };
50
- }
51
- async end() {
52
- await this.pool.end();
53
- }
54
- /**
55
- * Execute a function while holding a PostgreSQL advisory lock.
56
- * Uses a dedicated connection to ensure lock is held for the duration.
57
- */
58
- async withAdvisoryLock(lockId, fn) {
59
- const client = await this.pool.connect();
60
- try {
61
- await client.query("SELECT pg_advisory_lock($1)", [lockId]);
62
- return await fn();
63
- } finally {
64
- try {
65
- await client.query("SELECT pg_advisory_unlock($1)", [lockId]);
66
- } finally {
67
- client.release();
68
- }
69
- }
70
- }
71
- /**
72
- * Returns a pg-compatible client for use with libraries that expect pg.Client.
73
- * Used by pg-node-migrations to run database migrations.
74
- */
75
- toPgClient() {
76
- return {
77
- query: async (sql) => {
78
- const result = typeof sql === "string" ? await this.pool.query(sql) : await this.pool.query(sql.text, sql.values);
79
- return { rows: result.rows, rowCount: result.rowCount ?? 0 };
80
- }
81
- };
82
- }
83
- };
84
- // Annotate the CommonJS export names for ESM import in node:
85
- 0 && (module.exports = {
86
- PgAdapter
87
- });
package/dist/pg.d.cts DELETED
@@ -1,28 +0,0 @@
1
- import { PoolConfig } from 'pg';
2
- import { D as DatabaseAdapter, P as PgCompatibleClient } from './adapter-BtXT5w9r.cjs';
3
-
4
- /**
5
- * Database adapter implementation using node-postgres (pg).
6
- * This is the default adapter for Node.js environments.
7
- */
8
- declare class PgAdapter implements DatabaseAdapter {
9
- private pool;
10
- constructor(config: PoolConfig);
11
- query<T = Record<string, unknown>>(sql: string, params?: unknown[]): Promise<{
12
- rows: T[];
13
- rowCount: number;
14
- }>;
15
- end(): Promise<void>;
16
- /**
17
- * Execute a function while holding a PostgreSQL advisory lock.
18
- * Uses a dedicated connection to ensure lock is held for the duration.
19
- */
20
- withAdvisoryLock<T>(lockId: number, fn: () => Promise<T>): Promise<T>;
21
- /**
22
- * Returns a pg-compatible client for use with libraries that expect pg.Client.
23
- * Used by pg-node-migrations to run database migrations.
24
- */
25
- toPgClient(): PgCompatibleClient;
26
- }
27
-
28
- export { PgAdapter };
package/dist/pg.d.ts DELETED
@@ -1,28 +0,0 @@
1
- import { PoolConfig } from 'pg';
2
- import { D as DatabaseAdapter, P as PgCompatibleClient } from './adapter-BtXT5w9r.js';
3
-
4
- /**
5
- * Database adapter implementation using node-postgres (pg).
6
- * This is the default adapter for Node.js environments.
7
- */
8
- declare class PgAdapter implements DatabaseAdapter {
9
- private pool;
10
- constructor(config: PoolConfig);
11
- query<T = Record<string, unknown>>(sql: string, params?: unknown[]): Promise<{
12
- rows: T[];
13
- rowCount: number;
14
- }>;
15
- end(): Promise<void>;
16
- /**
17
- * Execute a function while holding a PostgreSQL advisory lock.
18
- * Uses a dedicated connection to ensure lock is held for the duration.
19
- */
20
- withAdvisoryLock<T>(lockId: number, fn: () => Promise<T>): Promise<T>;
21
- /**
22
- * Returns a pg-compatible client for use with libraries that expect pg.Client.
23
- * Used by pg-node-migrations to run database migrations.
24
- */
25
- toPgClient(): PgCompatibleClient;
26
- }
27
-
28
- export { PgAdapter };
package/dist/pg.js DELETED
@@ -1,50 +0,0 @@
1
- // src/database/pg-adapter.ts
2
- import pg from "pg";
3
- var PgAdapter = class {
4
- pool;
5
- constructor(config) {
6
- this.pool = new pg.Pool(config);
7
- }
8
- async query(sql, params) {
9
- const result = await this.pool.query(sql, params);
10
- return {
11
- rows: result.rows,
12
- rowCount: result.rowCount ?? 0
13
- };
14
- }
15
- async end() {
16
- await this.pool.end();
17
- }
18
- /**
19
- * Execute a function while holding a PostgreSQL advisory lock.
20
- * Uses a dedicated connection to ensure lock is held for the duration.
21
- */
22
- async withAdvisoryLock(lockId, fn) {
23
- const client = await this.pool.connect();
24
- try {
25
- await client.query("SELECT pg_advisory_lock($1)", [lockId]);
26
- return await fn();
27
- } finally {
28
- try {
29
- await client.query("SELECT pg_advisory_unlock($1)", [lockId]);
30
- } finally {
31
- client.release();
32
- }
33
- }
34
- }
35
- /**
36
- * Returns a pg-compatible client for use with libraries that expect pg.Client.
37
- * Used by pg-node-migrations to run database migrations.
38
- */
39
- toPgClient() {
40
- return {
41
- query: async (sql) => {
42
- const result = typeof sql === "string" ? await this.pool.query(sql) : await this.pool.query(sql.text, sql.values);
43
- return { rows: result.rows, rowCount: result.rowCount ?? 0 };
44
- }
45
- };
46
- }
47
- };
48
- export {
49
- PgAdapter
50
- };
@@ -1,90 +0,0 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
-
30
- // src/postgres-js.ts
31
- var postgres_js_exports = {};
32
- __export(postgres_js_exports, {
33
- PostgresJsAdapter: () => PostgresJsAdapter
34
- });
35
- module.exports = __toCommonJS(postgres_js_exports);
36
-
37
- // src/database/postgres-js-adapter.ts
38
- var import_postgres = __toESM(require("postgres"), 1);
39
- var PostgresJsAdapter = class {
40
- sql;
41
- constructor(config) {
42
- this.sql = (0, import_postgres.default)(config.connectionString, {
43
- max: config.max ?? 10,
44
- prepare: false
45
- // Required for Supabase connection pooling
46
- });
47
- }
48
- async query(sqlQuery, params) {
49
- const result = await this.sql.unsafe(
50
- sqlQuery,
51
- params
52
- );
53
- return {
54
- rows: [...result],
55
- rowCount: result.count ?? result.length
56
- };
57
- }
58
- async end() {
59
- await this.sql.end();
60
- }
61
- /**
62
- * Execute a function while holding a PostgreSQL advisory lock.
63
- * Uses a transaction to ensure lock is held for the duration.
64
- */
65
- async withAdvisoryLock(lockId, fn) {
66
- const result = await this.sql.begin(async (tx) => {
67
- await tx`SELECT pg_advisory_xact_lock(${lockId})`;
68
- return await fn();
69
- });
70
- return result;
71
- }
72
- /**
73
- * Returns a pg-compatible client for use with libraries that expect pg.Client.
74
- * Used by pg-node-migrations to run database migrations.
75
- */
76
- toPgClient() {
77
- return {
78
- query: async (sql) => {
79
- const text = typeof sql === "string" ? sql : sql.text;
80
- const values = typeof sql === "string" ? void 0 : sql.values;
81
- const rows = await this.sql.unsafe(text, values);
82
- return { rows: [...rows], rowCount: rows.length };
83
- }
84
- };
85
- }
86
- };
87
- // Annotate the CommonJS export names for ESM import in node:
88
- 0 && (module.exports = {
89
- PostgresJsAdapter
90
- });
@@ -1,31 +0,0 @@
1
- import { D as DatabaseAdapter, P as PgCompatibleClient } from './adapter-BtXT5w9r.cjs';
2
-
3
- interface PostgresJsConfig {
4
- connectionString: string;
5
- max?: number;
6
- }
7
- /**
8
- * Database adapter implementation using postgres.js.
9
- * Works in Node.js, Deno, Bun, and Cloudflare Workers.
10
- */
11
- declare class PostgresJsAdapter implements DatabaseAdapter {
12
- private sql;
13
- constructor(config: PostgresJsConfig);
14
- query<T = Record<string, unknown>>(sqlQuery: string, params?: unknown[]): Promise<{
15
- rows: T[];
16
- rowCount: number;
17
- }>;
18
- end(): Promise<void>;
19
- /**
20
- * Execute a function while holding a PostgreSQL advisory lock.
21
- * Uses a transaction to ensure lock is held for the duration.
22
- */
23
- withAdvisoryLock<T>(lockId: number, fn: () => Promise<T>): Promise<T>;
24
- /**
25
- * Returns a pg-compatible client for use with libraries that expect pg.Client.
26
- * Used by pg-node-migrations to run database migrations.
27
- */
28
- toPgClient(): PgCompatibleClient;
29
- }
30
-
31
- export { PostgresJsAdapter };
@@ -1,31 +0,0 @@
1
- import { D as DatabaseAdapter, P as PgCompatibleClient } from './adapter-BtXT5w9r.js';
2
-
3
- interface PostgresJsConfig {
4
- connectionString: string;
5
- max?: number;
6
- }
7
- /**
8
- * Database adapter implementation using postgres.js.
9
- * Works in Node.js, Deno, Bun, and Cloudflare Workers.
10
- */
11
- declare class PostgresJsAdapter implements DatabaseAdapter {
12
- private sql;
13
- constructor(config: PostgresJsConfig);
14
- query<T = Record<string, unknown>>(sqlQuery: string, params?: unknown[]): Promise<{
15
- rows: T[];
16
- rowCount: number;
17
- }>;
18
- end(): Promise<void>;
19
- /**
20
- * Execute a function while holding a PostgreSQL advisory lock.
21
- * Uses a transaction to ensure lock is held for the duration.
22
- */
23
- withAdvisoryLock<T>(lockId: number, fn: () => Promise<T>): Promise<T>;
24
- /**
25
- * Returns a pg-compatible client for use with libraries that expect pg.Client.
26
- * Used by pg-node-migrations to run database migrations.
27
- */
28
- toPgClient(): PgCompatibleClient;
29
- }
30
-
31
- export { PostgresJsAdapter };