From bbe53060886a9f935159f68bee0db0ffb99cf269 Mon Sep 17 00:00:00 2001 From: jorgep Date: Mon, 12 Aug 2024 16:36:43 +0200 Subject: [PATCH] feat: refs #7524 add default limit --- loopback/common/models/vn-model.js | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/loopback/common/models/vn-model.js b/loopback/common/models/vn-model.js index 56a4f4dd0..a11bed11d 100644 --- a/loopback/common/models/vn-model.js +++ b/loopback/common/models/vn-model.js @@ -5,7 +5,6 @@ const utils = require('loopback/lib/utils'); module.exports = function(Self) { Self.ParameterizedSQL = ParameterizedSQL; - let isSelect; require('../methods/vn-model/getSetValues')(Self); require('../methods/vn-model/getEnumValues')(Self); @@ -28,7 +27,9 @@ module.exports = function(Self) { }; }); - this.beforeRemote('find', async ctx => { + this.beforeRemote('**', async ctx => { + if (!this.hasFilter(ctx)) return; + const defaultLimit = this.app.orm.selectLimit; const filter = ctx.args.filter || {limit: defaultLimit}; @@ -38,22 +39,10 @@ module.exports = function(Self) { } }); - 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.afterRemote('**', async ctx => { + if (!this.hasFilter(ctx)) return; - 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 {result} = ctx; const length = Array.isArray(result) ? result.length : result ? 1 : 0; if (length >= this.app.orm.selectLimit) throw new UserError('Too many records'); }); @@ -357,6 +346,12 @@ module.exports = function(Self) { checkInsertAcls(ctx) { return this.checkAcls(ctx, 'insert'); + }, + + hasFilter(ctx) { + return ctx.req.method.toUpperCase() === 'GET' && + ctx.method.accepts.some(x => x.arg === 'filter' && x.type.toLowerCase() === 'object'); } + }); };