module.exports = Self => { Self.remoteMethod('listPackaging', { description: 'Returns all packages including item', accessType: 'READ', accepts: [{ arg: 'filter', type: 'Object', required: false, description: 'Filter defining where and paginated data', http: {source: 'query'} }], returns: { type: ["Object"], root: true }, http: { path: `/listPackaging`, verb: 'get' } }); Self.listPackaging = async filter => { let where = ''; let limit = ''; let order = ''; let params; if (filter) { let connector = Self.dataSource.connector; if (filter.where) { if (filter.where.packagingFk) { where = 'WHERE p.id = ?'; params = [filter.where.packagingFk]; } else { let search = filter.where.name.regexp; where = 'WHERE i.id REGEXP ? OR i.name REGEXP ?'; params = [search, search]; } } limit = connector._buildLimit(null, filter.limit, filter.offset); order = connector.buildOrderBy('Item', filter.order); } let query = `SELECT i.name, i.id, p.id packagingFk FROM item i JOIN packaging p ON i.id = p.itemFk ${where} ${order} ${limit}`; return await Self.rawSql(query, params); }; };