Skip to content

Commit

Permalink
feat(core): add forceConstructor option to @Entity() decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed Jan 24, 2023
1 parent 5e4e126 commit c89b4af
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
27 changes: 13 additions & 14 deletions docs/docs/decorators.md
Expand Up @@ -12,20 +12,19 @@ title: Decorators
`@Entity` decorator is used to mark your model classes as entities. Do not use it for
abstract base classes.

| Parameter | Type | Optional | Description |
|--------------------|--------------------------|----------|---------------------------------------------------------------------------------|
| `tableName` | `string` | yes | Override default collection/table name. |
| `schema` | `string` | yes | Sets the schema name |
| `collection` | `string` | yes | Alias for `tableName`. |
| `comment` | `string` | yes | Specify comment to table **(SQL only)** |
| `customRepository` | `() => EntityRepository` | yes | Set [custom repository class](repositories.md#custom-repository). |
| `discriminatorColumn` | `string` | yes | For [Single Table Inheritance](inheritance-mapping.md#single-table-inheritance) |
| `discriminatorMap` | `Dictionary<string>` | yes | For [Single Table Inheritance](inheritance-mapping.md#single-table-inheritance) |
| `discriminatorValue` | `number` &#124; `string` | yes | For [Single Table Inheritance](inheritance-mapping.md#single-table-inheritance) |

abstract | `boolean`;

readonly | `boolean`;
| Parameter | Type | Optional | Description |
|-----------------------|--------------------------|----------|----------------------------------------------------------------------------------|
| `tableName` | `string` | yes | Override default collection/table name. |
| `schema` | `string` | yes | Sets the schema name. |
| `collection` | `string` | yes | Alias for `tableName`. |
| `comment` | `string` | yes | Specify comment to table. **(SQL only)** |
| `customRepository` | `() => EntityRepository` | yes | Set [custom repository class](repositories.md#custom-repository). |
| `discriminatorColumn` | `string` | yes | For [Single Table Inheritance](inheritance-mapping.md#single-table-inheritance). |
| `discriminatorMap` | `Dictionary<string>` | yes | For [Single Table Inheritance](inheritance-mapping.md#single-table-inheritance). |
| `discriminatorValue` | `number` &#124; `string` | yes | For [Single Table Inheritance](inheritance-mapping.md#single-table-inheritance). |
| `forceConstructor` | `boolean` | yes | Enforce use of constructor when creating managed entity instances |
| `abstract` | `boolean` | yes | Marks entity as abstract, such entities are inlined during discovery. |
| `readonly` | `boolean` | yes | Disables change tracking - such entities are ignored during flush. |


```ts
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/decorators/Entity.ts
Expand Up @@ -24,6 +24,7 @@ export type EntityOptions<T> = {
discriminatorColumn?: string;
discriminatorMap?: Dictionary<string>;
discriminatorValue?: number | string;
forceConstructor?: boolean;
comment?: string;
abstract?: boolean;
readonly?: boolean;
Expand Down
7 changes: 3 additions & 4 deletions tests/issues/GH2406.test.ts
@@ -1,7 +1,7 @@
import { Collection, Entity, IdentifiedReference, ManyToOne, MikroORM, OneToMany, PrimaryKey } from '@mikro-orm/core';
import { SqliteDriver } from '@mikro-orm/sqlite';

@Entity()
@Entity({ forceConstructor: true })
export class Parent {

@PrimaryKey()
Expand All @@ -12,7 +12,7 @@ export class Parent {

}

@Entity()
@Entity({ forceConstructor: true })
export class Child {

@PrimaryKey()
Expand All @@ -31,15 +31,14 @@ describe('GH issue 2406', () => {
orm = await MikroORM.init({
entities: [Parent, Child],
dbName: ':memory:',
forceEntityConstructor: true,
driver: SqliteDriver,
});
await orm.schema.createSchema();
});

afterAll(() => orm.close(true));

test('should fetch children when forceEntityConstructor is turned on', async () => {
test('should fetch children when forceConstructor is turned on', async () => {
const parent = orm.em.create(Parent, {});
expect(parent.children.isInitialized()).toBe(true);
expect(parent.children.isDirty()).toBe(false);
Expand Down

0 comments on commit c89b4af

Please sign in to comment.