[Pg] Add MERGE statement support - #5749
Open
klotztech wants to merge 2 commits into
Open
Conversation
…am#1298) Implements db.merge(table).using(source, on) .whenMatched([condition]).thenUpdate(set) | .thenDelete() | .thenDoNothing() .whenNotMatched([condition]).thenInsert(values) | .thenDoNothing() .returning() -- requires PostgreSQL 17+ Adds PgMergeBuilder, PgMergeBase, and action builders in pg-core/query-builders/merge.ts, buildMergeQuery in PgDialect, and db.merge() on PgDatabase.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
MERGEstatement support for PostgreSQL 15+ via a newdb.merge()query builder, following the same fluent API style asUPDATE .. FROMandINSERT INTO .. SELECT.Resolves #1298
API
What was changed
drizzle-orm/src/pg-core/query-builders/merge.ts— New file. ImplementsPgMergeBuilder(returned bydb.merge()),PgMergeBase(the main builder after.using()),PgMergeMatchedActionBuilder(.whenMatched()actions), andPgMergeNotMatchedActionBuilder(.whenNotMatched()actions). Supports.returning()for PostgreSQL 17+,.toSQL(),.prepare(), and CTE integration viadb.with().drizzle-orm/src/pg-core/dialect.ts— AddedbuildMergeQuery()toPgDialect. Generates the fullMERGE INTO … USING … ON … WHEN … THEN …SQL, including conditional predicates on each clause and an optionalRETURNINGclause.drizzle-orm/src/pg-core/query-builders/index.ts— Re-exports all public types and classes frommerge.ts.drizzle-orm/src/pg-core/db.ts— Addeddb.merge(table)toPgDatabaseand to thedb.with(…)builder chain.drizzle-orm/tests/merge.test.ts— 16 unit tests covering all clause types (.update(),.delete(),.doNothing(),.insert()), conditional predicates, literal params, column references, SQL expressions,RETURNING, and subquery sources.drizzle-orm/type-tests/pg/merge.ts— Compile-time type tests verifying the return types of all builder combinations.