Skip to content

Commit

Permalink
fix: find operation in MongoDB do not include nullable values from do…
Browse files Browse the repository at this point in the history
…cuments (#7820)

Find operation should add all nullable values from documents if they exist

Closes: #7760
  • Loading branch information
SnapeEye committed Jun 30, 2021
1 parent e2a5939 commit 98c13cf
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
Expand Up @@ -70,7 +70,6 @@ export class DocumentToEntityTransformer {
metadata.ownColumns.forEach(column => {
const valueInObject = document[column.databaseNameWithoutPrefixes];
if (valueInObject !== undefined &&
valueInObject !== null &&
column.propertyName &&
!column.isVirtual) {
// const value = this.driver.prepareHydratedValue(valueInObject, column);
Expand Down
13 changes: 13 additions & 0 deletions test/github-issues/7760/entity/test.entity.ts
@@ -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;
}
65 changes: 65 additions & 0 deletions test/github-issues/7760/issue-7760.ts
@@ -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);
})));
});

0 comments on commit 98c13cf

Please sign in to comment.