const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; const mergeFilters = require('vn-loopback/util/filter').mergeFilters; module.exports = Self => { Self.remoteMethod('latest', { description: 'Returns the lastest campaigns', accessType: 'READ', accepts: [{ arg: 'filter', type: 'Object', description: `Filter defining where, order, offset, and limit - must be a JSON-encoded string` }], returns: { type: ['object'], root: true }, http: { path: `/latest`, verb: 'GET' } }); Self.latest = async filter => { const conn = Self.dataSource.connector; const minDate = new Date(); minDate.setFullYear(minDate.getFullYear() - 1); const where = {dated: {gte: minDate}}; filter = mergeFilters(filter, {where}); const stmt = new ParameterizedSQL( `SELECT * FROM (`); stmt.merge('SELECT * FROM campaign'); stmt.merge(conn.makeWhere(filter.where)); stmt.merge('ORDER BY dated ASC'); stmt.merge(') sub'); stmt.merge('GROUP BY code'); stmt.merge('LIMIT 10000000000000000000'); stmt.merge(conn.makePagination(filter)); console.log(stmt); console.log(minDate); return conn.executeStmt(stmt); }; };