@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
@@ -0,0 +1,241 @@
1
+ import { withDatabase } from "./with-database.js";
2
+ import { PostgreSqlContainer } from "@testcontainers/postgresql";
3
+ import { execFile } from "node:child_process";
4
+ import { randomUUID } from "node:crypto";
5
+ import { existsSync, readFileSync } from "node:fs";
6
+ import { dirname, join } from "node:path";
7
+ import { promisify } from "node:util";
8
+ import { Client } from "pg";
9
+ import { createTestPool } from "./create-test-pool.js";
10
+ import { recordRetiredDatabaseTouches } from "./global-setup/retired-database-touches.js";
11
+ import { privateDatabaseName, refuseUnverifiableSchemaKeyedRun, TEST_DATABASE_RUN_PREFIX, } from "./global-setup/verify-schema-keyed-suite.js";
12
+ import { postgresStorageSettings, testPostgresStorage, withPostgresMemoryLimit, withPostgresStorage, } from "./test-postgres-storage.js";
13
+ const execFileAsync = promisify(execFile);
14
+ /** Set by `global-setup/index.ts` or `scripts/with-shared-test-postgres.ts`. */
15
+ export const SHARED_TEST_POSTGRES_URL = "SHARED_TEST_POSTGRES_URL";
16
+ /** Escape hatch: force the old boot-per-file path (the nightly honesty job). */
17
+ export const CONTAINER_PER_FILE = "CONTAINER_PER_FILE";
18
+ /** The migrated database every per-file database is copied from. */
19
+ export const TEMPLATE_DATABASE = "dads_template";
20
+ /**
21
+ * A fully-migrated Postgres for one test file.
22
+ *
23
+ * The PROCESS is shared; the DATA never is. One container per Nx integration
24
+ * run migrates ONCE into a template database. Each caller then gets a private
25
+ * database cloned from that migrated template in milliseconds instead of a
26
+ * container boot plus 21 migrations. Suites still truncate
27
+ * between tests via `truncateTables`, exactly as before, so every suite is as
28
+ * isolated as it was when each one owned a container (docs/testing/speed.md).
29
+ *
30
+ * Falls back to booting a container per file when the shared one is absent —
31
+ * which is what `CONTAINER_PER_FILE=1` forces, and what the nightly job runs
32
+ * to prove the shared path has not started hiding cross-file leakage.
33
+ */
34
+ export async function startTestPostgres(options = {}) {
35
+ const shared = process.env[SHARED_TEST_POSTGRES_URL];
36
+ if (shared !== undefined && process.env[CONTAINER_PER_FILE] !== "1") {
37
+ return databaseFromTemplate(shared, options);
38
+ }
39
+ return bootOwnContainer(options);
40
+ }
41
+ /**
42
+ * A migrated Postgres whose pool stamps NOTHING, so a write that reaches the
43
+ * `audit_record` trigger arrives unattributed and is refused. Exactly one
44
+ * suite should want this: the one proving the refusal is real.
45
+ */
46
+ export async function startUnattributedTestPostgres() {
47
+ return startTestPostgres({ attributed: false });
48
+ }
49
+ /** A private empty database for tests that must prove the migration process itself. */
50
+ export async function startUnmigratedTestPostgres(options = {}) {
51
+ const shared = process.env[SHARED_TEST_POSTGRES_URL];
52
+ if (shared !== undefined && process.env[CONTAINER_PER_FILE] !== "1") {
53
+ return databaseWithoutTemplate(shared, options);
54
+ }
55
+ return bootOwnContainerWithMigration(false, options);
56
+ }
57
+ /** Per-file honesty path: one container, migrated in place, owned by this file. */
58
+ async function bootOwnContainer(options) {
59
+ return bootOwnContainerWithMigration(true, options);
60
+ }
61
+ async function bootOwnContainerWithMigration(applyMigrations, options) {
62
+ refuseUnverifiableSchemaKeyedRun({
63
+ env: process.env,
64
+ containerPerFile: process.env[CONTAINER_PER_FILE] === "1",
65
+ workspaceRoot: findWorkspaceRoot(),
66
+ });
67
+ const storage = testPostgresStorage();
68
+ const container = await withPostgresMemoryLimit(withPostgresStorage(new PostgreSqlContainer("postgres:16-alpine")
69
+ .withDatabase("dads_test")
70
+ .withCommand(["postgres", ...postgresStorageSettings(storage)]), storage), storage).start();
71
+ const connectionString = container.getConnectionUri();
72
+ const pool = createTestPool(connectionString, options);
73
+ if (applyMigrations) {
74
+ await applyCheckedInSchema(connectionString);
75
+ }
76
+ return {
77
+ pool,
78
+ connectionString,
79
+ stop: async () => {
80
+ await pool.end();
81
+ await container.stop();
82
+ },
83
+ };
84
+ }
85
+ /** Clone the migrated template into a database only this caller uses. */
86
+ async function databaseFromTemplate(sharedUrl, options) {
87
+ return createPrivateDatabase(sharedUrl, options, TEMPLATE_DATABASE);
88
+ }
89
+ async function databaseWithoutTemplate(sharedUrl, options) {
90
+ return createPrivateDatabase(sharedUrl, options);
91
+ }
92
+ export async function createPrivateDatabase(sharedUrl, options, template) {
93
+ // Underscores and a hex tail: a legal unquoted identifier, and unique even
94
+ // when several files clone at the same instant. The run prefix lets this
95
+ // process's teardown find its own databases among every project's in the
96
+ // shared container (verify-schema-keyed-suite.ts).
97
+ const name = privateDatabaseName(process.env[TEST_DATABASE_RUN_PREFIX], template !== undefined, randomUUID().replaceAll("-", ""));
98
+ const admin = createTestPool(adminUrl(sharedUrl));
99
+ try {
100
+ await admin.query(template === undefined
101
+ ? `CREATE DATABASE "${name}"`
102
+ : `CREATE DATABASE "${name}" TEMPLATE "${template}"`);
103
+ }
104
+ finally {
105
+ await admin.end();
106
+ }
107
+ const connectionString = withDatabase(sharedUrl, name);
108
+ const pool = createTestPool(connectionString, options);
109
+ return {
110
+ pool,
111
+ connectionString,
112
+ stop: async () => {
113
+ await pool.end();
114
+ const cleanup = createTestPool(adminUrl(sharedUrl));
115
+ try {
116
+ await retirePrivateDatabase(cleanup, connectionString, name);
117
+ }
118
+ finally {
119
+ await cleanup.end();
120
+ }
121
+ },
122
+ };
123
+ }
124
+ /**
125
+ * Shut a stopped file's database to every connection, and in RAM drop it.
126
+ *
127
+ * Connections are disallowed BEFORE anything is terminated and never allowed
128
+ * again, so a pool the file leaked cannot reconnect between the terminate pass
129
+ * and the DROP. Nothing may still be connected when the DROP ... WITH (FORCE)
130
+ * runs: it kills what it finds with 57P01, and on a connection nobody listens
131
+ * to that is an uncaught error in whichever test file is stopping.
132
+ * On disk the database is kept until the container goes, to avoid
133
+ * DROP's shared checkpoint; in RAM it is dropped now (test-postgres-storage.ts),
134
+ * after the tables it touched are written to the ledger the schema-keyed
135
+ * teardown reads (retired-database-touches.ts). That read needs a connection
136
+ * to the database, so in RAM the one reader connects first and is spared by
137
+ * the terminate pass.
138
+ */
139
+ async function retirePrivateDatabase(admin, connectionString, name) {
140
+ const dropNow = await serverKeepsDataInMemory(admin);
141
+ const statsReader = dropNow
142
+ ? await connectStatsReader(connectionString)
143
+ : undefined;
144
+ try {
145
+ await admin.query(`ALTER DATABASE "${name}" ALLOW_CONNECTIONS false`);
146
+ // Client backends only. Autovacuum workers ignore ALLOW_CONNECTIONS
147
+ // (they must reach template0), so the launcher can start one here
148
+ // between the terminate pass and the check below — which failed the
149
+ // nightly. A worker vacuuming a database nothing reuses leaks no data.
150
+ const spared = statsReader === undefined ? [] : [statsReader.pid];
151
+ await admin.query("SELECT pg_terminate_backend(pid, 5000) FROM pg_stat_activity WHERE datname = $1 AND backend_type = 'client backend' AND pid <> ALL($2::int[])", [name, spared]);
152
+ const remaining = await admin.query("SELECT pid FROM pg_stat_activity WHERE datname = $1 AND backend_type = 'client backend' AND pid <> ALL($2::int[])", [name, spared]);
153
+ if (remaining.rows.length > 0) {
154
+ throw new Error(`Test database ${name} still has active connections after termination`);
155
+ }
156
+ if (statsReader !== undefined) {
157
+ // A terminated backend publishes its table counters as it exits, and
158
+ // pg_terminate_backend waited for that exit, so the read sees them.
159
+ await recordRetiredDatabaseTouches(admin, statsReader.client, name);
160
+ }
161
+ }
162
+ finally {
163
+ // Awaited to the closed socket — unlike Pool.end(), which resolves while
164
+ // its clients are still closing — so the DROP never meets this backend.
165
+ await statsReader?.client.end();
166
+ }
167
+ if (dropNow) {
168
+ await admin.query(`DROP DATABASE "${name}" WITH (FORCE)`);
169
+ }
170
+ }
171
+ /** The one connection that reads a database's table counters before its drop. */
172
+ async function connectStatsReader(connectionString) {
173
+ const client = new Client({ connectionString });
174
+ // A failure surfaces as the rejected query; an `error` event with no
175
+ // listener would instead crash the test worker.
176
+ client.on("error", () => undefined);
177
+ await client.connect();
178
+ const result = await client.query("SELECT pg_backend_pid() AS pid");
179
+ const pid = result.rows[0]?.pid;
180
+ if (pid === undefined) {
181
+ await client.end();
182
+ throw new Error("Expected the stats reader's backend pid");
183
+ }
184
+ return { client, pid };
185
+ }
186
+ /**
187
+ * Whether the server was started with `TEST_POSTGRES_STORAGE=ram`. Asked of
188
+ * the server, not this process's env: a run that attaches to a shared
189
+ * container another run started gets that container's storage, and dropping
190
+ * on a disk-backed server would pay the checkpoint the default avoids.
191
+ */
192
+ async function serverKeepsDataInMemory(admin) {
193
+ const result = await admin.query("SELECT current_setting('fsync') AS fsync");
194
+ return result.rows[0]?.fsync === "off";
195
+ }
196
+ /** Same server, `postgres` database — the administrative connection. */
197
+ function adminUrl(url) {
198
+ return withDatabase(url, "postgres");
199
+ }
200
+ /** `@dads-framework/migrations`' runner, run from source so no build is needed. */
201
+ const MIGRATE_BIN = "packages/server/migrations/src/bin/migrate.ts";
202
+ export async function applyCheckedInSchema(databaseUrl) {
203
+ const workspaceRoot = findWorkspaceRoot();
204
+ await execFileAsync(process.execPath, ["--import", "tsx", join(workspaceRoot, MIGRATE_BIN)], {
205
+ cwd: workspaceRoot,
206
+ env: {
207
+ ...process.env,
208
+ DATABASE_URL: databaseUrl,
209
+ WORKSPACE_ROOT: workspaceRoot,
210
+ },
211
+ });
212
+ }
213
+ /**
214
+ * The root of the checkout this process runs in: the nearest `nx.json`, or the
215
+ * nearest `package.json` that declares `workspaces`.
216
+ */
217
+ export function findWorkspaceRoot() {
218
+ // Vitest runs with cwd at the project root or workspace root; walk up.
219
+ let dir = process.cwd();
220
+ let parent = dirname(dir);
221
+ while (parent !== dir) {
222
+ if (isWorkspaceRoot(dir))
223
+ return dir;
224
+ dir = parent;
225
+ parent = dirname(dir);
226
+ }
227
+ if (isWorkspaceRoot(dir)) {
228
+ return dir;
229
+ }
230
+ throw new Error("Unable to locate the workspace root");
231
+ }
232
+ function isWorkspaceRoot(dir) {
233
+ if (existsSync(join(dir, "nx.json")))
234
+ return true;
235
+ const manifest = join(dir, "package.json");
236
+ if (!existsSync(manifest))
237
+ return false;
238
+ const parsed = JSON.parse(readFileSync(manifest, "utf8"));
239
+ return (typeof parsed === "object" && parsed !== null && "workspaces" in parsed);
240
+ }
241
+ //# sourceMappingURL=start-test-postgres.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"start-test-postgres.js","sourceRoot":"","sources":["../../src/testing/start-test-postgres.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AACjE,OAAO,EAAE,QAAQ,EAAE,MAAM,oB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zD,CAAC;AAED,SAAS,eAAe,CAAC,GAAW;IAClC,IAAI,UAAU,CAAC,IAAI,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;QAAE,OAAO,IAAI,CAAC;IAClD,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,EAAE,cAAc,CAAC,CAAC;IAC3C,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC;QAAE,OAAO,KAAK,CAAC;IACxC,MAAM,MAAM,GAAY,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC,CAAC;IACnE,OAAO,CACL,OAAO,MAAM,KAAK,QAAQ,IAAI,MAAM,KAAK,IAAI,IAAI,YAAY,IAAI,MAAM,CACxE,CAAC;AACJ,CAAC"}
@@ -0,0 +1,44 @@
1
+ import type { PostgreSqlContainer } from "@testcontainers/postgresql";
2
+ /**
3
+ * Where test Postgres keeps its data: `disk` (the default) or `ram`.
4
+ *
5
+ * `ram` mounts the data directory on a size-capped tmpfs and turns off
6
+ * durability (fsync, synchronous commit, full-page writes) — test data is
7
+ * thrown away, so a crash losing it costs nothing. Because RAM is scarce,
8
+ * `ram` also drops each private database as soon as its file stops, where
9
+ * `disk` keeps it until the container goes (DROP DATABASE forces a
10
+ * checkpoint, which is slow on disk and nearly free without fsync).
11
+ *
12
+ * Opt in on machines with memory to spare (CI runners, the Proxmox test VM);
13
+ * a laptop running many suites at once stays on `disk` by default.
14
+ */
15
+ export declare const TEST_POSTGRES_STORAGE = "TEST_POSTGRES_STORAGE";
16
+ export type TestPostgresStorage = "disk" | "ram";
17
+ /**
18
+ * Ceiling on one container's in-memory data directory. Past it Postgres fails
19
+ * the write with "No space left on device" instead of the machine running out
20
+ * of memory and killing an unrelated process.
21
+ */
22
+ export declare const RAM_DATA_DIRECTORY_GB = 4;
23
+ export declare const RAM_DATA_DIRECTORY_LIMIT: string;
24
+ /**
25
+ * Memory ceiling on a test Postgres container's own processes, in GB, before
26
+ * the tmpfs. A runaway container is then killed by Docker on its own instead
27
+ * of taking the machine (or the Docker VM, and every container in it) down.
28
+ */
29
+ export declare const TEST_POSTGRES_MEMORY_GB = "DADS_TEST_POSTGRES_MEMORY_GB";
30
+ export declare function testPostgresStorage(env?: Readonly<Record<string, string | undefined>>): TestPostgresStorage;
31
+ /** Extra `postgres -c` settings for the chosen storage. */
32
+ export declare function postgresStorageSettings(storage: TestPostgresStorage): readonly string[];
33
+ /** Mount the data directory in memory when the storage is `ram`. */
34
+ export declare function withPostgresStorage(container: PostgreSqlContainer, storage: TestPostgresStorage): PostgreSqlContainer;
35
+ /**
36
+ * The container's memory limit for `storage`, in GB. In `ram` the tmpfs is
37
+ * charged to the container too, so the limit is the process ceiling plus the
38
+ * whole tmpfs — a full tmpfs then fails a write with "No space left on device"
39
+ * rather than the container being killed.
40
+ */
41
+ export declare function testPostgresMemoryLimitGb(storage: TestPostgresStorage, env?: Readonly<Record<string, string | undefined>>): number;
42
+ /** Cap the container's memory at `testPostgresMemoryLimitGb(storage)`. */
43
+ export declare function withPostgresMemoryLimit(container: PostgreSqlContainer, storage: TestPostgresStorage): PostgreSqlContainer;
44
+ //# sourceMappingURL=test-postgres-storage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test-postgres-storage.d.ts","sourceRoot":"","sources":["../../src/testing/test-postgres-storage.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAEtE;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,qBAAqB,0BAA0B,CAAC;AAE7D,MAAM,MAAM,mBAAmB,GAAG,MAAM,GAAG,KAAK,CAAC;AAEjD;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,IAAI,CAAC;AACvC,eAAO,MAAM,wBAAwB,QAAsC,CAAC;AAE5E;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,iCAAiC,CAAC;AAatE,wBAAgB,mBAAmB,CACjC,GAAG,GAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAe,GAC9D,mBAAmB,CAOrB;AAED,2DAA2D;AAC3D,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,mBAAmB,GAC3B,SAAS,MAAM,EAAE,CAUnB;AAED,oEAAoE;AACpE,wBAAgB,mBAAmB,CACjC,SAAS,EAAE,mBAAmB,EAC9B,OAAO,EAAE,mBAAmB,GAC3B,mBAAmB,CAKrB;AAED;;;;;GAKG;AACH,wBAAgB,yBAAyB,CACvC,OAAO,EAAE,mBAAmB,EAC5B,GAAG,GAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAe,GAC9D,MAAM,CAYR;AAED,0EAA0E;AAC1E,wBAAgB,uBAAuB,CACrC,SAAS,EAAE,mBAAmB,EAC9B,OAAO,EAAE,mBAAmB,GAC3B,mBAAmB,CAIrB"}
@@ -0,0 +1,88 @@
1
+ /**
2
+ * Where test Postgres keeps its data: `disk` (the default) or `ram`.
3
+ *
4
+ * `ram` mounts the data directory on a size-capped tmpfs and turns off
5
+ * durability (fsync, synchronous commit, full-page writes) — test data is
6
+ * thrown away, so a crash losing it costs nothing. Because RAM is scarce,
7
+ * `ram` also drops each private database as soon as its file stops, where
8
+ * `disk` keeps it until the container goes (DROP DATABASE forces a
9
+ * checkpoint, which is slow on disk and nearly free without fsync).
10
+ *
11
+ * Opt in on machines with memory to spare (CI runners, the Proxmox test VM);
12
+ * a laptop running many suites at once stays on `disk` by default.
13
+ */
14
+ export const TEST_POSTGRES_STORAGE = "TEST_POSTGRES_STORAGE";
15
+ /**
16
+ * Ceiling on one container's in-memory data directory. Past it Postgres fails
17
+ * the write with "No space left on device" instead of the machine running out
18
+ * of memory and killing an unrelated process.
19
+ */
20
+ export const RAM_DATA_DIRECTORY_GB = 4;
21
+ export const RAM_DATA_DIRECTORY_LIMIT = `${String(RAM_DATA_DIRECTORY_GB)}g`;
22
+ /**
23
+ * Memory ceiling on a test Postgres container's own processes, in GB, before
24
+ * the tmpfs. A runaway container is then killed by Docker on its own instead
25
+ * of taking the machine (or the Docker VM, and every container in it) down.
26
+ */
27
+ export const TEST_POSTGRES_MEMORY_GB = "DADS_TEST_POSTGRES_MEMORY_GB";
28
+ /**
29
+ * Measured 2026-11-18 on the test VM across 20 test Postgres containers (shared
30
+ * and per-file, disk and ram, beside other checkouts' runs): the largest held
31
+ * 1.3 GB of resident memory and shared buffers, most under 0.6 GB. The limit
32
+ * is more than twice the largest, so a normal run should never trip it
33
+ * (not yet measured over a whole gate run).
34
+ */
35
+ const DEFAULT_POSTGRES_MEMORY_GB = 3;
36
+ const POSTGRES_DATA_DIRECTORY = "/var/lib/postgresql/data";
37
+ export function testPostgresStorage(env = process.env) {
38
+ const value = env[TEST_POSTGRES_STORAGE];
39
+ if (value === undefined || value === "" || value === "disk")
40
+ return "disk";
41
+ if (value === "ram")
42
+ return "ram";
43
+ throw new Error(`${TEST_POSTGRES_STORAGE} must be "disk" or "ram", not "${value}".`);
44
+ }
45
+ /** Extra `postgres -c` settings for the chosen storage. */
46
+ export function postgresStorageSettings(storage) {
47
+ if (storage === "disk")
48
+ return [];
49
+ return [
50
+ "-c",
51
+ "fsync=off",
52
+ "-c",
53
+ "synchronous_commit=off",
54
+ "-c",
55
+ "full_page_writes=off",
56
+ ];
57
+ }
58
+ /** Mount the data directory in memory when the storage is `ram`. */
59
+ export function withPostgresStorage(container, storage) {
60
+ if (storage === "disk")
61
+ return container;
62
+ return container.withTmpFs({
63
+ [POSTGRES_DATA_DIRECTORY]: `rw,size=${RAM_DATA_DIRECTORY_LIMIT}`,
64
+ });
65
+ }
66
+ /**
67
+ * The container's memory limit for `storage`, in GB. In `ram` the tmpfs is
68
+ * charged to the container too, so the limit is the process ceiling plus the
69
+ * whole tmpfs — a full tmpfs then fails a write with "No space left on device"
70
+ * rather than the container being killed.
71
+ */
72
+ export function testPostgresMemoryLimitGb(storage, env = process.env) {
73
+ const raw = env[TEST_POSTGRES_MEMORY_GB];
74
+ const processGb = raw === undefined || raw.trim() === ""
75
+ ? DEFAULT_POSTGRES_MEMORY_GB
76
+ : Number(raw);
77
+ if (!Number.isFinite(processGb) || processGb <= 0) {
78
+ throw new Error(`${TEST_POSTGRES_MEMORY_GB} must be a number of GB above 0 — got "${String(raw)}"`);
79
+ }
80
+ return storage === "ram" ? processGb + RAM_DATA_DIRECTORY_GB : processGb;
81
+ }
82
+ /** Cap the container's memory at `testPostgresMemoryLimitGb(storage)`. */
83
+ export function withPostgresMemoryLimit(container, storage) {
84
+ return container.withResourcesQuota({
85
+ memory: testPostgresMemoryLimitGb(storage),
86
+ });
87
+ }
88
+ //# sourceMappingURL=test-postgres-storage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"test-postgres-storage.js","sourceRoot":"","sources":["../../src/testing/test-postgres-storage.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,uBAAuB,CAAC;AAI7D;;;;GAIG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,CAAC,CAAC;AACvC,MAAM,CAAC,MAAM,wBAAwB,GAAG,GAAG,MAAM,CAAC,qBAAqB,CAAC,GAAG,CAAC;AAE5E;;;;GAIG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,8BAA8B,CAAC;AAEtE;;;;;;GAMG;AACH,MAAM,0BAA0B,GAAG,CAAC,CAAC;AAErC,MAAM,uBAAuB,GAAG,0BAA0B,CAAC;AAE3D,MAAM,UAAU,mBAAmB,CACjC,GAAG,GAAiD,OAAO,CAAC,GAAG;IAE/D,MAAM,KAAK,GAAG,GAAG,CAAC,qBAAqB,CAAC,CAAC;IACzC,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,EAAE,IAAI,KAAK,KAAK,MAAM;QAAE,OAAO,MAAM,CAAC;IAC3E,IAAI,KAAK,KAAK,KAAK;QAAE,OAAO,KAAK,CAAC;IAClC,MAAM,IA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,SAAS,CAAC,kBAAkB,CAAC;QAClC,MAAM,EAAE,yBAAyB,CAAC,OAAO,CAAC;KAC3C,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,31 @@
1
+ import type { Pool } from "pg";
2
+ /**
3
+ * Truncate-based reset between tests (docs/testing/seeders.md#speed) —
4
+ * beats re-creating schemas or containers per test. Pass the domain tables
5
+ * the suite touches; identity/session state usually rides along via CASCADE.
6
+ */
7
+ export declare function truncateTables(pool: Pool, tableNames: readonly string[]): Promise<void>;
8
+ /**
9
+ * Clears the audit trail between tests.
10
+ *
11
+ * Kept out of the list callers pass to `truncateTables` because almost no
12
+ * suite should care: `audit_log` grows with every insert a seeder makes, and
13
+ * leaving it alone costs nothing. A suite that ASSERTS on audit rows is the
14
+ * exception, and it wants an empty table with `position` back at 1 so it can
15
+ * read the rows it caused in the order it caused them.
16
+ *
17
+ * `TRUNCATE` fires no row triggers, so emptying the trail records nothing and
18
+ * the immutability trigger — which guards UPDATE and DELETE — does not object.
19
+ */
20
+ export declare function truncateAuditLog(pool: Pool): Promise<void>;
21
+ /**
22
+ * Clears the request trail between tests.
23
+ *
24
+ * Separate from `truncateAuditLog` because the two tables answer different
25
+ * questions and are kept on different terms: `request_log` is pruned on a
26
+ * schedule and carries no trigger, `audit_log` is never pruned and carries
27
+ * the immutability one. A suite asserting on request rows wants only its own,
28
+ * and nothing else in the schema references them, so this is a plain truncate.
29
+ */
30
+ export declare function truncateRequestLog(pool: Pool): Promise<void>;
31
+ //# sourceMappingURL=truncate-tables.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"truncate-tables.d.ts","sourceRoot":"","sources":["../../src/testing/truncate-tables.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,IAAI,CAAC;AAE/B;;;;GAIG;AACH,wBAAsB,cAAc,CAClC,IAAI,EAAE,IAAI,EACV,UAAU,EAAE,SAAS,MAAM,EAAE,GAC5B,OAAO,CAAC,IAAI,CAAC,CAIf;AAED;;;;;;;;;;;GAWG;AACH,wBAAsB,gBAAgB,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAEhE;AAED;;;;;;;;GAQG;AACH,wBAAsB,kBAAkB,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAElE"}
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Truncate-based reset between tests (docs/testing/seeders.md#speed) —
3
+ * beats re-creating schemas or containers per test. Pass the domain tables
4
+ * the suite touches; identity/session state usually rides along via CASCADE.
5
+ */
6
+ export async function truncateTables(pool, tableNames) {
7
+ if (tableNames.length === 0)
8
+ return;
9
+ const quoted = tableNames.map((name) => `"${name}"`).join(", ");
10
+ await pool.query(`TRUNCATE TABLE ${quoted} RESTART IDENTITY CASCADE`);
11
+ }
12
+ /**
13
+ * Clears the audit trail between tests.
14
+ *
15
+ * Kept out of the list callers pass to `truncateTables` because almost no
16
+ * suite should care: `audit_log` grows with every insert a seeder makes, and
17
+ * leaving it alone costs nothing. A suite that ASSERTS on audit rows is the
18
+ * exception, and it wants an empty table with `position` back at 1 so it can
19
+ * read the rows it caused in the order it caused them.
20
+ *
21
+ * `TRUNCATE` fires no row triggers, so emptying the trail records nothing and
22
+ * the immutability trigger — which guards UPDATE and DELETE — does not object.
23
+ */
24
+ export async function truncateAuditLog(pool) {
25
+ await truncateTables(pool, ["audit_log"]);
26
+ }
27
+ /**
28
+ * Clears the request trail between tests.
29
+ *
30
+ * Separate from `truncateAuditLog` because the two tables answer different
31
+ * questions and are kept on different terms: `request_log` is pruned on a
32
+ * schedule and carries no trigger, `audit_log` is never pruned and carries
33
+ * the immutability one. A suite asserting on request rows wants only its own,
34
+ * and nothing else in the schema references them, so this is a plain truncate.
35
+ */
36
+ export async function truncateRequestLog(pool) {
37
+ await truncateTables(pool, ["request_log"]);
38
+ }
39
+ //# sourceMappingURL=truncate-tables.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"truncate-tables.js","sourceRoot":"","sources":["../../src/testing/truncate-tables.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,IAAU,EACV,UAA6B;IAE7B,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO;IACpC,MAAM,MAAM,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAChE,MAAM,IAAI,CAAC,KAAK,CAAC,kBAAkB,MAAM,2BAA2B,CAAC,CAAC;AACxE,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAAC,IAAU;IAC/C,MAAM,cAAc,CAAC,IAAI,EAAE,CAAC,WAAW,CAAC,CAAC,CAAC;AAC5C,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CAAC,IAAU;IACjD,MAAM,cAAc,CAAC,IAAI,EAAE,CAAC,aAAa,CAAC,CAAC,CAAC;AAC9C,CAAC"}
@@ -0,0 +1,14 @@
1
+ export type ForeignKeyRefusal = {
2
+ /** The table the refused row was being written to. */
3
+ readonly childTable: string;
4
+ /** The table the referenced key was missing from. */
5
+ readonly parentTable: string;
6
+ /** As Postgres stores it — truncated to 63 characters when longer. */
7
+ readonly constraint: string;
8
+ };
9
+ /**
10
+ * Awaits a write expected to be refused and describes the refusal. Throws if
11
+ * the write succeeded, or failed for any reason other than a foreign key.
12
+ */
13
+ export declare function violatedForeignKey(write: PromiseLike<unknown>): Promise<ForeignKeyRefusal>;
14
+ //# sourceMappingURL=violated-foreign-key.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"violated-foreign-key.d.ts","sourceRoot":"","sources":["../../src/testing/violated-foreign-key.ts"],"names":[],"mappings":"AA+BA,MAAM,MAAM,iBAAiB,GAAG;IAC9B,sDAAsD;IACtD,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,qDAAqD;IACrD,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,sEAAsE;IACtE,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B,CAAC;AAEF;;;GAGG;AACH,wBAAsB,kBAAkB,CACtC,KAAK,EAAE,WAAW,CAAC,OAAO,CAAC,GAC1B,OAAO,CAAC,iBAAiB,CAAC,CAW5B"}
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Describe the foreign key a write violated, for tests that prove a reference
3
+ * is database-enforced rather than merely intended.
4
+ *
5
+ * Two things make the obvious assertion wrong, and both fail misleadingly.
6
+ *
7
+ * `rejects.toThrow(/foreign key/i)` does not work: drizzle catches the driver
8
+ * error and rethrows its own, whose message is only `Failed query: insert into
9
+ * …`. The assertion then fails against a database that refused the write
10
+ * exactly as it should have, and the reader concludes the constraint is
11
+ * missing. So the driver's error is unwrapped from the `cause` chain and
12
+ * matched on SQLSTATE `23503` (`foreign_key_violation`) — a code, not prose.
13
+ *
14
+ * Asserting the constraint NAME does not work either, and this one is a trap
15
+ * worth naming: Postgres identifiers are capped at 63 characters, and drizzle's
16
+ * generated name for, say, `order_facility_authorizations.order_id` against
17
+ * `orders` runs well past it. Postgres truncates it SILENTLY, so the
18
+ * stored name ends mid-word and a test asserting the full name fails with two
19
+ * strings that look identical in the diff.
20
+ *
21
+ * So the tables are reported instead — which child table refused the row, and
22
+ * which parent it could not find. That is the relationship the test is actually
23
+ * about, it survives truncation, and it reads as the rule rather than as a
24
+ * string. The truncated `constraint` comes along for anyone who needs to find
25
+ * the constraint in `pg_constraint`.
26
+ */
27
+ const FOREIGN_KEY_VIOLATION = "23503";
28
+ /** Postgres phrasing: `Key (facility_id)=(…) is not present in table "x".` */
29
+ const PARENT_TABLE = /is not present in table "([^"]+)"/;
30
+ /**
31
+ * Awaits a write expected to be refused and describes the refusal. Throws if
32
+ * the write succeeded, or failed for any reason other than a foreign key.
33
+ */
34
+ export async function violatedForeignKey(write) {
35
+ try {
36
+ await write;
37
+ }
38
+ catch (error) {
39
+ const refusal = foreignKeyRefusalIn(error);
40
+ if (refusal !== undefined)
41
+ return refusal;
42
+ throw error;
43
+ }
44
+ throw new Error("expected the write to be refused by a foreign key, but it was accepted");
45
+ }
46
+ /** Walks the `cause` chain — drizzle wraps the driver error one level down. */
47
+ function foreignKeyRefusalIn(error) {
48
+ let current = error;
49
+ while (current !== null && current !== undefined) {
50
+ const refusal = readRefusal(current);
51
+ if (refusal !== undefined)
52
+ return refusal;
53
+ current = current instanceof Error ? current.cause : undefined;
54
+ }
55
+ return undefined;
56
+ }
57
+ function readRefusal(value) {
58
+ if (readStringField(value, "code") !== FOREIGN_KEY_VIOLATION)
59
+ return undefined;
60
+ const childTable = readStringField(value, "table");
61
+ const constraint = readStringField(value, "constraint");
62
+ const parentTable = PARENT_TABLE.exec(readStringField(value, "detail") ?? "");
63
+ if (childTable === undefined ||
64
+ constraint === undefined ||
65
+ parentTable?.[1] === undefined) {
66
+ return undefined;
67
+ }
68
+ return { childTable, parentTable: parentTable[1], constraint };
69
+ }
70
+ function readStringField(value, field) {
71
+ if (typeof value !== "object" || value === null)
72
+ return undefined;
73
+ if (!(field in value))
74
+ return undefined;
75
+ const found = Reflect.get(value, field);
76
+ return typeof found === "string" ? found : undefined;
77
+ }
78
+ //# sourceMappingURL=violated-foreign-key.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"violated-foreign-key.js","sourceRoot":"","sources":["../../src/testing/violated-foreign-key.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,qBAAqB,GAAG,OAAO,CAAC;AAEtC,8EAA8E;AAC9E,MAAM,YAAY,GAAG,mCAAmC,CAAC;AAWzD;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,KAA2B;IAE3B,IAAI,CAAC;QACH,MAAM,KAAK,CAAC;IACd,CAA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U,EAAE,CAAC;AACjE,CAAC;AAED,SAAS,eAAe,CAAC,KAAc,EAAE,KAAa;IACpD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI;QAAE,OAAO,SAAS,CAAC;IAClE,IAAI,CAAC,CAAC,KAAK,IAAI,KAAK,CAAC;QAAE,OAAO,SAAS,CAAC;IACxC,MAAM,KAAK,GAAY,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;IACjD,OAAO,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC;AACvD,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare function withDatabase(url: string, database: string): string;
2
+ //# sourceMappingURL=with-database.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"with-database.d.ts","sourceRoot":"","sources":["../../src/testing/with-database.ts"],"names":[],"mappings":"AAAA,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAIlE"}
@@ -0,0 +1,6 @@
1
+ export function withDatabase(url, database) {
2
+ const parsed = new URL(url);
3
+ parsed.pathname = `/${database}`;
4
+ return parsed.toString();
5
+ }
6
+ //# sourceMappingURL=with-database.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"with-database.js","sourceRoot":"","sources":["../../src/testing/with-database.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,YAAY,CAAC,GAAW,EAAE,QAAgB;IACxD,MAAM,MAAM,GAAG,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC;IAC5B,MAAM,CAAC,QAAQ,GAAG,IAAI,QAAQ,EAAE,CAAC;IACjC,OAAO,MAAM,CAAC,QAAQ,EAAE,CAAC;AAC3B,CAAC"}