diff --git a/app/src/controllers/object.js b/app/src/controllers/object.js index 49c0ee73..29b65831 100644 --- a/app/src/controllers/object.js +++ b/app/src/controllers/object.js @@ -880,7 +880,12 @@ const controller = { public: isTruthy(req.query.public), active: isTruthy(req.query.active), deleteMarker: isTruthy(req.query.deleteMarker), - latest: isTruthy(req.query.latest) + latest: isTruthy(req.query.latest), + page: req.query.page, + limit: req.query.limit, + sort: req.query.sort, + order: req.query.order ? req.query.order : 'asc', + permissions: isTruthy(req.query.permissions) }; // if scoping to current user permissions on objects if (config.has('server.privacyMask')) { diff --git a/app/src/db/models/tables/objectModel.js b/app/src/db/models/tables/objectModel.js index e391d203..8e2999d2 100644 --- a/app/src/db/models/tables/objectModel.js +++ b/app/src/db/models/tables/objectModel.js @@ -176,6 +176,13 @@ class ObjectModel extends Timestamps(Model) { }); }); } + }, + pagination(query, page, limit){ + query.withGraphFetched('objectPermission'); + if( page && limit) query.page(page - 1, limit); + }, + sortOrder(query, column, order){ + if(column) query.orderBy(column, order); } }; } diff --git a/app/src/services/object.js b/app/src/services/object.js index 736bb20b..36df837b 100644 --- a/app/src/services/object.js +++ b/app/src/services/object.js @@ -117,11 +117,12 @@ const service = { */ searchObjects: async (params, etrx = undefined) => { let trx; + let total = 0; try { trx = etrx ? etrx : await ObjectModel.startTransaction(); const response = await ObjectModel.query(trx) - .allowGraph('version') + // .allowGraph('version') .modify('filterIds', params.id) .modify('filterBucketIds', params.bucketId) .modify('filterName', params.name) @@ -136,16 +137,31 @@ const service = { tag: params.tag }) .modify('hasPermission', params.userId, 'READ') - // format result + .modify('pagination', params.page, params.limit) + .modify('sortOrder', params.sort, params.order) .then(result => { - // just return object table records - const res = result.map(row => { - // eslint-disable-next-line no-unused-vars - const { objectPermission, bucketPermission, version, ...object } = row; - return object; - }); - // remove duplicates - return [...new Map(res.map(item => [item.id, item])).values()]; + if (Object.hasOwn(result, 'results')) { + total = result.total; + const res = result.results.map(row => { + const { objectPermission, ...object } = row; + object.objectPermissions = []; + objectPermission.map(o => { + object.objectPermissions.push(o.permCode); + }); + return object; + }); + // remove duplicates + return [...new Map(res.map(item => [item.id, item])).values()]; + } else { + //just return object table records + const res = result.map(row => { + // eslint-disable-next-line no-unused-vars + const { objectPermission, bucketPermission, version, ...object } = row; + return object; + }); + // remove duplicates + return [...new Map(res.map(item => [item.id, item])).values()]; + } }); if (!etrx) await trx.commit(); diff --git a/app/src/validators/object.js b/app/src/validators/object.js index bfd66caa..6c3ae825 100644 --- a/app/src/validators/object.js +++ b/app/src/validators/object.js @@ -137,7 +137,12 @@ const schema = { public: type.truthy, active: type.truthy, deleteMarker: type.truthy, - latest: type.truthy + latest: type.truthy, + page: Joi.number().min(1), + limit: Joi.number(), + sort: Joi.string().valid('name', 'createdAt', "bucketId"), + order: Joi.string().valid('asc', 'desc'), + permissions: type.truthy, }) },