Support orUpdate for SQLite

This commit is contained in:
Luis Emilio Velasco Sánchez 2019-05-07 14:01:25 +02:00
parent 360468b791
commit f435d991eb
No known key found for this signature in database
GPG Key ID: 3920ED6E5C0CFCD5
4 changed files with 76 additions and 1 deletions

View File

@ -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)

View File

@ -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(", ");
}
}

View File

@ -0,0 +1,13 @@
import { Entity, PrimaryColumn, Column } from "../../../../src";
@Entity()
export class User {
@PrimaryColumn()
email: string;
@PrimaryColumn()
username: string;
@Column()
bio: string;
}

View 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);
}
})));
});