#node.js #typescript #nestjs
Вопрос:
Я видел документы nestjs о пользовательской фабрике соединений. Мне интересно, когда использовать пользовательскую фабрику соединений…
Я хочу знать разницу между этим и обычным соединением.
Это URL-адрес документа https://docs.nestjs.kr/techniques/database#custom-connection-factory
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
// Use useFactory, useClass, or useExisting
// to configure the ConnectionOptions.
useFactory: (configService: ConfigService) => ({
type: 'mysql',
host: configService.get('HOST'),
port: configService.get<number>('PORT'),
username: configService.get('USERNAME'),
password: configService.get('PASSWORD'),
database: configService.get('DATABASE'),
entities: [__dirname '/**/*.entity{.ts,.js}'],
synchronize: true,
}),
// connectionFactory receives the configured ConnectionOptions
// and returns a Promise<Connection>.
connectionFactory: async (options) => {
const connection = await createConnection(options);
return connection;
},
});
Комментарии:
1. Если вы хотите выполнить некоторую логику для подготовки соединения, вы создаете фабрику, чтобы иметь возможность это сделать.