How to use the class-transformer.TransformClassToPlain function in class-transformer

To help you get started, we’ve selected a few class-transformer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ZhiXiao-Lin / nestify / server / src / common / services / user.service.ts View on Github external
if (!!payload.isExport) {
            if (!payload.category) throw new BadRequestException('分类参数错误');

            const dataSource = await qb.getMany();

            // 执行导出逻辑
            // return await ExcelHelper.export(dataSource, Content.sheetsMap[payload.category], payload.fields.split(','));
        } else {
            qb.skip(payload.page * payload.pageSize);
            qb.take(payload.pageSize);
        }

        return await qb.getManyAndCount();
    }

    @TransformClassToPlain()
    async details(payload: any) {
        const qb = this.detailRepository.createQueryBuilder('t');

        qb.leftJoinAndSelect('t.user', 'user');

        if (!payload.page) {
            payload.page = 0;
        }

        if (!payload.pageSize) {
            payload.pageSize = 10;
        }

        qb.andWhere('user.id =:id', { id: payload.userId });

        if (!!payload.create_at) {
github ZhiXiao-Lin / nestify / server / src / common / services / content.service.ts View on Github external
if (!payload.category) throw new BadRequestException('分类参数错误');

            return await qb.getMany();
        } else {
            qb.skip(payload.page * payload.pageSize);
            qb.take(payload.pageSize);
        }

        const [list, count] = await qb.getManyAndCount();

        Promise.all(list.map(item => this.saveViewsFromCache(item.id)));

        return [list, count];
    }

    @TransformClassToPlain()
    async findOneById(id) {
        return await this.contentRepository.findOne({
            where: { id },
            relations: ['category']
        });
    }

    async saveViewsFromCache(id) {
        const currentTime = moment().format('YYYY-MM-DD HH:mm:ss');
        Logger.debug('currentTime', currentTime);

        // 文章最后访问时间
        const lastTime = await redis.hget('content_lastTime', `${id}`);
        Logger.debug('lastTime', lastTime);

        if (!!lastTime) {
github ZhiXiao-Lin / nestify / server / src / common / services / role.service.ts View on Github external
import { TransformClassToPlain } from 'class-transformer';
import { BaseService } from './base.service';
import { Role } from '../entities/role.entity';

@Injectable()
export class RoleService extends BaseService {
    constructor(@InjectRepository(Role) private readonly roleRepository: Repository) {
        super(roleRepository);
    }

    @TransformClassToPlain()
    async findOneAndRelations(id: string) {
        return await this.roleRepository.findOne({ where: { id }, relations: ['authorities'] });
    }

    @TransformClassToPlain()
    async query(payload: any) {
        const qb = this.roleRepository.createQueryBuilder('t');

        qb.leftJoinAndSelect('t.authorities', 'authorities');

        if (!!payload.keyword) {
            qb.andWhere(`t.name LIKE '%${payload.keyword}%'`);
        }

        if (!!payload.sort && !!payload.order) {
            qb.addOrderBy(`t.${payload.sort}`, payload.order);
        } else {
            // 默认排序规则
            qb.addOrderBy('t.sort', 'DESC');
        }
github ZhiXiao-Lin / nestify / server / src / common / services / user.service.ts View on Github external
return await this.jwtService.sign(_.toPlainObject(user));
    }

    async changePassword(id, dto) {
        const user = await this.userRepository.findOne({ where: { id }, relations: ['role'] });

        if (!(await bcrypt.compare(dto.oldPassword, user.password)))
            throw new BadRequestException('旧密码错误');

        const salt = await bcrypt.genSalt(10);
        user.password = await bcrypt.hash(dto.password, salt);

        return await this.userRepository.save(user);
    }

    @TransformClassToPlain()
    @Transaction()
    async save(
        payload: any,
        @TransactionRepository(User) userRepos?: Repository,
        @TransactionRepository(Detail) detailRepos?: Repository,
        @TransactionRepository(Role) rowRepos?: Repository
    ) {
        const user = User.create(payload) as User;

        if (!user.role) {
            // 添加默认角色
            const role = await rowRepos.findOne({ where: { token: 'user' } });
            user.role = role;
        }
github ZhiXiao-Lin / nestify / server / src / common / services / user.service.ts View on Github external
}

        if (!!payload.sort && !!payload.order) {
            qb.addOrderBy(`t.${payload.sort}`, payload.order);
        } else {
            qb.addOrderBy('t.create_at', 'DESC');
        }

        qb.skip(payload.page * payload.pageSize);
        qb.take(payload.pageSize);


        return await qb.getManyAndCount();
    }

    @TransformClassToPlain()
    async findOneById(id) {
        const qb = this.userRepository.createQueryBuilder('t');

        qb.andWhere('t.id = :id', { id });

        qb.leftJoinAndSelect('t.role', 'role');
        qb.leftJoinAndSelect('t.org', 'organization');
        qb.leftJoinAndSelect('t.flows', 'flows');
        qb.leftJoinAndSelect('role.authorities', 'authorities');

        return await qb.getOne();
    }

    async findOne(where) {
        const qb = this.userRepository.createQueryBuilder('t');
github ZhiXiao-Lin / nestify / server / src / common / services / content.service.ts View on Github external
import { Repository } from 'typeorm';
import { TransformClassToPlain } from 'class-transformer';
import { BaseService } from './base.service';
import { Content } from '../entities/content.entity';
import { Logger } from '../lib/logger';
import redis from '../lib/redis';

@Injectable()
export class ContentService extends BaseService<content> {
    constructor(
        @InjectRepository(Content) private readonly contentRepository: Repository<content>
    ) {
        super(contentRepository);
    }

    @TransformClassToPlain()
    async query(payload: any) {
        const qb = this.contentRepository.createQueryBuilder('t');

        qb.leftJoinAndSelect('t.category', 'category');

        if (!payload.page) {
            payload.page = 0;
        }

        if (!payload.pageSize) {
            payload.pageSize = 10;
        }

        if (!!payload.category) {
            qb.andWhere('category.id =:category', { category: payload.category });
        }</content></content>
github ZhiXiao-Lin / nestify / server / src / common / services / role.service.ts View on Github external
import * as _ from 'lodash';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { TransformClassToPlain } from 'class-transformer';
import { BaseService } from './base.service';
import { Role } from '../entities/role.entity';

@Injectable()
export class RoleService extends BaseService {
    constructor(@InjectRepository(Role) private readonly roleRepository: Repository) {
        super(roleRepository);
    }

    @TransformClassToPlain()
    async findOneAndRelations(id: string) {
        return await this.roleRepository.findOne({ where: { id }, relations: ['authorities'] });
    }

    @TransformClassToPlain()
    async query(payload: any) {
        const qb = this.roleRepository.createQueryBuilder('t');

        qb.leftJoinAndSelect('t.authorities', 'authorities');

        if (!!payload.keyword) {
            qb.andWhere(`t.name LIKE '%${payload.keyword}%'`);
        }

        if (!!payload.sort &amp;&amp; !!payload.order) {
            qb.addOrderBy(`t.${payload.sort}`, payload.order);
github ZhiXiao-Lin / nestify / server / src / common / services / flow.service.ts View on Github external
async requirement(payload: any, user: User) {

        payload.template = FlowTemplateEnum.WORK_OR;

        return await this.authCheck(await this.list(payload), user);
    }

    async task(payload: any, user: User) {

        payload.template = FlowTemplateEnum.WORK_OR;

        return await this.authCheck(await this.list(payload), user);
    }

    @TransformClassToPlain()
    async list(payload: any) {
        const qb = this.flowRepository.createQueryBuilder('t');

        qb.leftJoinAndSelect('t.template', 'template');
        qb.leftJoinAndSelect('t.user', 'user');
        qb.leftJoinAndSelect('t.operator', 'operator');
        qb.leftJoinAndSelect('t.executor', 'executor');

        if (!payload.page) {
            payload.page = 0;
        }

        if (!payload.pageSize) {
            payload.pageSize = 10;
        }
github ZhiXiao-Lin / nestify / server / src / common / services / service.service.ts View on Github external
qb.addOrderBy('t.create_at', 'DESC');
        }

        if (!!payload.isExport) {
            if (!payload.category) throw new BadRequestException('分类参数错误');

            return await qb.getMany();
        } else {
            qb.skip(payload.page * payload.pageSize);
            qb.take(payload.pageSize);
        }

        return await qb.getManyAndCount();
    }

    @TransformClassToPlain()
    async findOneById(id) {
        return await this.serviceRepository.findOne({
            where: { id },
            relations: ['category']
        });
    }

    async save(payload: any) {
        const target = Service.create(payload) as Service;

        return await this.serviceRepository.save(target);
    }

    async apply(user: User, payload: any) {

        payload.service = await this.findOneById(payload.id);
github ZhiXiao-Lin / nestify / server / src / common / services / service.service.ts View on Github external
import { FlowService } from './flow.service';
import wf from '../lib/wf';
import { FlowTemplateEnum } from '../aspects/enum';
import { User } from '../entities/user.entity';


@Injectable()
export class ServiceService extends BaseService {
    constructor(
        private readonly flowService: FlowService,
        @InjectRepository(Service) private readonly serviceRepository: Repository
    ) {
        super(serviceRepository);
    }

    @TransformClassToPlain()
    async query(payload: any) {
        const qb = this.serviceRepository.createQueryBuilder('t');

        qb.leftJoinAndSelect('t.category', 'category');

        if (!payload.page) {
            payload.page = 0;
        }

        if (!payload.pageSize) {
            payload.pageSize = 10;
        }

        if (!!payload.category) {
            qb.andWhere('category.id =:category', { category: payload.category });
        }