node.js - How to serialize / represent database objects as JSON returned by API in node -


i'm using express.js , sequelize.js build api. once retrieved object db using sequelize, want to

  • filter out object attributes (e.g. retrieve user, don't render user's password hash returned json)
  • add new object attributes

before return api json.

similar these rails libraries do:

what's common framework in node? or sequelize.js / express.js contain functionality that?

update

ok, there basic example, passport.js gets authenticated user's object db , attaches req.user;

router.get('/me/data',   passport.authenticate('bearer', { session: false }),   function(req, res) {     res.status(200).send(req.user);   } ); 

that return following json response:

{   "id": 24,   "first_name": "john",   "last_name": "doe",   "email": "mymail@example.com",   "password": "8d23cb9c4827bc06bb30ac47c06af0efbdbeb575001ab7de5387da4085f7184a381335c0f04b45f4a40e5a7042d47ae1e2d29d28fd5be1d534f09ba3db04e8ca",   "updatedat": "2016-01-25t09:19:07.422z",   "createdat": "2016-01-25t09:19:07.422z",   "data": null } 

but want return this:

{   "id": 24,   "full_name": "john doe",   "email": "mymail@example.com",   "data": null } 

and not 1 case, in case user object rendered.

the simplest solution edit req.user before sending it:

function

render (user) {     callback({         "id": user.id,         "full_name": user.first_name + ' ' + user.last_name,         "email": user.email,         "data": null     }); }  router.get('/me/data',   passport.authenticate('bearer', { session: false }),   function(req, res) {     render(req.user, function(user) {         res.status(200).send(user);     });   } ); 

Comments