REST API Message Retriever

Hi
i need a proper response function for this flow

Note that am using this code for the query :
...........................................................................................................
if(!msg.req.params.count)
msg.req.params.count = 1;

msg.topic = "SELECT id,topic,payload,timestamp " +
"FROM thingData " +
"WHERE topic='" + escape(msg.req.params.topic) + "' " +
"AND deleted=0 " +
"ORDER BY id DESC " +
"LIMIT " + msg.req.params.count + ";";
return msg;
...............................................................................................................................