@vrplatform/kysely 1.3.41-stage.4319 → 1.3.45-4440

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,69 @@
1
+ import type { Kysely } from 'kysely';
2
+ import { sql } from 'kysely';
3
+
4
+ export async function up(db: Kysely<any>): Promise<void> {
5
+ await sql`create schema if not exists health;`.execute(db);
6
+
7
+ await sql`
8
+ create table health.tenant_issue_snapshot (
9
+ tenant_id uuid primary key references public.tenant(id) on delete cascade,
10
+ version bigint not null default 0,
11
+ content_hash text,
12
+ dirty_at timestamptz not null default now(),
13
+ computed_at timestamptz,
14
+ created_at timestamptz not null default now(),
15
+ updated_at timestamptz not null default now(),
16
+ constraint tenant_issue_snapshot_version_check check (version >= 0)
17
+ );
18
+ `.execute(db);
19
+
20
+ await sql`
21
+ create index tenant_issue_snapshot_dirty_idx
22
+ on health.tenant_issue_snapshot (dirty_at, computed_at);
23
+ `.execute(db);
24
+
25
+ await sql`
26
+ create table health.tenant_issue (
27
+ id uuid primary key default gen_random_uuid(),
28
+ tenant_id uuid not null references public.tenant(id) on delete cascade,
29
+ code text not null,
30
+ status text not null,
31
+ version bigint not null,
32
+ snapshot_version bigint not null,
33
+ severity text not null,
34
+ category text not null,
35
+ message text not null,
36
+ affected integer not null,
37
+ context jsonb not null,
38
+ content_hash text not null,
39
+ first_seen_at timestamptz not null,
40
+ opened_at timestamptz not null,
41
+ last_changed_at timestamptz not null,
42
+ resolved_at timestamptz,
43
+ created_at timestamptz not null default now(),
44
+ updated_at timestamptz not null default now(),
45
+ constraint tenant_issue_tenant_code_key unique (tenant_id, code),
46
+ constraint tenant_issue_status_check check (status in ('open', 'resolved')),
47
+ constraint tenant_issue_version_check check (version > 0),
48
+ constraint tenant_issue_snapshot_version_check check (snapshot_version > 0),
49
+ constraint tenant_issue_severity_check check (severity in ('error', 'warning')),
50
+ constraint tenant_issue_category_check
51
+ check (category in ('criticalToUser', 'criticalToSystem')),
52
+ constraint tenant_issue_affected_check
53
+ check (
54
+ (status = 'open' and affected > 0)
55
+ or (status = 'resolved' and affected = 0)
56
+ )
57
+ );
58
+ `.execute(db);
59
+
60
+ await sql`
61
+ create index tenant_issue_open_idx
62
+ on health.tenant_issue (tenant_id, code)
63
+ where status = 'open';
64
+ `.execute(db);
65
+ }
66
+
67
+ export async function down(db: Kysely<any>): Promise<void> {
68
+ await sql`drop schema if exists health cascade;`.execute(db);
69
+ }
@@ -0,0 +1,135 @@
1
+ import type { Kysely } from 'kysely';
2
+ import { sql } from 'kysely';
3
+
4
+ export async function up(db: Kysely<any>): Promise<void> {
5
+ await sql`create schema if not exists webhook;`.execute(db);
6
+
7
+ await sql`
8
+ create table webhook.subscription (
9
+ id uuid primary key default gen_random_uuid(),
10
+ owner_tenant_id uuid not null,
11
+ scope text not null,
12
+ url text not null,
13
+ status text not null default 'pending',
14
+ api_version text not null,
15
+ event_types text[] not null,
16
+ description text,
17
+ verified_at timestamptz,
18
+ disabled_at timestamptz,
19
+ created_at timestamptz not null default now(),
20
+ updated_at timestamptz not null default now(),
21
+ constraint webhook_subscription_scope_check
22
+ check (scope in ('self', 'managedTeams')),
23
+ constraint webhook_subscription_status_check
24
+ check (status in ('pending', 'active', 'disabled')),
25
+ constraint webhook_subscription_event_types_check
26
+ check (cardinality(event_types) > 0)
27
+ );
28
+ `.execute(db);
29
+
30
+ await sql`
31
+ create index webhook_subscription_owner_idx
32
+ on webhook.subscription (owner_tenant_id, created_at desc);
33
+ `.execute(db);
34
+
35
+ await sql`
36
+ create table webhook.subscription_secret (
37
+ id uuid primary key default gen_random_uuid(),
38
+ subscription_id uuid not null
39
+ references webhook.subscription(id) on delete cascade,
40
+ encrypted_secret text not null,
41
+ encryption_key_id text not null,
42
+ active_from timestamptz not null default now(),
43
+ expires_at timestamptz,
44
+ revoked_at timestamptz,
45
+ created_at timestamptz not null default now()
46
+ );
47
+ `.execute(db);
48
+
49
+ await sql`
50
+ create index webhook_subscription_secret_active_idx
51
+ on webhook.subscription_secret (subscription_id, active_from desc);
52
+ `.execute(db);
53
+
54
+ await sql`
55
+ create table webhook.delivery (
56
+ id uuid primary key default gen_random_uuid(),
57
+ source_event_key text not null,
58
+ subscription_id uuid not null
59
+ references webhook.subscription(id) on delete cascade,
60
+ source_tenant_id uuid,
61
+ event_type text not null,
62
+ api_version text not null,
63
+ resource_type text,
64
+ resource_id text,
65
+ body text not null,
66
+ status text not null default 'queued',
67
+ attempt_count integer not null default 0,
68
+ next_attempt_at timestamptz not null default now(),
69
+ claimed_at timestamptz,
70
+ claimed_by text,
71
+ last_error_code text,
72
+ delivered_at timestamptz,
73
+ failed_at timestamptz,
74
+ created_at timestamptz not null default now(),
75
+ updated_at timestamptz not null default now(),
76
+ constraint webhook_delivery_subscription_event_key
77
+ unique (subscription_id, source_event_key),
78
+ constraint webhook_delivery_status_check
79
+ check (
80
+ status in (
81
+ 'queued',
82
+ 'delivering',
83
+ 'retrying',
84
+ 'succeeded',
85
+ 'failed',
86
+ 'canceled'
87
+ )
88
+ ),
89
+ constraint webhook_delivery_attempt_count_check check (attempt_count >= 0)
90
+ );
91
+ `.execute(db);
92
+
93
+ await sql`
94
+ create index webhook_delivery_recovery_idx
95
+ on webhook.delivery (status, next_attempt_at);
96
+ `.execute(db);
97
+ await sql`
98
+ create index webhook_delivery_subscription_created_idx
99
+ on webhook.delivery (subscription_id, created_at desc);
100
+ `.execute(db);
101
+ await sql`
102
+ create index webhook_delivery_source_tenant_created_idx
103
+ on webhook.delivery (source_tenant_id, created_at desc);
104
+ `.execute(db);
105
+
106
+ await sql`
107
+ create table webhook.delivery_attempt (
108
+ id uuid primary key default gen_random_uuid(),
109
+ delivery_id uuid not null references webhook.delivery(id) on delete cascade,
110
+ subscription_secret_id uuid not null
111
+ references webhook.subscription_secret(id) on delete restrict,
112
+ attempt_number integer not null,
113
+ started_at timestamptz not null,
114
+ finished_at timestamptz not null,
115
+ duration_ms integer not null,
116
+ response_status integer,
117
+ error_class text,
118
+ response_excerpt text,
119
+ created_at timestamptz not null default now(),
120
+ constraint webhook_delivery_attempt_number_key
121
+ unique (delivery_id, attempt_number),
122
+ constraint webhook_delivery_attempt_number_check check (attempt_number > 0),
123
+ constraint webhook_delivery_attempt_duration_check check (duration_ms >= 0)
124
+ );
125
+ `.execute(db);
126
+
127
+ await sql`
128
+ create index webhook_delivery_attempt_delivery_created_idx
129
+ on webhook.delivery_attempt (delivery_id, created_at);
130
+ `.execute(db);
131
+ }
132
+
133
+ export async function down(db: Kysely<any>): Promise<void> {
134
+ await sql`drop schema if exists webhook cascade;`.execute(db);
135
+ }
@@ -0,0 +1,42 @@
1
+ import type { Kysely } from 'kysely';
2
+ import { sql } from 'kysely';
3
+
4
+ export async function up(db: Kysely<any>): Promise<void> {
5
+ await sql`
6
+ do $$
7
+ begin
8
+ if exists (select 1 from pg_roles where rolname = 'application') then
9
+ grant usage on schema health, webhook to application;
10
+ grant select, insert, update, delete
11
+ on all tables in schema health, webhook to application;
12
+ grant usage, select, update
13
+ on all sequences in schema health, webhook to application;
14
+ alter default privileges in schema health, webhook
15
+ grant select, insert, update, delete on tables to application;
16
+ alter default privileges in schema health, webhook
17
+ grant usage, select, update on sequences to application;
18
+ end if;
19
+ end
20
+ $$;
21
+ `.execute(db);
22
+ }
23
+
24
+ export async function down(db: Kysely<any>): Promise<void> {
25
+ await sql`
26
+ do $$
27
+ begin
28
+ if exists (select 1 from pg_roles where rolname = 'application') then
29
+ alter default privileges in schema health, webhook
30
+ revoke select, insert, update, delete on tables from application;
31
+ alter default privileges in schema health, webhook
32
+ revoke usage, select, update on sequences from application;
33
+ revoke select, insert, update, delete
34
+ on all tables in schema health, webhook from application;
35
+ revoke usage, select, update
36
+ on all sequences in schema health, webhook from application;
37
+ revoke usage on schema health, webhook from application;
38
+ end if;
39
+ end
40
+ $$;
41
+ `.execute(db);
42
+ }
@@ -0,0 +1,13 @@
1
+ import type { Kysely } from 'kysely';
2
+
3
+ // CUS-2503: retained as a no-op because this migration name reached staging.
4
+ // Feature evaluation belongs in /me and must not manufacture team enablements
5
+ // from user approvals. Keeping the file lets environments with an existing
6
+ // migration-ledger entry continue to migrate safely.
7
+ export async function up(db: Kysely<unknown>): Promise<void> {
8
+ void db;
9
+ }
10
+
11
+ export async function down(db: Kysely<unknown>): Promise<void> {
12
+ void db;
13
+ }
@@ -0,0 +1,296 @@
1
+ import type { Kysely } from 'kysely';
2
+ import { sql } from 'kysely';
3
+
4
+ export async function up(db: Kysely<any>): Promise<void> {
5
+ await sql`
6
+ create type health.tenant_issue_code as enum (
7
+ 'teamInactive',
8
+ 'partnerInactive',
9
+ 'partnerBillingInactive',
10
+ 'brokenConnections',
11
+ 'outdatedConnections',
12
+ 'pmsMissingAccountingStart',
13
+ 'operatingBankAccountsWithoutOpex',
14
+ 'duplicatedPayments',
15
+ 'unbalancedJournalEntries',
16
+ 'unbalancedTransactionJournalEntries',
17
+ 'closedPeriodUnattachedJournalEntries',
18
+ 'missingOwnershipOrDeactivation',
19
+ 'unpaidReservations',
20
+ 'cancelledReservationPaidWithoutAdjustment',
21
+ 'publishedStatementUnpaid',
22
+ 'endedOwnershipNonZeroBalance',
23
+ 'reservationPaymentProjectionMismatch',
24
+ 'listingOwnershipPeriodNotFoundOnActiveJournals',
25
+ 'reservationGuestTotalsMismatch',
26
+ 'reservationCurrencyMismatch'
27
+ )
28
+ `.execute(db);
29
+ await sql`
30
+ create type health.tenant_issue_status as enum ('open', 'resolved')
31
+ `.execute(db);
32
+ await sql`
33
+ create type health.tenant_issue_severity as enum ('error', 'warning')
34
+ `.execute(db);
35
+ await sql`
36
+ create type health.tenant_issue_category as enum (
37
+ 'criticalToUser',
38
+ 'criticalToSystem'
39
+ )
40
+ `.execute(db);
41
+ await sql`
42
+ create type webhook.subscription_scope as enum ('self', 'managedTeams')
43
+ `.execute(db);
44
+ await sql`
45
+ create type webhook.subscription_status as enum (
46
+ 'pending',
47
+ 'active',
48
+ 'disabled'
49
+ )
50
+ `.execute(db);
51
+ await sql`
52
+ create type webhook.subscription_event_type as enum (
53
+ 'team.issues.changed'
54
+ )
55
+ `.execute(db);
56
+ await sql`
57
+ create type webhook.event_type as enum (
58
+ 'webhook.test',
59
+ 'team.issues.changed'
60
+ )
61
+ `.execute(db);
62
+ await sql`
63
+ create type webhook.api_version as enum ('2026-07-01')
64
+ `.execute(db);
65
+ await sql`
66
+ create type webhook.resource_type as enum ('webhook', 'team')
67
+ `.execute(db);
68
+ await sql`
69
+ create type webhook.delivery_status as enum (
70
+ 'queued',
71
+ 'delivering',
72
+ 'retrying',
73
+ 'succeeded',
74
+ 'failed',
75
+ 'canceled'
76
+ )
77
+ `.execute(db);
78
+
79
+ await sql`drop index health.tenant_issue_open_idx`.execute(db);
80
+ await sql`
81
+ alter table health.tenant_issue
82
+ drop constraint tenant_issue_status_check,
83
+ drop constraint tenant_issue_severity_check,
84
+ drop constraint tenant_issue_category_check,
85
+ drop constraint tenant_issue_affected_check
86
+ `.execute(db);
87
+ await sql`
88
+ alter table health.tenant_issue
89
+ alter column code type health.tenant_issue_code
90
+ using code::health.tenant_issue_code,
91
+ alter column status type health.tenant_issue_status
92
+ using status::health.tenant_issue_status,
93
+ alter column severity type health.tenant_issue_severity
94
+ using severity::health.tenant_issue_severity,
95
+ alter column category type health.tenant_issue_category
96
+ using category::health.tenant_issue_category
97
+ `.execute(db);
98
+ await sql`
99
+ alter table health.tenant_issue
100
+ add constraint tenant_issue_affected_check
101
+ check (
102
+ (status = 'open' and affected > 0)
103
+ or (status = 'resolved' and affected = 0)
104
+ )
105
+ `.execute(db);
106
+ await sql`
107
+ create index tenant_issue_open_idx
108
+ on health.tenant_issue (tenant_id, code)
109
+ where status = 'open'
110
+ `.execute(db);
111
+
112
+ await sql`drop index webhook.webhook_delivery_recovery_idx`.execute(db);
113
+ await sql`
114
+ alter table webhook.subscription
115
+ drop constraint webhook_subscription_scope_check,
116
+ drop constraint webhook_subscription_status_check,
117
+ drop constraint webhook_subscription_event_types_check,
118
+ alter column status drop default
119
+ `.execute(db);
120
+ await sql`
121
+ alter table webhook.delivery
122
+ drop constraint webhook_delivery_status_check,
123
+ alter column status drop default
124
+ `.execute(db);
125
+ await sql`
126
+ alter table webhook.subscription
127
+ alter column scope type webhook.subscription_scope
128
+ using scope::webhook.subscription_scope,
129
+ alter column status type webhook.subscription_status
130
+ using status::webhook.subscription_status,
131
+ alter column api_version type webhook.api_version
132
+ using api_version::webhook.api_version,
133
+ alter column event_types type webhook.subscription_event_type[]
134
+ using event_types::text::webhook.subscription_event_type[],
135
+ alter column status set default 'pending'
136
+ `.execute(db);
137
+ await sql`
138
+ alter table webhook.subscription
139
+ add constraint webhook_subscription_event_types_check
140
+ check (cardinality(event_types) > 0)
141
+ `.execute(db);
142
+ await sql`
143
+ alter table webhook.delivery
144
+ alter column event_type type webhook.event_type
145
+ using event_type::webhook.event_type,
146
+ alter column api_version type webhook.api_version
147
+ using api_version::webhook.api_version,
148
+ alter column resource_type type webhook.resource_type
149
+ using resource_type::webhook.resource_type,
150
+ alter column status type webhook.delivery_status
151
+ using status::webhook.delivery_status,
152
+ alter column status set default 'queued'
153
+ `.execute(db);
154
+ await sql`
155
+ create index webhook_delivery_recovery_idx
156
+ on webhook.delivery (status, next_attempt_at)
157
+ `.execute(db);
158
+
159
+ await sql`
160
+ do $$
161
+ begin
162
+ if exists (select 1 from pg_roles where rolname = 'application') then
163
+ grant usage on type
164
+ health.tenant_issue_code,
165
+ health.tenant_issue_status,
166
+ health.tenant_issue_severity,
167
+ health.tenant_issue_category,
168
+ webhook.subscription_scope,
169
+ webhook.subscription_status,
170
+ webhook.subscription_event_type,
171
+ webhook.event_type,
172
+ webhook.api_version,
173
+ webhook.resource_type,
174
+ webhook.delivery_status
175
+ to application;
176
+ alter default privileges in schema health, webhook
177
+ grant usage on types to application;
178
+ end if;
179
+ end
180
+ $$;
181
+ `.execute(db);
182
+ }
183
+
184
+ export async function down(db: Kysely<any>): Promise<void> {
185
+ await sql`
186
+ do $$
187
+ begin
188
+ if exists (select 1 from pg_roles where rolname = 'application') then
189
+ alter default privileges in schema health, webhook
190
+ revoke usage on types from application;
191
+ end if;
192
+ end
193
+ $$;
194
+ `.execute(db);
195
+
196
+ await sql`drop index health.tenant_issue_open_idx`.execute(db);
197
+ await sql`
198
+ alter table health.tenant_issue
199
+ drop constraint tenant_issue_affected_check
200
+ `.execute(db);
201
+ await sql`
202
+ alter table health.tenant_issue
203
+ alter column code type text using code::text,
204
+ alter column status type text using status::text,
205
+ alter column severity type text using severity::text,
206
+ alter column category type text using category::text
207
+ `.execute(db);
208
+ await sql`
209
+ alter table health.tenant_issue
210
+ add constraint tenant_issue_status_check
211
+ check (status in ('open', 'resolved')),
212
+ add constraint tenant_issue_severity_check
213
+ check (severity in ('error', 'warning')),
214
+ add constraint tenant_issue_category_check
215
+ check (category in ('criticalToUser', 'criticalToSystem')),
216
+ add constraint tenant_issue_affected_check
217
+ check (
218
+ (status = 'open' and affected > 0)
219
+ or (status = 'resolved' and affected = 0)
220
+ )
221
+ `.execute(db);
222
+ await sql`
223
+ create index tenant_issue_open_idx
224
+ on health.tenant_issue (tenant_id, code)
225
+ where status = 'open'
226
+ `.execute(db);
227
+
228
+ await sql`drop index webhook.webhook_delivery_recovery_idx`.execute(db);
229
+ await sql`
230
+ alter table webhook.subscription
231
+ drop constraint webhook_subscription_event_types_check,
232
+ alter column status drop default
233
+ `.execute(db);
234
+ await sql`
235
+ alter table webhook.delivery
236
+ alter column status drop default
237
+ `.execute(db);
238
+ await sql`
239
+ alter table webhook.subscription
240
+ alter column scope type text using scope::text,
241
+ alter column status type text using status::text,
242
+ alter column api_version type text using api_version::text,
243
+ alter column event_types type text[] using event_types::text::text[],
244
+ alter column status set default 'pending'
245
+ `.execute(db);
246
+ await sql`
247
+ alter table webhook.subscription
248
+ add constraint webhook_subscription_scope_check
249
+ check (scope in ('self', 'managedTeams')),
250
+ add constraint webhook_subscription_status_check
251
+ check (status in ('pending', 'active', 'disabled')),
252
+ add constraint webhook_subscription_event_types_check
253
+ check (cardinality(event_types) > 0)
254
+ `.execute(db);
255
+ await sql`
256
+ alter table webhook.delivery
257
+ alter column event_type type text using event_type::text,
258
+ alter column api_version type text using api_version::text,
259
+ alter column resource_type type text using resource_type::text,
260
+ alter column status type text using status::text,
261
+ alter column status set default 'queued'
262
+ `.execute(db);
263
+ await sql`
264
+ alter table webhook.delivery
265
+ add constraint webhook_delivery_status_check
266
+ check (
267
+ status in (
268
+ 'queued',
269
+ 'delivering',
270
+ 'retrying',
271
+ 'succeeded',
272
+ 'failed',
273
+ 'canceled'
274
+ )
275
+ )
276
+ `.execute(db);
277
+ await sql`
278
+ create index webhook_delivery_recovery_idx
279
+ on webhook.delivery (status, next_attempt_at)
280
+ `.execute(db);
281
+
282
+ await sql`
283
+ drop type
284
+ webhook.delivery_status,
285
+ webhook.resource_type,
286
+ webhook.api_version,
287
+ webhook.event_type,
288
+ webhook.subscription_event_type,
289
+ webhook.subscription_status,
290
+ webhook.subscription_scope,
291
+ health.tenant_issue_category,
292
+ health.tenant_issue_severity,
293
+ health.tenant_issue_status,
294
+ health.tenant_issue_code
295
+ `.execute(db);
296
+ }
@@ -9,7 +9,7 @@ export * from './query';
9
9
  export * from './regional';
10
10
  export * from './transaction';
11
11
  import { parseDuration } from '@vrplatform/utils';
12
- const retryableMessages = ['CONNECTION_CLOSED'];
12
+ const retryableMessages = ['CONNECTION_CLOSED', 'CONNECT_TIMEOUT'];
13
13
  /**
14
14
  * @deprecated This method is deprecated in favor of the useAsyncKysely method.
15
15
  */
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"src/","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAE9C,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,QAAsB,MAAM,UAAU,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACtC,OAAO,EAAE,SAA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AAO,CAAC,CAAC;QACf,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;CACF","sourcesContent":["import { backOff } from 'exponential-backoff';\nimport type { Kysely as K, LogConfig } from 'kysely';\nimport { PostgresJSDialect } from 'kysely-postgres-js';\nimport postgres, { type Sql } from 'postgres';\nimport { KyselyError } from './error';\nimport { getKysely } from './plugins';\n\nexport type { Insertable, LogEvent } from 'kysely';\nexport * from './error';\nexport * from './isomorphic';\nexport * from './query';\nexport * from './regional';\nexport * from './transaction';\n\nimport { parseDuration } from '@vrplatform/utils';\nimport type { DB } from './v1.generated';\n\ntype KyselyOpts = {\n repositoryName?: string;\n log?: true | LogConfig;\n};\n\ntype PgOpt = {\n forceDisableSSL?: boolean;\n probe?: false;\n retries?: false | number;\n};\n\nconst retryableMessages = ['CONNECTION_CLOSED'];\n\nexport type Kysely = K<DB>;\nexport type Postgres = Sql;\n/**\n * @deprecated This method is deprecated in favor of the useAsyncKysely method.\n */\nexport function useKysely(postgres: Sql, options?: KyselyOpts) {\n console.debug('Creating Kysely instance');\n const kysely = getKysely(\n new PostgresJSDialect({\n postgres,\n onReserveConnection: async (connection) => {\n const executeQuery = connection.executeQuery.bind(connection);\n connection.executeQuery = (arg) =>\n backOff(async () => await executeQuery(arg), {\n numOfAttempts: 3,\n startingDelay: parseDuration('1s'),\n maxDelay: parseDuration('5s'),\n retry: (error) => {\n const message =\n error instanceof Error ? error.message : String(error);\n if (retryableMessages.some((m) => message?.includes(m))) {\n return true;\n }\n return false;\n },\n });\n },\n }),\n {\n repositoryName: options?.repositoryName,\n log: options?.log,\n }\n );\n kysely.destroy = async () => {\n console.debug('Closing Kysely connection');\n await Promise.race([\n postgres.end({ timeout: 5000 }).catch(() => undefined),\n new Promise((res) => setTimeout(res, 6000)),\n ]);\n const index = _connections.indexOf(postgres);\n if (index !== -1) _connections.splice(index, 1);\n };\n\n return kysely;\n}\n\nlet _connections: postgres.Sql[] = [];\nexport async function useAsyncPostgres(connectionString: string, opt?: PgOpt) {\n const delayMs = 2500;\n const retries =\n opt?.probe === false ? 1 : opt?.retries === false ? 0 : opt?.retries || 3;\n const ssl = opt?.forceDisableSSL !== true;\n const url = new URL(connectionString);\n\n console.debug(\n `Creating Postgres connection with ${retries} retries and ssl=${ssl} and port=${url.port}`\n );\n if (_connections.length > 0)\n console.warn(`We already have ${_connections.length} Postgres connections`);\n\n for (let attempt = 1; attempt <= retries; attempt++) {\n try {\n const sql = postgres(connectionString, {\n ssl: ssl ? 'require' : false,\n // connect_timeout: 10\n // idle_timeout: 10\n // enable prepare statements, pgbouncer on crunchy allows for prepared statements\n prepare: true,\n connect_timeout: 10,\n idle_timeout: 60,\n });\n\n if (opt?.probe !== false) {\n await sql`select 1`;\n }\n // statement_timeout/lock_timeout are role-level settings on the cluster\n // (ALTER ROLE application SET ...) so they cover every pooled\n // connection; per-session SETs here only ever applied to one of them\n\n _connections.push(sql);\n return sql;\n } catch (err) {\n if (attempt === retries) throw err;\n console.warn(\n `Postgres connection failed (attempt ${attempt}), retrying...`\n );\n console.error('error', err);\n await new Promise((res) => setTimeout(res, delayMs));\n }\n }\n throw new KyselyError('Unable to connect to Postgres');\n}\n\nexport async function useAsyncKysely(\n connectionString: string,\n opt?: PgOpt & KyselyOpts\n) {\n const sql = await useAsyncPostgres(connectionString, opt);\n return useKysely(sql, opt);\n}\n\n/**\n * @deprecated This method is deprecated in favor of the useAsyncKysely method.\n */\nexport function usePostgres(\n connectionString?: string,\n opt?: Pick<PgOpt, 'forceDisableSSL'>\n) {\n if (!connectionString) throw new KyselyError('No connection string');\n const ssl = opt?.forceDisableSSL !== true;\n const url = new URL(connectionString);\n console.debug(\n `Creating sync Postgres connection with ssl=${ssl} and port=${url.port}`\n );\n const pg = postgres(connectionString, {\n ssl: ssl ? 'require' : false,\n });\n _connections.push(pg);\n return pg;\n}\n\nexport async function forceCloseAllPostgres(timeoutMs = 5000) {\n await Promise.race([\n Promise.all(\n _connections.map((pg) =>\n pg.end({ timeout: timeoutMs }).catch(() => undefined)\n )\n ),\n new Promise((res) => setTimeout(res, timeoutMs + 1000)),\n ]);\n _connections = [];\n}\n\nexport class DatabaseError extends Error {\n constructor(message: string, cause: any) {\n super(message);\n this.cause = cause;\n }\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"src/","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAC;AAE9C,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,QAAsB,MAAM,UAAU,CAAC;AAC9C,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAGtC,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,SAAS,CAAC;AACxB,cAAc,YAAY,CAAC;AAC3B,cAAc,eAAe,CAAC;AAE9B,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAclD,MAAM,iBAAiB,GAAG,CAAC,mBAAmB,EAAE,iBAAiB,CAAC,CAAC;AAInE;;GAEG;AACH,MAAM,UAAU,SAAS,CAAC,QAAa,EAAE,OAAoB;IAC3D,OAAO,CAAC,KAAK,CAAC,0BAA0B,CAAC,CAAC;IAC1C,MAAM,MAAM,GAAG,SAAS,CACtB,IAAI,iBAAiB,CAAC;QACpB,QAAQ;QACR,mBAAmB,EAAE,KAAK,EAAE,UAAU,EAAE,EAAE;YACxC,MAAM,YAAY,GAAG,UAAU,CAAC,YAAY,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;YAC9D,UAAU,CAAC,YAAY,GAAG,CAAC,GAAG,EAAE,EAAE,CAChC,OAAO,CAAC,KAAK,IAAI,EAAE,CAAC,MAAM,YAAY,CAAC,GAAG,CAAC,EAAE;gBAC3C,aAAa,EAAE,CAAC;gBAChB,aAAa,EAAE,aAAa,CAAC,IAAI,CAAC;gBAClC,QAAQ,EA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O,CAAC,GAAG,CACT,YAAY,CAAC,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CACtB,EAAE,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,SAAS,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CACtD,CACF;QACD,IAAI,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,UAAU,CAAC,GAAG,EAAE,SAAS,GAAG,IAAI,CAAC,CAAC;KACxD,CAAC,CAAC;IACH,YAAY,GAAG,EAAE,CAAC;AACpB,CAAC;AAED,MAAM,OAAO,aAAc,SAAQ,KAAK;IACtC,YAAY,OAAe,EAAE,KAAU;QACrC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;CACF","sourcesContent":["import { backOff } from 'exponential-backoff';\nimport type { Kysely as K, LogConfig } from 'kysely';\nimport { PostgresJSDialect } from 'kysely-postgres-js';\nimport postgres, { type Sql } from 'postgres';\nimport { KyselyError } from './error';\nimport { getKysely } from './plugins';\n\nexport type { Insertable, LogEvent } from 'kysely';\nexport * from './error';\nexport * from './isomorphic';\nexport * from './query';\nexport * from './regional';\nexport * from './transaction';\n\nimport { parseDuration } from '@vrplatform/utils';\nimport type { DB } from './v1.generated';\n\ntype KyselyOpts = {\n repositoryName?: string;\n log?: true | LogConfig;\n};\n\ntype PgOpt = {\n forceDisableSSL?: boolean;\n probe?: false;\n retries?: false | number;\n};\n\nconst retryableMessages = ['CONNECTION_CLOSED', 'CONNECT_TIMEOUT'];\n\nexport type Kysely = K<DB>;\nexport type Postgres = Sql;\n/**\n * @deprecated This method is deprecated in favor of the useAsyncKysely method.\n */\nexport function useKysely(postgres: Sql, options?: KyselyOpts) {\n console.debug('Creating Kysely instance');\n const kysely = getKysely(\n new PostgresJSDialect({\n postgres,\n onReserveConnection: async (connection) => {\n const executeQuery = connection.executeQuery.bind(connection);\n connection.executeQuery = (arg) =>\n backOff(async () => await executeQuery(arg), {\n numOfAttempts: 3,\n startingDelay: parseDuration('1s'),\n maxDelay: parseDuration('5s'),\n retry: (error) => {\n const message =\n error instanceof Error ? error.message : String(error);\n if (retryableMessages.some((m) => message?.includes(m))) {\n return true;\n }\n return false;\n },\n });\n },\n }),\n {\n repositoryName: options?.repositoryName,\n log: options?.log,\n }\n );\n kysely.destroy = async () => {\n console.debug('Closing Kysely connection');\n await Promise.race([\n postgres.end({ timeout: 5000 }).catch(() => undefined),\n new Promise((res) => setTimeout(res, 6000)),\n ]);\n const index = _connections.indexOf(postgres);\n if (index !== -1) _connections.splice(index, 1);\n };\n\n return kysely;\n}\n\nlet _connections: postgres.Sql[] = [];\nexport async function useAsyncPostgres(connectionString: string, opt?: PgOpt) {\n const delayMs = 2500;\n const retries =\n opt?.probe === false ? 1 : opt?.retries === false ? 0 : opt?.retries || 3;\n const ssl = opt?.forceDisableSSL !== true;\n const url = new URL(connectionString);\n\n console.debug(\n `Creating Postgres connection with ${retries} retries and ssl=${ssl} and port=${url.port}`\n );\n if (_connections.length > 0)\n console.warn(`We already have ${_connections.length} Postgres connections`);\n\n for (let attempt = 1; attempt <= retries; attempt++) {\n try {\n const sql = postgres(connectionString, {\n ssl: ssl ? 'require' : false,\n // connect_timeout: 10\n // idle_timeout: 10\n // enable prepare statements, pgbouncer on crunchy allows for prepared statements\n prepare: true,\n connect_timeout: 10,\n idle_timeout: 60,\n });\n\n if (opt?.probe !== false) {\n await sql`select 1`;\n }\n // statement_timeout/lock_timeout are role-level settings on the cluster\n // (ALTER ROLE application SET ...) so they cover every pooled\n // connection; per-session SETs here only ever applied to one of them\n\n _connections.push(sql);\n return sql;\n } catch (err) {\n if (attempt === retries) throw err;\n console.warn(\n `Postgres connection failed (attempt ${attempt}), retrying...`\n );\n console.error('error', err);\n await new Promise((res) => setTimeout(res, delayMs));\n }\n }\n throw new KyselyError('Unable to connect to Postgres');\n}\n\nexport async function useAsyncKysely(\n connectionString: string,\n opt?: PgOpt & KyselyOpts\n) {\n const sql = await useAsyncPostgres(connectionString, opt);\n return useKysely(sql, opt);\n}\n\n/**\n * @deprecated This method is deprecated in favor of the useAsyncKysely method.\n */\nexport function usePostgres(\n connectionString?: string,\n opt?: Pick<PgOpt, 'forceDisableSSL'>\n) {\n if (!connectionString) throw new KyselyError('No connection string');\n const ssl = opt?.forceDisableSSL !== true;\n const url = new URL(connectionString);\n console.debug(\n `Creating sync Postgres connection with ssl=${ssl} and port=${url.port}`\n );\n const pg = postgres(connectionString, {\n ssl: ssl ? 'require' : false,\n });\n _connections.push(pg);\n return pg;\n}\n\nexport async function forceCloseAllPostgres(timeoutMs = 5000) {\n await Promise.race([\n Promise.all(\n _connections.map((pg) =>\n pg.end({ timeout: timeoutMs }).catch(() => undefined)\n )\n ),\n new Promise((res) => setTimeout(res, timeoutMs + 1000)),\n ]);\n _connections = [];\n}\n\nexport class DatabaseError extends Error {\n constructor(message: string, cause: any) {\n super(message);\n this.cause = cause;\n }\n}\n"]}