@dads-framework/postgres 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (160) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +97 -2
  3. package/dist/drizzle.d.ts +9 -0
  4. package/dist/drizzle.d.ts.map +1 -0
  5. package/dist/drizzle.js +2 -0
  6. package/dist/drizzle.js.map +1 -0
  7. package/dist/index.d.ts +13 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +12 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/lib/attributed-pool.d.ts +95 -0
  12. package/dist/lib/attributed-pool.d.ts.map +1 -0
  13. package/dist/lib/attributed-pool.js +128 -0
  14. package/dist/lib/attributed-pool.js.map +1 -0
  15. package/dist/lib/bytea-column.d.ts +37 -0
  16. package/dist/lib/bytea-column.d.ts.map +1 -0
  17. package/dist/lib/bytea-column.js +20 -0
  18. package/dist/lib/bytea-column.js.map +1 -0
  19. package/dist/lib/checked-out-client-errors.d.ts +22 -0
  20. package/dist/lib/checked-out-client-errors.d.ts.map +1 -0
  21. package/dist/lib/checked-out-client-errors.js +28 -0
  22. package/dist/lib/checked-out-client-errors.js.map +1 -0
  23. package/dist/lib/connect-postgres-client.d.ts +16 -0
  24. package/dist/lib/connect-postgres-client.d.ts.map +1 -0
  25. package/dist/lib/connect-postgres-client.js +14 -0
  26. package/dist/lib/connect-postgres-client.js.map +1 -0
  27. package/dist/lib/counting-client.d.ts +39 -0
  28. package/dist/lib/counting-client.d.ts.map +1 -0
  29. package/dist/lib/counting-client.js +102 -0
  30. package/dist/lib/counting-client.js.map +1 -0
  31. package/dist/lib/create-postgres-connection.d.ts +65 -0
  32. package/dist/lib/create-postgres-connection.d.ts.map +1 -0
  33. package/dist/lib/create-postgres-connection.js +68 -0
  34. package/dist/lib/create-postgres-connection.js.map +1 -0
  35. package/dist/lib/event-actor-columns.d.ts +49 -0
  36. package/dist/lib/event-actor-columns.d.ts.map +1 -0
  37. package/dist/lib/event-actor-columns.js +37 -0
  38. package/dist/lib/event-actor-columns.js.map +1 -0
  39. package/dist/lib/inet-column.d.ts +37 -0
  40. package/dist/lib/inet-column.d.ts.map +1 -0
  41. package/dist/lib/inet-column.js +17 -0
  42. package/dist/lib/inet-column.js.map +1 -0
  43. package/dist/lib/load-page-and-total.d.ts +16 -0
  44. package/dist/lib/load-page-and-total.d.ts.map +1 -0
  45. package/dist/lib/load-page-and-total.js +16 -0
  46. package/dist/lib/load-page-and-total.js.map +1 -0
  47. package/dist/lib/postgres-pool-laws.d.ts +6 -0
  48. package/dist/lib/postgres-pool-laws.d.ts.map +1 -0
  49. package/dist/lib/postgres-pool-laws.js +58 -0
  50. package/dist/lib/postgres-pool-laws.js.map +1 -0
  51. package/dist/lib/synced-rows.d.ts +97 -0
  52. package/dist/lib/synced-rows.d.ts.map +1 -0
  53. package/dist/lib/synced-rows.js +158 -0
  54. package/dist/lib/synced-rows.js.map +1 -0
  55. package/dist/lib/unique-violation.d.ts +10 -0
  56. package/dist/lib/unique-violation.d.ts.map +1 -0
  57. package/dist/lib/unique-violation.js +24 -0
  58. package/dist/lib/unique-violation.js.map +1 -0
  59. package/dist/lib/update-at-row-version.d.ts +31 -0
  60. package/dist/lib/update-at-row-version.d.ts.map +1 -0
  61. package/dist/lib/update-at-row-version.js +14 -0
  62. package/dist/lib/update-at-row-version.js.map +1 -0
  63. package/dist/lib/xid8-column.d.ts +38 -0
  64. package/dist/lib/xid8-column.d.ts.map +1 -0
  65. package/dist/lib/xid8-column.js +18 -0
  66. package/dist/lib/xid8-column.js.map +1 -0
  67. package/dist/postgres.tsbuildinfo +1 -0
  68. package/dist/testing/create-test-pool.d.ts +35 -0
  69. package/dist/testing/create-test-pool.d.ts.map +1 -0
  70. package/dist/testing/create-test-pool.js +47 -0
  71. package/dist/testing/create-test-pool.js.map +1 -0
  72. package/dist/testing/deterministic-uuid.d.ts +2 -0
  73. package/dist/testing/deterministic-uuid.d.ts.map +1 -0
  74. package/dist/testing/deterministic-uuid.js +20 -0
  75. package/dist/testing/deterministic-uuid.js.map +1 -0
  76. package/dist/testing/global-setup/container-tier-semaphore.d.ts +51 -0
  77. package/dist/testing/global-setup/container-tier-semaphore.d.ts.map +1 -0
  78. package/dist/testing/global-setup/container-tier-semaphore.js +129 -0
  79. package/dist/testing/global-setup/container-tier-semaphore.js.map +1 -0
  80. package/dist/testing/global-setup/decide-shared-test-postgres.d.ts +51 -0
  81. package/dist/testing/global-setup/decide-shared-test-postgres.d.ts.map +1 -0
  82. package/dist/testing/global-setup/decide-shared-test-postgres.js +109 -0
  83. package/dist/testing/global-setup/decide-shared-test-postgres.js.map +1 -0
  84. package/dist/testing/global-setup/index.d.ts +16 -0
  85. package/dist/testing/global-setup/index.d.ts.map +1 -0
  86. package/dist/testing/global-setup/index.js +151 -0
  87. package/dist/testing/global-setup/index.js.map +1 -0
  88. package/dist/testing/global-setup/refuse-disabled-container-cleanup.d.ts +34 -0
  89. package/dist/testing/global-setup/refuse-disabled-container-cleanup.d.ts.map +1 -0
  90. package/dist/testing/global-setup/refuse-disabled-container-cleanup.js +45 -0
  91. package/dist/testing/global-setup/refuse-disabled-container-cleanup.js.map +1 -0
  92. package/dist/testing/global-setup/refuse-low-memory-runs.d.ts +176 -0
  93. package/dist/testing/global-setup/refuse-low-memory-runs.d.ts.map +1 -0
  94. package/dist/testing/global-setup/refuse-low-memory-runs.js +388 -0
  95. package/dist/testing/global-setup/refuse-low-memory-runs.js.map +1 -0
  96. package/dist/testing/global-setup/refuse-slow-local-test-mode.d.ts +5 -0
  97. package/dist/testing/global-setup/refuse-slow-local-test-mode.d.ts.map +1 -0
  98. package/dist/testing/global-setup/refuse-slow-local-test-mode.js +18 -0
  99. package/dist/testing/global-setup/refuse-slow-local-test-mode.js.map +1 -0
  100. package/dist/testing/global-setup/refuse-stacked-container-runs.d.ts +106 -0
  101. package/dist/testing/global-setup/refuse-stacked-container-runs.d.ts.map +1 -0
  102. package/dist/testing/global-setup/refuse-stacked-container-runs.js +284 -0
  103. package/dist/testing/global-setup/refuse-stacked-container-runs.js.map +1 -0
  104. package/dist/testing/global-setup/remove-stale-testcontainers.d.ts +33 -0
  105. package/dist/testing/global-setup/remove-stale-testcontainers.d.ts.map +1 -0
  106. package/dist/testing/global-setup/remove-stale-testcontainers.js +165 -0
  107. package/dist/testing/global-setup/remove-stale-testcontainers.js.map +1 -0
  108. package/dist/testing/global-setup/retired-database-touches.d.ts +16 -0
  109. package/dist/testing/global-setup/retired-database-touches.d.ts.map +1 -0
  110. package/dist/testing/global-setup/retired-database-touches.js +73 -0
  111. package/dist/testing/global-setup/retired-database-touches.js.map +1 -0
  112. package/dist/testing/global-setup/schema-keyed-coverage.d.ts +56 -0
  113. package/dist/testing/global-setup/schema-keyed-coverage.d.ts.map +1 -0
  114. package/dist/testing/global-setup/schema-keyed-coverage.js +84 -0
  115. package/dist/testing/global-setup/schema-keyed-coverage.js.map +1 -0
  116. package/dist/testing/global-setup/skip-unless-container-tests.d.ts +31 -0
  117. package/dist/testing/global-setup/skip-unless-container-tests.d.ts.map +1 -0
  118. package/dist/testing/global-setup/skip-unless-container-tests.js +33 -0
  119. package/dist/testing/global-setup/skip-unless-container-tests.js.map +1 -0
  120. package/dist/testing/global-setup/start-migrated-template-postgres.d.ts +20 -0
  121. package/dist/testing/global-setup/start-migrated-template-postgres.d.ts.map +1 -0
  122. package/dist/testing/global-setup/start-migrated-template-postgres.js +53 -0
  123. package/dist/testing/global-setup/start-migrated-template-postgres.js.map +1 -0
  124. package/dist/testing/global-setup/verify-schema-keyed-suite.d.ts +43 -0
  125. package/dist/testing/global-setup/verify-schema-keyed-suite.d.ts.map +1 -0
  126. package/dist/testing/global-setup/verify-schema-keyed-suite.js +201 -0
  127. package/dist/testing/global-setup/verify-schema-keyed-suite.js.map +1 -0
  128. package/dist/testing/global-setup/worktree-identity.d.ts +21 -0
  129. package/dist/testing/global-setup/worktree-identity.d.ts.map +1 -0
  130. package/dist/testing/global-setup/worktree-identity.js +30 -0
  131. package/dist/testing/global-setup/worktree-identity.js.map +1 -0
  132. package/dist/testing/index.d.ts +11 -0
  133. package/dist/testing/index.d.ts.map +1 -0
  134. package/dist/testing/index.js +11 -0
  135. package/dist/testing/index.js.map +1 -0
  136. package/dist/testing/rehearsal-postgres.d.ts +20 -0
  137. package/dist/testing/rehearsal-postgres.d.ts.map +1 -0
  138. package/dist/testing/rehearsal-postgres.js +61 -0
  139. package/dist/testing/rehearsal-postgres.js.map +1 -0
  140. package/dist/testing/start-test-postgres.d.ts +59 -0
  141. package/dist/testing/start-test-postgres.d.ts.map +1 -0
  142. package/dist/testing/start-test-postgres.js +241 -0
  143. package/dist/testing/start-test-postgres.js.map +1 -0
  144. package/dist/testing/test-postgres-storage.d.ts +44 -0
  145. package/dist/testing/test-postgres-storage.d.ts.map +1 -0
  146. package/dist/testing/test-postgres-storage.js +88 -0
  147. package/dist/testing/test-postgres-storage.js.map +1 -0
  148. package/dist/testing/truncate-tables.d.ts +31 -0
  149. package/dist/testing/truncate-tables.d.ts.map +1 -0
  150. package/dist/testing/truncate-tables.js +39 -0
  151. package/dist/testing/truncate-tables.js.map +1 -0
  152. package/dist/testing/violated-foreign-key.d.ts +14 -0
  153. package/dist/testing/violated-foreign-key.d.ts.map +1 -0
  154. package/dist/testing/violated-foreign-key.js +78 -0
  155. package/dist/testing/violated-foreign-key.js.map +1 -0
  156. package/dist/testing/with-database.d.ts +2 -0
  157. package/dist/testing/with-database.d.ts.map +1 -0
  158. package/dist/testing/with-database.js +6 -0
  159. package/dist/testing/with-database.js.map +1 -0
  160. package/package.json +88 -4
package/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Bryton Cooper
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,98 @@
1
- # Temporary Holding Version
1
+ # @dads-framework/postgres — the Postgres pool and Drizzle handle
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Open and close Postgres connections in application wiring so database adapters
4
+ can share one pool. This package owns the pool and the Drizzle instance (`createPostgresConnection`),
5
+ plus pool lifecycle/teardown, and the adapter helpers `isUniqueViolation` and
6
+ `loadPageAndTotal`/`sqlOffsetForPage`. Domain code never imports `drizzle-orm`
7
+ directly — it talks to the per-domain `<entity>-reader`/`<entity>-writer`
8
+ contracts whose `drizzle-` implementations live in each domain's `database/`
9
+ folder (docs/architecture/api-domains.md; exact surface in
10
+ docs/reference/database.md). `./testing` has Testcontainers helpers.
11
+
12
+ `connectPostgresClient(connectionString)` opens a dedicated client for operations
13
+ that hold session state, such as the migration runner's advisory lock. The
14
+ caller ends the client. `RehearsalPostgres.start()` from `./testing` starts an
15
+ isolated cluster for migration rehearsals that create databases and compare
16
+ cluster-wide state. Call the returned `stop()` when the rehearsal ends.
17
+
18
+ Each test file gets its own database on the shared test container. Stopping it
19
+ closes its pool, disables connections, and waits for its remaining backends to
20
+ terminate. The inaccessible database stays on disk until the shared-container
21
+ owner stops the container, bounding retention to that owner's lifetime and
22
+ avoiding the server-wide checkpoint required by `DROP DATABASE`. A file that
23
+ owns its own container closes its pool and stops that container.
24
+
25
+ For several isolated databases on one fixture server, call
26
+ `createPrivateDatabase(server.connectionString, {})` from `./testing` for each
27
+ database. Stop each database when its case ends and stop the fixture server
28
+ after the suite.
29
+
30
+ ## Versioned updates
31
+
32
+ Call `updateAtRowVersion(tx, table, input)` in the transaction that owns a save. The caller scopes `input.where` to one row. The helper advances the integer revision and returns an empty result when the predicate or expected revision did not match. See the [versioned update guide](../../../docs/architecture/stale-save.md).
33
+
34
+ ## Schema-keyed integration tests
35
+
36
+ Nx integration suites using the shared database can opt in to table-granular
37
+ hashing with the `databaseSharedState` named input. Include each generated table
38
+ folder the suite can reach in its inputs, including tables owned by framework
39
+ packages. A suite that runs migrations itself or reads the migration journal
40
+ needs the `databaseMigrations` named input instead.
41
+
42
+ ## Try it
43
+
44
+ In a Node.js 22+ project, install the package.
45
+
46
+ ```sh
47
+ npm install @dads-framework/postgres
48
+ ```
49
+
50
+ Set `DATABASE_URL` to the connection string for a running PostgreSQL database.
51
+
52
+ Save this as `example.mts` and run `node --experimental-strip-types example.mts`.
53
+
54
+ <!-- dads:check-example -->
55
+
56
+ ```ts
57
+ import { connectPostgresClient } from "@dads-framework/postgres";
58
+
59
+ const databaseUrl = process.env["DATABASE_URL"];
60
+ if (!databaseUrl) throw new Error("Set DATABASE_URL");
61
+
62
+ const client = await connectPostgresClient(databaseUrl);
63
+ try {
64
+ const result = await client.query<{ answer: number }>("SELECT 1 AS answer");
65
+ console.log(result.rows[0]?.answer);
66
+ } finally {
67
+ await client.end();
68
+ }
69
+ ```
70
+
71
+ The script prints `1` and closes the connection.
72
+
73
+ In the generated app, `assembly/services.ts` opens the shared application
74
+ connection and closes it when the provider stops. [Starter service wiring](../../../apps/kit-workbench/assembly/services.ts)
75
+
76
+ ## What it refuses to do
77
+
78
+ This package manages database connections. Applications provide connection configuration and own schema migrations.
79
+
80
+ ## Drizzle integration
81
+
82
+ Use `@dads-framework/postgres/drizzle` for the native database handle and its
83
+ schema-generic query type. The query type follows the native factory result
84
+ and preserves the original default schema. The root still exports
85
+ `DrizzleHandle` for compatibility.
86
+
87
+ ## Connection contract
88
+
89
+ Plain and attributed pools provide the same checkout, release, and teardown
90
+ behavior. The Drizzle handle uses the same pool, and each transaction keeps
91
+ its writes isolated until commit. Both modes run the connection conformance
92
+ suite against live PostgreSQL.
93
+
94
+ The adapter's `dadsConformance` declaration points to one suite and the two
95
+ provider registrations that run it. Both use the same PostgreSQL server. The
96
+ native pool implements checkout directly and the attributed pool adds a
97
+ checkout stamp while preserving native behavior. This tests pool semantics
98
+ and does not imply portability to another database protocol.
@@ -0,0 +1,9 @@
1
+ import type { drizzle, NodePgDatabase } from "drizzle-orm/node-postgres";
2
+ export type { NodePgDatabase as DrizzleDatabase } from "drizzle-orm/node-postgres";
3
+ /**
4
+ * The query handle returned by the native Drizzle factory, without its
5
+ * pool-owned client. Keep the schema inferred from the factory result and
6
+ * preserve the historical unconfigured schema default.
7
+ */
8
+ export type DrizzleHandle<TSchema extends Record<string, unknown> = Record<string, unknown>> = ReturnType<typeof drizzle<TSchema>> extends NodePgDatabase<infer Schema> ? NodePgDatabase<Schema> : never;
9
+ //# sourceMappingURL=drizzle.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"drizzle.d.ts","sourceRoot":"","sources":["../src/drizzle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAEzE,YAAY,EAAE,cAAc,IAAI,eAAe,EAAE,MAAM,2BAA2B,CAAC;AAEnF;;;;GAIG;AACH,MAAM,MAAM,aAAa,CACvB,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,IAEjE,UAAU,CAAC,OAAO,OAAO,CAAC,OAAO,CAAC,CAAC,SAAS,cAAc,CAAC,MAAM,MAAM,CAAC,GACpE,cAAc,CAAC,MAAM,CAAC,GACtB,KAAK,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=drizzle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"drizzle.js","sourceRoot":"","sources":["../src/drizzle.ts"],"names":[],"mappings":""}
@@ -0,0 +1,13 @@
1
+ export { createAttributedPool, stampClientWithActor, type ActorStampableClient, type AuditActorSource, } from "./lib/attributed-pool.js";
2
+ export { bytea } from "./lib/bytea-column.js";
3
+ export { connectPostgresClient, type PostgresClient, type PostgresQueryConfig, type PostgresQueryResult, type PostgresQueryResultRow, } from "./lib/connect-postgres-client.js";
4
+ export { inet } from "./lib/inet-column.js";
5
+ export { createPostgresConnection, type CreatePostgresConnectionOptions, type PostgresConnection, type PostgresPoolErrorLog, } from "./lib/create-postgres-connection.js";
6
+ export type { DrizzleHandle } from "./drizzle.js";
7
+ export { eventActorColumns, type EventActorColumns, type PersistedEventActor, } from "./lib/event-actor-columns.js";
8
+ export { loadPageAndTotal, sqlOffsetForPage, } from "./lib/load-page-and-total.js";
9
+ export { isUniqueViolation } from "./lib/unique-violation.js";
10
+ export { applySyncedRows, excluded, excludedAll, newerOrDifferent, sqlStateOf, SYNCED_ROW_IMMUTABLE_DIFFERS, SYNCED_VALUE_UNKNOWN, type SyncedRowChange, type SyncedRowOutcome, type SyncedRowTransaction, type SyncedRowWriter, } from "./lib/synced-rows.js";
11
+ export { xid8 } from "./lib/xid8-column.js";
12
+ export { updateAtRowVersion } from "./lib/update-at-row-version.js";
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,oBAAoB,EACpB,KAAK,oBAAoB,EACzB,KAAK,gBAAgB,GACtB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAC9C,OAAO,EACL,qBAAqB,EACrB,KAAK,cAAc,EACnB,KAAK,mBAAmB,EACxB,KAAK,mBAAmB,EACxB,KAAK,sBAAsB,GAC5B,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAC5C,OAAO,EACL,wBAAwB,EACxB,KAAK,+BAA+B,EACpC,KAAK,kBAAkB,EACvB,KAAK,oBAAoB,GAC1B,MAAM,qCAAqC,CAAC;AAC7C,YAAY,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EACL,iBAAiB,EACjB,KAAK,iBAAiB,EACtB,KAAK,mBAAmB,GACzB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EACL,gBAAgB,EAChB,gBAAgB,GACjB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,EACL,eAAe,EACf,QAAQ,EACR,WAAW,EACX,gBAAgB,EAChB,UAAU,EACV,4BAA4B,EAC5B,oBAAoB,EACpB,KAAK,eAAe,EACpB,KAAK,gBAAgB,EACrB,KAAK,oBAAoB,EACzB,KAAK,eAAe,GACrB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAC5C,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,12 @@
1
+ export { createAttributedPool, stampClientWithActor, } from "./lib/attributed-pool.js";
2
+ export { bytea } from "./lib/bytea-column.js";
3
+ export { connectPostgresClient, } from "./lib/connect-postgres-client.js";
4
+ export { inet } from "./lib/inet-column.js";
5
+ export { createPostgresConnection, } from "./lib/create-postgres-connection.js";
6
+ export { eventActorColumns, } from "./lib/event-actor-columns.js";
7
+ export { loadPageAndTotal, sqlOffsetForPage, } from "./lib/load-page-and-total.js";
8
+ export { isUniqueViolation } from "./lib/unique-violation.js";
9
+ export { applySyncedRows, excluded, excludedAll, newerOrDifferent, sqlStateOf, SYNCED_ROW_IMMUTABLE_DIFFERS, SYNCED_VALUE_UNKNOWN, } from "./lib/synced-rows.js";
10
+ export { xid8 } from "./lib/xid8-column.js";
11
+ export { updateAtRowVersion } from "./lib/update-at-row-version.js";
12
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,EACpB,oBAAoB,GAGrB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAC9C,OAAO,EACL,qBAAqB,GAKtB,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAC5C,OAAO,EACL,wBAAwB,GAIzB,MAAM,qCAAqC,CAAC;AAE7C,OAAO,EACL,iBAAiB,GAGlB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EACL,gBAAgB,EAChB,gBAAgB,GACjB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,EACL,eAAe,EACf,QAAQ,EACR,WAAW,EACX,gBAAgB,EAChB,UAAU,EACV,4BAA4B,EAC5B,oBAAoB,GAKrB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAC5C,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC"}
@@ -0,0 +1,95 @@
1
+ /**
2
+ * The attributed pool: every connection checkout carries who is writing.
3
+ *
4
+ * ADR-0112 makes attribution a property of the CONNECTION rather than of the
5
+ * call site. The `audit_record` trigger reads `dads.audit_actor` out of the
6
+ * session and refuses any write that reaches it unattributed, so something has
7
+ * to put the actor there before the caller's first statement. That something
8
+ * lives here, because the checkout is the one place every writer passes
9
+ * through: Drizzle's plain statements (`pool.query`), Drizzle's transactions
10
+ * (`pool.connect()` then `begin`), `PostgresOwnerLock`, and the Better Auth
11
+ * adapter all share this single pool object.
12
+ *
13
+ * Three decisions worth keeping:
14
+ *
15
+ * - **A subclass of `pg.Pool`, not a delegating wrapper.** Drizzle decides
16
+ * whether its client is a pool with `client instanceof Pool`, and only then
17
+ * runs a transaction on ONE checked-out client; a wrapper object would send
18
+ * `begin`, the statements, and `commit` down three different connections.
19
+ * Subclassing also keeps `$client: Pool` on the Drizzle handle honest with
20
+ * no cast.
21
+ * - **`connect` is the only override.** `pg.Pool.prototype.query` is itself
22
+ * connect → query → release, so inheriting it stamps the single-statement
23
+ * path for free and leaves pg's own release-on-error semantics as pg's,
24
+ * instead of a re-implementation that has to be kept in step with them.
25
+ * - **Session level (`set_config(..., false)`), re-stamped on every
26
+ * checkout.** Most writes in this codebase are a lone `insert` with no
27
+ * transaction, so `SET LOCAL` would be gone before the statement ran. The
28
+ * flip side is that a released client keeps the setting — which is exactly
29
+ * why the next checkout overwrites it, including with `''` when no actor is
30
+ * in force, so one request can never inherit the previous one's identity.
31
+ *
32
+ * Cost: one extra round trip per checkout, never per statement.
33
+ */
34
+ import { Pool, type PoolConfig } from "pg";
35
+ import { type AuditActor } from "@dads-framework/audit-actor";
36
+ /**
37
+ * Where the pool reads the actor at checkout time. Production passes
38
+ * `currentAuditActor`; tests pass a source that falls back to a system actor.
39
+ * A source that returns `undefined` stamps the empty string, which is the
40
+ * value the trigger refuses.
41
+ */
42
+ export type AuditActorSource = () => AuditActor | undefined;
43
+ /**
44
+ * The stamp, spelled once. `false` is `is_local`: the setting outlives the
45
+ * statement so a later single-statement write on the same client still sees
46
+ * it. Parameterised rather than interpolated — the actor JSON carries user
47
+ * input in `reason`.
48
+ */
49
+ export declare const AUDIT_ACTOR_STAMP_SQL = "select set_config($1, $2, false)";
50
+ /**
51
+ * The part of any client the stamp touches. Narrow on purpose: it is what lets
52
+ * the stamp be proved without a database, and it is satisfied by both a pooled
53
+ * `PoolClient` and a standalone `pg.Client` — which is how the migrator and the
54
+ * one-shot scripts, which hold no pool, stamp themselves.
55
+ */
56
+ export type ActorStampableClient = {
57
+ query(text: string, values: string[]): Promise<unknown>;
58
+ };
59
+ /**
60
+ * The part of a checked-out client the pool's stamp touches: it also has to be
61
+ * releasable, because a failed stamp destroys the connection rather than
62
+ * returning it.
63
+ */
64
+ export type StampableClient = ActorStampableClient & {
65
+ release(error?: Error): void;
66
+ };
67
+ /**
68
+ * Sets `dads.audit_actor` on `client` to `actor`, or to the empty string —
69
+ * the value the trigger refuses — when there is none. Exactly one statement,
70
+ * so attributing a connection costs one round trip whatever the caller goes on
71
+ * to do.
72
+ *
73
+ * Long-lived one-shot clients (the migration runner, the cutover, the seeds)
74
+ * call this once after connecting; the setting is session level, so every
75
+ * statement that follows on that connection carries it.
76
+ */
77
+ export declare function stampClientWithActor(client: ActorStampableClient, actor: AuditActor | undefined): Promise<void>;
78
+ /** The pool's form of the stamp: whoever the source names at checkout time. */
79
+ export declare function stampAuditActor(client: StampableClient, actorSource: AuditActorSource): Promise<void>;
80
+ /**
81
+ * Checks a client out and stamps it before the caller can reach it.
82
+ *
83
+ * A client whose stamp failed is released WITH the error, so pg destroys it
84
+ * instead of returning a connection carrying an unknown actor to the pool.
85
+ * The original failure is what the caller sees.
86
+ */
87
+ export declare function checkOutStampedClient<TClient extends StampableClient>(checkOut: () => Promise<TClient>, actorSource: AuditActorSource): Promise<TClient>;
88
+ /**
89
+ * The one construction point for an attributed pool. Takes the pool's config
90
+ * rather than an existing pool: a subclass cannot adopt another instance's
91
+ * identity, and identity is the point — Drizzle's `instanceof Pool` check and
92
+ * the `$client: Pool` type both need this object to BE a pool.
93
+ */
94
+ export declare function createAttributedPool(config: PoolConfig, actorSource: AuditActorSource): Pool;
95
+ //# sourceMappingURL=attributed-pool.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attributed-pool.d.ts","sourceRoot":"","sources":["../../src/lib/attributed-pool.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AAEH,OAAO,EAAE,IAAI,EAAmB,KAAK,UAAU,EAAE,MAAM,IAAI,CAAC;AAC5D,OAAO,EAGL,KAAK,UAAU,EAChB,MAAM,6BAA6B,CAAC;AAIrC;;;;;GAKG;AACH,MAAM,MAAM,gBAAgB,GAAG,MAAM,UAAU,GAAG,SAAS,CAAC;AAE5D;;;;;GAKG;AACH,eAAO,MAAM,qBAAqB,qCAAqC,CAAC;AAExE;;;;;GAKG;AACH,MAAM,MAAM,oBAAoB,GAAG;IACjC,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CACzD,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,eAAe,GAAG,oBAAoB,GAAG;IACnD,OAAO,CAAC,KAAK,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC;CAC9B,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAAsB,oBAAoB,CACxC,MAAM,EAAE,oBAAoB,EAC5B,KAAK,EAAE,UAAU,GAAG,SAAS,GAC5B,OAAO,CAAC,IAAI,CAAC,CAKf;AAED,+EAA+E;AAC/E,wBAAsB,eAAe,CACnC,MAAM,EAAE,eAAe,EACvB,WAAW,EAAE,gBAAgB,GAC5B,OAAO,CAAC,IAAI,CAAC,CAEf;AAED;;;;;;GAMG;AACH,wBAAsB,qBAAqB,CAAC,OAAO,SAAS,eAAe,EACzE,QAAQ,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,EAChC,WAAW,EAAE,gBAAgB,GAC5B,OAAO,CAAC,OAAO,CAAC,CAYlB;AAkDD;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,UAAU,EAClB,WAAW,EAAE,gBAAgB,GAC5B,IAAI,CAEN"}
@@ -0,0 +1,128 @@
1
+ /**
2
+ * The attributed pool: every connection checkout carries who is writing.
3
+ *
4
+ * ADR-0112 makes attribution a property of the CONNECTION rather than of the
5
+ * call site. The `audit_record` trigger reads `dads.audit_actor` out of the
6
+ * session and refuses any write that reaches it unattributed, so something has
7
+ * to put the actor there before the caller's first statement. That something
8
+ * lives here, because the checkout is the one place every writer passes
9
+ * through: Drizzle's plain statements (`pool.query`), Drizzle's transactions
10
+ * (`pool.connect()` then `begin`), `PostgresOwnerLock`, and the Better Auth
11
+ * adapter all share this single pool object.
12
+ *
13
+ * Three decisions worth keeping:
14
+ *
15
+ * - **A subclass of `pg.Pool`, not a delegating wrapper.** Drizzle decides
16
+ * whether its client is a pool with `client instanceof Pool`, and only then
17
+ * runs a transaction on ONE checked-out client; a wrapper object would send
18
+ * `begin`, the statements, and `commit` down three different connections.
19
+ * Subclassing also keeps `$client: Pool` on the Drizzle handle honest with
20
+ * no cast.
21
+ * - **`connect` is the only override.** `pg.Pool.prototype.query` is itself
22
+ * connect → query → release, so inheriting it stamps the single-statement
23
+ * path for free and leaves pg's own release-on-error semantics as pg's,
24
+ * instead of a re-implementation that has to be kept in step with them.
25
+ * - **Session level (`set_config(..., false)`), re-stamped on every
26
+ * checkout.** Most writes in this codebase are a lone `insert` with no
27
+ * transaction, so `SET LOCAL` would be gone before the statement ran. The
28
+ * flip side is that a released client keeps the setting — which is exactly
29
+ * why the next checkout overwrites it, including with `''` when no actor is
30
+ * in force, so one request can never inherit the previous one's identity.
31
+ *
32
+ * Cost: one extra round trip per checkout, never per statement.
33
+ */
34
+ import { Pool } from "pg";
35
+ import { AUDIT_ACTOR_SETTING, serializeAuditActor, } from "@dads-framework/audit-actor";
36
+ import { countStatementsOn } from "./counting-client.js";
37
+ /**
38
+ * The stamp, spelled once. `false` is `is_local`: the setting outlives the
39
+ * statement so a later single-statement write on the same client still sees
40
+ * it. Parameterised rather than interpolated — the actor JSON carries user
41
+ * input in `reason`.
42
+ */
43
+ export const AUDIT_ACTOR_STAMP_SQL = "select set_config($1, $2, false)";
44
+ /**
45
+ * Sets `dads.audit_actor` on `client` to `actor`, or to the empty string —
46
+ * the value the trigger refuses — when there is none. Exactly one statement,
47
+ * so attributing a connection costs one round trip whatever the caller goes on
48
+ * to do.
49
+ *
50
+ * Long-lived one-shot clients (the migration runner, the cutover, the seeds)
51
+ * call this once after connecting; the setting is session level, so every
52
+ * statement that follows on that connection carries it.
53
+ */
54
+ export async function stampClientWithActor(client, actor) {
55
+ await client.query(AUDIT_ACTOR_STAMP_SQL, [
56
+ AUDIT_ACTOR_SETTING,
57
+ actor === undefined ? "" : serializeAuditActor(actor),
58
+ ]);
59
+ }
60
+ /** The pool's form of the stamp: whoever the source names at checkout time. */
61
+ export async function stampAuditActor(client, actorSource) {
62
+ await stampClientWithActor(client, actorSource());
63
+ }
64
+ /**
65
+ * Checks a client out and stamps it before the caller can reach it.
66
+ *
67
+ * A client whose stamp failed is released WITH the error, so pg destroys it
68
+ * instead of returning a connection carrying an unknown actor to the pool.
69
+ * The original failure is what the caller sees.
70
+ */
71
+ export async function checkOutStampedClient(checkOut, actorSource) {
72
+ const client = await checkOut();
73
+ try {
74
+ await stampAuditActor(client, actorSource);
75
+ }
76
+ catch (error) {
77
+ client.release(asError(error));
78
+ throw error;
79
+ }
80
+ // After the stamp, so the pool's own round trip is never charged to the
81
+ // request, and once per client, because pg hands the same object out again.
82
+ countStatementsOn(client);
83
+ return client;
84
+ }
85
+ /**
86
+ * A `pg.Pool` whose every checkout is stamped with the current audit actor.
87
+ * Construct it with {@link createAttributedPool}.
88
+ */
89
+ class AttributedPool extends Pool {
90
+ #actorSource;
91
+ constructor(config, actorSource) {
92
+ super(config);
93
+ this.#actorSource = actorSource;
94
+ }
95
+ connect(callback) {
96
+ const checkedOut = checkOutStampedClient(() => super.connect(), this.#actorSource);
97
+ if (callback === undefined)
98
+ return checkedOut;
99
+ checkedOut.then((client) => {
100
+ // Released through an arrow rather than by handing over
101
+ // `client.release`: the callback owns the release, and an arrow keeps
102
+ // the method attached to the client it releases.
103
+ callback(undefined, client, (releaseError) => {
104
+ client.release(releaseError);
105
+ });
106
+ }, (error) => {
107
+ callback(asError(error), undefined, releaseNothing);
108
+ });
109
+ return undefined;
110
+ }
111
+ }
112
+ /**
113
+ * The one construction point for an attributed pool. Takes the pool's config
114
+ * rather than an existing pool: a subclass cannot adopt another instance's
115
+ * identity, and identity is the point — Drizzle's `instanceof Pool` check and
116
+ * the `$client: Pool` type both need this object to BE a pool.
117
+ */
118
+ export function createAttributedPool(config, actorSource) {
119
+ return new AttributedPool(config, actorSource);
120
+ }
121
+ /** There is no client to release when the checkout itself failed. */
122
+ function releaseNothing() {
123
+ return undefined;
124
+ }
125
+ function asError(value) {
126
+ return value instanceof Error ? value : new Error(String(value));
127
+ }
128
+ //# sourceMappingURL=attributed-pool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"attributed-pool.js","sourceRoot":"","sources":["../../src/lib/attributed-pool.ts"],"names":[],"mappings":"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"}
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Postgres `bytea` as a Drizzle column.
3
+ *
4
+ * drizzle-orm ships no `bytea` builder, so every binary column would otherwise
5
+ * hand-roll the same `customType`. Bytes stay `Uint8Array` on the TypeScript
6
+ * side because the packed formats that use this column (signature strokes
7
+ * today) are produced by isomorphic code that must not touch `Buffer`;
8
+ * node-postgres only serializes a real `Buffer`, so `toDriver` widens at the
9
+ * boundary and callers never have to remember.
10
+ */
11
+ export declare const bytea: {
12
+ (): import("drizzle-orm/pg-core").PgCustomColumnBuilder<{
13
+ name: "";
14
+ dataType: 'custom';
15
+ columnType: 'PgCustomColumn';
16
+ data: Uint8Array<ArrayBufferLike>;
17
+ driverParam: Buffer<ArrayBufferLike>;
18
+ enumValues: undefined;
19
+ }>;
20
+ <TConfig extends Record<string, any> & unknown>(fieldConfig?: TConfig | undefined): import("drizzle-orm/pg-core").PgCustomColumnBuilder<{
21
+ name: "";
22
+ dataType: 'custom';
23
+ columnType: 'PgCustomColumn';
24
+ data: Uint8Array<ArrayBufferLike>;
25
+ driverParam: Buffer<ArrayBufferLike>;
26
+ enumValues: undefined;
27
+ }>;
28
+ <TName extends string>(dbName: TName, fieldConfig?: unknown): import("drizzle-orm/pg-core").PgCustomColumnBuilder<{
29
+ name: TName;
30
+ dataType: 'custom';
31
+ columnType: 'PgCustomColumn';
32
+ data: Uint8Array<ArrayBufferLike>;
33
+ driverParam: Buffer<ArrayBufferLike>;
34
+ enumValues: undefined;
35
+ }>;
36
+ };
37
+ //# sourceMappingURL=bytea-column.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bytea-column.d.ts","sourceRoot":"","sources":["../../src/lib/bytea-column.ts"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AACH,eAAO,MAAM,KAAK;;;;;;;;;;;;;;;;;;;;;;;;;CAOhB,CAAC"}
@@ -0,0 +1,20 @@
1
+ import { customType } from "drizzle-orm/pg-core";
2
+ /**
3
+ * Postgres `bytea` as a Drizzle column.
4
+ *
5
+ * drizzle-orm ships no `bytea` builder, so every binary column would otherwise
6
+ * hand-roll the same `customType`. Bytes stay `Uint8Array` on the TypeScript
7
+ * side because the packed formats that use this column (signature strokes
8
+ * today) are produced by isomorphic code that must not touch `Buffer`;
9
+ * node-postgres only serializes a real `Buffer`, so `toDriver` widens at the
10
+ * boundary and callers never have to remember.
11
+ */
12
+ export const bytea = customType({
13
+ dataType() {
14
+ return "bytea";
15
+ },
16
+ toDriver(value) {
17
+ return Buffer.from(value.buffer, value.byteOffset, value.byteLength);
18
+ },
19
+ });
20
+ //# sourceMappingURL=bytea-column.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bytea-column.js","sourceRoot":"","sources":["../../src/lib/bytea-column.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAEjD;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,KAAK,GAAG,UAAU,CAA2C;IACxE,QAAQ;QACN,OAAO,OAAO,CAAC;IACjB,CAAC;IACD,QAAQ,CAAC,KAAiB;QACxB,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,UAAU,CAAC,CAAC;IACvE,CAAC;CACF,CAAC,CAAC"}
@@ -0,0 +1,22 @@
1
+ import type { Pool } from "pg";
2
+ /**
3
+ * Gives every client the pool hands out an `error` listener for as long as
4
+ * it is checked out.
5
+ *
6
+ * pg-pool listens for `error` only on IDLE clients. Once a client is checked
7
+ * out — a Drizzle transaction, the owner lock's session, the audit-actor
8
+ * stamp inside `pool.query` — nothing listens, and when the server drops the
9
+ * connection (restart, failover, `pg_terminate_backend`) the client's
10
+ * `'end'` handler emits `Connection terminated unexpectedly` as an `error`
11
+ * event. An `error` event with no listener is an uncaught exception: the API
12
+ * process dies.
13
+ *
14
+ * The listener only keeps the process alive. The caller still learns of the
15
+ * failure, because the in-flight query rejects and the dead client is
16
+ * destroyed when it is released.
17
+ *
18
+ * Attached on `acquire`, detached on `release`, so an idle client's error is
19
+ * reported once — by the pool's own `error` listener — not twice.
20
+ */
21
+ export declare function listenForCheckedOutClientErrors(pool: Pool, onError: (error: Error) => void): void;
22
+ //# sourceMappingURL=checked-out-client-errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checked-out-client-errors.d.ts","sourceRoot":"","sources":["../../src/lib/checked-out-client-errors.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAc,MAAM,IAAI,CAAC;AAE3C;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,+BAA+B,CAC7C,IAAI,EAAE,IAAI,EACV,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,GAC9B,IAAI,CAON"}
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Gives every client the pool hands out an `error` listener for as long as
3
+ * it is checked out.
4
+ *
5
+ * pg-pool listens for `error` only on IDLE clients. Once a client is checked
6
+ * out — a Drizzle transaction, the owner lock's session, the audit-actor
7
+ * stamp inside `pool.query` — nothing listens, and when the server drops the
8
+ * connection (restart, failover, `pg_terminate_backend`) the client's
9
+ * `'end'` handler emits `Connection terminated unexpectedly` as an `error`
10
+ * event. An `error` event with no listener is an uncaught exception: the API
11
+ * process dies.
12
+ *
13
+ * The listener only keeps the process alive. The caller still learns of the
14
+ * failure, because the in-flight query rejects and the dead client is
15
+ * destroyed when it is released.
16
+ *
17
+ * Attached on `acquire`, detached on `release`, so an idle client's error is
18
+ * reported once — by the pool's own `error` listener — not twice.
19
+ */
20
+ export function listenForCheckedOutClientErrors(pool, onError) {
21
+ pool.on("acquire", (client) => {
22
+ client.on("error", onError);
23
+ });
24
+ pool.on("release", (_error, client) => {
25
+ client.removeListener("error", onError);
26
+ });
27
+ }
28
+ //# sourceMappingURL=checked-out-client-errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checked-out-client-errors.js","sourceRoot":"","sources":["../../src/lib/checked-out-client-errors.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,+BAA+B,CAC7C,IAAU,EACV,OAA+B;IAE/B,IAAI,CAAC,EAAE,CAAC,SAAS,EAAE,CAAC,MAAkB,EAAE,EAAE;QACxC,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAC9B,CAAC,CAAC,CAAC;IACH,IAAI,CAAC,EAAE,CAAC,SAAS,EAAE,CAAC,MAAa,EAAE,MAAkB,EAAE,EAAE;QACvD,MAAM,CAAC,cAAc,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAC1C,CAAC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,16 @@
1
+ import { Client } from "pg";
2
+ /** The operations used on an already connected Postgres session. */
3
+ export type PostgresClient = {
4
+ readonly query: Client["query"];
5
+ end(): Promise<void>;
6
+ };
7
+ export type { QueryConfig as PostgresQueryConfig, QueryResult as PostgresQueryResult, QueryResultRow as PostgresQueryResultRow, } from "pg";
8
+ /**
9
+ * A connected client outside any pool, for work that holds session state for
10
+ * its whole run (an advisory lock, a migration journal, a schema rehearsal).
11
+ * The caller ends it. Kept here so the postgres adapter is the one package
12
+ * that opens the driver. The factory returns the native client because the
13
+ * legacy migration runner needs to hand the same session to node-pg-migrate.
14
+ */
15
+ export declare function connectPostgresClient(connectionString: string): Promise<Client>;
16
+ //# sourceMappingURL=connect-postgres-client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connect-postgres-client.d.ts","sourceRoot":"","sources":["../../src/lib/connect-postgres-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AAE5B,oEAAoE;AACpE,MAAM,MAAM,cAAc,GAAG;IAC3B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC;IAChC,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACtB,CAAC;AAEF,YAAY,EACV,WAAW,IAAI,mBAAmB,EAClC,WAAW,IAAI,mBAAmB,EAClC,cAAc,IAAI,sBAAsB,GACzC,MAAM,IAAI,CAAC;AAEZ;;;;;;GAMG;AACH,wBAAsB,qBAAqB,CACzC,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,MAAM,CAAC,CAIjB"}
@@ -0,0 +1,14 @@
1
+ import { Client } from "pg";
2
+ /**
3
+ * A connected client outside any pool, for work that holds session state for
4
+ * its whole run (an advisory lock, a migration journal, a schema rehearsal).
5
+ * The caller ends it. Kept here so the postgres adapter is the one package
6
+ * that opens the driver. The factory returns the native client because the
7
+ * legacy migration runner needs to hand the same session to node-pg-migrate.
8
+ */
9
+ export async function connectPostgresClient(connectionString) {
10
+ const client = new Client({ connectionString });
11
+ await client.connect();
12
+ return client;
13
+ }
14
+ //# sourceMappingURL=connect-postgres-client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connect-postgres-client.js","sourceRoot":"","sources":["../../src/lib/connect-postgres-client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,IAAI,CAAC;AAc5B;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CACzC,gBAAwB;IAExB,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,EAAE,gBAAgB,EAAE,CAAC,CAAC;IAChD,MAAM,MAAM,CAAC,OAAO,EAAE,CAAC;IACvB,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Counting the statements a request actually issues.
3
+ *
4
+ * `request_log.duration_ms` has never said whether 900ms was three slow
5
+ * queries or three hundred fast ones. This installs the counter at the one
6
+ * place every statement passes through — the checked-out client — so the
7
+ * answer arrives without a single call site changing.
8
+ *
9
+ * Three things about `pg` make this less obvious than it looks:
10
+ *
11
+ * - **`pool.query` uses the CALLBACK form.** `pg-pool` dispatches a plain
12
+ * statement as `client.query(text, values, callback)` and hands back its own
13
+ * promise, so a wrapper that only understood promises would silently stop
14
+ * counting the single-statement path — which is most of this codebase, since
15
+ * Drizzle issues its non-transactional queries that way.
16
+ * - **A client outlives its checkout.** `pg` returns the same client object to
17
+ * the pool and hands it out again, so instrumenting on every checkout would
18
+ * wrap the wrapper and count one statement twice, then four times. The
19
+ * `WeakSet` below makes it once per client, for the life of that client.
20
+ * - **`query` is overloaded eight ways** — text, config, values, callback,
21
+ * submittable. Rather than re-implement pg's overload resolution (and get it
22
+ * wrong the first time pg adds a shape), the wrapper forwards whatever it
23
+ * was given, untouched, and only decides how to learn that the statement
24
+ * finished. `Object.defineProperty` installs it for the same reason: it
25
+ * replaces the method without having to restate a signature this module has
26
+ * no business owning.
27
+ *
28
+ * The actor stamp is deliberately NOT counted: it is the pool's own cost, not
29
+ * the request's, and it is issued before this wrapper is installed.
30
+ */
31
+ /**
32
+ * Installs the statement counter on `client`, once. Safe to call on every
33
+ * checkout — the second call and every one after it do nothing.
34
+ *
35
+ * Takes any object so it can be proved against a hand-written client with no
36
+ * database in sight, and does nothing to one that has no `query` to replace.
37
+ */
38
+ export declare function countStatementsOn(client: object): void;
39
+ //# sourceMappingURL=counting-client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"counting-client.d.ts","sourceRoot":"","sources":["../../src/lib/counting-client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AA4BH;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAYtD"}