From b64b91e50fe9bbc3c1fb2f368d8a10a3f2f4a731 Mon Sep 17 00:00:00 2001 From: jorgep Date: Wed, 7 Aug 2024 10:12:54 +0200 Subject: [PATCH] feat: refs #7524 wip remote hooks --- loopback/common/models/vn-model.js | 32 ++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/loopback/common/models/vn-model.js b/loopback/common/models/vn-model.js index 20c75ca8c..56a4f4dd0 100644 --- a/loopback/common/models/vn-model.js +++ b/loopback/common/models/vn-model.js @@ -28,21 +28,33 @@ module.exports = function(Self) { }; }); - /* - * Intercept GET request for find - */ this.beforeRemote('find', async ctx => { - isSelect = true; - const filter = ctx.args.filter || {}; - if (filter.limit === undefined) { - filter.limit = this.app.orm.selectLimit; + const defaultLimit = this.app.orm.selectLimit; + const filter = ctx.args.filter || {limit: defaultLimit}; + + if (filter.limit > defaultLimit) { + filter.limit = defaultLimit; ctx.args.filter = filter; } }); - this.observe('loaded', async({data}) => { - if (!isSelect) return; - const length = Array.isArray(data) ? data.length : data ? 1 : 0; + this.afterRemote('find', async({result}) => { + const length = Array.isArray(result) ? result.length : result ? 1 : 0; + if (length >= this.app.orm.selectLimit) throw new UserError('Too many records'); + }); + + this.beforeRemote('filter', async ctx => { + const defaultLimit = this.app.orm.selectLimit; + const filter = ctx.args.filter || {limit: defaultLimit}; + + if (filter.limit > defaultLimit) { + filter.limit = defaultLimit; + ctx.args.filter = filter; + } + }); + + this.afterRemote('filter', async({result}) => { + const length = Array.isArray(result) ? result.length : result ? 1 : 0; if (length >= this.app.orm.selectLimit) throw new UserError('Too many records'); });