@owlmeans/postgres 0.1.18-rc.43 → 0.1.18-rc.45

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 (46) hide show
  1. package/README.md +5 -4
  2. package/agent-meta/manifest.json +2 -2
  3. package/agent-meta/skills/postgres/SKILL.md +1 -1
  4. package/build/bootstrap.d.ts.map +1 -1
  5. package/build/bootstrap.js +12 -7
  6. package/build/bootstrap.js.map +1 -1
  7. package/build/index.d.ts +2 -0
  8. package/build/index.d.ts.map +1 -1
  9. package/build/index.js.map +1 -1
  10. package/build/service.d.ts +1 -5
  11. package/build/service.d.ts.map +1 -1
  12. package/build/service.js +18 -9
  13. package/build/service.js.map +1 -1
  14. package/build/types.local.d.ts +5 -0
  15. package/build/types.local.d.ts.map +1 -0
  16. package/build/types.local.js +2 -0
  17. package/build/types.local.js.map +1 -0
  18. package/build/utils/config/types.d.ts +30 -0
  19. package/build/utils/config/types.d.ts.map +1 -0
  20. package/build/utils/config/types.js +2 -0
  21. package/build/utils/config/types.js.map +1 -0
  22. package/build/utils/config.d.ts +4 -24
  23. package/build/utils/config.d.ts.map +1 -1
  24. package/build/utils/config.js +74 -87
  25. package/build/utils/config.js.map +1 -1
  26. package/build/utils/connection/types.d.ts +17 -0
  27. package/build/utils/connection/types.d.ts.map +1 -0
  28. package/build/utils/connection/types.js +2 -0
  29. package/build/utils/connection/types.js.map +1 -0
  30. package/build/utils/connection.d.ts +2 -13
  31. package/build/utils/connection.d.ts.map +1 -1
  32. package/build/utils/connection.js +33 -37
  33. package/build/utils/connection.js.map +1 -1
  34. package/package.json +13 -12
  35. package/src/bootstrap.ts +13 -11
  36. package/src/index.ts +2 -0
  37. package/src/service.ts +23 -20
  38. package/src/types.local.ts +5 -0
  39. package/src/utils/config/types.ts +28 -0
  40. package/src/utils/config.ts +80 -89
  41. package/src/utils/connection/types.ts +17 -0
  42. package/src/utils/connection.ts +36 -36
  43. package/tests/context.ts +3 -3
  44. package/tests/criteria.spec.ts +2 -2
  45. package/tests/migration.spec.ts +3 -3
  46. package/tests/sync.spec.ts +3 -3
@@ -1,8 +1,12 @@
1
- import { pgErrorToResourceError, PostgresConnectionError, quoteIdent } from '@owlmeans/postgres-resource'
1
+ import { pgErrorHelper, pgNameHelper, PostgresConnectionError } from '@owlmeans/postgres-resource'
2
2
  import type { PostgresMeta } from '@owlmeans/postgres-resource'
3
+ import { logger } from '@owlmeans/log'
3
4
  import type { Pool } from 'pg'
4
5
 
5
6
  import { DEF_RETRIES, DEF_RETRY_DELAY, TERMINAL_CONNECT_CODES } from '../consts.js'
7
+ import type { PgConnectionHelper } from './connection/types.js'
8
+
9
+ const log = logger('postgres')
6
10
 
7
11
  /**
8
12
  * Retryable means "the server isn't up yet". A driver level failure carries no `code` at
@@ -18,47 +22,43 @@ const isTransient = (error: unknown): boolean => {
18
22
  return !TERMINAL_CONNECT_CODES.includes(code)
19
23
  }
20
24
 
21
- /**
22
- * Wait for the server to answer a query, not merely to accept a socket.
23
- *
24
- * Postgres binds its port before it finishes recovery, so a sidecar or an operator managed
25
- * instance routinely accepts a connection and then refuses to run anything. Every OwlMeans
26
- * deployment grew its own copy of this loop; this is the one.
27
- *
28
- * @throws {PostgresConnectionError}
29
- */
30
- export const probe = async (pool: Pool, meta: PostgresMeta, location: string): Promise<void> => {
31
- const retries = Math.max(1, meta.retries ?? DEF_RETRIES)
32
- const delay = meta.retryDelayMillis ?? DEF_RETRY_DELAY
33
- let last: unknown
25
+ export const makePgConnectionHelper = (pool: Pool): PgConnectionHelper => {
26
+ const { pgErrorToResourceError } = pgErrorHelper
34
27
 
35
- for (let attempt = 1; attempt <= retries; ++attempt) {
36
- try {
37
- await pool.query('SELECT 1')
28
+ const probe = async (meta: PostgresMeta, location: string): Promise<void> => {
29
+ const retries = Math.max(1, meta.retries ?? DEF_RETRIES)
30
+ const delay = meta.retryDelayMillis ?? DEF_RETRY_DELAY
31
+ let last: unknown
38
32
 
39
- return
40
- } catch (error) {
41
- last = error
42
- if (attempt === retries || !isTransient(error)) {
43
- break
33
+ for (let attempt = 1; attempt <= retries; ++attempt) {
34
+ try {
35
+ await pool.query('SELECT 1')
36
+
37
+ return
38
+ } catch (error) {
39
+ last = error
40
+ if (attempt === retries || !isTransient(error)) {
41
+ break
42
+ }
43
+ log.debug('Postgres not ready, retrying', { location, attempt, retries, delay })
44
+ await new Promise(resolve => setTimeout(resolve, delay))
44
45
  }
45
- console.log(`${location}: not ready (${attempt}/${retries}), retrying in ${delay}ms…`)
46
- await new Promise(resolve => setTimeout(resolve, delay))
47
46
  }
48
- }
49
47
 
50
- const translated = pgErrorToResourceError(last)
51
- const failure = new PostgresConnectionError(`unreachable:${location}:${translated.message}`)
52
- failure.cause = last
48
+ const translated = pgErrorToResourceError(last)
49
+ const failure = new PostgresConnectionError(`unreachable:${location}:${translated.message}`)
50
+ failure.cause = last
53
51
 
54
- throw failure
55
- }
52
+ throw failure
53
+ }
56
54
 
57
- /** `CREATE SCHEMA IF NOT EXISTS` on a pooled connection. */
58
- export const ensureSchema = async (pool: Pool, schema: string): Promise<void> => {
59
- try {
60
- await pool.query(`CREATE SCHEMA IF NOT EXISTS ${quoteIdent(schema)}`)
61
- } catch (error) {
62
- throw pgErrorToResourceError(error)
55
+ const ensureSchema = async (schema: string): Promise<void> => {
56
+ try {
57
+ await pool.query(`CREATE SCHEMA IF NOT EXISTS ${pgNameHelper.quoteIdent(schema)}`)
58
+ } catch (error) {
59
+ throw pgErrorToResourceError(error)
60
+ }
63
61
  }
62
+
63
+ return { probe, ensureSchema }
64
64
  }
package/tests/context.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { postgresGate, randomNamespace } from '@owlmeans/test-integration'
1
+ import { randomNamespace, gateHelper } from '@owlmeans/test-integration'
2
2
  import type { IntegrationGate, PostgresEnv } from '@owlmeans/test-integration'
3
3
  import { PgAutoSync, resetPlaceholderCache } from '@owlmeans/postgres-resource'
4
4
  import type { PostgresResource } from '@owlmeans/postgres-resource'
@@ -19,7 +19,7 @@ import type { PostgresService } from '@owlmeans/postgres'
19
19
  * that backs it, which is why the specs live in this package rather than the resource one
20
20
  * (the reverse would make `@owlmeans/postgres-resource` dev-depend on its own dependent).
21
21
  */
22
- export const gate: IntegrationGate<PostgresEnv> = postgresGate()
22
+ export const gate: IntegrationGate<PostgresEnv> = gateHelper.postgresGate()
23
23
 
24
24
  const url = (): string => gate.env.POSTGRES_URL as string
25
25
 
@@ -103,7 +103,7 @@ export const makeSuite = (label: string): PgSuite => {
103
103
  * Declarations are deliberately *not* reset here. They survive a context switch on
104
104
  * purpose (that is the whole point of keying them by alias), and a `.migration()`
105
105
  * registered before `boot()` would be silently dropped. A spec that wants a clean
106
- * slate calls `resetDeclarations(alias)` itself, which is exactly what a spec
106
+ * slate calls `pgDeclarationHelper.resetDeclarations(alias)` itself, which is exactly what a spec
107
107
  * simulating a restarted process should have to say out loud.
108
108
  */
109
109
  resetPlaceholderCache()
@@ -1,7 +1,7 @@
1
1
  import { afterAll, beforeAll, describe, expect, test } from 'bun:test'
2
2
  import { makePostgresResource } from '@owlmeans/postgres-resource'
3
3
  import type { PostgresResource } from '@owlmeans/postgres-resource'
4
- import { matchCriteria, UnsupportedArgumentError } from '@owlmeans/resource'
4
+ import { recordQueryHelper, UnsupportedArgumentError } from '@owlmeans/resource'
5
5
  import type { Criteria, ResourceRecord } from '@owlmeans/resource'
6
6
 
7
7
  import { gate, makeSuite } from './context.js'
@@ -62,7 +62,7 @@ describe('@owlmeans/postgres — criteria answer what the in-memory engine answe
62
62
 
63
63
  const byBoth = async (criteria: Criteria<Plot>): Promise<void> => {
64
64
  const listed = (await plots.list(criteria, { size: 0 })).items.map(plot => plot.id).sort()
65
- const expected = seeded.filter(plot => matchCriteria(plot, criteria)).map(plot => plot.id).sort()
65
+ const expected = seeded.filter(plot => recordQueryHelper.matchCriteria(plot, criteria)).map(plot => plot.id).sort()
66
66
  expect([JSON.stringify(criteria), listed]).toEqual([JSON.stringify(criteria), expected])
67
67
  }
68
68
 
@@ -1,5 +1,5 @@
1
1
  import { afterAll, describe, expect, test } from 'bun:test'
2
- import { makePostgresResource, resetDeclarations } from '@owlmeans/postgres-resource'
2
+ import { makePostgresResource, pgDeclarationHelper } from '@owlmeans/postgres-resource'
3
3
  import type { PostgresResource, PostgresTx } from '@owlmeans/postgres-resource'
4
4
  import { MigrationConflict, MigrationError, MigrationStage } from '@owlmeans/resource'
5
5
  import type { ResourceRecord } from '@owlmeans/resource'
@@ -253,7 +253,7 @@ describe('@owlmeans/postgres — code registered migrations', () => {
253
253
  })
254
254
 
255
255
  /** A restarted process with an edited source file — the registry has to be rebuilt. */
256
- resetDeclarations('mig-e')
256
+ pgDeclarationHelper.resetDeclarations('mig-e')
257
257
 
258
258
  await expect(boot({
259
259
  alias: 'mig-e',
@@ -262,7 +262,7 @@ describe('@owlmeans/postgres — code registered migrations', () => {
262
262
  })
263
263
 
264
264
  it('rejects a changed body under a name already registered in this process', () => {
265
- resetDeclarations('mig-conflict')
265
+ pgDeclarationHelper.resetDeclarations('mig-conflict')
266
266
  const resource = makePostgresResource<Note, PostgresResource<Note>>('mig-conflict')
267
267
  resource.migration('0001-drift', driftBody)
268
268
 
@@ -1,6 +1,6 @@
1
1
  import { afterAll, describe, expect, test } from 'bun:test'
2
2
  import {
3
- introspectTable, makePostgresResource, PgAutoSync, planSync, PostgresCastRequired,
3
+ makePgIntrospectHelper, makePostgresResource, PgAutoSync, pgDiffHelper, PostgresCastRequired,
4
4
  } from '@owlmeans/postgres-resource'
5
5
  import type { PostgresResource } from '@owlmeans/postgres-resource'
6
6
  import type { ResourceRecord } from '@owlmeans/resource'
@@ -236,8 +236,8 @@ describe('@owlmeans/postgres — reconciling a table already at its shape', () =
236
236
  const client = await pool.connect()
237
237
  try {
238
238
  const table = second.resource.table
239
- const live = await introspectTable(client, table.schema, table.table, table.qualified)
240
- expect(planSync(table, live).statements.map(statement => statement.sql)).toEqual([])
239
+ const live = await makePgIntrospectHelper(client).introspectTable(table.schema, table.table, table.qualified)
240
+ expect(pgDiffHelper.planSync(table, live).statements.map(statement => statement.sql)).toEqual([])
241
241
  } finally {
242
242
  client.release()
243
243
  }