在 nest.js 中将配置文件与 nestjsx-automapper 一起使用
Using Profiles with nestjsx-automapper in nest.js
我正在使用 Chau Tran 的 nestjsx-automapper (https://automapper.netlify.app/docs/usages/init/add-profile)(感谢那段很酷的代码)。我已经像文档中显示的那样实现了它,并且已经在这里讨论过:
How use profiles from nartc/automapper into a nestjs application
但我仍然无法从我的个人资料中访问 AutoMapperclass。
这是我的设置:
app.module.ts:
import { Module } from '@nestjs/common';
import { AppService } from './app.service';
import { MerchantModule } from './merchant/merchant.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AutomapperModule, AutoMapper } from 'nestjsx-automapper';
@Module({
imports: [
TypeOrmModule.forRoot({
...
}),
AutomapperModule.withMapper(),
MerchantModule
],
providers: [],
controllers: [],
})
export class AppModule {}
merchant.module.ts:
import { Module } from '@nestjs/common';
import { MerchantController } from './merchant.controller';
import { MerchantService } from './merchant.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Merchant } from './entities/merchant.entity';
import { MerchantProfile } from './profiles/merchant.profile';
import { AutoMapper, AutomapperModule } from 'nestjsx-automapper';
@Module({
imports: [TypeOrmModule.forFeature([Merchant]), AutomapperModule, MerchantProfile],
exports: [TypeOrmModule],
controllers: [MerchantController],
providers: [MerchantService]
})
export class MerchantModule {}
merchant.profile.ts:
import {
ProfileBase,
Profile,
AutoMapper
} from 'nestjsx-automapper';
import { Merchant } from '../entities/merchant.entity';
import { MerchantDto } from '../dto/merchant.dto';
@Profile()
export class MerchantProfile extends ProfileBase {
constructor(
private readonly mapper: AutoMapper) {
super();
mapper.createMap(Merchant, MerchantDto);
}
configure(): void {
return null;
}
}
merchant.controller.ts:
import { Controller, Get, Param, Post, Body, Put, Delete } from '@nestjs/common';
import { MerchantService } from './merchant.service';
import { Merchant } from './entities/merchant.entity';
import { MerchantDto } from './dto/merchant.dto';
import { DeleteResult } from 'typeorm';
import { AutoMapper, InjectMapper } from 'nestjsx-automapper';
@Controller('merchant')
export class MerchantController {
constructor(
private merchantService: MerchantService,
@InjectMapper() private readonly mapper: AutoMapper) { }
@Get()
public async findAll(): Promise<MerchantDto[]> {
return this.mapper.mapArray(await this.merchantService.find(),MerchantDto);
}
}
当我 运行 使用此设置的应用程序时,出现以下异常:
Nest 无法解析 AutomapperModule (?) 的依赖项。请确保索引 [0] 处的参数 AutomapperExplorer 在 AutomapperModule 上下文中可用。
我不太熟悉 AutoMappModule
的工作原理,但在你的 MerchantModule
中你似乎导入了 AutoMapperModule
而你 应该 导入 AutoMapperModule.withMapper()
,类似于您在 AppModule
中的操作。
AutoMapperModule.withMapper()
in AppModule
是您唯一需要使用 AutoMapperModule
.
的时候
withMapper()
创建一个 AutoMapper
的单例,当您想在 Service
(或任何Injectable
)。
至于Profile
,正确的语法如下:
@Profile()
export class MerchantProfile extends ProfileBase {
constructor(mapper: AutoMapper) { // no private readonly.
super();
mapper.createMap(Merchant, MerchantDto);
}
// no configure() method
}
下面是@nartc/automapper
源码中addProfile()
的写法:
addProfile(profile: new (mapper: AutoMapper) => MappingProfile): AutoMapper {
this._profileStorage.add(this, new profile(this));
return this;
}
您可以看到,在内部,@nartc/automapper
将实例化 (new profile()
) 并将 AutoMapper
实例传递给配置文件的构造函数,以便您可以在 Profile's constructor
对于你的这段代码 MerchantModule
import { Module } from '@nestjs/common';
import { MerchantController } from './merchant.controller';
import { MerchantService } from './merchant.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Merchant } from './entities/merchant.entity';
// import { MerchantProfile } from './profiles/merchant.profile';
// this is all you need which is to import the profile so TypeScript can execute it. Don't need `MerchantProfile` at all
import './profiles/merchant.profile';
import { AutoMapper, AutomapperModule } from 'nestjsx-automapper';
@Module({
imports: [TypeOrmModule.forFeature([Merchant])], // don't need to import AutoMapperModule again. MerchantProfile is not a Module so you can't import it
exports: [TypeOrmModule],
controllers: [MerchantController],
providers: [MerchantService]
})
export class MerchantModule {}
在你的MerchantController
中:
@Controller('merchant')
export class MerchantController {
constructor(
private merchantService: MerchantService,
@InjectMapper() private readonly mapper: AutoMapper) { }
@Get()
public async findAll(): Promise<MerchantDto[]> {
// make sure `this.merchantService.find()` returns an Array of
// Merchant instances. If not, please provide an extra param to map()
// return this.mapper.mapArray(await this.merchantService.find(),MerchantDto);
return this.mapper.mapArray(await this.merchantService.find(), MerchantDto, Merchant); // notice the 3rd parameter is the Source model.
}
}
请告诉我这是否适合您。如果没有,请在 nestjsx-automapper
repo 中创建一个问题并提供可重现的存储库,我会尽快查看。
我正在使用 Chau Tran 的 nestjsx-automapper (https://automapper.netlify.app/docs/usages/init/add-profile)(感谢那段很酷的代码)。我已经像文档中显示的那样实现了它,并且已经在这里讨论过: How use profiles from nartc/automapper into a nestjs application
但我仍然无法从我的个人资料中访问 AutoMapperclass。
这是我的设置:
app.module.ts:
import { Module } from '@nestjs/common';
import { AppService } from './app.service';
import { MerchantModule } from './merchant/merchant.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AutomapperModule, AutoMapper } from 'nestjsx-automapper';
@Module({
imports: [
TypeOrmModule.forRoot({
...
}),
AutomapperModule.withMapper(),
MerchantModule
],
providers: [],
controllers: [],
})
export class AppModule {}
merchant.module.ts:
import { Module } from '@nestjs/common';
import { MerchantController } from './merchant.controller';
import { MerchantService } from './merchant.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Merchant } from './entities/merchant.entity';
import { MerchantProfile } from './profiles/merchant.profile';
import { AutoMapper, AutomapperModule } from 'nestjsx-automapper';
@Module({
imports: [TypeOrmModule.forFeature([Merchant]), AutomapperModule, MerchantProfile],
exports: [TypeOrmModule],
controllers: [MerchantController],
providers: [MerchantService]
})
export class MerchantModule {}
merchant.profile.ts:
import {
ProfileBase,
Profile,
AutoMapper
} from 'nestjsx-automapper';
import { Merchant } from '../entities/merchant.entity';
import { MerchantDto } from '../dto/merchant.dto';
@Profile()
export class MerchantProfile extends ProfileBase {
constructor(
private readonly mapper: AutoMapper) {
super();
mapper.createMap(Merchant, MerchantDto);
}
configure(): void {
return null;
}
}
merchant.controller.ts:
import { Controller, Get, Param, Post, Body, Put, Delete } from '@nestjs/common';
import { MerchantService } from './merchant.service';
import { Merchant } from './entities/merchant.entity';
import { MerchantDto } from './dto/merchant.dto';
import { DeleteResult } from 'typeorm';
import { AutoMapper, InjectMapper } from 'nestjsx-automapper';
@Controller('merchant')
export class MerchantController {
constructor(
private merchantService: MerchantService,
@InjectMapper() private readonly mapper: AutoMapper) { }
@Get()
public async findAll(): Promise<MerchantDto[]> {
return this.mapper.mapArray(await this.merchantService.find(),MerchantDto);
}
}
当我 运行 使用此设置的应用程序时,出现以下异常: Nest 无法解析 AutomapperModule (?) 的依赖项。请确保索引 [0] 处的参数 AutomapperExplorer 在 AutomapperModule 上下文中可用。
我不太熟悉 AutoMappModule
的工作原理,但在你的 MerchantModule
中你似乎导入了 AutoMapperModule
而你 应该 导入 AutoMapperModule.withMapper()
,类似于您在 AppModule
中的操作。
AutoMapperModule.withMapper()
in AppModule
是您唯一需要使用 AutoMapperModule
.
withMapper()
创建一个 AutoMapper
的单例,当您想在 Service
(或任何Injectable
)。
至于Profile
,正确的语法如下:
@Profile()
export class MerchantProfile extends ProfileBase {
constructor(mapper: AutoMapper) { // no private readonly.
super();
mapper.createMap(Merchant, MerchantDto);
}
// no configure() method
}
下面是@nartc/automapper
源码中addProfile()
的写法:
addProfile(profile: new (mapper: AutoMapper) => MappingProfile): AutoMapper {
this._profileStorage.add(this, new profile(this));
return this;
}
您可以看到,在内部,@nartc/automapper
将实例化 (new profile()
) 并将 AutoMapper
实例传递给配置文件的构造函数,以便您可以在 Profile's constructor
对于你的这段代码 MerchantModule
import { Module } from '@nestjs/common';
import { MerchantController } from './merchant.controller';
import { MerchantService } from './merchant.service';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Merchant } from './entities/merchant.entity';
// import { MerchantProfile } from './profiles/merchant.profile';
// this is all you need which is to import the profile so TypeScript can execute it. Don't need `MerchantProfile` at all
import './profiles/merchant.profile';
import { AutoMapper, AutomapperModule } from 'nestjsx-automapper';
@Module({
imports: [TypeOrmModule.forFeature([Merchant])], // don't need to import AutoMapperModule again. MerchantProfile is not a Module so you can't import it
exports: [TypeOrmModule],
controllers: [MerchantController],
providers: [MerchantService]
})
export class MerchantModule {}
在你的MerchantController
中:
@Controller('merchant')
export class MerchantController {
constructor(
private merchantService: MerchantService,
@InjectMapper() private readonly mapper: AutoMapper) { }
@Get()
public async findAll(): Promise<MerchantDto[]> {
// make sure `this.merchantService.find()` returns an Array of
// Merchant instances. If not, please provide an extra param to map()
// return this.mapper.mapArray(await this.merchantService.find(),MerchantDto);
return this.mapper.mapArray(await this.merchantService.find(), MerchantDto, Merchant); // notice the 3rd parameter is the Source model.
}
}
请告诉我这是否适合您。如果没有,请在 nestjsx-automapper
repo 中创建一个问题并提供可重现的存储库,我会尽快查看。