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: support nullable embedded entities
Support nullable embedded fields such that embedded documents in MongoDB can correctly be returned as null for a subdocument that is explicitly nullable. Closes: typeorm#3913
- Loading branch information
1 parent
e7649d2
commit 2b6aeb2
Showing
10 changed files
with
135 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
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,20 @@ | ||
import { ObjectId } from "mongodb" | ||
import { | ||
BaseEntity, | ||
Column, | ||
Entity, | ||
ObjectIdColumn | ||
} from "../../../../src" | ||
|
||
export class Embedded { | ||
a: string | ||
} | ||
|
||
@Entity() | ||
export class TestMongo extends BaseEntity { | ||
@ObjectIdColumn() | ||
_id: ObjectId | ||
|
||
@Column(() => Embedded, { nullable: true }) | ||
embedded: Embedded | 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,19 @@ | ||
import { | ||
BaseEntity, | ||
Column, | ||
Entity, | ||
PrimaryGeneratedColumn | ||
} from "../../../../src" | ||
|
||
export class Embedded { | ||
a: string | ||
} | ||
|
||
@Entity() | ||
export class TestSQL extends BaseEntity { | ||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@Column(() => Embedded, { nullable: true }) | ||
embedded: Embedded | 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,64 @@ | ||
import "reflect-metadata" | ||
import { expect } from "chai" | ||
import { | ||
closeTestingConnections, | ||
createTestingConnections, | ||
reloadTestingDatabases, | ||
} from "../../utils/test-utils" | ||
import { DataSource } from "../../../src/data-source/DataSource" | ||
import { TestMongo } from "./entity/TestMongo" | ||
import { TestSQL } from "./entity/TestSQL" | ||
import { MongoDriver } from "../../../src/driver/mongodb/MongoDriver" | ||
|
||
describe("github issues > #3913 Cannnot set embedded entity to null", () => { | ||
let connections: DataSource[] | ||
before( | ||
async () => | ||
(connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
cache: { | ||
alwaysEnabled: true, | ||
}, | ||
})), | ||
) | ||
beforeEach(() => reloadTestingDatabases(connections)) | ||
after(() => closeTestingConnections(connections)) | ||
|
||
it("should set the embedded entity to null in the database for mongodb", () => | ||
Promise.all( | ||
connections.map(async (connection) => { | ||
if (!(connection.driver instanceof MongoDriver)) return // Only run this test for mongodb | ||
const test = new TestMongo() | ||
test.embedded = null | ||
|
||
await connection.manager.save(test) | ||
|
||
const loadedTest = await connection.manager.findOne(TestMongo, { | ||
where: { _id: test._id }, | ||
}) | ||
expect(loadedTest).to.be.eql({ | ||
_id: test._id, | ||
embedded: null, | ||
}) | ||
}), | ||
)) | ||
|
||
it("should set the embedded entity to null in the database for non mongodb", () => | ||
Promise.all( | ||
connections.map(async (connection) => { | ||
if (connection.driver instanceof MongoDriver) return // Don't run this test for mongodb | ||
const test = new TestSQL() | ||
test.embedded = null | ||
|
||
await connection.manager.save(test) | ||
|
||
const loadedTest = await connection.manager.findOne(TestSQL, { | ||
where: { id: test.id }, | ||
}) | ||
expect(loadedTest).to.be.eql({ | ||
id: 1, | ||
embedded: null, | ||
}) | ||
}), | ||
)) | ||
}) |