Skip to content

Commit

Permalink
feat(core): allow specifying the runtimeType explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
B4nan committed May 2, 2024
1 parent 5a3e30e commit e9c0c07
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
41 changes: 21 additions & 20 deletions docs/docs/decorators.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,27 @@ export class Author { ... }

`@Property()` decorator is used to define regular entity property. All following decorators extend the `@Property()` decorator, so you can also use its parameters there.

| Parameter | Type | Optional | Description |
|--------------------|---------------------------------------------------|----------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `fieldName` | `string` | yes | Override default property name (see [Naming Strategy](./naming-strategy.md)). |
| `type` | `string` &#124; `Constructor<Type>` &#124; `Type` | yes | Explicitly specify the runtime type (see [Metadata Providers](./metadata-providers.md) and [Custom Types](./custom-types.md)). |
| `returning` | `boolean` | yes | Whether this property should be part of `returning` clause. Works only in PostgreSQL and SQLite drivers. |
| `onUpdate` | `() => any` | yes | Automatically update the property value every time entity gets updated. |
| `persist` | `boolean` | yes | Set to `false` to define [Shadow Property](serializing.md#shadow-properties). |
| `hydrate` | `boolean` | yes | Set to `false` to disable hydration of this property. Useful for persisted getters. |
| `hidden` | `boolean` | yes | Set to `true` to omit the property when [Serializing](serializing.md). |
| `groups` | `string[]` | yes | Specify serialization groups for [explicit serialization](serializing.md#explicit-serialization). If a property does not specify any group, it will be included, otherwise only properties with a matching group are included. |
| `columnType` | `string` | yes | Specify exact database column type for [Schema Generator](schema-generator.md). **(SQL only)** |
| `length` | `number` | yes | Length/precision of database column, used for `datetime/timestamp/varchar` column types for [Schema Generator](schema-generator.md). **(SQL only)** |
| `default` | `any` | yes | Specify default column value for [Schema Generator](schema-generator.md). **(SQL only)** |
| `unique` | `boolean` | yes | Set column as unique for [Schema Generator](schema-generator.md). **(SQL only)** |
| `nullable` | `boolean` | yes | Set column as nullable for [Schema Generator](schema-generator.md). **(SQL only)** |
| `unsigned` | `boolean` | yes | Set column as unsigned for [Schema Generator](schema-generator.md). **(SQL only)** |
| `comment` | `string` | yes | Specify comment of column for [Schema Generator](schema-generator.md). **(SQL only)** |
| `version` | `boolean` | yes | Set to true to enable [Optimistic Locking](transactions.md#optimistic-locking) via version field. **(SQL only)** |
| `concurrencyCheck` | `boolean` | yes | Set to true to enable [Concurrency Check](transactions.md#concurrency-checks) via concurrency fields. |
| `customOrder` | `string[]` &#124; `number[]` &#124; `boolean[]` | yes | Specify a custom order for the column. **(SQL only)** |
| Parameter | Type | Optional | Description |
|--------------------|---------------------------------------------------|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `fieldName` | `string` | yes | Override default property name (see [Naming Strategy](./naming-strategy.md)). |
| `type` | `string` &#124; `Constructor<Type>` &#124; `Type` | yes | Explicitly specify the property type. This value is mapped based on the current driver (see [Metadata Providers](./metadata-providers.md) and [Custom Types](./custom-types.md)). |
| `runtimeType` | `string` | yes | Runtime type of the property. This is the JS type that your property is mapped to, e.g. `string` or `number`, and is normally inferred automatically via `reflect-metadata`. In some cases, the inference won't work, and you might need to specify the `runtimeType` explicitly - the most common one is when you use a union type with null like `foo: number | null`. |
| `returning` | `boolean` | yes | Whether this property should be part of `returning` clause. Works only in PostgreSQL and SQLite drivers. |
| `onUpdate` | `() => any` | yes | Automatically update the property value every time entity gets updated. |
| `persist` | `boolean` | yes | Set to `false` to define [Shadow Property](serializing.md#shadow-properties). |
| `hydrate` | `boolean` | yes | Set to `false` to disable hydration of this property. Useful for persisted getters. |
| `hidden` | `boolean` | yes | Set to `true` to omit the property when [Serializing](serializing.md). |
| `groups` | `string[]` | yes | Specify serialization groups for [explicit serialization](serializing.md#explicit-serialization). If a property does not specify any group, it will be included, otherwise only properties with a matching group are included. |
| `columnType` | `string` | yes | Specify exact database column type for [Schema Generator](schema-generator.md). **(SQL only)** |
| `length` | `number` | yes | Length/precision of database column, used for `datetime/timestamp/varchar` column types for [Schema Generator](schema-generator.md). **(SQL only)** |
| `default` | `any` | yes | Specify default column value for [Schema Generator](schema-generator.md). **(SQL only)** |
| `unique` | `boolean` | yes | Set column as unique for [Schema Generator](schema-generator.md). **(SQL only)** |
| `nullable` | `boolean` | yes | Set column as nullable for [Schema Generator](schema-generator.md). **(SQL only)** |
| `unsigned` | `boolean` | yes | Set column as unsigned for [Schema Generator](schema-generator.md). **(SQL only)** |
| `comment` | `string` | yes | Specify comment of column for [Schema Generator](schema-generator.md). **(SQL only)** |
| `version` | `boolean` | yes | Set to true to enable [Optimistic Locking](transactions.md#optimistic-locking) via version field. **(SQL only)** |
| `concurrencyCheck` | `boolean` | yes | Set to true to enable [Concurrency Check](transactions.md#concurrency-checks) via concurrency fields. |
| `customOrder` | `string[]` &#124; `number[]` &#124; `boolean[]` | yes | Specify a custom order for the column. **(SQL only)** |

> You can use property initializers as usual.
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/decorators/Property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ export type PropertyOptions<Owner> = {
* @see https://mikro-orm.io/docs/custom-types
*/
type?: keyof typeof types | 'ObjectId' | Date | Constructor<AnyEntity> | Constructor<Type<any>> | Type<any> | (() => unknown) | ColumnType | AnyString;
/**
* Runtime type of the property. This is the JS type that your property is mapped to, e.g. `string` or `number`, and is normally inferred automatically via `reflect-metadata.
* In some cases, the inference won't work, and you might need to specify the `runtimeType` explicitly - the most common one is when you use a union type with null like `foo: number | null`.
*/
runtimeType?: string;
/**
* Set length of database column, used for datetime/timestamp/varchar column types for {@link https://mikro-orm.io/docs/schema-generator Schema Generator}. (SQL only)
*/
Expand Down
1 change: 0 additions & 1 deletion tests/issues/GH4899.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ beforeAll(async () => {
orm = await MikroORM.init({
dbName: ':memory:',
entities: [UserSkill, User, Skill],
// debug: true,
});
await orm.schema.createSchema();
});
Expand Down
1 change: 0 additions & 1 deletion tests/issues/GH5527.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ let orm: MikroORM;
beforeAll(async () => {
orm = await MikroORM.init({
dbName: '5527',
debug: true,
entities: [Test],
});
await orm.schema.refreshDatabase();
Expand Down

0 comments on commit e9c0c07

Please sign in to comment.