Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: find operation in MongoDB do not include nullable values from do…
…cuments (#7820) Find operation should add all nullable values from documents if they exist Closes: #7760
- Loading branch information
Showing
3 changed files
with
78 additions
and
1 deletion.
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 {Column, Entity, ObjectIdColumn} from "../../../../src"; | ||
|
||
@Entity("test") | ||
export class TestEntity { | ||
@ObjectIdColumn() | ||
_id: string; | ||
|
||
@Column() | ||
name: string; | ||
|
||
@Column({ nullable: true }) | ||
testId: string | null; | ||
} |
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,65 @@ | ||
import "reflect-metadata"; | ||
import { expect } from "chai"; | ||
import { Connection } from "../../../src"; | ||
import { closeTestingConnections, createTestingConnections, reloadTestingDatabases } from "../../utils/test-utils"; | ||
import { TestEntity } from "./entity/test.entity"; | ||
|
||
describe("github issues > #7760 Mongodb: When field is null in db, typeorm query sets it to undefined", () => { | ||
|
||
let connections: Connection[]; | ||
before(async () => { | ||
connections = await createTestingConnections({ | ||
enabledDrivers: ["mongodb"], | ||
entities: [TestEntity], | ||
schemaCreate: false, | ||
dropSchema: true | ||
}); | ||
}); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should delete all documents related to search pattern", () => Promise.all(connections.map(async connection => { | ||
const testEntityRepository = connection.getRepository(TestEntity); | ||
|
||
// save few documents | ||
const firstEntity = new TestEntity(); | ||
firstEntity.name = "First"; | ||
firstEntity.testId = "1"; | ||
await testEntityRepository.save(firstEntity); | ||
|
||
const secondEntity = new TestEntity(); | ||
secondEntity.name = "Second"; | ||
secondEntity.testId = null; | ||
await testEntityRepository.save(secondEntity); | ||
|
||
const thirdEntity = new TestEntity(); | ||
thirdEntity.name = "Third"; | ||
thirdEntity.testId = "3"; | ||
await testEntityRepository.save(thirdEntity); | ||
|
||
const fourthEntity = new TestEntity(); | ||
fourthEntity.name = "Fourth"; | ||
fourthEntity.testId = null; | ||
await testEntityRepository.save(fourthEntity); | ||
|
||
const loadedEntities = await testEntityRepository.find(); | ||
|
||
expect(loadedEntities.length).to.be.eql(4); | ||
|
||
expect(loadedEntities[0]).to.be.instanceOf(TestEntity); | ||
expect(loadedEntities[0]!.name).to.be.equal(firstEntity.name); | ||
expect(loadedEntities[0]!.testId).to.be.eql("1"); | ||
|
||
expect(loadedEntities[1]).to.be.instanceOf(TestEntity); | ||
expect(loadedEntities[1]!.name).to.be.equal(secondEntity.name); | ||
expect(loadedEntities[1]!.testId).to.be.eql(null); | ||
|
||
expect(loadedEntities[2]).to.be.instanceOf(TestEntity); | ||
expect(loadedEntities[2]!.name).to.be.equal(thirdEntity.name); | ||
expect(loadedEntities[2]!.testId).to.be.eql("3"); | ||
|
||
expect(loadedEntities[3]).to.be.instanceOf(TestEntity); | ||
expect(loadedEntities[3]!.name).to.be.equal(fourthEntity.name); | ||
expect(loadedEntities[3]!.testId).to.be.eql(null); | ||
}))); | ||
}); |