@sixb/pg 0.0.1
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/LICENSE +21 -0
- package/README.md +73 -0
- package/dist/agents/agent-storage.d.ts +20 -0
- package/dist/agents/agent-storage.d.ts.map +1 -0
- package/dist/agents/index.d.ts +6 -0
- package/dist/agents/index.d.ts.map +1 -0
- package/dist/agents/messages.d.ts +13 -0
- package/dist/agents/messages.d.ts.map +1 -0
- package/dist/agents/rows.d.ts +77 -0
- package/dist/agents/rows.d.ts.map +1 -0
- package/dist/agents/runs.d.ts +25 -0
- package/dist/agents/runs.d.ts.map +1 -0
- package/dist/agents/threads.d.ts +13 -0
- package/dist/agents/threads.d.ts.map +1 -0
- package/dist/auth-storage/access-tokens.d.ts +33 -0
- package/dist/auth-storage/access-tokens.d.ts.map +1 -0
- package/dist/auth-storage/auth-storage.d.ts +41 -0
- package/dist/auth-storage/auth-storage.d.ts.map +1 -0
- package/dist/auth-storage/group-memberships.d.ts +21 -0
- package/dist/auth-storage/group-memberships.d.ts.map +1 -0
- package/dist/auth-storage/identities.d.ts +17 -0
- package/dist/auth-storage/identities.d.ts.map +1 -0
- package/dist/auth-storage/index.d.ts +3 -0
- package/dist/auth-storage/index.d.ts.map +1 -0
- package/dist/auth-storage/invitations.d.ts +29 -0
- package/dist/auth-storage/invitations.d.ts.map +1 -0
- package/dist/auth-storage/magic-links.d.ts +28 -0
- package/dist/auth-storage/magic-links.d.ts.map +1 -0
- package/dist/auth-storage/oidc-attempts.d.ts +18 -0
- package/dist/auth-storage/oidc-attempts.d.ts.map +1 -0
- package/dist/auth-storage/rows.d.ts +138 -0
- package/dist/auth-storage/rows.d.ts.map +1 -0
- package/dist/auth-storage/service-account-group-memberships.d.ts +17 -0
- package/dist/auth-storage/service-account-group-memberships.d.ts.map +1 -0
- package/dist/auth-storage/service-accounts.d.ts +14 -0
- package/dist/auth-storage/service-accounts.d.ts.map +1 -0
- package/dist/auth-storage/sessions.d.ts +55 -0
- package/dist/auth-storage/sessions.d.ts.map +1 -0
- package/dist/auth-storage/shared.d.ts +159 -0
- package/dist/auth-storage/shared.d.ts.map +1 -0
- package/dist/auth-storage/users.d.ts +19 -0
- package/dist/auth-storage/users.d.ts.map +1 -0
- package/dist/index.d.ts +127 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11957 -0
- package/dist/index.js.map +55 -0
- package/dist/latest-run-query.d.ts +24 -0
- package/dist/latest-run-query.d.ts.map +1 -0
- package/dist/migrations.d.ts +20 -0
- package/dist/migrations.d.ts.map +1 -0
- package/dist/ontology-storage/commits.d.ts +11 -0
- package/dist/ontology-storage/commits.d.ts.map +1 -0
- package/dist/ontology-storage/index.d.ts +22 -0
- package/dist/ontology-storage/index.d.ts.map +1 -0
- package/dist/ontology-storage/materialization-session.d.ts +63 -0
- package/dist/ontology-storage/materialization-session.d.ts.map +1 -0
- package/dist/ontology-storage/materialization-state.d.ts +69 -0
- package/dist/ontology-storage/materialization-state.d.ts.map +1 -0
- package/dist/ontology-storage/materialization-writer.d.ts +15 -0
- package/dist/ontology-storage/materialization-writer.d.ts.map +1 -0
- package/dist/ontology-storage/materializations.d.ts +33 -0
- package/dist/ontology-storage/materializations.d.ts.map +1 -0
- package/dist/ontology-storage/outbox.d.ts +12 -0
- package/dist/ontology-storage/outbox.d.ts.map +1 -0
- package/dist/ontology-storage/shared.d.ts +95 -0
- package/dist/ontology-storage/shared.d.ts.map +1 -0
- package/dist/ontology-storage/sources.d.ts +25 -0
- package/dist/ontology-storage/sources.d.ts.map +1 -0
- package/dist/pg-action-run-storage.d.ts +21 -0
- package/dist/pg-action-run-storage.d.ts.map +1 -0
- package/dist/pg-client.d.ts +58 -0
- package/dist/pg-client.d.ts.map +1 -0
- package/dist/pg-object-query-compiler.d.ts +55 -0
- package/dist/pg-object-query-compiler.d.ts.map +1 -0
- package/dist/pg-object-storage.d.ts +90 -0
- package/dist/pg-object-storage.d.ts.map +1 -0
- package/dist/pg-pipeline-run-storage.d.ts +18 -0
- package/dist/pg-pipeline-run-storage.d.ts.map +1 -0
- package/dist/pg-projection-run-storage.d.ts +19 -0
- package/dist/pg-projection-run-storage.d.ts.map +1 -0
- package/dist/pg-rules-storage.d.ts +25 -0
- package/dist/pg-rules-storage.d.ts.map +1 -0
- package/dist/pg-sync-run-storage.d.ts +15 -0
- package/dist/pg-sync-run-storage.d.ts.map +1 -0
- package/dist/pg-timeseries-storage.d.ts +32 -0
- package/dist/pg-timeseries-storage.d.ts.map +1 -0
- package/dist/pg-webhook-delivery-storage.d.ts +17 -0
- package/dist/pg-webhook-delivery-storage.d.ts.map +1 -0
- package/dist/pg-webhook-run-storage.d.ts +14 -0
- package/dist/pg-webhook-run-storage.d.ts.map +1 -0
- package/dist/pg-workflow-intervention-storage.d.ts +16 -0
- package/dist/pg-workflow-intervention-storage.d.ts.map +1 -0
- package/dist/pg-workflow-run-storage.d.ts +49 -0
- package/dist/pg-workflow-run-storage.d.ts.map +1 -0
- package/dist/run-list-query.d.ts +30 -0
- package/dist/run-list-query.d.ts.map +1 -0
- package/dist/storage-errors.d.ts +16 -0
- package/dist/storage-errors.d.ts.map +1 -0
- package/dist/transactions.d.ts +10 -0
- package/dist/transactions.d.ts.map +1 -0
- package/dist/tsconfig.json +5 -0
- package/package.json +60 -0
- package/src/agents/agent-storage.ts +26 -0
- package/src/agents/index.ts +5 -0
- package/src/agents/messages.ts +144 -0
- package/src/agents/rows.ts +227 -0
- package/src/agents/runs.ts +330 -0
- package/src/agents/threads.ts +116 -0
- package/src/auth-storage/access-tokens.ts +254 -0
- package/src/auth-storage/auth-storage.ts +798 -0
- package/src/auth-storage/group-memberships.ts +47 -0
- package/src/auth-storage/identities.ts +85 -0
- package/src/auth-storage/index.ts +2 -0
- package/src/auth-storage/invitations.ts +280 -0
- package/src/auth-storage/magic-links.ts +140 -0
- package/src/auth-storage/oidc-attempts.ts +93 -0
- package/src/auth-storage/rows.ts +352 -0
- package/src/auth-storage/service-account-group-memberships.ts +150 -0
- package/src/auth-storage/service-accounts.ts +154 -0
- package/src/auth-storage/sessions.ts +193 -0
- package/src/auth-storage/shared.ts +799 -0
- package/src/auth-storage/users.ts +196 -0
- package/src/index.ts +367 -0
- package/src/latest-run-query.ts +42 -0
- package/src/migrations/001-initial-schema.sql +1195 -0
- package/src/migrations.ts +280 -0
- package/src/ontology-storage/commits.ts +115 -0
- package/src/ontology-storage/index.ts +35 -0
- package/src/ontology-storage/materialization-session.ts +426 -0
- package/src/ontology-storage/materialization-state.ts +1109 -0
- package/src/ontology-storage/materialization-writer.ts +509 -0
- package/src/ontology-storage/materializations.ts +939 -0
- package/src/ontology-storage/outbox.ts +216 -0
- package/src/ontology-storage/shared.ts +293 -0
- package/src/ontology-storage/sources.ts +658 -0
- package/src/pg-action-run-storage.ts +657 -0
- package/src/pg-client.ts +124 -0
- package/src/pg-object-query-compiler.ts +1545 -0
- package/src/pg-object-storage.ts +638 -0
- package/src/pg-pipeline-run-storage.ts +457 -0
- package/src/pg-projection-run-storage.ts +526 -0
- package/src/pg-rules-storage.ts +308 -0
- package/src/pg-sync-run-storage.ts +263 -0
- package/src/pg-timeseries-storage.ts +180 -0
- package/src/pg-webhook-delivery-storage.ts +180 -0
- package/src/pg-webhook-run-storage.ts +185 -0
- package/src/pg-workflow-intervention-storage.ts +339 -0
- package/src/pg-workflow-run-storage.ts +1065 -0
- package/src/run-list-query.ts +108 -0
- package/src/sql.d.ts +4 -0
- package/src/storage-errors.ts +72 -0
- package/src/transactions.ts +58 -0
package/src/pg-client.ts
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import postgres from "postgres"
|
|
2
|
+
|
|
3
|
+
// Data-type policy (porsager defaults, deliberately not overridden):
|
|
4
|
+
// - `timestamptz` -> JS `Date`. Row mappers normalize via `toIsoString(Date | string)`.
|
|
5
|
+
// - `bigint` -> string (porsager avoids `Number` precision loss). The only bigint values
|
|
6
|
+
// read here are `COUNT(*)` results, which fit safely in `Number`, so callers use
|
|
7
|
+
// `Number(...)`. We do NOT register `postgres.BigInt` — it would break those `Number()`
|
|
8
|
+
// conversions and JSON serialization.
|
|
9
|
+
// - `numeric`/`decimal` -> string. The schema has no such columns; revisit if one is added.
|
|
10
|
+
// - `jsonb` -> parsed JS value (objects/arrays/scalars), same as before.
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Connection handle backing every Postgres storage adapter.
|
|
14
|
+
*
|
|
15
|
+
* This is the porsager `postgres` pool — not Bun's built-in SQL. bun:sql's pool orphaned
|
|
16
|
+
* connections under request bursts (a query whose client disconnected mid-flight left its
|
|
17
|
+
* connection checked out but never returned), so the pool drained to zero usable slots and
|
|
18
|
+
* the whole API hung until a restart. porsager's pool reclaims connections reliably, which
|
|
19
|
+
* is the actual fix for that wedge.
|
|
20
|
+
*
|
|
21
|
+
* Queries declare their row type per call (`` sql<RowType[]>`...` ``) rather than via an open
|
|
22
|
+
* generic, so results are typed without `as` casts.
|
|
23
|
+
*/
|
|
24
|
+
export type SQL = postgres.Sql<Record<string, never>>
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* A query runner that may be the pool or an open transaction. Adapters that run the same
|
|
28
|
+
* statement on either should accept this rather than {@link SQL} (only the pool exposes
|
|
29
|
+
* `begin`/`reserve`/`end`).
|
|
30
|
+
*/
|
|
31
|
+
export type SQLClient = postgres.ISql<Record<string, never>>
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* A value accepted as a positional parameter by {@link SQLClient.unsafe}. Dynamically built
|
|
35
|
+
* parameter arrays (from the query-IR compiler / run-list helpers) are cast to this since
|
|
36
|
+
* their element types can't be inferred statically.
|
|
37
|
+
*/
|
|
38
|
+
export type SqlParameter = postgres.ParameterOrJSON<never>
|
|
39
|
+
|
|
40
|
+
export interface CreatePgClientOptions {
|
|
41
|
+
readonly connectionString?: string
|
|
42
|
+
readonly host?: string
|
|
43
|
+
readonly port?: number
|
|
44
|
+
readonly database?: string
|
|
45
|
+
readonly user?: string
|
|
46
|
+
readonly password?: string
|
|
47
|
+
/** Maximum pooled connections. */
|
|
48
|
+
readonly max: number
|
|
49
|
+
/** Schema pinned via `search_path` on every connection. */
|
|
50
|
+
readonly schemaName: string
|
|
51
|
+
/** Close idle connections after this many ms (frees slots back to the server). Default 30s. */
|
|
52
|
+
readonly idleTimeoutMillis?: number
|
|
53
|
+
/** Per-connection `statement_timeout` (ms). Unset = no timeout. */
|
|
54
|
+
readonly statementTimeoutMillis?: number
|
|
55
|
+
/** Per-connection `idle_in_transaction_session_timeout` (ms). Unset = no timeout. */
|
|
56
|
+
readonly idleInTransactionSessionTimeoutMillis?: number
|
|
57
|
+
/** Seconds to wait when establishing a connection. Default 10. */
|
|
58
|
+
readonly connectTimeoutMillis?: number
|
|
59
|
+
/**
|
|
60
|
+
* Whether porsager creates server-side prepared statements (its default; faster against a
|
|
61
|
+
* direct Postgres connection). Keep `true` for a direct connection. Behind a transaction-mode
|
|
62
|
+
* pooler, prepared statements are supported by PgBouncer >= 1.21 (Oct 2023) when
|
|
63
|
+
* `max_prepared_statements > 0`; set `false` only for an older PgBouncer or when that setting
|
|
64
|
+
* is 0. (The blanket incompatibility in porsager#93 predates PgBouncer 1.21.)
|
|
65
|
+
*/
|
|
66
|
+
readonly prepare?: boolean
|
|
67
|
+
readonly ssl?: boolean | "require" | "prefer"
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const DEFAULT_IDLE_TIMEOUT_SECONDS = 30
|
|
71
|
+
const DEFAULT_CONNECT_TIMEOUT_SECONDS = 10
|
|
72
|
+
|
|
73
|
+
/** Build a configured porsager `postgres` pool. */
|
|
74
|
+
export function createPgClient(options: CreatePgClientOptions): SQL {
|
|
75
|
+
// GUCs applied to every connection at startup.
|
|
76
|
+
const connection: Record<string, string | number | boolean> = {
|
|
77
|
+
search_path: options.schemaName,
|
|
78
|
+
}
|
|
79
|
+
if (options.statementTimeoutMillis !== undefined) {
|
|
80
|
+
connection.statement_timeout = options.statementTimeoutMillis
|
|
81
|
+
}
|
|
82
|
+
if (options.idleInTransactionSessionTimeoutMillis !== undefined) {
|
|
83
|
+
connection.idle_in_transaction_session_timeout = options.idleInTransactionSessionTimeoutMillis
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const idleTimeout =
|
|
87
|
+
options.idleTimeoutMillis !== undefined
|
|
88
|
+
? Math.max(1, Math.round(options.idleTimeoutMillis / 1000))
|
|
89
|
+
: DEFAULT_IDLE_TIMEOUT_SECONDS
|
|
90
|
+
|
|
91
|
+
const connectTimeout =
|
|
92
|
+
options.connectTimeoutMillis !== undefined
|
|
93
|
+
? Math.max(1, Math.round(options.connectTimeoutMillis / 1000))
|
|
94
|
+
: DEFAULT_CONNECT_TIMEOUT_SECONDS
|
|
95
|
+
|
|
96
|
+
const base = {
|
|
97
|
+
max: options.max,
|
|
98
|
+
// `idle_timeout` is important on managed Postgres (e.g. DigitalOcean), which closes idle
|
|
99
|
+
// server-side connections — without it porsager can hand out a dead socket (porsager#179).
|
|
100
|
+
idle_timeout: idleTimeout,
|
|
101
|
+
connect_timeout: connectTimeout,
|
|
102
|
+
// `max_lifetime` is intentionally left at porsager's default (a random 45–90 min): it
|
|
103
|
+
// cycles connections to avoid server-side memory bloat from long-lived prepared
|
|
104
|
+
// statements and plays nicely with managed databases. Disabling it is discouraged.
|
|
105
|
+
connection,
|
|
106
|
+
// Match the previous (bun:sql) behavior of not surfacing routine PostgreSQL NOTICEs.
|
|
107
|
+
onnotice: () => {},
|
|
108
|
+
...(options.prepare !== undefined ? { prepare: options.prepare } : {}),
|
|
109
|
+
...(options.ssl ? { ssl: options.ssl } : {}),
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (options.connectionString) {
|
|
113
|
+
return postgres(options.connectionString, base)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return postgres({
|
|
117
|
+
...base,
|
|
118
|
+
host: options.host ?? "localhost",
|
|
119
|
+
port: options.port ?? 5432,
|
|
120
|
+
...(options.database !== undefined ? { database: options.database } : {}),
|
|
121
|
+
...(options.user !== undefined ? { username: options.user } : {}),
|
|
122
|
+
...(options.password !== undefined ? { password: options.password } : {}),
|
|
123
|
+
})
|
|
124
|
+
}
|