Skip to content

Commit

Permalink
Merge pull request #22 from hpi-schul-cloud/microORM-entity-experience
Browse files Browse the repository at this point in the history
Defining entity in mikro-orm
  • Loading branch information
EzzatOmar authored Feb 20, 2024
2 parents 7f2398d + 2feb5b8 commit e2c7474
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions docs/schulcloud-server/Coding-Guidelines/micro-orm.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Defining Entities

When defining entities with MikroORM (Version 5), the following should be considered:
The property decorator requires explicit assignment of the type to the property and may not work correctly when working with type inference or assigning union types to a property. In these cases, the metadata may not be set correctly, which can lead to exceptions, for example, when using the `em.assign()` or `em.aggregate()` functions.

Therefore, the following is **not** sufficient:

```TypeScript
@Property()
termsAccepted = false;

@Property()
createdAt = new Date();

```

The following works:

```TypeScript
@Property()
termsAccepted: boolean = false;

@Property()
createdAt: Date = new Date();

```

The better way is to provide the type through the decorator:

```TypeScript
@Property({ type: 'boolean' })
termsAccepted = false;

@Property({ type: Date })
createdAt = new Date();

```

Errors can also occur when specifying multiple types (union types):

```TypeScript
@Poperty({ nullable: true })
dueDate: Date | null;

```

To set the metadata correctly, do the following:

```TypeScript
@Property({ type: Date, nullable: true })
dueDate: Date | null;

```

If type inference is not used, specifying the type through the property decorator is not necessary:

```TypeScript
@Property()
name: string;

```

0 comments on commit e2c7474

Please sign in to comment.