mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
Support orUpdate for SQLite
This commit is contained in:
parent
360468b791
commit
f435d991eb
@ -10,6 +10,7 @@ feel free to ask us and community.
|
||||
### Features
|
||||
|
||||
* deprecate column `readonly` option in favor of `update` and `insert` options ([#4035](https://github.com/typeorm/typeorm/pull/4035))
|
||||
* added support for `orUpdate` in SQLlite ([#4097](https://github.com/typeorm/typeorm/pull/4097))
|
||||
|
||||
## 0.2.17 (2019-05-01)
|
||||
|
||||
|
||||
@ -249,7 +249,7 @@ export class InsertQueryBuilder<Entity> extends QueryBuilder<Entity> {
|
||||
if (statement && statement.overwrite instanceof Array) {
|
||||
if (this.connection.driver instanceof MysqlDriver) {
|
||||
this.expressionMap.onUpdate.overwrite = statement.overwrite.map(column => `${column} = VALUES(${column})`).join(", ");
|
||||
} else if (this.connection.driver instanceof PostgresDriver) {
|
||||
} else if (this.connection.driver instanceof PostgresDriver || this.connection.driver instanceof AbstractSqliteDriver) {
|
||||
this.expressionMap.onUpdate.overwrite = statement.overwrite.map(column => `${column} = EXCLUDED.${column}`).join(", ");
|
||||
}
|
||||
}
|
||||
|
||||
13
test/github-issues/4096/entity/User.ts
Normal file
13
test/github-issues/4096/entity/User.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { Entity, PrimaryColumn, Column } from "../../../../src";
|
||||
|
||||
@Entity()
|
||||
export class User {
|
||||
@PrimaryColumn()
|
||||
email: string;
|
||||
|
||||
@PrimaryColumn()
|
||||
username: string;
|
||||
|
||||
@Column()
|
||||
bio: string;
|
||||
}
|
||||
61
test/github-issues/4096/issue-4096.ts
Normal file
61
test/github-issues/4096/issue-4096.ts
Normal file
@ -0,0 +1,61 @@
|
||||
import "reflect-metadata";
|
||||
import {createTestingConnections, closeTestingConnections, reloadTestingDatabases} from "../../utils/test-utils";
|
||||
import {Connection} from "../../../src/connection/Connection";
|
||||
import {expect} from "chai";
|
||||
import {User} from "./entity/User";
|
||||
|
||||
describe("github issues > #4096 SQLite support for orUpdate", () => {
|
||||
let connections: Connection[];
|
||||
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [User],
|
||||
enabledDrivers: ["sqlite"],
|
||||
schemaCreate: true,
|
||||
dropSchema: true,
|
||||
}));
|
||||
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
const user1 = new User();
|
||||
user1.email = "example@example.org";
|
||||
user1.username = "example";
|
||||
user1.bio = "My bio";
|
||||
|
||||
const user2 = new User();
|
||||
user2.email = "example@example.org";
|
||||
user2.username = "example";
|
||||
user2.bio = "Updated bio";
|
||||
|
||||
it("should overwrite using current value in SQLite", () => Promise.all(connections.map(async connection => {
|
||||
try {
|
||||
const UserRepository = connection.manager.getRepository(User);
|
||||
|
||||
await UserRepository
|
||||
.createQueryBuilder()
|
||||
.insert()
|
||||
.into(User)
|
||||
.values(user1)
|
||||
.execute();
|
||||
|
||||
await UserRepository
|
||||
.createQueryBuilder()
|
||||
.insert()
|
||||
.into(User)
|
||||
.values(user2)
|
||||
.orUpdate({
|
||||
conflict_target: [ "email", "username" ],
|
||||
overwrite: ["bio"],
|
||||
})
|
||||
.execute();
|
||||
|
||||
const users = await UserRepository.find();
|
||||
expect(users).not.to.be.undefined;
|
||||
expect(users).to.have.lengthOf(1);
|
||||
expect(users[0]).to.includes({ bio: "Updated bio" });
|
||||
} catch (err) {
|
||||
throw new Error(err);
|
||||
}
|
||||
})));
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user