orez 0.2.26 → 0.2.27

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 (103) hide show
  1. package/dist/cf-do/worker.d.ts.map +1 -1
  2. package/dist/cf-do/worker.js +9 -1
  3. package/dist/cf-do/worker.js.map +1 -1
  4. package/dist/pg-proxy-do-backend.d.ts +2 -0
  5. package/dist/pg-proxy-do-backend.d.ts.map +1 -1
  6. package/dist/pg-proxy-do-backend.js +49 -7
  7. package/dist/pg-proxy-do-backend.js.map +1 -1
  8. package/dist/pg-sqlite-compiler/catalog/seed.d.ts +67 -0
  9. package/dist/pg-sqlite-compiler/catalog/seed.d.ts.map +1 -0
  10. package/dist/pg-sqlite-compiler/catalog/seed.js +436 -0
  11. package/dist/pg-sqlite-compiler/catalog/seed.js.map +1 -0
  12. package/dist/pg-sqlite-compiler/index.d.ts +12 -0
  13. package/dist/pg-sqlite-compiler/index.d.ts.map +1 -0
  14. package/dist/pg-sqlite-compiler/index.js +59 -0
  15. package/dist/pg-sqlite-compiler/index.js.map +1 -0
  16. package/dist/pg-sqlite-compiler/passes/ast-utils.d.ts +48 -0
  17. package/dist/pg-sqlite-compiler/passes/ast-utils.d.ts.map +1 -0
  18. package/dist/pg-sqlite-compiler/passes/ast-utils.js +93 -0
  19. package/dist/pg-sqlite-compiler/passes/ast-utils.js.map +1 -0
  20. package/dist/pg-sqlite-compiler/passes/catalog.d.ts +34 -0
  21. package/dist/pg-sqlite-compiler/passes/catalog.d.ts.map +1 -0
  22. package/dist/pg-sqlite-compiler/passes/catalog.js +30 -0
  23. package/dist/pg-sqlite-compiler/passes/catalog.js.map +1 -0
  24. package/dist/pg-sqlite-compiler/passes/datetime.d.ts +21 -0
  25. package/dist/pg-sqlite-compiler/passes/datetime.d.ts.map +1 -0
  26. package/dist/pg-sqlite-compiler/passes/datetime.js +53 -0
  27. package/dist/pg-sqlite-compiler/passes/datetime.js.map +1 -0
  28. package/dist/pg-sqlite-compiler/passes/index.d.ts +21 -0
  29. package/dist/pg-sqlite-compiler/passes/index.d.ts.map +1 -0
  30. package/dist/pg-sqlite-compiler/passes/index.js +39 -0
  31. package/dist/pg-sqlite-compiler/passes/index.js.map +1 -0
  32. package/dist/pg-sqlite-compiler/passes/types.d.ts +41 -0
  33. package/dist/pg-sqlite-compiler/passes/types.d.ts.map +1 -0
  34. package/dist/pg-sqlite-compiler/passes/types.js +103 -0
  35. package/dist/pg-sqlite-compiler/passes/types.js.map +1 -0
  36. package/dist/pg-sqlite-compiler/test/oracle.d.ts +34 -0
  37. package/dist/pg-sqlite-compiler/test/oracle.d.ts.map +1 -0
  38. package/dist/pg-sqlite-compiler/test/oracle.js +204 -0
  39. package/dist/pg-sqlite-compiler/test/oracle.js.map +1 -0
  40. package/dist/pg-sqlite-compiler/types.d.ts +55 -0
  41. package/dist/pg-sqlite-compiler/types.d.ts.map +1 -0
  42. package/dist/pg-sqlite-compiler/types.js +2 -0
  43. package/dist/pg-sqlite-compiler/types.js.map +1 -0
  44. package/package.json +7 -2
  45. package/src/cf-do/.wrangler/cache/cf.json +1 -1
  46. package/src/cf-do/.wrangler/state/v3/cache/miniflare-CacheObject/metadata.sqlite-shm +0 -0
  47. package/src/cf-do/.wrangler/state/v3/cache/miniflare-CacheObject/metadata.sqlite-wal +0 -0
  48. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/metadata.sqlite-shm +0 -0
  49. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/metadata.sqlite-wal +0 -0
  50. package/src/cf-do/.wrangler/tmp/bundle-0z4CpE/middleware-insertion-facade.js +11 -0
  51. package/src/cf-do/.wrangler/tmp/bundle-0z4CpE/middleware-loader.entry.ts +134 -0
  52. package/src/cf-do/.wrangler/tmp/bundle-vYmw0E/middleware-insertion-facade.js +11 -0
  53. package/src/cf-do/.wrangler/tmp/bundle-vYmw0E/middleware-loader.entry.ts +134 -0
  54. package/src/cf-do/.wrangler/tmp/dev-cbILNo/worker.js +1059 -0
  55. package/src/cf-do/.wrangler/tmp/dev-cbILNo/worker.js.map +8 -0
  56. package/src/cf-do/.wrangler/tmp/dev-qbho19/worker.js +1059 -0
  57. package/src/cf-do/.wrangler/tmp/dev-qbho19/worker.js.map +8 -0
  58. package/src/cf-do/ARCHITECTURE.md +10 -0
  59. package/src/cf-do/CHAT_E2E.md +213 -0
  60. package/src/cf-do/worker.ts +11 -3
  61. package/src/cli.test.ts +3 -1
  62. package/src/pg-proxy-do-backend.ts +44 -10
  63. package/src/pg-sqlite-compiler/README.md +53 -0
  64. package/src/pg-sqlite-compiler/catalog/seed.ts +524 -0
  65. package/src/pg-sqlite-compiler/fixtures/pgsqlite/arithmetic.json +307 -0
  66. package/src/pg-sqlite-compiler/fixtures/pgsqlite/array.json +377 -0
  67. package/src/pg-sqlite-compiler/fixtures/pgsqlite/cast.json +12 -0
  68. package/src/pg-sqlite-compiler/fixtures/pgsqlite/catalog.json +447 -0
  69. package/src/pg-sqlite-compiler/fixtures/pgsqlite/create-table.json +32 -0
  70. package/src/pg-sqlite-compiler/fixtures/pgsqlite/datetime.json +397 -0
  71. package/src/pg-sqlite-compiler/fixtures/pgsqlite/enum.json +337 -0
  72. package/src/pg-sqlite-compiler/fixtures/pgsqlite/insert.json +337 -0
  73. package/src/pg-sqlite-compiler/fixtures/pgsqlite/json.json +537 -0
  74. package/src/pg-sqlite-compiler/fixtures/pgsqlite/misc.json +1837 -0
  75. package/src/pg-sqlite-compiler/index.ts +73 -0
  76. package/src/pg-sqlite-compiler/integration.test.ts +136 -0
  77. package/src/pg-sqlite-compiler/passes/ast-utils.ts +113 -0
  78. package/src/pg-sqlite-compiler/passes/catalog.ts +65 -0
  79. package/src/pg-sqlite-compiler/passes/datetime.ts +74 -0
  80. package/src/pg-sqlite-compiler/passes/index.ts +49 -0
  81. package/src/pg-sqlite-compiler/passes/types.ts +156 -0
  82. package/src/pg-sqlite-compiler/smoke.test.ts +69 -0
  83. package/src/pg-sqlite-compiler/test/catalog.test.ts +171 -0
  84. package/src/pg-sqlite-compiler/test/corpus.test.ts +161 -0
  85. package/src/pg-sqlite-compiler/test/datetime.oracle.test.ts +102 -0
  86. package/src/pg-sqlite-compiler/test/oracle.ts +237 -0
  87. package/src/pg-sqlite-compiler/test/types.test.ts +109 -0
  88. package/src/pg-sqlite-compiler/types.ts +63 -0
  89. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/0f0f3bdf0abda097eb6f1246db4657d9fc622081362d894d82c1a1ce067b05b6.sqlite +0 -0
  90. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/1ddd3a4a48a11b51658444f5458a1fb175194b1d5b6a5bda20ef3fe3205b900c.sqlite +0 -0
  91. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/3835f242df9728adba3d127a238793fd054ed3e51df3f60749ee744c469bf2a2.sqlite +0 -0
  92. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/4aa9c80eb716cf55b8995ccf7afab0b36c683e6da07d7c37a3f9c570136036df.sqlite +0 -0
  93. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/533e2fd1d6ea46e7a9a0017916ef341802d438d72583462755f2c1f8225e9bf2.sqlite +0 -0
  94. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/5ffa1aced1225ecaeac6366f7586aa3de92761cdff8711d81fbd81f248076abd.sqlite +0 -0
  95. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/686c3a9f0d7e59ed2ab607efd4b76d779c97cafeb3818380033bf7c7eb86c819.sqlite +0 -0
  96. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/6e8214e8dcfadd0deb52d64e5e9ca85c6b329ace11193909845995396914c473.sqlite +0 -0
  97. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/78d9ec9ff873d3fe3507ff53c2a6f6dfc408b4268eb0db3f2a146c0678965366.sqlite +0 -0
  98. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/7eff9f0ed7e27ad0d3f9d923de0682fab1928591172c1ba336c5f79a134a5d85.sqlite +0 -0
  99. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/836cda5b995b25867d722ed4f4c2292167e80351a3c6038db626648eb247dd8b.sqlite +0 -0
  100. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/91ef63b112209ab30172763acd8a0935106c248f7f1bcae5545ce37a9f201551.sqlite +0 -0
  101. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/a66ea4293a5f5938bc6d116edfa2522bb85bc37aea3541fbc09c3b613b9b32c0.sqlite +0 -0
  102. package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/ceb2ab26b80590840b65651deb6e948d3bf81565c6751f3a58752cf4bf4aecae.sqlite +0 -0
  103. /package/src/cf-do/.wrangler/state/v3/do/zero-do-ZeroDO/{204a39120310d37e972c5914cfd71ad55c151bdb9e8ed289a5f8c5b052dd60e4.sqlite → 0ffaabee41a60e04dd0eb7db3073f0a40139e6a97ccd26823967acb652b89a7b.sqlite} +0 -0
@@ -0,0 +1,237 @@
1
+ /**
2
+ * pgsqlite-backed oracle for compiler quality.
3
+ *
4
+ * Spawns a real pgsqlite server, sends PG SQL via the PG wire protocol,
5
+ * captures the result set. Compares against running the same query through
6
+ * our compiler + bun:sqlite. Equivalence at the result-set level is what we
7
+ * actually care about — pgsqlite is the oracle, not the spec.
8
+ *
9
+ * The pgsqlite binary path comes from `vendor/pgsqlite/.resolved-path`,
10
+ * populated by `scripts/pgsqlite/ensure.ts`. If empty, oracle tests should
11
+ * be marked `it.skip` so the suite still runs without pgsqlite installed.
12
+ */
13
+ import { spawn, type ChildProcess } from 'node:child_process'
14
+ import { existsSync, mkdtempSync, readFileSync, rmSync } from 'node:fs'
15
+ import { createConnection, createServer } from 'node:net'
16
+ import { tmpdir } from 'node:os'
17
+ import { resolve } from 'node:path'
18
+
19
+ import Database from '@rocicorp/zero-sqlite3'
20
+
21
+ const VENDOR_PATH_FILE = resolve(
22
+ import.meta.dirname,
23
+ '..',
24
+ '..',
25
+ '..',
26
+ 'vendor',
27
+ 'pgsqlite',
28
+ '.resolved-path'
29
+ )
30
+
31
+ export function pgsqliteBinPath(): string | null {
32
+ if (!existsSync(VENDOR_PATH_FILE)) return null
33
+ const path = readFileSync(VENDOR_PATH_FILE, 'utf-8').trim()
34
+ return path && existsSync(path) ? path : null
35
+ }
36
+
37
+ export const ORACLE_AVAILABLE = pgsqliteBinPath() !== null
38
+
39
+ export interface OracleServer {
40
+ port: number
41
+ dbPath: string
42
+ stop(): Promise<void>
43
+ }
44
+
45
+ /**
46
+ * Pick a free TCP port by binding ephemeral and reading what we got.
47
+ * Then close immediately and hand the port to pgsqlite. There's a tiny TOCTOU
48
+ * window where another process could grab it before pgsqlite binds, but
49
+ * vitest's parallel test files all go through this helper, so the only races
50
+ * are against unrelated processes on the host — vanishingly unlikely in CI.
51
+ */
52
+ async function getFreePort(): Promise<number> {
53
+ return new Promise((resolveFn, reject) => {
54
+ const server = createServer()
55
+ server.unref()
56
+ server.on('error', reject)
57
+ server.listen(0, '127.0.0.1', () => {
58
+ const addr = server.address()
59
+ if (!addr || typeof addr === 'string') {
60
+ server.close()
61
+ reject(new Error('failed to get free port'))
62
+ return
63
+ }
64
+ const port = addr.port
65
+ server.close(() => resolveFn(port))
66
+ })
67
+ })
68
+ }
69
+
70
+ async function probePort(port: number, timeoutMs: number): Promise<boolean> {
71
+ return new Promise((resolveFn) => {
72
+ const sock = createConnection({ host: '127.0.0.1', port })
73
+ const cleanup = (val: boolean) => {
74
+ try {
75
+ sock.destroy()
76
+ } catch {}
77
+ resolveFn(val)
78
+ }
79
+ const timer = setTimeout(() => cleanup(false), timeoutMs)
80
+ sock.once('connect', () => {
81
+ clearTimeout(timer)
82
+ cleanup(true)
83
+ })
84
+ sock.once('error', () => {
85
+ clearTimeout(timer)
86
+ cleanup(false)
87
+ })
88
+ })
89
+ }
90
+
91
+ /**
92
+ * Start a pgsqlite server on an OS-assigned ephemeral port with an ephemeral
93
+ * database. Throws on probe timeout. Caller is responsible for calling
94
+ * `stop()` in a `finally` / `afterAll` hook — `stop()` awaits child exit
95
+ * and cleans up the tempdir.
96
+ */
97
+ export async function startPgsqliteServer(): Promise<OracleServer> {
98
+ const bin = pgsqliteBinPath()
99
+ if (!bin) throw new Error('pgsqlite binary not available')
100
+
101
+ const port = await getFreePort()
102
+ const tmpDir = mkdtempSync(resolve(tmpdir(), 'orez-oracle-'))
103
+ const dbPath = resolve(tmpDir, 'pg.db')
104
+
105
+ const proc: ChildProcess = spawn(
106
+ bin,
107
+ ['--port', String(port), '--database', dbPath, '--log-level', 'error'],
108
+ { stdio: ['ignore', 'pipe', 'pipe'] }
109
+ )
110
+
111
+ const exited: Promise<void> = new Promise((res) => proc.once('exit', () => res()))
112
+
113
+ // wait for "ready" — poll until the server accepts a connection or we time out
114
+ let ready = false
115
+ const start = Date.now()
116
+ while (Date.now() - start < 10_000) {
117
+ if (await probePort(port, 250)) {
118
+ ready = true
119
+ break
120
+ }
121
+ if (proc.exitCode !== null) {
122
+ throw new Error(`pgsqlite exited before becoming ready (code=${proc.exitCode})`)
123
+ }
124
+ await new Promise((r) => setTimeout(r, 50))
125
+ }
126
+ if (!ready) {
127
+ try {
128
+ proc.kill('SIGKILL')
129
+ } catch {}
130
+ try {
131
+ rmSync(tmpDir, { recursive: true, force: true })
132
+ } catch {}
133
+ throw new Error(`pgsqlite did not become ready on port ${port} within 10s`)
134
+ }
135
+
136
+ return {
137
+ port,
138
+ dbPath,
139
+ async stop() {
140
+ try {
141
+ proc.kill('SIGTERM')
142
+ } catch {}
143
+ // wait up to 2s for graceful exit, then SIGKILL
144
+ const killTimer = setTimeout(() => {
145
+ try {
146
+ proc.kill('SIGKILL')
147
+ } catch {}
148
+ }, 2_000)
149
+ try {
150
+ await exited
151
+ } finally {
152
+ clearTimeout(killTimer)
153
+ }
154
+ try {
155
+ rmSync(tmpDir, { recursive: true, force: true })
156
+ } catch {}
157
+ },
158
+ }
159
+ }
160
+
161
+ /**
162
+ * Helper: open a postgres.js client to a running pgsqlite server.
163
+ * Requires `postgres` to be available (we already ship it via @rocicorp/zero).
164
+ */
165
+ export async function connectOracle(server: OracleServer): Promise<{
166
+ exec: (sql: string, params?: any[]) => Promise<any[]>
167
+ end: () => Promise<void>
168
+ }> {
169
+ const { default: postgres } = await import('postgres')
170
+ const sql = postgres({
171
+ host: '127.0.0.1',
172
+ port: server.port,
173
+ user: 'oracle',
174
+ password: '',
175
+ database: 'main',
176
+ ssl: false,
177
+ max: 1,
178
+ idle_timeout: 5,
179
+ connect_timeout: 5,
180
+ fetch_types: false,
181
+ prepare: false,
182
+ })
183
+ return {
184
+ exec: async (s: string, params: any[] = []) => {
185
+ const rows = await sql.unsafe(s, params as any[])
186
+ return rows as any[]
187
+ },
188
+ end: () => sql.end({ timeout: 2 }).then(() => undefined),
189
+ }
190
+ }
191
+
192
+ /**
193
+ * Run the same query against pgsqlite (oracle) and our compiler+SQLite (under
194
+ * test). Return both result sets so the test can diff them however it wants.
195
+ *
196
+ * Uses freshly-spawned servers/dbs for each call — slow but isolated. For
197
+ * batches use the lower-level helpers above.
198
+ */
199
+ export async function runOracleAndCompiler(
200
+ setupSql: string[],
201
+ pgSql: string,
202
+ params: any[] = []
203
+ ): Promise<{
204
+ oracle: any[]
205
+ ours: any[]
206
+ }> {
207
+ const server = await startPgsqliteServer()
208
+ try {
209
+ const conn = await connectOracle(server)
210
+ try {
211
+ for (const s of setupSql) await conn.exec(s)
212
+ const oracle = await conn.exec(pgSql, params)
213
+
214
+ // ours: setup + query against fresh in-memory sqlite (after compile())
215
+ const { compile } = await import('../index.js')
216
+ const db = new Database(':memory:')
217
+ for (const s of setupSql) {
218
+ const { sql: translated } = compile(s)
219
+ db.exec(translated)
220
+ }
221
+ const { sql: translatedQuery } = compile(pgSql)
222
+ // postgres.js uses $1 params; sqlite uses ?
223
+ const sqliteSql = translatedQuery.replace(/\$(\d+)/g, '?')
224
+ const stmt = db.prepare(sqliteSql)
225
+ const ours = (
226
+ params.length > 0 ? stmt.all(...(params as any[])) : stmt.all()
227
+ ) as any[]
228
+ db.close()
229
+
230
+ return { oracle, ours }
231
+ } finally {
232
+ await conn.end()
233
+ }
234
+ } finally {
235
+ await server.stop()
236
+ }
237
+ }
@@ -0,0 +1,109 @@
1
+ import Database from '@rocicorp/zero-sqlite3'
2
+ /**
3
+ * Snapshot tests for the types pass. Each case asserts that the emitted
4
+ * SQLite SQL has the expected normalized type name AND that the result
5
+ * actually executes against a fresh @rocicorp/zero-sqlite3 in-memory db.
6
+ */
7
+ import { describe, expect, it } from 'vitest'
8
+
9
+ import { compile } from '../index.js'
10
+
11
+ function compilesAndRuns(pgSql: string): { sql: string } {
12
+ const { sql, warnings } = compile(pgSql)
13
+ expect(warnings).toEqual([])
14
+ const db = new Database(':memory:')
15
+ db.exec(sql)
16
+ db.close()
17
+ return { sql }
18
+ }
19
+
20
+ describe('types pass', () => {
21
+ it('BIGSERIAL → INTEGER (rowid alias on PRIMARY KEY)', () => {
22
+ const { sql } = compilesAndRuns('CREATE TABLE t (id BIGSERIAL PRIMARY KEY)')
23
+ expect(sql).toMatch(/INTEGER PRIMARY KEY/i)
24
+ expect(sql).not.toMatch(/bigserial/i)
25
+ })
26
+
27
+ it('jsonb → TEXT', () => {
28
+ const { sql } = compilesAndRuns(
29
+ 'CREATE TABLE t (id text PRIMARY KEY, p jsonb NOT NULL)'
30
+ )
31
+ expect(sql).toMatch(/p TEXT NOT NULL/i)
32
+ expect(sql).not.toMatch(/jsonb/i)
33
+ })
34
+
35
+ it('text[] → TEXT (arrays as JSON text)', () => {
36
+ const { sql } = compilesAndRuns(
37
+ 'CREATE TABLE t (id text PRIMARY KEY, tags text[] NOT NULL)'
38
+ )
39
+ expect(sql).toMatch(/tags TEXT NOT NULL/i)
40
+ expect(sql).not.toMatch(/\[/)
41
+ })
42
+
43
+ it('timestamp with time zone → TEXT', () => {
44
+ const { sql } = compilesAndRuns(
45
+ 'CREATE TABLE t (id text PRIMARY KEY, ts timestamp with time zone NOT NULL)'
46
+ )
47
+ expect(sql).toMatch(/ts TEXT NOT NULL/i)
48
+ expect(sql).not.toMatch(/timestamp/i)
49
+ expect(sql).not.toMatch(/with time zone/i)
50
+ })
51
+
52
+ it('varchar(N) → TEXT (drop length typmod)', () => {
53
+ const { sql } = compilesAndRuns(
54
+ 'CREATE TABLE t (id text PRIMARY KEY, val varchar(64))'
55
+ )
56
+ expect(sql).toMatch(/val TEXT/i)
57
+ expect(sql).not.toMatch(/varchar/i)
58
+ expect(sql).not.toMatch(/\(64\)/)
59
+ })
60
+
61
+ it('boolean → INTEGER', () => {
62
+ const { sql } = compilesAndRuns(
63
+ 'CREATE TABLE t (id text PRIMARY KEY, enabled boolean NOT NULL DEFAULT false)'
64
+ )
65
+ expect(sql).toMatch(/enabled INTEGER NOT NULL/i)
66
+ expect(sql).not.toMatch(/boolean/i)
67
+ })
68
+
69
+ it('uuid → TEXT', () => {
70
+ const { sql } = compilesAndRuns('CREATE TABLE t (id uuid PRIMARY KEY)')
71
+ expect(sql).toMatch(/id TEXT PRIMARY KEY/i)
72
+ expect(sql).not.toMatch(/uuid/i)
73
+ })
74
+
75
+ it('bytea → BLOB', () => {
76
+ const { sql } = compilesAndRuns('CREATE TABLE t (id text PRIMARY KEY, body bytea)')
77
+ expect(sql).toMatch(/body BLOB/i)
78
+ expect(sql).not.toMatch(/bytea/i)
79
+ })
80
+
81
+ it('numeric(10,2) → NUMERIC (drops precision)', () => {
82
+ const { sql } = compilesAndRuns(
83
+ 'CREATE TABLE t (id text PRIMARY KEY, amount numeric(10,2))'
84
+ )
85
+ expect(sql).toMatch(/amount NUMERIC/i)
86
+ expect(sql).not.toMatch(/\(10/)
87
+ })
88
+
89
+ it('composite: all common chat-app types in one table', () => {
90
+ const { sql } = compilesAndRuns(
91
+ 'CREATE TABLE event (' +
92
+ 'id BIGSERIAL PRIMARY KEY, ' +
93
+ 'user_id uuid NOT NULL, ' +
94
+ '"createdAt" timestamp with time zone DEFAULT NOW() NOT NULL, ' +
95
+ "payload jsonb NOT NULL DEFAULT '{}', " +
96
+ "tags text[] NOT NULL DEFAULT '{}', " +
97
+ 'amount numeric(10,2), ' +
98
+ 'enabled boolean NOT NULL DEFAULT true' +
99
+ ')'
100
+ )
101
+ expect(sql).toMatch(/INTEGER PRIMARY KEY/i)
102
+ expect(sql).toMatch(/user_id TEXT NOT NULL/i)
103
+ expect(sql).toMatch(/"createdAt" TEXT DEFAULT CURRENT_TIMESTAMP NOT NULL/i)
104
+ expect(sql).toMatch(/payload TEXT NOT NULL/i)
105
+ expect(sql).toMatch(/tags TEXT NOT NULL/i)
106
+ expect(sql).toMatch(/amount NUMERIC/i)
107
+ expect(sql).toMatch(/enabled INTEGER NOT NULL/i)
108
+ })
109
+ })
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Schema metadata available to passes for type-aware translation.
3
+ *
4
+ * Implementations are caller-provided. The default NOOP_SCHEMA returns nothing
5
+ * and passes degrade gracefully (regex/AST-shape fallbacks).
6
+ *
7
+ * For orez, the eventual implementation will read from `ctx.storage.sql`
8
+ * PRAGMA + the `_orez_pg_metadata` catalog table.
9
+ */
10
+ export interface SchemaInfo {
11
+ /** PG type name (e.g. "jsonb", "text[]") for a column, or undefined. */
12
+ getColumnType(schema: string, table: string, column: string): string | undefined
13
+
14
+ /** ENUM definition lookup by PG type name. */
15
+ getEnum(typeName: string): EnumInfo | undefined
16
+
17
+ /** Validate an ENUM literal value. */
18
+ isEnumValue(typeOid: number, label: string): boolean
19
+
20
+ /** Column list for a table (for SELECT * expansion, RETURNING * etc.). */
21
+ getTableColumns(schema: string, table: string): readonly string[] | undefined
22
+ }
23
+
24
+ export interface EnumInfo {
25
+ typeOid: number
26
+ values: readonly string[]
27
+ }
28
+
29
+ export interface CompileWarning {
30
+ kind: string
31
+ message: string
32
+ /** PG AST node tag where the warning was raised. */
33
+ near?: string
34
+ }
35
+
36
+ export interface CompileResult {
37
+ sql: string
38
+ warnings: CompileWarning[]
39
+ }
40
+
41
+ export interface CompileOptions {
42
+ schema?: SchemaInfo
43
+ pgVersion?: number
44
+ /** Override pass list (mainly for testing individual passes). */
45
+ passes?: Pass[]
46
+ }
47
+
48
+ /** Context passed to every pass. */
49
+ export interface PassContext {
50
+ schema: SchemaInfo
51
+ warnings: CompileWarning[]
52
+ /**
53
+ * Optional pass list — if set, runPasses uses these instead of the default
54
+ * pipeline. Otherwise the full default pipeline runs.
55
+ */
56
+ passes?: Pass[]
57
+ }
58
+
59
+ /** A pass is a function that mutates a RawStmt in place. */
60
+ export interface Pass {
61
+ name: string
62
+ run(rawStmt: any, ctx: PassContext): void
63
+ }