From bfd2286348e32d89e1d656273a88bc0b929b2cc5 Mon Sep 17 00:00:00 2001 From: Csaky Date: Mon, 16 Oct 2023 13:07:19 -0700 Subject: [PATCH 1/2] Use isLatest db value in object search query --- app/src/db/models/tables/objectModel.js | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/app/src/db/models/tables/objectModel.js b/app/src/db/models/tables/objectModel.js index 01232709..86dd0f35 100644 --- a/app/src/db/models/tables/objectModel.js +++ b/app/src/db/models/tables/objectModel.js @@ -103,21 +103,13 @@ class ObjectModel extends Timestamps(Model) { query.modifyGraph('version', builder => { builder .select('version.*') - .distinctOn('version.objectId') - .orderBy([ - { column: 'version.objectId' }, - { column: 'version.createdAt', order: 'desc' } - ]); + .where('version.isLatest', value); }); } else { // TODO: Consider modifying graph to join on all versions except latest const subquery = Version.query() .select('version.id') - .distinctOn('objectId') - .orderBy([ - { column: 'objectId' }, - { column: 'version.createdAt', order: 'desc' } - ]); + .where('version.isLatest', true); query.whereNotIn('version.id', builder => { builder.intersect(subquery); }); From 862f0975ce48f913f859e022ccd21ac128f22281 Mon Sep 17 00:00:00 2001 From: Csaky Date: Mon, 16 Oct 2023 13:22:36 -0700 Subject: [PATCH 2/2] invert logic for isLatest in Object search --- app/src/db/models/tables/objectModel.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/app/src/db/models/tables/objectModel.js b/app/src/db/models/tables/objectModel.js index 86dd0f35..e391d203 100644 --- a/app/src/db/models/tables/objectModel.js +++ b/app/src/db/models/tables/objectModel.js @@ -100,17 +100,18 @@ class ObjectModel extends Timestamps(Model) { query.withGraphJoined('version'); if (value) { + // join on version where isLatest = true query.modifyGraph('version', builder => { builder .select('version.*') - .where('version.isLatest', value); + .where('version.isLatest', true); }); } else { - // TODO: Consider modifying graph to join on all versions except latest + // join on ALL versions where isLatest = false const subquery = Version.query() .select('version.id') - .where('version.isLatest', true); - query.whereNotIn('version.id', builder => { + .where('version.isLatest', false); + query.whereIn('version.id', builder => { builder.intersect(subquery); }); }