feat: refs #7524 add default limit
gitea/salix/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Jorge Penadés 2024-08-12 16:36:43 +02:00
parent 3adb73cddf
commit bbe5306088
1 changed files with 12 additions and 17 deletions

View File

@ -5,7 +5,6 @@ const utils = require('loopback/lib/utils');
module.exports = function(Self) { module.exports = function(Self) {
Self.ParameterizedSQL = ParameterizedSQL; Self.ParameterizedSQL = ParameterizedSQL;
let isSelect;
require('../methods/vn-model/getSetValues')(Self); require('../methods/vn-model/getSetValues')(Self);
require('../methods/vn-model/getEnumValues')(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 defaultLimit = this.app.orm.selectLimit;
const filter = ctx.args.filter || {limit: defaultLimit}; const filter = ctx.args.filter || {limit: defaultLimit};
@ -38,22 +39,10 @@ module.exports = function(Self) {
} }
}); });
this.afterRemote('find', async({result}) => { this.afterRemote('**', async ctx => {
const length = Array.isArray(result) ? result.length : result ? 1 : 0; if (!this.hasFilter(ctx)) return;
if (length >= this.app.orm.selectLimit) throw new UserError('Too many records');
});
this.beforeRemote('filter', async ctx => { const {result} = 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; const length = Array.isArray(result) ? result.length : result ? 1 : 0;
if (length >= this.app.orm.selectLimit) throw new UserError('Too many records'); if (length >= this.app.orm.selectLimit) throw new UserError('Too many records');
}); });
@ -357,6 +346,12 @@ module.exports = function(Self) {
checkInsertAcls(ctx) { checkInsertAcls(ctx) {
return this.checkAcls(ctx, 'insert'); 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');
} }
}); });
}; };