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.
fix: update/delete/softDelete by criteria of condition objects
- Loading branch information
1 parent
79960e1
commit 8abe2e3
Showing
4 changed files
with
160 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src" | ||
|
||
@Entity() | ||
export class Post { | ||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@Column() | ||
title: string | ||
} |
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,138 @@ | ||
import "reflect-metadata" | ||
import { expect } from "chai" | ||
import { DataSource } from "../../../src/data-source/DataSource" | ||
import { | ||
closeTestingConnections, | ||
createTestingConnections, | ||
reloadTestingDatabases, | ||
} from "../../utils/test-utils" | ||
import { Post } from "./entity/Post" | ||
|
||
describe("github issues > #10517 EntityManager update/delete/softDelete don't work with list of where condition objects", function () { | ||
// ------------------------------------------------------------------------- | ||
// Configuration | ||
// ------------------------------------------------------------------------- | ||
|
||
let connections: DataSource[] | ||
before( | ||
async () => | ||
(connections = await createTestingConnections({ | ||
entities: [Post], | ||
})), | ||
) | ||
beforeEach(() => reloadTestingDatabases(connections)) | ||
after(() => closeTestingConnections(connections)) | ||
|
||
// ------------------------------------------------------------------------- | ||
// Specifications | ||
// ------------------------------------------------------------------------- | ||
|
||
it("update by array of condition objects", () => | ||
Promise.all( | ||
connections.map(async (connection) => { | ||
const postRepository = connection.getRepository(Post) | ||
|
||
// save a new posts | ||
const newPost1 = postRepository.create() | ||
newPost1.title = "Super post #1" | ||
const newPost2 = postRepository.create() | ||
newPost2.title = "Super post #2" | ||
const newPost3 = postRepository.create() | ||
newPost3.title = "Super post #3" | ||
const newPost4 = postRepository.create() | ||
newPost4.title = "Super post #4" | ||
|
||
await postRepository.save(newPost1) | ||
await postRepository.save(newPost2) | ||
await postRepository.save(newPost3) | ||
await postRepository.save(newPost4) | ||
|
||
// update many | ||
await postRepository.update( | ||
[ | ||
{ | ||
title: "Super post #1", | ||
}, | ||
{ | ||
title: "Super post #2", | ||
}, | ||
], | ||
{ title: "Super post" }, | ||
) | ||
|
||
// load to check | ||
const loadedPost1 = await postRepository.findOne({ | ||
where: { | ||
id: 1, | ||
}, | ||
}) | ||
const loadedPost2 = await postRepository.findOne({ | ||
where: { | ||
id: 2, | ||
}, | ||
}) | ||
|
||
// assert | ||
expect(loadedPost1).to.be.eql({ | ||
id: 1, | ||
title: "Super post", | ||
}) | ||
|
||
expect(loadedPost2).to.be.eql({ | ||
id: 2, | ||
title: "Super post", | ||
}) | ||
}), | ||
)) | ||
|
||
it("delete by array of condition objects", () => | ||
Promise.all( | ||
connections.map(async (connection) => { | ||
const postRepository = connection.getRepository(Post) | ||
|
||
// save a new posts | ||
const newPost1 = postRepository.create() | ||
newPost1.title = "Super post #1" | ||
const newPost2 = postRepository.create() | ||
newPost2.title = "Super post #2" | ||
const newPost3 = postRepository.create() | ||
newPost3.title = "Super post #3" | ||
const newPost4 = postRepository.create() | ||
newPost4.title = "Super post #4" | ||
|
||
await postRepository.save(newPost1) | ||
await postRepository.save(newPost2) | ||
await postRepository.save(newPost3) | ||
await postRepository.save(newPost4) | ||
|
||
// update many | ||
await postRepository.delete( | ||
[ | ||
{ | ||
title: "Super post #1", | ||
}, | ||
{ | ||
title: "Super post #2", | ||
}, | ||
], | ||
) | ||
|
||
// load to check | ||
const loadedPost1 = await postRepository.findOne({ | ||
where: { | ||
id: 1, | ||
}, | ||
}) | ||
const loadedPost2 = await postRepository.findOne({ | ||
where: { | ||
id: 2, | ||
}, | ||
}) | ||
|
||
// assert | ||
expect(loadedPost1).to.be.eql(null) | ||
|
||
expect(loadedPost2).to.be.eql(null) | ||
}), | ||
)) | ||
}) |