refs #6694 perf: improve filter
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Javier Segarra 2024-01-24 08:36:08 +01:00
parent 5d53d69754
commit 5b2b6a15ad
2 changed files with 27 additions and 15 deletions

View File

@ -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, ';');

View File

@ -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);