Skip to content

Commit

Permalink
feature: adds support for UPSERT for CockroachDB (fixes typeorm#9199)
Browse files Browse the repository at this point in the history
  • Loading branch information
simplenotezy committed Jul 12, 2022
1 parent 1671889 commit f035231
Show file tree
Hide file tree
Showing 16 changed files with 189 additions and 141 deletions.
1 change: 1 addition & 0 deletions docs/repository-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ await repository.upsert(
{
conflictPaths: ["externalId"],
skipUpdateIfNoValuesChanged: true, // supported by postgres, skips update if it would not change row values
upsertType: "upsert", // "on-conflict-do-update" | "on-duplicate-key-update" | "upsert" - optionally provide an UpsertType - 'upsert' is currently only sypported by CockroachDB
},
)
/** executes
Expand Down
2 changes: 1 addition & 1 deletion src/driver/Driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export interface Driver {
/**
* Returns type of upsert supported by driver if any
*/
supportedUpsertType?: UpsertType
supportedUpsertTypes?: UpsertType[]

/**
* Default values of length, precision and scale depends on column data type.
Expand Down
3 changes: 2 additions & 1 deletion src/driver/aurora-mysql/AuroraMysqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { Table } from "../../schema-builder/table/Table"
import { View } from "../../schema-builder/view/View"
import { TableForeignKey } from "../../schema-builder/table/TableForeignKey"
import { InstanceChecker } from "../../util/InstanceChecker"
import { UpsertType } from "../types/UpsertType"

/**
* Organizes communication with MySQL DBMS.
Expand Down Expand Up @@ -152,7 +153,7 @@ export class AuroraMysqlDriver implements Driver {
/**
* Returns type of upsert supported by driver if any
*/
readonly supportedUpsertType = "on-duplicate-key-update"
readonly supportedUpsertTypes: UpsertType[] = ["on-duplicate-key-update"]

/**
* Gets list of spatial column data types.
Expand Down
48 changes: 26 additions & 22 deletions src/driver/cockroachdb/CockroachDriver.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
import { Driver } from "../Driver"
import { ConnectionIsNotSetError } from "../../error/ConnectionIsNotSetError"
import { ObjectLiteral } from "../../common/ObjectLiteral"
import { DataSource } from "../../data-source/DataSource"
import { TypeORMError } from "../../error"
import { ConnectionIsNotSetError } from "../../error/ConnectionIsNotSetError"
import { DriverPackageNotInstalledError } from "../../error/DriverPackageNotInstalledError"
import { DriverUtils } from "../DriverUtils"
import { ColumnMetadata } from "../../metadata/ColumnMetadata"
import { CteCapabilities } from "../types/CteCapabilities"
import { CockroachConnectionCredentialsOptions } from "./CockroachConnectionCredentialsOptions"
import { CockroachConnectionOptions } from "./CockroachConnectionOptions"
import { DateUtils } from "../../util/DateUtils"
import { EntityMetadata } from "../../metadata/EntityMetadata"
import { PlatformTools } from "../../platform/PlatformTools"
import { DataSource } from "../../data-source/DataSource"
import { RdbmsSchemaBuilder } from "../../schema-builder/RdbmsSchemaBuilder"
import { MappedColumnTypes } from "../types/MappedColumnTypes"
import { ColumnType } from "../types/ColumnTypes"
import { QueryRunner } from "../../query-runner/QueryRunner"
import { DataTypeDefaults } from "../types/DataTypeDefaults"
import { TableColumn } from "../../schema-builder/table/TableColumn"
import { EntityMetadata } from "../../metadata/EntityMetadata"
import { OrmUtils } from "../../util/OrmUtils"
import { CockroachQueryRunner } from "./CockroachQueryRunner"
import { ApplyValueTransformers } from "../../util/ApplyValueTransformers"
import { ReplicationMode } from "../types/ReplicationMode"
import { TypeORMError } from "../../error"
import { RdbmsSchemaBuilder } from "../../schema-builder/RdbmsSchemaBuilder"
import { Table } from "../../schema-builder/table/Table"
import { View } from "../../schema-builder/view/View"
import { TableColumn } from "../../schema-builder/table/TableColumn"
import { TableForeignKey } from "../../schema-builder/table/TableForeignKey"
import { ObjectUtils } from "../../util/ObjectUtils"
import { View } from "../../schema-builder/view/View"
import { ApplyValueTransformers } from "../../util/ApplyValueTransformers"
import { DateUtils } from "../../util/DateUtils"
import { InstanceChecker } from "../../util/InstanceChecker"
import { ObjectUtils } from "../../util/ObjectUtils"
import { OrmUtils } from "../../util/OrmUtils"
import { Driver } from "../Driver"
import { DriverUtils } from "../DriverUtils"
import { ColumnType } from "../types/ColumnTypes"
import { CteCapabilities } from "../types/CteCapabilities"
import { DataTypeDefaults } from "../types/DataTypeDefaults"
import { MappedColumnTypes } from "../types/MappedColumnTypes"
import { ReplicationMode } from "../types/ReplicationMode"
import { UpsertType } from "../types/UpsertType"
import { CockroachConnectionCredentialsOptions } from "./CockroachConnectionCredentialsOptions"
import { CockroachConnectionOptions } from "./CockroachConnectionOptions"
import { CockroachQueryRunner } from "./CockroachQueryRunner"

/**
* Organizes communication with Cockroach DBMS.
Expand Down Expand Up @@ -161,7 +162,10 @@ export class CockroachDriver implements Driver {
/**
* Returns type of upsert supported by driver if any
*/
readonly supportedUpsertType = "on-conflict-do-update"
readonly supportedUpsertTypes: UpsertType[] = [
"on-conflict-do-update",
"upsert",
]

/**
* Gets list of spatial column data types.
Expand Down
3 changes: 2 additions & 1 deletion src/driver/mysql/MysqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { View } from "../../schema-builder/view/View"
import { TableForeignKey } from "../../schema-builder/table/TableForeignKey"
import { VersionUtils } from "../../util/VersionUtils"
import { InstanceChecker } from "../../util/InstanceChecker"
import { UpsertType } from "../types/UpsertType"

/**
* Organizes communication with MySQL DBMS.
Expand Down Expand Up @@ -151,7 +152,7 @@ export class MysqlDriver implements Driver {
/**
* Returns type of upsert supported by driver if any
*/
readonly supportedUpsertType = "on-duplicate-key-update"
readonly supportedUpsertTypes: UpsertType[] = ["on-duplicate-key-update"]

/**
* Gets list of spatial column data types.
Expand Down
3 changes: 2 additions & 1 deletion src/driver/postgres/PostgresDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { Table } from "../../schema-builder/table/Table"
import { View } from "../../schema-builder/view/View"
import { TableForeignKey } from "../../schema-builder/table/TableForeignKey"
import { InstanceChecker } from "../../util/InstanceChecker"
import { UpsertType } from "../types/UpsertType"

/**
* Organizes communication with PostgreSQL DBMS.
Expand Down Expand Up @@ -183,7 +184,7 @@ export class PostgresDriver implements Driver {
/**
* Returns type of upsert supported by driver if any
*/
readonly supportedUpsertType = "on-conflict-do-update"
readonly supportedUpsertTypes: UpsertType[] = ["on-conflict-do-update"]

/**
* Gets list of spatial column data types.
Expand Down
2 changes: 1 addition & 1 deletion src/driver/spanner/SpannerDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class SpannerDriver implements Driver {
/**
* Returns type of upsert supported by driver if any
*/
readonly supportedUpsertType = undefined
readonly supportedUpsertTypes = undefined

/**
* Gets list of spatial column data types.
Expand Down
3 changes: 2 additions & 1 deletion src/driver/sqlite-abstract/AbstractSqliteDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { Table } from "../../schema-builder/table/Table"
import { View } from "../../schema-builder/view/View"
import { TableForeignKey } from "../../schema-builder/table/TableForeignKey"
import { InstanceChecker } from "../../util/InstanceChecker"
import { UpsertType } from "../types/UpsertType"

type DatabasesMap = Record<
string,
Expand Down Expand Up @@ -131,7 +132,7 @@ export abstract class AbstractSqliteDriver implements Driver {
/**
* Returns type of upsert supported by driver if any
*/
readonly supportedUpsertType = "on-conflict-do-update"
readonly supportedUpsertTypes: UpsertType[] = ["on-conflict-do-update"]

/**
* Gets list of column data types that support length by a driver.
Expand Down
5 changes: 4 additions & 1 deletion src/driver/types/UpsertType.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export type UpsertType = "on-conflict-do-update" | "on-duplicate-key-update"
export type UpsertType =
| "on-conflict-do-update"
| "on-duplicate-key-update"
| "upsert"
3 changes: 3 additions & 0 deletions src/entity-manager/EntityManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,9 @@ export class EntityManager {
{
skipUpdateIfNoValuesChanged:
options.skipUpdateIfNoValuesChanged,
upsertType:
options.upsertType ||
this.connection.driver.supportedUpsertTypes?.[0],
},
)
.execute()
Expand Down
3 changes: 3 additions & 0 deletions src/query-builder/InsertOrUpdateOptions.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { UpsertType } from "../driver/types/UpsertType"

export type InsertOrUpdateOptions = {
skipUpdateIfNoValuesChanged?: boolean
upsertType?: UpsertType
}
Loading

0 comments on commit f035231

Please sign in to comment.