Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter properties from ES6 class

I have a Dto that I want to enable the service layer to filter: The method selectFields takes an array of field names that should be returned, the other properties will be removed.
What is a short way to enumerate the properties on the class so I can loop through them and set the filtered ones to null?
In the BaseDto I take care of cleaning falsy values (well I need the same function here too as a matter of fact).

class UserServiceDto extends BaseDto {
  constructor(userDto) {
    super();
    this.fbUserId = userDto.fbUserId;
    this.fbFirstName = userDto.fbFirstName;
    this.fbLastName = userDto.fbLastName;
    this.gender = userDto.gender;
    this.birthdate = userDto.birthdate;
    this.aboutMe = userDto.aboutMe;
    this.deviceToken = userDto.deviceToken;
    this.refreshToken = userDto.refreshToken;
    this.updatedAt = userDto.updatedAt;
    this.createdAt = userDto.createdAt;
  }

  selectFields(fields) {
    // --> what's your take? 
  }

  toJson() {
    return super.toJson();
  }
}

Edit:
The service layer receives a dto from repository layer including all database fields. The ServiceLayerDto aims at filtering out fields that are not required by the web api (or should not be exposed as a security measure e.g. PK field, isDeleted, etc). So the result would I'm looking at the end of a service method for would look something like:

return new UserServiceDto(userDto)
  .selectFields('fbUserId', 'fbFirstName', 'fbLastName', 'birthdate', 'aboutMe', 'updatedAt', 'createdAt')
  .toJson();

The return value would be a plain json object that the web layer (controller) sends back to the http client.

like image 438
html_programmer Avatar asked Dec 11 '25 15:12

html_programmer


1 Answers

If you are ok with spread operator, you may try following approach:

class UserServiceDto {
  constructor() {
    this.a = 1;
    this.b = 2;
    this.c = 3;
  }
  selectFields(...fields) {
    const result = {};
    fields.forEach(key => result[key] = this[key]);
    return result;
  }
}

new UserServiceDto().selectFields('a', 'c'); // {a: 1, c: 3}

Looking to super.toJson() call, I think that it would not work due to the result of my selectFields() call would not be an instance of UserServiceDto class. There are some possible ways from this point I see:

  • instantiate new UserServiceDto object inside selectFields() body, remove all fields that not listed in the ...fields array (javascript delete is okey) and return it;
  • play with UserServiceDto constructor params to save positive logic on selectFields(), and pass to constructor only that props that need to be set up; in this case instantiating a temporary object will not require properties removing;
  • change the signature of toJson method, or better add a new signature, which would allow to pass fields array and then put current selectFields logic inside toJson method (and remove selectFields method at all): new UserServiceDto().toJson('a', 'c')...
like image 183
dhilt Avatar answered Dec 13 '25 04:12

dhilt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!