-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use libs for mapping between Dtos and Entities #23
Comments
Alternative: Use NestJs's interceptors: import { Injectable, NestInterceptor, ExecutionContext } from '@nestjs/common';
import { Observable } from 'rxjs';
import { mergeMap, map } from 'rxjs/operators';
import { FolderDto } from './dto/Folder.dto';
import { FolderService } from './folder/folder.service';
import { Folder } from './entity/folder.entity';
@Injectable()
export class TransformInterceptor implements NestInterceptor<Folder, FolderDto | FolderDto[]> {
constructor(
private readonly folderService: FolderService
) { }
intercept(context: ExecutionContext, call$: Observable<Folder|Folder[]>): Observable<FolderDto | FolderDto[]> {
return call$.pipe(
map(data => {
if (Array.isArray(data)) {
return data;
} else {
return [data];
}
}),
mergeMap(entity => {
return this.mapAllToDto(entity);
})
);
}
mapAllToDto(entities: Folder[]): Promise<FolderDto[]> {
if (entities) {
const r = entities.map(async (entity: Folder) => await this.mapToDto(entity));
return Promise.all(r);
}
}
async mapToDto(entity: Folder): Promise<FolderDto> {
if (entity) {
const dto = new FolderDto();
dto.id = entity.id;
dto.name = entity.name;
dto.absolutePath = await this.folderService.buildPathByFolderId(entity.id);
dto.parent = await this.mapToDto(entity.parent);
dto.children = await this.mapAllToDto(entity.children);
dto.dateAdded = entity.dateAdded;
return dto;
}
}
} |
Maybe it's enough to only pass the IDs instead of complete related/nested objects, e.g. pass the parent folder ID in |
Check which properties of an Entity are really needed by the client |
Wiat for |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: