Skip to content

feat(pg-codegen): generated Prisma-like db client over query-builder - #1733

Merged
pyramation merged 3 commits into
mainfrom
feat/pg-client-runtime
Aug 15, 2026
Merged

feat(pg-codegen): generated Prisma-like db client over query-builder#1733
pyramation merged 3 commits into
mainfrom
feat/pg-client-runtime

Conversation

@pyramation

Copy link
Copy Markdown
Contributor

Summary

pg-codegen output now includes a high-level database client mirroring the GraphQL ORM's surface, so consumers stop hand-assembling SELECT lists, columnByField lookups, and per-column decoder calls:

const db = createAgentDb(pool, { schema: agent.schema, tables: { agentThread: agent.threadTable } });

const thread = await db.agentThread.findFirst({
  where: { id: threadId },
  select: { id: true, model: true, systemPrompt: true, agentId: true }
}); // Pick<AgentThread, ...> | null — decoded, camelCase

const msg = await db.agentMessage.create({ data: { threadId, parts }, select: { id: true, createdAt: true } });
await db.agentTask.updateOrThrow({ where: { id, threadId }, data: { status } });

const txDb = db.$with(txClient); // rebind to a transaction's PoolClient

Two new emitted files:

  • client.ts — shared runtime, copied from a typechecked template (src/emit/templates/client.ts, same pattern as graphql-codegen templates). TableClient<App> wraps @constructive-io/query-builder: findMany / findFirst / findFirstOrThrow / count / create / update / updateOrThrow / delete, camelCase where in the shared query-spec grammar (equalTo, in, greaterThan, startsWith, and/or/not; bare value = equalTo, null = isNull), select: { field: true } narrowing the result type via SelectResult, orderBy: { createdAt: 'DESC' }, rows decoded through the generated field decoders, writes serialized (Dates → ISO, json fields → stringify, arrays passed through to pg). Zero-row updateOrThrow/findFirstOrThrow throw RowNotFoundError — never silent.
  • <schema>/db.ts — AST-emitted create<Schema>Db(db, options?) factory binding one TableClient per table over the existing <TABLE>_TABLE / <TABLE>_FIELDS constants, with runtime schema/per-table name overrides for tables provisioned into per-tenant schemas, plus $with for transactions.

Notes:

  • pg-codegen gains a @constructive-io/query-builder dependency (generated output imports it); templates are copied into dist/ at build like graphql-codegen.
  • Unlike the GraphQL ORM there is no .execute() envelope — pg errors throw, so methods are plain promises.
  • New live round-trip suite (__tests__/client.test.ts) runs the committed fixture client against a provisioned DB: CRUD, projections, filter grammar, and/or/not, count, $with. 48 tests green.

Link to Devin session: https://app.devin.ai/sessions/b1fb8d90ec0a4cd1bd95e3c77c6f6680
Requested by: @pyramation

Emits a shared client.ts runtime (TableClient: findMany/findFirst/findFirstOrThrow/count/create/update/updateOrThrow/delete, camelCase where/select/orderBy in the query-spec filter grammar, decoded rows, $with rebinding) plus a per-schema createXDb factory binding one TableClient per table with runtime schema/table overrides.
@pyramation pyramation self-assigned this Aug 15, 2026
@devin-ai-integration

Copy link
Copy Markdown
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

@pyramation
pyramation merged commit 67f94a8 into main Aug 15, 2026
20 checks passed
@pyramation
pyramation deleted the feat/pg-client-runtime branch August 15, 2026 03:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant