-
-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mikro-orm): add entityManager() and orm() functions to inject re…
…spectively ORM and EM instance
- Loading branch information
Showing
6 changed files
with
218 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 92 additions & 17 deletions
109
packages/orm/mikro-orm/src/decorators/entityManager.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,97 @@ | ||
import {DecoratorTypes, Store} from "@tsed/core"; | ||
import {Controller, INJECTABLE_PROP} from "@tsed/di"; | ||
import {MikroORM} from "@mikro-orm/core"; | ||
import {MongoEntityManager} from "@mikro-orm/mongodb"; | ||
import {EntityManager} from "./entityManager.js"; | ||
|
||
@Controller("/users") | ||
export class UsersCtrl { | ||
@EntityManager() | ||
public readonly em!: MongoEntityManager; | ||
} | ||
|
||
describe("@Orm", () => { | ||
it("should decorate property", () => { | ||
expect(Store.from(UsersCtrl).get(INJECTABLE_PROP)).toEqual({ | ||
em: { | ||
propertyKey: "em", | ||
bindingType: DecoratorTypes.PROP, | ||
resolver: expect.any(Function) | ||
import {DITest, Injectable} from "@tsed/di"; | ||
import {afterEach, beforeEach} from "vitest"; | ||
import {MikroOrmRegistry} from "../services/MikroOrmRegistry.js"; | ||
import {Em, entityManager, EntityManager} from "./entityManager.js"; | ||
|
||
describe("@EntityManager()", () => { | ||
beforeEach(() => DITest.create()); | ||
afterEach(() => DITest.reset()); | ||
|
||
describe("decorator", () => { | ||
it("should decorate property (without context)", async () => { | ||
@Injectable() | ||
class UsersService { | ||
@Em() | ||
public readonly em!: MongoEntityManager; | ||
} | ||
|
||
const ormRegistry = { | ||
get: vi.fn().mockReturnValue({em: {id: "id"}} as never) | ||
}; | ||
|
||
const usersService = await DITest.invoke<UsersService>(UsersService, [ | ||
{ | ||
token: MikroOrmRegistry, | ||
use: ormRegistry | ||
} | ||
]); | ||
expect(ormRegistry.get).toHaveBeenCalledWith(undefined); | ||
expect(usersService.em).toEqual({id: "id"}); | ||
}); | ||
it("should decorate property (with context)", async () => { | ||
@Injectable() | ||
class UsersService { | ||
@EntityManager("context") | ||
public readonly orm!: MikroORM; | ||
} | ||
|
||
const ormRegistry = { | ||
get: vi.fn().mockReturnValue({em: {id: "id"}} as never) | ||
}; | ||
|
||
const usersService = await DITest.invoke<UsersService>(UsersService, [ | ||
{ | ||
token: MikroOrmRegistry, | ||
use: ormRegistry | ||
} | ||
]); | ||
|
||
expect(ormRegistry.get).toHaveBeenCalledWith("context"); | ||
expect(usersService.orm).toEqual({id: "id"}); | ||
}); | ||
}); | ||
|
||
describe("prop function", () => { | ||
it("should inject property (without context)", async () => { | ||
@Injectable() | ||
class UsersService { | ||
public readonly em = entityManager(); | ||
} | ||
|
||
const ormRegistry = { | ||
get: vi.fn().mockReturnValue({em: {id: "id"}} as never) | ||
}; | ||
|
||
const usersService = await DITest.invoke<UsersService>(UsersService, [ | ||
{ | ||
token: MikroOrmRegistry, | ||
use: ormRegistry | ||
} | ||
]); | ||
expect(ormRegistry.get).toHaveBeenCalledWith(undefined); | ||
expect(usersService.em).toEqual({id: "id"}); | ||
}); | ||
it("should inject property (with context)", async () => { | ||
@Injectable() | ||
class UsersService { | ||
public readonly em = entityManager("context"); | ||
} | ||
|
||
const ormRegistry = { | ||
get: vi.fn().mockReturnValue({em: {id: "id"}} as never) | ||
}; | ||
|
||
const usersService = await DITest.invoke<UsersService>(UsersService, [ | ||
{ | ||
token: MikroOrmRegistry, | ||
use: ormRegistry | ||
} | ||
]); | ||
|
||
expect(ormRegistry.get).toHaveBeenCalledWith("context"); | ||
expect(usersService.em).toEqual({id: "id"}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,96 @@ | ||
import {DecoratorTypes, Store} from "@tsed/core"; | ||
import {Controller, INJECTABLE_PROP} from "@tsed/di"; | ||
import {Orm} from "./orm.js"; | ||
import {MikroORM} from "@mikro-orm/core"; | ||
import {DITest, Injectable} from "@tsed/di"; | ||
import {afterEach, beforeEach} from "vitest"; | ||
import {MikroOrmRegistry} from "../services/MikroOrmRegistry.js"; | ||
import {orm, Orm} from "./orm.js"; | ||
|
||
@Controller("/users") | ||
export class UsersCtrl { | ||
@Orm() | ||
public readonly orm!: MikroORM; | ||
} | ||
|
||
describe("@Orm", () => { | ||
it("should decorate property", () => { | ||
expect(Store.from(UsersCtrl).get(INJECTABLE_PROP)).toEqual({ | ||
orm: { | ||
propertyKey: "orm", | ||
bindingType: DecoratorTypes.PROP, | ||
resolver: expect.any(Function) | ||
describe("@Orm()", () => { | ||
beforeEach(() => DITest.create()); | ||
afterEach(() => DITest.reset()); | ||
|
||
describe("decorator", () => { | ||
it("should decorate property (without context)", async () => { | ||
@Injectable() | ||
class UsersService { | ||
@Orm() | ||
public readonly orm!: MikroORM; | ||
} | ||
|
||
const ormRegistry = { | ||
get: vi.fn().mockReturnValue({id: "id"} as never) | ||
}; | ||
|
||
const usersService = await DITest.invoke<UsersService>(UsersService, [ | ||
{ | ||
token: MikroOrmRegistry, | ||
use: ormRegistry | ||
} | ||
]); | ||
expect(ormRegistry.get).toHaveBeenCalledWith(undefined); | ||
expect(usersService.orm).toEqual({id: "id"}); | ||
}); | ||
it("should decorate property (with context)", async () => { | ||
@Injectable() | ||
class UsersService { | ||
@Orm("context") | ||
public readonly orm!: MikroORM; | ||
} | ||
|
||
const ormRegistry = { | ||
get: vi.fn().mockReturnValue({id: "id"} as never) | ||
}; | ||
|
||
const usersService = await DITest.invoke<UsersService>(UsersService, [ | ||
{ | ||
token: MikroOrmRegistry, | ||
use: ormRegistry | ||
} | ||
]); | ||
|
||
expect(ormRegistry.get).toHaveBeenCalledWith("context"); | ||
expect(usersService.orm).toEqual({id: "id"}); | ||
}); | ||
}); | ||
|
||
describe("prop function", () => { | ||
it("should inject property (without context)", async () => { | ||
@Injectable() | ||
class UsersService { | ||
public readonly orm = orm(); | ||
} | ||
|
||
const ormRegistry = { | ||
get: vi.fn().mockReturnValue({id: "id"} as never) | ||
}; | ||
|
||
const usersService = await DITest.invoke<UsersService>(UsersService, [ | ||
{ | ||
token: MikroOrmRegistry, | ||
use: ormRegistry | ||
} | ||
]); | ||
expect(ormRegistry.get).toHaveBeenCalledWith(undefined); | ||
expect(usersService.orm).toEqual({id: "id"}); | ||
}); | ||
it("should inject property (with context)", async () => { | ||
@Injectable() | ||
class UsersService { | ||
public readonly orm = orm("context"); | ||
} | ||
|
||
const ormRegistry = { | ||
get: vi.fn().mockReturnValue({id: "id"} as never) | ||
}; | ||
|
||
const usersService = await DITest.invoke<UsersService>(UsersService, [ | ||
{ | ||
token: MikroOrmRegistry, | ||
use: ormRegistry | ||
} | ||
]); | ||
|
||
expect(ormRegistry.get).toHaveBeenCalledWith("context"); | ||
expect(usersService.orm).toEqual({id: "id"}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,18 @@ | ||
import {MikroORM} from "@mikro-orm/core"; | ||
import {inject, Inject} from "@tsed/di"; | ||
import {MikroOrmRegistry} from "../services/MikroOrmRegistry.js"; | ||
import {Inject} from "@tsed/di"; | ||
|
||
/** | ||
* Get the ORM for the given context name using new inject() function. | ||
* @param contextName | ||
*/ | ||
export function orm(contextName?: string): MikroORM | undefined { | ||
return inject(MikroOrmRegistry).get(contextName); | ||
} | ||
|
||
/** | ||
* Get the ORM for the given context name. | ||
* @param {String} contextName | ||
*/ | ||
export const Orm = (contextName?: string): PropertyDecorator => | ||
Inject(MikroOrmRegistry, (registry: MikroOrmRegistry) => registry.get(contextName)) as PropertyDecorator; | ||
Inject(MikroOrmRegistry, {transform: (registry: MikroOrmRegistry) => registry.get(contextName)}) as PropertyDecorator; |
29 changes: 19 additions & 10 deletions
29
packages/orm/mikro-orm/src/decorators/transactional.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,33 @@ | ||
import {Post} from "@tsed/common"; | ||
import {Store} from "@tsed/core"; | ||
import {Controller, INJECTABLE_PROP} from "@tsed/di"; | ||
import {Controller, DITest} from "@tsed/di"; | ||
import {afterEach, beforeEach} from "vitest"; | ||
import {TransactionalInterceptor} from "../interceptors/TransactionalInterceptor.js"; | ||
import {Transactional} from "./transactional.js"; | ||
|
||
@Controller("/users") | ||
export class UsersCtrl { | ||
@Post("/") | ||
@Transactional() | ||
create() {} | ||
create(): any {} | ||
} | ||
|
||
describe("@Transactional", () => { | ||
it("should decorate method", () => { | ||
expect(Store.from(UsersCtrl).get(INJECTABLE_PROP)).toEqual({ | ||
create: { | ||
bindingType: "interceptor", | ||
propertyKey: "create", | ||
useType: TransactionalInterceptor | ||
beforeEach(() => DITest.create()); | ||
afterEach(() => DITest.reset()); | ||
it("should decorate method", async () => { | ||
const interceptor = { | ||
intercept: vi.fn().mockResolvedValue({}) | ||
}; | ||
|
||
const usersService = await DITest.invoke<UsersCtrl>(UsersCtrl, [ | ||
{ | ||
token: TransactionalInterceptor, | ||
use: interceptor | ||
} | ||
}); | ||
]); | ||
|
||
const result = await usersService.create(); | ||
|
||
expect(result).toEqual({}); | ||
}); | ||
}); |