Skip to content

Commit 9c01dda

Browse files
authored
docs: add code comments to commonly used types and decorators (#183)
1 parent 611a33c commit 9c01dda

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/mikro-orm.common.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,49 @@ export const MIKRO_ORM_MODULE_OPTIONS = Symbol('mikro-orm-module-options');
66
export const CONTEXT_NAMES: string[] = [];
77
export const logger = new Logger(MikroORM.name);
88

9+
/**
10+
* Gets the injection token based on context name for the relevant MikroORM provider.
11+
* @param name The context name of the database connection.
12+
* @returns The MikroORM provider injection token for the supplied context name.
13+
*/
914
export const getMikroORMToken = (name: string) => `${name}_MikroORM`;
15+
/**
16+
* Injects a MikroORM provider based on the supplied context name.
17+
*
18+
* @param name The context name of the database connection.
19+
* @returns A parameter decorator which will cause NestJS to inject the relevant MikroORM provider.
20+
*/
1021
export const InjectMikroORM = (name: string) => Inject(getMikroORMToken(name));
1122

23+
/**
24+
* Gets the injection token based on context name for the relevant EntityManager provider.
25+
* @param name The context name of the database connection.
26+
* @returns The EntityManager provider injection token for the supplied context name.
27+
*/
1228
export const getEntityManagerToken = (name: string) => `${name}_EntityManager`;
29+
/**
30+
* Injects an EntityManager provider based on the supplied context name.
31+
*
32+
* @param name The context name of the database connection.
33+
* @returns A parameter decorator which will cause NestJS to inject the relevant EntityManager provider.
34+
*/
1335
export const InjectEntityManager = (name: string) => Inject(getEntityManagerToken(name));
1436

37+
/**
38+
* Gets the injection token based on class and optionally based on context name.
39+
* @param entity The class of the Entity to use for the injected repository provider.
40+
* @param name An optional context name - required for multiple database connections. See: [Multiple Database Connections](https://mikro-orm.io/docs/usage-with-nestjs#multiple-database-connections)
41+
* @returns The EntityRepository provider injection token based on the supplied entity and context name.
42+
*/
1543
export const getRepositoryToken = <T extends object> (entity: EntityName<T>, name?: string) => {
1644
const suffix = name ? `_${name}` : '';
1745
return `${Utils.className(entity)}Repository${suffix}`;
1846
};
47+
/**
48+
* Injects an EntityRepository provider.
49+
*
50+
* @param entity The class of the Entity to use for the injected repository provider.
51+
* @param name An optional context name - required for multiple database connections. See: [Multiple Database Connections](https://mikro-orm.io/docs/usage-with-nestjs#multiple-database-connections)
52+
* @returns A parameter decorator which will cause NestJS to inject the relevant EntityRepository provider.
53+
*/
1954
export const InjectRepository = <T extends object> (entity: EntityName<T>, name?: string) => Inject(getRepositoryToken(entity, name));

src/typings.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ export interface NestMiddlewareConsumer extends MiddlewareConsumer {
77
}
88

99
type MikroOrmNestScopeOptions = {
10+
/**
11+
* The NestJS provider scope to use for the EntityManager (and any subsequent downstream records).
12+
*
13+
* This scope will also impact the scope of Entity Repositories as they depend on the EntityManager.
14+
*
15+
* @see [NestJS Scope Hierarchy](https://docs.nestjs.com/fundamentals/injection-scopes#scope-hierarchy)
16+
*/
1017
scope?: Scope;
1118
};
1219

@@ -23,11 +30,28 @@ export type MikroOrmMiddlewareModuleOptions = {
2330

2431
export type MikroOrmModuleOptions<D extends IDatabaseDriver = IDatabaseDriver> = {
2532
registerRequestContext?: boolean;
33+
/**
34+
* Specifies whether or not to automatically load the entities based on MikroOrmModule.forFeature invocations.
35+
*
36+
* @see [MikroOrm - NestJS - Load Entities Automatically](https://mikro-orm.io/docs/usage-with-nestjs#load-entities-automatically)
37+
*
38+
* @default false
39+
*/
2640
autoLoadEntities?: boolean;
2741
} & Options<D> & MikroOrmMiddlewareModuleOptions;
2842

2943
export interface MikroOrmModuleFeatureOptions {
44+
/**
45+
* The entities to provide an EntityRepository for.
46+
*
47+
* @see [MikroOrm - NestJS - Repositories](https://mikro-orm.io/docs/usage-with-nestjs#repositories)
48+
*/
3049
entities?: EntityName<AnyEntity>[];
50+
/**
51+
* The context (database connection) to use for the entity repository.
52+
*
53+
* @see [MikroOrm - NestJS - Multiple Database Connections](https://mikro-orm.io/docs/usage-with-nestjs#multiple-database-connections)
54+
*/
3155
contextName?: string;
3256
}
3357

@@ -38,6 +62,13 @@ export interface MikroOrmOptionsFactory<D extends IDatabaseDriver = IDatabaseDri
3862
export interface MikroOrmModuleSyncOptions extends MikroOrmModuleOptions, MikroOrmNestScopeOptions { }
3963

4064
export interface MikroOrmModuleAsyncOptions<D extends IDatabaseDriver = IDatabaseDriver> extends Pick<ModuleMetadata, 'imports' | 'providers'>, MikroOrmNestScopeOptions {
65+
/**
66+
* The context name (database connection) to specify for this instance.
67+
*
68+
* When injecting repositories or entity manager instances, this context name will need to be specified where there are multiple datbaase connections.
69+
*
70+
* @see [MikroOrm - NestJS - Multiple Database Connections](https://mikro-orm.io/docs/usage-with-nestjs#multiple-database-connections)
71+
*/
4172
contextName?: string;
4273
useExisting?: Type<MikroOrmOptionsFactory<D>>;
4374
useClass?: Type<MikroOrmOptionsFactory<D>>;

0 commit comments

Comments
 (0)