Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defining entity in mikro-orm #22

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

```
Loading