From 616777ffa5d704932d2a591bb8ae9b8bc3c07b48 Mon Sep 17 00:00:00 2001 From: "val.istar.guo" Date: Wed, 27 Nov 2024 21:13:32 +0800 Subject: [PATCH] docs: add .preload(options) --- README.md | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index df26d55..4b2f4df 100644 --- a/README.md +++ b/README.md @@ -177,7 +177,7 @@ export class AppModule {} ### Add prefix to all class properties ```typescript -// app.config.ts +// mysql.config.ts import { Configuration } from "@buka/nestjs-config"; import { IsString } from "class-validator"; @@ -278,3 +278,38 @@ import { KafkaConfig } from "./kafka.config"; }) export class AppModule {} ``` + +### Preload Config + +Sometimes, we have to get config outside the nestjs lifecycle. `ConfigModule.preload(options)` is designed for this. + +There is an example of [MikroORM](https://mikro-orm.io/) config file: + +```typescript +// mikro-orm.config.ts +import { ConfigModule } from "@buka/nestjs-config"; +import { MySqlDriver, defineConfig } from "@mikro-orm/mysql"; +import { MysqlConfig } from "./config/mysql.config"; +import { Migrator } from "@mikro-orm/migrations"; +import { BadRequestException } from "@nestjs/common"; + +export default (async function loadConfig() { + // Load MysqlConfig + await ConfigModule.preload({ + providers: [MysqlConfig], + }); + + // Get MysqlConfig Instance + const config = await ConfigModule.get(MysqlConfig); + + return defineConfig({ + ...config, + entities: ["dist/**/*.entity.js"], + driver: MySqlDriver, + }); +})(); +``` + +> [!TIP] +> +> The `options` of `ConfigModule.preload(options)` is the `options` of `ConfigModule.register(options)`