From 5d53d69754aa5a1c36570ae1302428d65acdabee Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Tue, 23 Jan 2024 12:52:35 +0100 Subject: [PATCH 1/3] refs #6694 perf: update postCode/filter method --- back/methods/postcode/filter.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/back/methods/postcode/filter.js b/back/methods/postcode/filter.js index 9986a16c9..8dbe2965f 100644 --- a/back/methods/postcode/filter.js +++ b/back/methods/postcode/filter.js @@ -7,10 +7,6 @@ module.exports = Self => { description: 'Find all postcodes of the model matched by postcode, town, province or country.', accessType: 'READ', - returns: { - type: ['object'], - root: true, - }, accepts: [ { arg: 'filter', @@ -25,6 +21,10 @@ module.exports = Self => { http: {source: 'query'} }, ], + returns: { + type: ['object'], + root: true, + }, http: { path: `/filter`, verb: 'GET', @@ -32,12 +32,11 @@ module.exports = Self => { }); Self.filter = async(ctx, filter, options) => { const myOptions = {}; - if (typeof options == 'object') Object.assign(myOptions, options); const conn = Self.dataSource.connector; - const where = buildFilter(ctx.args, (param, value) => { + const where = buildFilter(ctx.where, (param, value) => { switch (param) { case 'search': return {or: [ @@ -50,7 +49,7 @@ module.exports = Self => { } }) ?? {}; - filter = mergeFilters(ctx.args?.filter ?? {}, {where}); + filter = mergeFilters(ctx?.where ?? {}, where); const stmts = []; let stmt; @@ -67,7 +66,7 @@ module.exports = Self => { JOIN country c on c.id = p.countryFk `); - stmt.merge(conn.makeSuffix(filter)); + stmt.merge(conn.makeSuffix({where: filter, limit: ctx?.limit ?? 30})); const itemsIndex = stmts.push(stmt) - 1; const sql = ParameterizedSQL.join(stmts, ';'); -- 2.40.1 From 5b2b6a15ad704a67f357b7dcc8577bc2151e7cd5 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Wed, 24 Jan 2024 08:36:08 +0100 Subject: [PATCH 2/3] refs #6694 perf: improve filter --- back/methods/postcode/filter.js | 12 ++++++--- back/methods/postcode/specs/filter.spec.js | 30 ++++++++++++++-------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/back/methods/postcode/filter.js b/back/methods/postcode/filter.js index 8dbe2965f..2ea70e15a 100644 --- a/back/methods/postcode/filter.js +++ b/back/methods/postcode/filter.js @@ -35,8 +35,10 @@ module.exports = Self => { if (typeof options == 'object') Object.assign(myOptions, options); + filter = ctx?.filter ?? {}; + const conn = Self.dataSource.connector; - const where = buildFilter(ctx.where, (param, value) => { + const where = buildFilter(filter?.where, (param, value) => { switch (param) { case 'search': return {or: [ @@ -48,13 +50,15 @@ module.exports = Self => { }; } }) ?? {}; - - filter = mergeFilters(ctx?.where ?? {}, where); + delete ctx.filter.where; const stmts = []; let stmt; stmt = new ParameterizedSQL(` SELECT + pc.townFk, + t.provinceFk, + p.countryFk, pc.code, t.name as town, p.name as province, @@ -66,7 +70,7 @@ module.exports = Self => { JOIN country c on c.id = p.countryFk `); - stmt.merge(conn.makeSuffix({where: filter, limit: ctx?.limit ?? 30})); + stmt.merge(conn.makeSuffix({where, ...ctx})); const itemsIndex = stmts.push(stmt) - 1; const sql = ParameterizedSQL.join(stmts, ';'); diff --git a/back/methods/postcode/specs/filter.spec.js b/back/methods/postcode/specs/filter.spec.js index c393b629a..60ac24809 100644 --- a/back/methods/postcode/specs/filter.spec.js +++ b/back/methods/postcode/specs/filter.spec.js @@ -7,13 +7,13 @@ describe('Postcode filter()', () => { try { const ctx = { - args: { - + filter: { }, + limit: 1 }; const results = await models.Postcode.filter(ctx, options); - expect(results.length).toBeGreaterThan(0); + expect(results.length).toEqual(1); await tx.rollback(); } catch (e) { await tx.rollback(); @@ -27,8 +27,10 @@ describe('Postcode filter()', () => { try { const ctx = { - args: { - search: 46, + filter: { + where: { + search: 46, + } }, }; const results = await models.Postcode.filter(ctx, options); @@ -47,8 +49,10 @@ describe('Postcode filter()', () => { try { const ctx = { - args: { - search: 'Alz', + filter: { + where: { + search: 'Alz', + } }, }; const results = await models.Postcode.filter(ctx, options); @@ -67,8 +71,10 @@ describe('Postcode filter()', () => { try { const ctx = { - args: { - search: 'one', + filter: { + where: { + search: 'one', + } }, }; const results = await models.Postcode.filter(ctx, options); @@ -87,8 +93,10 @@ describe('Postcode filter()', () => { try { const ctx = { - args: { - search: 'Ec', + filter: { + where: { + search: 'Ec', + } }, }; const results = await models.Postcode.filter(ctx, options); -- 2.40.1 From 40d5e8662b501eae200f913545a61e70005885d8 Mon Sep 17 00:00:00 2001 From: Javier Segarra Date: Wed, 24 Jan 2024 08:38:34 +0100 Subject: [PATCH 3/3] refs #6694 perf: remove unused imports --- back/methods/postcode/filter.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/back/methods/postcode/filter.js b/back/methods/postcode/filter.js index 2ea70e15a..4c3fc0cd3 100644 --- a/back/methods/postcode/filter.js +++ b/back/methods/postcode/filter.js @@ -1,6 +1,5 @@ const {ParameterizedSQL} = require('loopback-connector'); -const {buildFilter, mergeFilters} = require('vn-loopback/util/filter'); -// const {models} = require('vn-loopback/server/server'); +const {buildFilter} = require('vn-loopback/util/filter'); module.exports = Self => { Self.remoteMethod('filter', { @@ -41,12 +40,13 @@ module.exports = Self => { const where = buildFilter(filter?.where, (param, value) => { switch (param) { case 'search': - return {or: [ - {'pc.code': {like: `%${value}%`}}, - {'t.name': {like: `%${value}%`}}, - {'p.name': {like: `%${value}%`}}, - {'c.country': {like: `%${value}%`}} - ] + return { + or: [ + {'pc.code': {like: `%${value}%`}}, + {'t.name': {like: `%${value}%`}}, + {'p.name': {like: `%${value}%`}}, + {'c.country': {like: `%${value}%`}} + ] }; } }) ?? {}; -- 2.40.1