Skip to content

Add returning() to inserts on Postgres - #10

Merged
Makisuo merged 1 commit into
feat/insertsfrom
feat/insert-returning
Oct 4, 2026
Merged

Makisuo merged 1 commit into
feat/insertsfrom
feat/insert-returning

Conversation

@Makisuo

@Makisuo Makisuo commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Phase 2 of design/writes.md. Stacked on #9; review that first.

const created = CH.insertInto(ApiKeys)
	.values({ id: CH.param.string("id"), org_id: CH.param.string("orgId"), name: "default" })
	.returning(($) => ({ id: $.id, createdAt: $.created_at }))

const [row] = yield* Db.run(created, { id, orgId }) // { id: string; createdAt: DateTime.Utc }

What changes

  • returning takes column names or a callback, the same two forms as select. Calling it again replaces the list.
  • Decoding. The row schema is derived from the RETURNING list, so run returns decoded rows. An untypedExpr (such as Maple's pg_current_xact_id()::xid::text) leaves the insert undecoded, with rowSchemaSource: "none" and the alias in untypedColumns.
  • CompiledQuery.returning lists the aliases. run reads it to choose between the row path and the command path, so a precompiled insert also runs correctly.
  • Dialects. New optional DialectClauses.returning (absent means no). Postgres sets it. On ClickHouse, returning is a QueryBuilderDefect at compile, the same as .format() on Postgres.

Testing

  • bun run typecheck and bun run test: 497 unit tests and the doc checks pass.
  • New unit tests cover the exact SQL, the derived schema, untyped expressions and the ClickHouse defect; type tests cover both forms and RowOf.
  • The PGlite round trip now inserts with returning and checks the decoded defaults (uses, revoked, created_at).

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need. Autofix is disabled.

returning takes column names or a callback, as select does. The row
schema is derived from the list, so Database.run decodes the inserted
rows; an untyped expression leaves it undecoded and names the alias.
CompiledQuery.returning lists the aliases, which run reads to pick the
row path over the command path, so a precompiled insert runs correctly.

DialectClauses.returning is optional (absent means no). Postgres sets
it; on ClickHouse, returning is a defect at compile, like format() on
Postgres.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Oct 3, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 12 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 62291b09-3476-492c-839e-eda4ced7c7c5
📥 Commits

Reviewing files that changed from the base of the PR and between ecace6a and ee97ecb.

📒 Files selected for processing (13)
  • CHANGELOG.md
  • design/writes.md
  • docs/database.md
  • docs/inserts.md
  • docs/reference.md
  • src/ch/compile.ts
  • src/ch/dialect.ts
  • src/ch/insert.test-d.ts
  • src/ch/insert.test.ts
  • src/ch/insert.ts
  • src/database/database.test.ts
  • src/database/database.ts
  • src/pg/dialect.ts
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@Makisuo
Makisuo added this pull request to stack #13 October 3, 2026 23:07
@Makisuo
Makisuo merged commit 70f674a into main Oct 4, 2026
4 checks passed
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