-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add connection option
entitySkipConstructor
this adds the option for bypassing the constructor when deserializing the Entities. note that this may cause problems with class-based JavaScript features that rely on the constructor to operate - such as private properties or default values
- Loading branch information
1 parent
9bbdb01
commit f43d561
Showing
8 changed files
with
149 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import "reflect-metadata"; | ||
import { Connection } from "../../../src/connection/Connection"; | ||
import { expect } from "chai"; | ||
import { closeTestingConnections, createTestingConnections, reloadTestingDatabases } from "../../utils/test-utils"; | ||
import { TestCreate } from "./entity/TestCreate"; | ||
|
||
describe("entity-metadata > create", () => { | ||
describe("without entitySkipConstructor", () => { | ||
let connections: Connection[]; | ||
before(async () => connections = await createTestingConnections({ | ||
enabledDrivers: [ "sqlite" ], | ||
entities: [ | ||
TestCreate | ||
] | ||
}) | ||
); | ||
|
||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should call the constructor when creating an object", () => Promise.all(connections.map(async connection => { | ||
const entity = connection.manager.create(TestCreate); | ||
|
||
expect(entity.hasCalledConstructor).to.be.true; | ||
}))) | ||
|
||
it("should set the default property values", () => Promise.all(connections.map(async connection => { | ||
const entity = connection.manager.create(TestCreate); | ||
|
||
expect(entity.foo).to.be.equal("bar"); | ||
}))) | ||
|
||
it("should call the constructor when retrieving an object", () => Promise.all(connections.map(async connection => { | ||
const repo = connection.manager.getRepository(TestCreate); | ||
|
||
const { id } = await repo.save({ foo: "baz" }); | ||
|
||
const entity = await repo.findOneOrFail(id); | ||
|
||
expect(entity.hasCalledConstructor).to.be.true; | ||
}))) | ||
}) | ||
|
||
describe("with entitySkipConstructor", () => { | ||
let connections: Connection[]; | ||
before(async () => connections = await createTestingConnections({ | ||
enabledDrivers: [ "sqlite" ], | ||
entities: [ | ||
TestCreate | ||
], | ||
driverSpecific: { | ||
entitySkipConstructor: true, | ||
} | ||
})); | ||
|
||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should call the constructor when creating an object", () => Promise.all(connections.map(async connection => { | ||
const entity = connection.manager.create(TestCreate); | ||
|
||
expect(entity.hasCalledConstructor).to.be.true; | ||
}))) | ||
|
||
it("should set the default property values when creating an object", () => Promise.all(connections.map(async connection => { | ||
const entity = connection.manager.create(TestCreate); | ||
|
||
expect(entity.foo).to.be.equal("bar"); | ||
}))) | ||
|
||
it("should not call the constructor when retrieving an object", () => Promise.all(connections.map(async connection => { | ||
const repo = connection.manager.getRepository(TestCreate); | ||
|
||
const { id } = await repo.save({ foo: "baz" }); | ||
|
||
const entity = await repo.findOneOrFail(id); | ||
|
||
expect(entity.hasCalledConstructor).not.to.be.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,18 @@ | ||
import { Column } from "../../../../src/decorator/columns/Column"; | ||
import { Entity } from "../../../../src/decorator/entity/Entity"; | ||
import { PrimaryGeneratedColumn } from "../../../../src"; | ||
|
||
@Entity() | ||
export class TestCreate { | ||
constructor() { | ||
this.hasCalledConstructor = true; | ||
} | ||
|
||
hasCalledConstructor = false; | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
foo: string = 'bar'; | ||
} |