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: UpdateResult returns affected rows in postgresql (typeorm#4432)
* Added 'affected' field in UpdateResult as well as in DeleteResult. * PostgresQueryRunner returns the number of affected rows properly * UpdateQueryBuilder retrieves the affected rows returned by PostgresQueryRunner and sets the added 'affected' field of UpdateResult properly. Closes: typeorm#1308
- Loading branch information
Showing
6 changed files
with
137 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
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,34 @@ | ||
import {EntitySchemaOptions} from "../../../../src/entity-schema/EntitySchemaOptions"; | ||
import {Post} from "./Post"; | ||
|
||
export class Author { | ||
id: number; | ||
|
||
name: string; | ||
|
||
posts: Post[]; | ||
} | ||
|
||
export const AuthorSchema: EntitySchemaOptions<Author> = { | ||
name: "Author", | ||
|
||
target: Author, | ||
|
||
columns: { | ||
id: { | ||
primary: true, | ||
type: Number | ||
}, | ||
|
||
name: { | ||
type: "varchar" | ||
} | ||
}, | ||
|
||
relations: { | ||
posts: { | ||
target: () => Post, | ||
type: "one-to-many" | ||
} | ||
} | ||
}; |
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,35 @@ | ||
import {EntitySchemaOptions} from "../../../../src/entity-schema/EntitySchemaOptions"; | ||
import {Author} from "./Author"; | ||
|
||
export class Post { | ||
id: number; | ||
|
||
title: string; | ||
|
||
author: Author; | ||
} | ||
|
||
export const PostSchema: EntitySchemaOptions<Post> = { | ||
name: "Post", | ||
|
||
target: Post, | ||
|
||
columns: { | ||
id: { | ||
primary: true, | ||
type: Number | ||
}, | ||
|
||
title: { | ||
type: "varchar" | ||
} | ||
}, | ||
|
||
relations: { | ||
author: { | ||
target: () => Author, | ||
type: "many-to-one", | ||
eager: true | ||
} | ||
} | ||
}; |
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,49 @@ | ||
import { closeTestingConnections, createTestingConnections, reloadTestingDatabases } from "../../utils/test-utils"; | ||
import { Connection } from "../../../src/connection/Connection"; | ||
import { EntitySchema } from "../../../src"; | ||
import { Author, AuthorSchema } from "./entity/Author"; | ||
import { Post, PostSchema } from "./entity/Post"; | ||
|
||
describe("github issues > #1308 Raw Postgresql Update query result is always an empty array", () => { | ||
let connections: Connection[]; | ||
before( | ||
async () => | ||
(connections = await createTestingConnections({ | ||
entities: [new EntitySchema<Author>(AuthorSchema), new EntitySchema<Post>(PostSchema)], | ||
dropSchema: true, | ||
enabledDrivers: ["postgres"], | ||
})) | ||
); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
async function prepareData(connection: Connection) { | ||
const author = new Author(); | ||
author.id = 1; | ||
author.name = "Jane Doe"; | ||
await connection.manager.save(author); | ||
} | ||
|
||
it("Update query returns the number of affected rows", () => | ||
Promise.all( | ||
connections.map(async connection => { | ||
await prepareData(connection); | ||
|
||
const result1 = await connection.createQueryBuilder() | ||
.update(Author) | ||
.set({ name: "John Doe" }) | ||
.where("name = :name", { name: "Jonas Doe" }) | ||
.execute(); | ||
|
||
result1.affected!.should.be.eql(0); | ||
|
||
const result2 = await connection.createQueryBuilder() | ||
.update(Author) | ||
.set({ name: "John Doe" }) | ||
.where("name = :name", { name: "Jane Doe" }) | ||
.execute(); | ||
|
||
result2.affected!.should.be.eql(1); | ||
}) | ||
)); | ||
}); |