Skip to content

Add support for CockroachDB 'upsert' #9199

@simplenotezy

Description

@simplenotezy

Feature Description

Should add support for CockroachDB's UPSERT query which has better performance, and works better with Composite Primary keys.

The Problem

Cockroach has a dedicated UPSERT method. Currently TypeORM's .upsert() method calls an INSERT ON CONFLICT query instead, which has worse performance and causes some bugs in our case.

The Solution

  • Add a upsert option to UpsertType (src/driver/types/UpsertType.ts).
  • Rename supportedUpsertType to supportedUpsertTypes and make it an array (since technically cockroachdb supports both approaches)
  • Add an optional UpsertType to InsertOrUpdateOptions that let's the user define a custom Upsert for a given .upsert() query
  • The first UpsertType value in the driver will be the default UpsertType if none was provided in the query. For backwards compatibility, to have .upsert() work the same as it does currently.
  • Tweak InsertQueryBuilder.ts to output UPSERT instead of INSERT if UpsertType is equal to upsert. Also omit the ON CONFLICT part that is not supported in an UPSERT query

Considered Alternatives

Writing an raw query, but that kinda defeats the purpose

Relevant Database Driver(s)

DB Type Relevant
aurora-mysql no
aurora-postgres no
better-sqlite3 no
cockroachdb yes
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres no
react-native no
sap no
spanner no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

  • ✅ Yes, I have the time, and I know how to start.
  • ✖️ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions