Skip to content

Add insertInto(...).values(...) for ClickHouse and Postgres - #9

Merged
Makisuo merged 1 commit into
mainfrom
feat/inserts
Oct 4, 2026
Merged

Makisuo merged 1 commit into
mainfrom
feat/inserts

Conversation

@Makisuo

@Makisuo Makisuo commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Phase 1 of the write builders planned in design/writes.md: INSERT ... VALUES from the same table definitions queries read.

const insertKey = CH.insertInto(ApiKeys).values({
	id: CH.param.string("id"),
	org_id: CH.param.string("orgId"),
	name: "default",
})
yield* Db.run(insertKey, { id, orgId })

What changes

  • Row type. A column is required unless it's nullable or has a default. null only type-checks on nullable columns. A value can be a plain value, a param.*, or any expression of the column's type.
  • Defaults. New TableOptions.defaults. defineTable works defaults out from default / defaultExpr and records MATERIALIZED / ALIAS columns as not insertable (a type error, and a compile error at runtime).
  • Compiling. Values go through each column's codec: inlined as literals on ClickHouse, bound as $n on Postgres. Columns are written in table order whatever the key order; a key some rows omit becomes DEFAULT (checked on ClickHouse 26.2 and 26.8).
  • Limits. New ParamStyle.maxParameters; Postgres sets 65535 and a statement over it fails to compile instead of being split.
  • Tenant scope. Derived as for queries: single-tenant when every row gives the tenant column the same value or param.
  • Running. compile / compileUnsafe are overloaded for inserts, and Database.run accepts one. New CompiledQuery.kind ("select" / "insert") lets run send an insert through command (needed on ClickHouse) and return [].

Not in this PR

RETURNING, ON CONFLICT, INSERT ... SELECT and ClickHouse SETTINGS are phases 2–4 of design/writes.md.

Testing

  • bun run typecheck and bun run test: 493 unit tests and the doc checks pass.
  • Exact SQL, error and tenant-scope tests in src/ch/insert.test.ts; type tests in src/ch/insert.test-d.ts.
  • PGlite round trip covering jsonb, arrays, timestamptz, DEFAULT, and rollback inside a transaction.
  • Live ClickHouse suite (248 tests) on 26.2.19.43 and 26.8.2.7, including an insert round trip covering Map, Array, Nullable, DateTime64 and an expression default.

🤖 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.

INSERT ... VALUES built from the same table definitions queries read.
The row type requires every column that is not nullable and has no
default; TableOptions.defaults lists defaulted columns, and defineTable
derives them and marks MATERIALIZED / ALIAS columns as not insertable.

Values are encoded through each column's codec: inlined as literals on
ClickHouse, bound as $n on Postgres. Columns are written in table order,
and a key some rows omit becomes DEFAULT, which both ClickHouse matrix
servers accept. Tenant scope is derived as for queries.

compile and Database.run accept an insert. CompiledQuery.kind tells run
to send an insert through command and return no rows. Postgres declares
ParamStyle.maxParameters (65535); a statement over it fails to compile.

Plan for the remaining write phases in design/writes.md.

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: ffa72271-84a1-47c4-a7ff-8177fce18cd6
📥 Commits

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

📒 Files selected for processing (23)
  • CHANGELOG.md
  • README.md
  • design/writes.md
  • docs/README.md
  • docs/database.md
  • docs/inserts.md
  • docs/reference.md
  • docs/tables-and-types.md
  • src/ch/compile.ts
  • src/ch/dialect.ts
  • src/ch/expr.ts
  • src/ch/index.ts
  • src/ch/insert.test-d.ts
  • src/ch/insert.test.ts
  • src/ch/insert.ts
  • src/ch/table.ts
  • src/database/database.test.ts
  • src/database/database.ts
  • src/pg/dialect.ts
  • src/postgres.ts
  • src/schema.ts
  • src/schema/define.ts
  • tests/database.clickhouse.test.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