Q:

Include ressource link in Sequelize result

I'm building a rest api that uses Sequelize to interact with the database. A query looks like this: 
function read_category(req, res) {
    Category.findById(req.params.categoryId, {rejectOnEmpty: true}).then(category => {
        res.json(category);
    }).catch(Sequelize.EmptyResultError, function () {
            res.status(404).json({message: 'No category found'});
        }
    ).catch(function (err) {
            res.send(err);
        }
    );
}

Now I want the category object that is returned from Sequelize and then returned to the user to include the linkto the ressource. I could do: 
category.dataValues.link = config.base_url + 'categories/' + category.dataValues.id;
Which would result in:
{
    "id": 1,
    "name": "TestCategory 1",
    "position": 1,
    "createdAt": "2018-08-19T11:42:09.000Z",
    "updatedAt": "2018-08-19T11:42:09.000Z",
    "link": "http://localhost:3000/categories/1"
}

Since I have more routes than this one I'm wondering if there's a dynamic way to add the link property to every category. I don't want to save it in the database because the base-url might differ.
Thanks!

A:

Better way to do it is , create a getter method :
const Category = sequelize.define( 'category' , {
    ....
    your_fields
    ....
},
{    
    getterMethods:{
        link() {
            return config.base_url + 'categories/' + this.id;
        }
    }
});

module.exports = Category;

Then 
Category.findAll(...).then(categories => {
    // Now there is no need to append data manually , it will added each time when you query 
    console.log(categories); // <-- Check the output
})

