module.exports = Self => { Self.remoteMethod('getVehiclesSorted', { description: 'Sort the vehicles by warehouse', accessType: 'WRITE', accepts: [{ arg: 'warehouseFk', type: 'number' }], returns: { type: ['object'], root: true }, http: { path: `/getVehiclesSorted`, verb: `POST` } }); Self.getVehiclesSorted = async warehouseFk => { const vehicles = await Self.rawSql(` SELECT ROW_NUMBER() OVER (ORDER BY v.warehouseFk = ? DESC, w.id, v.numberPlate) AS 'order', v.id, v.warehouseFk, CONCAT(v.numberPlate, ' - ', w.name) as description FROM vehicle v JOIN warehouse w ON w.id = v.warehouseFk ORDER BY v.warehouseFk = ? DESC, w.id, v.numberPlate ASC`, [warehouseFk, warehouseFk]); return vehicles; }; };