Skip to content
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

Open
pschild opened this issue Feb 4, 2019 · 5 comments
Open

Use libs for mapping between Dtos and Entities #23

pschild opened this issue Feb 4, 2019 · 5 comments
Assignees
Labels
enhancement New feature or request waiting
Milestone

Comments

@pschild
Copy link
Owner

pschild commented Feb 4, 2019

Client Controller Service Repository Database
Dto Dto <=> Entity (Mapping) Entity Entity
@pschild pschild added the enhancement New feature or request label Feb 4, 2019
@pschild pschild added this to the first release milestone Feb 4, 2019
@pschild pschild self-assigned this Feb 4, 2019
@pschild
Copy link
Owner Author

pschild commented Feb 10, 2019

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;
        }
    }
}

@pschild
Copy link
Owner Author

pschild commented Feb 11, 2019

@pschild
Copy link
Owner Author

pschild commented Feb 14, 2019

Maybe it's enough to only pass the IDs instead of complete related/nested objects, e.g. pass the parent folder ID in ImageDto

@pschild
Copy link
Owner Author

pschild commented Feb 21, 2019

Check which properties of an Entity are really needed by the client

@pschild
Copy link
Owner Author

pschild commented Feb 27, 2019

Wiat for plainToClass(newType, item, { excludeExtraneousValues: true }) to be implemented by class-transformer package. Then remove @Exclude annotations from DTOs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request waiting
Projects
None yet
Development

No branches or pull requests

1 participant