This repository has been archived by the owner on Mar 18, 2022. It is now read-only.
forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for ON CONFLICT for cockroach (typeorm#4518)
Closes: typeorm#4513
- Loading branch information
Showing
3 changed files
with
156 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Entity, PrimaryColumn, Column } from "../../../../src"; | ||
|
||
@Entity() | ||
export class User { | ||
@PrimaryColumn() | ||
name: string; | ||
|
||
@PrimaryColumn() | ||
email: string; | ||
|
||
@Column() | ||
age: number; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import "reflect-metadata"; | ||
import { createTestingConnections, closeTestingConnections, reloadTestingDatabases } from "../../utils/test-utils"; | ||
import { Connection } from "../../../src/connection/Connection"; | ||
import { User } from "./entity/User"; | ||
|
||
describe("github issues > #4513 CockroachDB support for onConflict", () => { | ||
|
||
let connections: Connection[]; | ||
before(async () => connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
enabledDrivers: ["cockroachdb"] | ||
})); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should insert if no conflict", () => Promise.all(connections.map(async connection => { | ||
const user1 = new User(); | ||
user1.name = "example"; | ||
user1.email = "example@example.com"; | ||
user1.age = 30; | ||
|
||
await connection.createQueryBuilder() | ||
.insert() | ||
.into(User) | ||
.values(user1) | ||
.execute(); | ||
|
||
const user2 = new User(); | ||
user2.name = "example2"; | ||
user2.email = "example2@example.com"; | ||
user2.age = 42; | ||
|
||
await connection.createQueryBuilder() | ||
.insert() | ||
.into(User) | ||
.values(user2) | ||
.onConflict(`("name", "email") DO NOTHING`) | ||
.execute(); | ||
|
||
await connection.manager.find(User).should.eventually.have.lengthOf(2); | ||
}))); | ||
|
||
it("should update on conflict with do update", () => Promise.all(connections.map(async connection => { | ||
const user1 = new User(); | ||
user1.name = "example"; | ||
user1.email = "example@example.com"; | ||
user1.age = 30; | ||
|
||
await connection.createQueryBuilder() | ||
.insert() | ||
.into(User) | ||
.values(user1) | ||
.execute(); | ||
|
||
const user2 = new User(); | ||
user2.name = "example"; | ||
user2.email = "example@example.com"; | ||
user2.age = 42; | ||
|
||
await connection.createQueryBuilder() | ||
.insert() | ||
.into(User) | ||
.values(user2) | ||
.onConflict(`("name", "email") DO UPDATE SET age = EXCLUDED.age`) | ||
.execute(); | ||
|
||
await connection.manager.findOne(User, { name: "example", email: "example@example.com" }).should.eventually.be.eql({ | ||
name: "example", | ||
email: "example@example.com", | ||
age: 42, | ||
}); | ||
}))); | ||
|
||
it("should not update on conflict with do nothing", () => Promise.all(connections.map(async connection => { | ||
const user1 = new User(); | ||
user1.name = "example"; | ||
user1.email = "example@example.com"; | ||
user1.age = 30; | ||
|
||
await connection.createQueryBuilder() | ||
.insert() | ||
.into(User) | ||
.values(user1) | ||
.execute(); | ||
|
||
const user2 = new User(); | ||
user2.name = "example"; | ||
user2.email = "example@example.com"; | ||
user2.age = 42; | ||
|
||
await connection.createQueryBuilder() | ||
.insert() | ||
.into(User) | ||
.values(user2) | ||
.onConflict(`("name", "email") DO NOTHING`) | ||
.execute(); | ||
|
||
await connection.manager.findOne(User, { name: "example", email: "example@example.com" }).should.eventually.be.eql({ | ||
name: "example", | ||
email: "example@example.com", | ||
age: 30, | ||
}); | ||
}))); | ||
|
||
it("should update with orUpdate", () => Promise.all(connections.map(async connection => { | ||
const user1 = new User(); | ||
user1.name = "example"; | ||
user1.email = "example@example.com"; | ||
user1.age = 30; | ||
|
||
await connection.createQueryBuilder() | ||
.insert() | ||
.into(User) | ||
.values(user1) | ||
.execute(); | ||
|
||
const user2 = new User(); | ||
user2.name = "example"; | ||
user2.email = "example@example.com"; | ||
user2.age = 42; | ||
|
||
await connection.createQueryBuilder() | ||
.insert() | ||
.into(User) | ||
.values(user2) | ||
.orUpdate({ | ||
conflict_target: ["name", "email"], | ||
overwrite: ["age"], | ||
}) | ||
.execute(); | ||
|
||
await connection.manager.findOne(User, { name: "example", email: "example@example.com" }).should.eventually.be.eql({ | ||
name: "example", | ||
email: "example@example.com", | ||
age: 42, | ||
}); | ||
}))); | ||
}); |