Skip to content

Commit

Permalink
feat(mikro-orm): Add EntityManager decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed Dec 30, 2021
1 parent 5bdb3f8 commit 4840857
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 21 deletions.
80 changes: 71 additions & 9 deletions docs/tutorials/mikroorm.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,35 @@ Currently, `@tsed/mikro-orm` allows you:

To begin, install the MikroORM module for TS.ED:

<Tabs class="-code">
<Tab label="NPM">

```bash
npm install @mikro-orm/core @tsed/mikro-orm @mikro-orm/mongodb # for mongo
npm install @mikro-orm/core @tsed/mikro-orm @mikro-orm/mysql # for mysql/mariadb
npm install @mikro-orm/core @tsed/mikro-orm @mikro-orm/mariadb # for mysql/mariadb
npm install @mikro-orm/core @tsed/mikro-orm @mikro-orm/postgresql # for postgresql
npm install @mikro-orm/core @tsed/mikro-orm @mikro-orm/sqlite # for sqlite
```

</Tab>
<Tab label="Yarn">

```bash
npm install --save @tsed/mikro-orm
npm install --save @mikro-orm/core
yarn add @mikro-orm/core @tsed/mikro-orm @mikro-orm/mongodb # for mongo
yarn add @mikro-orm/core @tsed/mikro-orm @mikro-orm/mysql # for mysql/mariadb
yarn add @mikro-orm/core @tsed/mikro-orm @mikro-orm/mariadb # for mysql/mariadb
yarn add @mikro-orm/core @tsed/mikro-orm @mikro-orm/postgresql # for postgresql
yarn add @mikro-orm/core @tsed/mikro-orm @mikro-orm/sqlite # for sqlite
```

Then import `@tsed/mikro-orm` in your Server:
</Tab>
</Tabs>

Once the installation process is completed, we can import the `MikroOrmModule` into the `Server` configuration:

```typescript
import {Configuration} from "@tsed/common";
import {Configuration} from "@tsed/di";
import {MikroOrmModule} from "@tsed/mikro-orm";

@Configuration({
Expand All @@ -52,16 +72,14 @@ import {MikroOrmModule} from "@tsed/mikro-orm";
}
]
})
export class Server {

}
export class Server {}
```

For more information about MikroORM look his documentation [here](https://mikro-orm.io/docs);
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

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

```typescript
import {Injectable, AfterRoutesInit} from "@tsed/common";
Expand Down Expand Up @@ -93,6 +111,50 @@ export class UsersService {
}
```

## Obtain EntityManager

`@EntityManager` and `@Em` decorators lets you retrieve an instance of EntityManager.

```typescript
import {Injectable, AfterRoutesInit} from "@tsed/common";
import {Em} from "@tsed/mikro-orm";
import {EntityManager} from '@mikro-orm/mysql'; // Import EntityManager from your driver package or `@mikro-orm/knex`

@Injectable()
export class UsersService {
@Em()
private readonly em!: EntityManager;

async create(user: User): Promise<User> {
await this.em.persistAndFlush(user);
console.log("Saved a new user with id: " + user.id);

return user;
}
}
```

It's also possible to inject Entity manager by his connection name:

```typescript
import {Injectable, AfterRoutesInit} from "@tsed/common";
import {Em} from "@tsed/mikro-orm";
import {EntityManager} from '@mikro-orm/mysql'; // Import EntityManager from your driver package or `@mikro-orm/knex`

@Injectable()
export class UsersService {
@Em("connectionName")
private readonly em!: EntityManager;

async create(user: User): Promise<User> {
await this.em.persistAndFlush(user);
console.log("Saved a new user with id: " + user.id);

return user;
}
}
```

## Use Entity with Controller

To begin, we need to define an Entity MikroORM like this and use Ts.ED Decorator to define the JSON Schema.
Expand Down
93 changes: 83 additions & 10 deletions packages/orm/mikro-orm/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,35 @@ Currently, `@tsed/mikro-orm` allows you:

To begin, install the MikroOrm module for TS.ED:

<Tabs class="-code">
<Tab label="NPM">

```bash
npm install @mikro-orm/core @tsed/mikro-orm @mikro-orm/mongodb # for mongo
npm install @mikro-orm/core @tsed/mikro-orm @mikro-orm/mysql # for mysql/mariadb
npm install @mikro-orm/core @tsed/mikro-orm @mikro-orm/mariadb # for mysql/mariadb
npm install @mikro-orm/core @tsed/mikro-orm @mikro-orm/postgresql # for postgresql
npm install @mikro-orm/core @tsed/mikro-orm @mikro-orm/sqlite # for sqlite
```

</Tab>
<Tab label="Yarn">

```bash
npm install --save @tsed/mikro-orm
npm install --save @mikro-orm/core
yarn add @mikro-orm/core @tsed/mikro-orm @mikro-orm/mongodb # for mongo
yarn add @mikro-orm/core @tsed/mikro-orm @mikro-orm/mysql # for mysql/mariadb
yarn add @mikro-orm/core @tsed/mikro-orm @mikro-orm/mariadb # for mysql/mariadb
yarn add @mikro-orm/core @tsed/mikro-orm @mikro-orm/postgresql # for postgresql
yarn add @mikro-orm/core @tsed/mikro-orm @mikro-orm/sqlite # for sqlite
```

Then import `@tsed/mikro-orm` in your Server:
</Tab>
</Tabs>

Once the installation process is completed, we can import the `MikroOrmModule` into the `Server` configuration:

```typescript
import {Configuration} from "@tsed/common";
import {Configuration} from "@tsed/di";
import {MikroOrmModule} from "@tsed/mikro-orm";

@Configuration({
Expand All @@ -71,16 +91,14 @@ import {MikroOrmModule} from "@tsed/mikro-orm";
}
]
})
export class Server {

}
export class Server {}
```

For more information about MikroOrm look his documentation [here](https://mikro-orm.io/docs);
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

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

```typescript
import {Injectable, AfterRoutesInit} from "@tsed/common";
Expand All @@ -93,7 +111,6 @@ export class UsersService {
private readonly connection!: MikroORM;

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

// do something
// ...
// Then save
Expand All @@ -112,6 +129,62 @@ export class UsersService {
}
```

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

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

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

## Obtain EntityManager

`@EntityManager` and `@Em` decorators lets you retrieve an instance of EntityManager.

```typescript
import {Injectable, AfterRoutesInit} from "@tsed/common";
import {Em} from "@tsed/mikro-orm";
import {EntityManager} from '@mikro-orm/mysql'; // Import EntityManager from your driver package or `@mikro-orm/knex`

@Injectable()
export class UsersService {
@Em()
private readonly em!: EntityManager;

async create(user: User): Promise<User> {
await this.em.persistAndFlush(user);
console.log("Saved a new user with id: " + user.id);

return user;
}
}
```

It's also possible to inject Entity manager by his connection name:

```typescript
import {Injectable, AfterRoutesInit} from "@tsed/common";
import {Em} from "@tsed/mikro-orm";
import {EntityManager} from '@mikro-orm/mysql'; // Import EntityManager from your driver package or `@mikro-orm/knex`

@Injectable()
export class UsersService {
@Em("connectionName")
private readonly em!: EntityManager;

async create(user: User): Promise<User> {
await this.em.persistAndFlush(user);
console.log("Saved a new user with id: " + user.id);

return user;
}
}
```

## Use Entity with Controller

To begin, we need to define an Entity MikroOrm like this and use Ts.ED Decorator to define the JSON Schema.
Expand Down
19 changes: 19 additions & 0 deletions packages/orm/mikro-orm/src/decorators/entityManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {Inject} from "@tsed/di";
import {MikroOrmRegistry} from "../services";

/**
* Get the entity manager for the given connection name.
* @param connectionName
* @decorator
* @mikroOrm
*/
export const EntityManager = (connectionName?: string): PropertyDecorator =>
Inject(MikroOrmRegistry, (registry: MikroOrmRegistry) => registry.get(connectionName)?.em) as PropertyDecorator;

/**
* Get the entity manager for the given connection name.
* @param connectionName
* @decorator
* @mikroOrm
*/
export const Em = EntityManager;
1 change: 1 addition & 0 deletions packages/orm/mikro-orm/src/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./transactional";
export * from "./connection";
export * from "./entityManager";
16 changes: 14 additions & 2 deletions packages/orm/mikro-orm/test/helpers/services/UserService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Injectable} from "@tsed/di";
import {Connection} from "../../../src";
import {MikroORM} from "@mikro-orm/core";
import {EntityManager, MikroORM} from "@mikro-orm/core";
import {Connection, Em} from "../../../src";

@Injectable()
export class UserService {
Expand All @@ -12,4 +12,16 @@ export class UserService {

@Connection("db2")
connection2!: MikroORM;

@Em()
em!: EntityManager;

@Em("db1")
em1!: EntityManager;

@Em("db2")
em2!: EntityManager;

@Em("db3")
em3!: EntityManager;
}
6 changes: 6 additions & 0 deletions packages/orm/mikro-orm/test/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,16 @@ describe("TypeORM integration", () => {
expect(service).toBeInstanceOf(UserService);
expect(service.connection).toBeInstanceOf(MikroORM);
expect(service.connection.em.name).toBe("default");
expect(service.em.name).toBe("default");

expect(service.connection1).toBeInstanceOf(MikroORM);
expect(service.connection1.em.name).toBe("db1");
expect(service.em1.name).toBe("db1");

expect(service.connection2).toBeInstanceOf(MikroORM);
expect(service.connection2.em.name).toBe("db2");
expect(service.em2.name).toBe("db2");

expect(service.em3).toEqual(undefined);
});
});

0 comments on commit 4840857

Please sign in to comment.