|
| 1 | +/** |
| 2 | + * #286 — the CRUD helpers must work on Postgres, in BOTH adapters. |
| 3 | + * |
| 4 | + * Drizzle's `.all()` and `.get()` are libsql/better-sqlite3-only. The node-postgres |
| 5 | + * builder is thenable but has neither, so calling them 500s every GET with |
| 6 | + * `TypeError: q.all is not a function`. The Fastify adapter was fixed for this and |
| 7 | + * carries a comment explaining it; **Hono was not**, and shipped broken on Postgres |
| 8 | + * through 0.21.2 and 0.21.3. Reported from a real adoption. |
| 9 | + * |
| 10 | + * The reason it survived is the interesting part, and it is what this file exists to |
| 11 | + * close: EVERY adapter test in this package used libsql (`drizzle-orm/libsql`, |
| 12 | + * `:memory:`), where `.all()`/`.get()` exist and work. No test could have caught the |
| 13 | + * incompatibility for either adapter — Fastify's fix was reasoned, not gated. A |
| 14 | + * per-adapter test suite with a single dialect cannot detect a dialect divergence. |
| 15 | + * |
| 16 | + * So this is a MATRIX: the same read paths, asserted across both adapters, against a |
| 17 | + * REAL Postgres. Adding a third adapter should mean adding a row here, not a new file. |
| 18 | + * |
| 19 | + * Gated on METAOBJECTS_TEST_PG_URL (CI's ts-slow lane supplies a Postgres sidecar); |
| 20 | + * skips loudly-but-harmlessly when absent, and the sentinel below fails if a lane |
| 21 | + * claims it intends Postgres and then does not provide it. |
| 22 | + */ |
| 23 | + |
| 24 | +import { describe, test, expect, beforeAll, afterAll } from "bun:test"; |
| 25 | +import { Hono } from "hono"; |
| 26 | +import Fastify, { type FastifyInstance } from "fastify"; |
| 27 | +import { Pool } from "pg"; |
| 28 | +import { drizzle } from "drizzle-orm/node-postgres"; |
| 29 | +import { pgTable, serial, text, boolean } from "drizzle-orm/pg-core"; |
| 30 | +import { z } from "zod"; |
| 31 | +import { mountCrudRoutes as mountHono } from "../src/hono/index.js"; |
| 32 | +import { mountCrudRoutes as mountFastify } from "../src/drizzle-fastify/index.js"; |
| 33 | +import type { FilterAllowlist, SortAllowlist } from "../src/drizzle-fastify/filter-allowlist.js"; |
| 34 | + |
| 35 | +const PG_URL = process.env["METAOBJECTS_TEST_PG_URL"]; |
| 36 | + |
| 37 | +const subscribers = pgTable("mo286_subscribers", { |
| 38 | + id: serial("id").primaryKey(), |
| 39 | + email: text("email").notNull(), |
| 40 | + firstName: text("first_name").notNull(), |
| 41 | + subscribed: boolean("subscribed").notNull().default(true), |
| 42 | +}); |
| 43 | + |
| 44 | +const InsertSchema = z.object({ |
| 45 | + email: z.string(), |
| 46 | + firstName: z.string(), |
| 47 | + subscribed: z.boolean().optional(), |
| 48 | +}); |
| 49 | +const UpdateSchema = InsertSchema.partial(); |
| 50 | + |
| 51 | +const filterAllowlist: FilterAllowlist = { |
| 52 | + email: { ops: ["eq", "ne", "in", "like", "isNull"], subType: "string", leadingWildcard: true }, |
| 53 | + firstName: { ops: ["eq", "ne", "in", "like", "isNull"], subType: "string", leadingWildcard: false }, |
| 54 | + subscribed: { ops: ["eq", "isNull"], subType: "boolean", leadingWildcard: false }, |
| 55 | +}; |
| 56 | +const sortAllowlist: SortAllowlist = { email: {}, firstName: {} }; |
| 57 | + |
| 58 | +describe("real-PG dialect matrix sentinel", () => { |
| 59 | + test("METAOBJECTS_TEST_PG_URL is set when the lane declares RUNTIME_TS_PG_EXPECT=1", () => { |
| 60 | + if (process.env["RUNTIME_TS_PG_EXPECT"] === "1") { |
| 61 | + expect(PG_URL).toBeTruthy(); |
| 62 | + } |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +describe("#286 — CRUD helpers on real Postgres, both adapters", () => { |
| 67 | + if (!PG_URL) { |
| 68 | + test.skip("skipped — METAOBJECTS_TEST_PG_URL not set", () => {}); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + let pool: Pool; |
| 73 | + // biome-ignore lint/suspicious/noExplicitAny: drizzle db handle, structurally typed by the mounts |
| 74 | + let db: any; |
| 75 | + let hono: Hono; |
| 76 | + let fastify: FastifyInstance; |
| 77 | + |
| 78 | + beforeAll(async () => { |
| 79 | + pool = new Pool({ connectionString: PG_URL }); |
| 80 | + db = drizzle(pool); |
| 81 | + await pool.query(`DROP TABLE IF EXISTS mo286_subscribers`); |
| 82 | + await pool.query( |
| 83 | + `CREATE TABLE mo286_subscribers ( |
| 84 | + id serial PRIMARY KEY, |
| 85 | + email text NOT NULL, |
| 86 | + first_name text NOT NULL, |
| 87 | + subscribed boolean NOT NULL DEFAULT true |
| 88 | + )`, |
| 89 | + ); |
| 90 | + await pool.query( |
| 91 | + `INSERT INTO mo286_subscribers (email, first_name, subscribed) |
| 92 | + VALUES ('alice@x.com','Alice',true), ('bob@x.com','Bob',false), ('carol@y.com','Carol',true)`, |
| 93 | + ); |
| 94 | + |
| 95 | + hono = new Hono(); |
| 96 | + mountHono({ |
| 97 | + app: hono, path: "/subscribers", db, table: subscribers, |
| 98 | + insertSchema: InsertSchema, updateSchema: UpdateSchema, |
| 99 | + filterAllowlist, sortAllowlist, dialect: "postgres", |
| 100 | + }); |
| 101 | + |
| 102 | + fastify = Fastify(); |
| 103 | + mountFastify({ |
| 104 | + fastify, path: "/subscribers", db, table: subscribers, |
| 105 | + insertSchema: InsertSchema, updateSchema: UpdateSchema, |
| 106 | + filterAllowlist, sortAllowlist, dialect: "postgres", |
| 107 | + }); |
| 108 | + await fastify.ready(); |
| 109 | + }); |
| 110 | + |
| 111 | + afterAll(async () => { |
| 112 | + await fastify?.close(); |
| 113 | + await pool.query(`DROP TABLE IF EXISTS mo286_subscribers`); |
| 114 | + await pool.end(); |
| 115 | + }); |
| 116 | + |
| 117 | + /** Uniform request surface so the same assertions run against both adapters. */ |
| 118 | + const adapters: Array<{ name: string; get: (url: string) => Promise<{ status: number; body: unknown }> }> = [ |
| 119 | + { |
| 120 | + name: "hono", |
| 121 | + get: async (url) => { |
| 122 | + const res = await hono.request(url); |
| 123 | + return { status: res.status, body: res.status === 204 ? null : await res.json() }; |
| 124 | + }, |
| 125 | + }, |
| 126 | + { |
| 127 | + name: "fastify", |
| 128 | + get: async (url) => { |
| 129 | + const res = await fastify.inject({ method: "GET", url }); |
| 130 | + return { status: res.statusCode, body: res.statusCode === 204 ? null : res.json() }; |
| 131 | + }, |
| 132 | + }, |
| 133 | + ]; |
| 134 | + |
| 135 | + for (const a of adapters) { |
| 136 | + describe(a.name, () => { |
| 137 | + test("list returns rows (the exact call that threw `q.all is not a function`)", async () => { |
| 138 | + const { status, body } = await a.get("/subscribers"); |
| 139 | + expect(status).toBe(200); |
| 140 | + expect(Array.isArray(body)).toBe(true); |
| 141 | + expect((body as unknown[]).length).toBe(3); |
| 142 | + }); |
| 143 | + |
| 144 | + test("get-by-id returns one row (the `.get()` path)", async () => { |
| 145 | + const list = (await a.get("/subscribers")).body as Array<{ id: number; email: string }>; |
| 146 | + const target = list.find((r) => r.email === "bob@x.com"); |
| 147 | + expect(target).toBeDefined(); |
| 148 | + const { status, body } = await a.get(`/subscribers/${target?.id}`); |
| 149 | + expect(status).toBe(200); |
| 150 | + expect((body as { email: string }).email).toBe("bob@x.com"); |
| 151 | + }); |
| 152 | + |
| 153 | + test("get-by-id 404s for a missing row rather than throwing", async () => { |
| 154 | + const { status } = await a.get("/subscribers/99999"); |
| 155 | + expect(status).toBe(404); |
| 156 | + }); |
| 157 | + |
| 158 | + test("withCount returns the {rows,total} envelope (the count-query `.all()` path)", async () => { |
| 159 | + const { status, body } = await a.get("/subscribers?withCount=1"); |
| 160 | + expect(status).toBe(200); |
| 161 | + const env = body as { rows: unknown[]; total: number }; |
| 162 | + expect(Array.isArray(env.rows)).toBe(true); |
| 163 | + expect(env.total).toBe(3); |
| 164 | + }); |
| 165 | + |
| 166 | + test("filter + sort execute on Postgres", async () => { |
| 167 | + const { status, body } = await a.get("/subscribers?filter[subscribed]=true&sort=email:desc"); |
| 168 | + expect(status).toBe(200); |
| 169 | + const rows = body as Array<{ email: string }>; |
| 170 | + expect(rows.map((r) => r.email)).toEqual(["carol@y.com", "alice@x.com"]); |
| 171 | + }); |
| 172 | + }); |
| 173 | + } |
| 174 | + |
| 175 | + test("both adapters return the SAME list payload — the matrix's whole point", async () => { |
| 176 | + const [h, f] = await Promise.all([ |
| 177 | + a0().get("/subscribers?sort=email:asc"), |
| 178 | + a1().get("/subscribers?sort=email:asc"), |
| 179 | + ]); |
| 180 | + expect(h.status).toBe(f.status); |
| 181 | + expect(h.body).toEqual(f.body); |
| 182 | + }); |
| 183 | + |
| 184 | + function a0() { return adapters[0]!; } |
| 185 | + function a1() { return adapters[1]!; } |
| 186 | +}); |
0 commit comments