Saturday 18 March 2017

NodeJs: Data Transformers like in Laravel PHP Framework

I've created multiple REST API projects using the Laravel framework and basing my code structure on the Laracasts tutorial. However we are deciding to move some projects using NodeJs as a backend. I'm beginning to learn node and I'm trying to replicate it in Node. I was able to do it for a singe object response but for multiple objects I can't seem to make it work.

Here is my controller:

index(req,res) {
    User
      .findAll()
      .then(function(users){
        res.json(api.respond(transfomer.transform(user)));
      })
      .catch(function(error){
        res.json(api.respondWithError('users not found',error));
      });
  }

api controller:

module.exports = {

  // response w/o error
  respond: function($data,$msg,$status) {
    if ($msg == null) {
      return {
        'status': $status || true,
        'data': $data
      };
    } else {
      return {
        'status': true,
        'message': $msg,
        'data': $data
      };
    }
  },

  // response with error
  respondWithError: function($msg,$error) {
    var self = this;
    var status = false;
    var data = {
      'error': $error
    };
    return this.respond(data,$msg,status);
  },
};

transformer.js

module.exports = {

  // single transformation
  transform (user) {
    return {
      'id' : user.id,
      'username': user.username,
      'firstname': user.firstname,
      'lastname': user.lastname,
      'address': user.address,
      'phone': user.phone,
      'mobile': user.mobile,
      'status': user.status
    };
  },

  //
  transformCollection(users) {
    var self = this;
    var data = [];
    for (var i = 0; i <= users.length; i++) {
        data.push(this.transform(users[i]));
    }
    return data;
  }

};

sample output

{
  "status": true,
  "data": [ 
    {
        "id": 1,
        "username": "b@email.com",
        "firstname": "Jon",
        "lastname": "Doe",
        "address": "Homes",
        "phone": "+966501212121",
        "mobile": "+966501212121",
        "status": "NOT VERIFIED"
    },
    {
        "id": 1,
        "username": "b@email.com",
        "firstname": "Jon",
        "lastname": "Doe",
        "address": "Homes",
        "phone": "+966501212121",
        "mobile": "+966501212121",
        "status": "NOT VERIFIED"
    },
    {
        "id": 1,
        "username": "b@email.com",
        "firstname": "Jon",
        "lastname": "Doe",
        "address": "Homes",
        "phone": "+966501212121",
        "mobile": "+966501212121",
        "status": "NOT VERIFIED"
    },
    {
        "id": 1,
        "username": "b@email.com",
        "firstname": "Jon",
        "lastname": "Doe",
        "address": "Homes",
        "phone": "+966501212121",
        "mobile": "+966501212121",
        "status": "NOT VERIFIED"
    },
  ]
}

Sorry for asking this as I'm a bit newb with node. Is it possible to achieve that output as I tried different ways but Im still getting confused.

Thanks.



via Drew Adorador

No comments:

Post a Comment