Skip to content

Commit

Permalink
docs(mikro-orm): use @Orm() decorator instead of @Connection
Browse files Browse the repository at this point in the history
closes #1741
  • Loading branch information
derevnjuk authored and Romakita committed Jan 29, 2022
1 parent 1efc2ed commit 6e91d4d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 33 deletions.
41 changes: 26 additions & 15 deletions docs/tutorials/mikroorm.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,40 +77,51 @@ export class Server {}

The `mikroOrm` options accepts the same configuration object as `init()` from the MikroORM package. Check [this page](https://mikro-orm.io/docs/configuration) for the complete configuration documentation.

## Obtain a connection
## Obtain ORM instance

`@Connection` decorator lets you retrieve an instance of MikroORM Connection.
`@Orm` decorator lets you retrieve an instance of MikroOrm.

```typescript
import {Injectable, AfterRoutesInit} from "@tsed/common";
import {Connection} from "@tsed/mikro-orm";
import {Orm} from "@tsed/mikro-orm";
import {MikroORM} from "@mikro-orm/core";

@Injectable()
export class UsersService {
@Connection()
private readonly connection!: MikroORM;
@Orm()
private readonly orm!: MikroORM;

async create(user: User): Promise<User> {

// do something
// ...
// Then save
await this.connection.em.persistAndFlush(user);
await this.orm.em.persistAndFlush(user);
console.log("Saved a new user with id: " + user.id);

return user;
}

async find(): Promise<User[]> {
const users = await this.connection.em.find(User, {});
const users = await this.orm.em.find(User, {});
console.log("Loaded users: ", users);

return users;
}
}
```

It's also possible to inject an ORM by a connection name:

```ts
import {Injectable} from "@tsed/di";

@Injectable()
export class MyService {
@Orm("mongo")
private readonly orm!: MikroORM;
}
```

## Obtain EntityManager

`@EntityManager` and `@Em` decorators lets you retrieve an instance of EntityManager.
Expand Down Expand Up @@ -167,22 +178,22 @@ import {Entity, Property, PrimaryKey, Property as Column} from "@mikro-orm/core"
export class User {
@PrimaryKey()
@Property()
id: number;
id!: number;

@Column()
@MaxLength(100)
@Required()
firstName: string;
firstName!: string;

@Column()
@MaxLength(100)
@Required()
lastName: string;
lastName!: string;

@Column()
@Mininum(0)
@Maximum(100)
age: number;
age!: number;
}
```

Expand All @@ -198,7 +209,7 @@ import {Controller, Post, BodyParams, Inject, Post, Get} from "@tsed/common";
@Controller("/users")
export class UsersCtrl {
@Inject()
private usersService: UsersService;
private readonly usersService!: UsersService;

@Post("/")
create(@BodyParams() user: User): Promise<User> {
Expand Down Expand Up @@ -227,7 +238,7 @@ import {Transactional} from "@tsed/mikro-orm";
@Controller("/users")
export class UsersCtrl {
@Inject()
private usersService: UsersService;
private readonly usersService!: UsersService;

@Post("/")
@Transactional()
Expand Down Expand Up @@ -314,7 +325,7 @@ import {Transactional} from "@tsed/mikro-orm";
@Controller("/users")
export class UsersCtrl {
@Inject()
private usersService: UsersService;
private readonly usersService!: UsersService;

@Post("/")
@Transactional({retry: true})
Expand Down
35 changes: 17 additions & 18 deletions packages/orm/mikro-orm/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,48 +96,48 @@ export class Server {}

The `mikroOrm` options accepts the same configuration object as `init()` from the MikroORM package. Check [this page](https://mikro-orm.io/docs/configuration) for the complete configuration documentation.

## Obtain a connection
## Obtain ORM instance

`@Connection` decorator lets you retrieve an instance of MikroOrm Connection.
`@Orm` decorator lets you retrieve an instance of MikroOrm.

```typescript
import {Injectable, AfterRoutesInit} from "@tsed/common";
import {Connection} from "@tsed/mikro-orm";
import {Orm} from "@tsed/mikro-orm";
import {MikroORM} from "@mikro-orm/core";

@Injectable()
export class UsersService {
@Connection()
private readonly connection!: MikroORM;
@Orm()
private readonly orm!: MikroORM;

async create(user: User): Promise<User> {
// do something
// ...
// Then save
await this.connection.em.persistAndFlush(user);
await this.orm.em.persistAndFlush(user);
console.log("Saved a new user with id: " + user.id);

return user;
}

async find(): Promise<User[]> {
const users = await this.connection.em.find(User, {});
const users = await this.orm.em.find(User, {});
console.log("Loaded users: ", users);

return users;
}
}
```

It's also possible to inject a connection by his name:
It's also possible to inject an ORM by a connection name:

```ts
import {Injectable} from "@tsed/di";

@Injectable()
export class MyService {
@Connection("mongo")
private readonly connection!: MikroORM;
@Orm("mongo")
private readonly orm!: MikroORM;
}
```

Expand Down Expand Up @@ -195,25 +195,24 @@ import {Entity, Property, PrimaryKey, Property as Column} from "@mikro-orm/core"

@Entity()
export class User {

@PrimaryKey()
@Property()
id: number;
id!: number;

@Column()
@MaxLength(100)
@Required()
firstName: string;
firstName!: string;

@Column()
@MaxLength(100)
@Required()
lastName: string;
lastName!: string;

@Column()
@Mininum(0)
@Maximum(100)
age: number;
age!: number;
}
```

Expand All @@ -229,7 +228,7 @@ import {Controller, Post, BodyParams, Inject, Post, Get} from "@tsed/common";
@Controller("/users")
export class UsersCtrl {
@Inject()
private usersService: UsersService;
private readonly usersService!: UsersService;

@Post("/")
create(@BodyParams() user: User): Promise<User> {
Expand Down Expand Up @@ -258,7 +257,7 @@ import {Transactional} from "@tsed/mikro-orm";
@Controller("/users")
export class UsersCtrl {
@Inject()
private usersService: UsersService;
private readonly usersService!: UsersService;

@Post("/")
@Transactional()
Expand Down Expand Up @@ -345,7 +344,7 @@ import {Transactional} from "@tsed/mikro-orm";
@Controller("/users")
export class UsersCtrl {
@Inject()
private usersService: UsersService;
private readonly usersService!: UsersService;

@Post("/")
@Transactional({retry: true})
Expand Down

0 comments on commit 6e91d4d

Please sign in to comment.