如何在环回4中设置除持久性模型以外的单独DTO

时间:2018-11-28 07:51:01

标签: node.js loopbackjs dto

考虑这个lb4模型

@model({
  name: 'users'
})
export class User extends Entity {
  @property({
    type: 'number',
    id: true,
  })
  id: number;

  @property({
    type: 'string',
    required: true,
  })
  first_name: string;

  @property({
    type: 'string',
  })
  middle_name?: string;

  @property({
    type: 'string',
  })
  last_name?: string;

  @property({
    type: 'string',
    required: true,
  })
  username: string;

  @property({
    type: 'string',
  })
  email?: string;

  @property({
    type: 'string',
  })
  phone?: string;

  @property({
    type: 'string',
    required: true,
  })
  password: string;

  @property({
    type: 'string',
  })
  external_id: string;

  @belongsTo(() => UserTenant)
  created_by: number;

  @belongsTo(() => UserTenant)
  modified_by: number;

  constructor(data?: Partial<User>) {
    super(data);
  }
}

当前,如果我们使用lb4 cli为该模型创建存储库和控制器,它将使用与输入/输出相同的模型生成路由方法CRUD。但是,我们想要的是使用一个单独的DTO模型(非持久性DTO模型)用作控制器的输入/输出DTO,但不包括属性密码,created_by和Modifyed_by。一种方法是手动创建这样的模型类,并写下一个转换器类,它将上面的UserDTO对象转换为User模型(复制单个属性)。但这似乎是开销。此外,我们希望对更多模型进行此操作。因此,以这种方式进行操作似乎不是正确的方法。 lb4是否提供任何更好的方法来实现这一目标?

1 个答案:

答案 0 :(得分:1)

目前看来,现在有一种方法可以在LB4中本地“隐藏”属性。然后,我最终使用新的实体HideableEntity(扩展实体)修改了Entity类。在HideableEntity中,我这样修改了toJson()函数:

import {Entity, AnyObject} from '@loopback/repository';
import {Options} from '@loopback/repository/src/common-types';

export abstract class HideableEntity extends Entity {
  /**
   * Serialize into a plain JSON object
   */
  toJSON(): Object {
    const def = (<typeof HideableEntity>this.constructor).definition;
    if (def == null || def.settings.strict === false) {
      return this.toObject({ignoreUnknownProperties: false});
    }

    const json: AnyObject = {};
    for (const p in def.properties) {
      if (p in this) {
        json[p] = asJSON((this as AnyObject)[p]);
      }
    }
    return json;
  }

  /**
   * Convert to a plain object as DTO
   */
  toObject(options?: Options): Object {
    const def = (<typeof HideableEntity>this.constructor).definition;

    let obj: AnyObject;
    if (options && options.ignoreUnknownProperties === false) {
      obj = {};
      for (const p in this) {
        if (def != null && def.properties[p] && def.properties[p]['hide']) {
          continue;
        }
        let val = (this as AnyObject)[p];
        obj[p] = asObject(val, options);
      }
    } else {
      obj = this.toJSON();
    }
    return obj;
  }
}

function asJSON(value: any): any {
  if (value == null) return value;
  if (typeof value.toJSON === 'function') {
    return value.toJSON();
  }
  // Handle arrays
  if (Array.isArray(value)) {
    return value.map(item => asJSON(item));
  }
  return value;
}

function asObject(value: any, options?: Options): any {
  if (value == null) return value;
  if (typeof value.toObject === 'function') {
    return value.toObject(options);
  }
  if (typeof value.toJSON === 'function') {
    return value.toJSON();
  }
  if (Array.isArray(value)) {
    return value.map(item => asObject(item, options));
  }
  return value;
}

然后在我的模型扩展名HideableEntity中,添加属性hide: true,它将不会添加到JSON输出中:

export class User extends HideableEntity {
  @property({
    type: 'number',
    id: true,
    required: false,
  })
  id: number;

    @property({
        type: 'string',
        required: true,
    })
    email: string;

    @property({
        type: 'string',
        required: true,
        hide: true,
    })
    password: string;

    [...]

在上述情况下,密码将被隐藏。