I have searched through the docs but am unable to find how I can add additional data to my json responses.
Lets say I am executing a GET call to /api/customers
I would like each customer to return the field "address" which is a concatenation of the street_name and door_number properties defined in the Customer model.
So somewhere I need to define "address" = this.street_name + ' ' + this.door_number and add this to the response. Can someone point me to an example of this?
UPDATE
I have tried to add this to customer.js
'use strict';
const app = require('../../server/server');
module.exports = function(Customer) {
const query = "SELECT *, CONCAT( door_number|| ' ' || street_name) as address from my_customer_table";
app.dataSources.mysqlIDs.connector.query(query, (err, res) => {
if (err) {
console.log(err.message);
}
return res;
});
};
UPDATE 2:
If I do a console log on res:
[ RowDataPacket {
id: 5,
...
address: '491 Hondsruglaan'
},
RowDataPacket {...
}
]
When I use the pipe characters in the SQL query address will show as:
address: '1'
However the address field is not yet reflected in the API response. All the fields are there, but not address. It seems that the code is only executed when the app is booted.
This is an elegant solution in my opinion:
// customer.js
module.exports = function(Customer) {
Customer.afterRemote('find', function(context, customers, next) {
context.result.forEach(customer => {
customer.address = customer.street_name + ' ' + customer.door_number
})
next()
})
}