Skip to content

Commit

Permalink
Merge pull request #222 from bcgov/isLatest
Browse files Browse the repository at this point in the history
Use isLatest db value in object search query
  • Loading branch information
jujaga authored Oct 17, 2023
2 parents ded5cee + 862f097 commit 4ecf7b6
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions app/src/db/models/tables/objectModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,25 +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.*')
.distinctOn('version.objectId')
.orderBy([
{ column: 'version.objectId' },
{ column: 'version.createdAt', order: 'desc' }
]);
.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')
.distinctOn('objectId')
.orderBy([
{ column: 'objectId' },
{ column: 'version.createdAt', order: 'desc' }
]);
query.whereNotIn('version.id', builder => {
.where('version.isLatest', false);
query.whereIn('version.id', builder => {
builder.intersect(subquery);
});
}
Expand Down

0 comments on commit 4ecf7b6

Please sign in to comment.