Skip to content

Commit

Permalink
object search version filters - Make query simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
TimCsaky committed Jan 17, 2024
1 parent f3ad6b5 commit 779956f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 64 deletions.
2 changes: 1 addition & 1 deletion app/knexfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module.exports = {
},
debug: ['silly', 'debug'].includes(config.get('server.logLevel')),
log: {
debug: (msg) => logWrapper('debug', msg),
// debug: (msg) => logWrapper('debug', msg),
deprecate: (msg) => logWrapper('warn', msg),
error: (msg) => logWrapper('error', msg),
info: (msg) => logWrapper('info', msg),
Expand Down
2 changes: 2 additions & 0 deletions app/src/controllers/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,8 @@ const controller = {
};
const response = await storageService.headObject(data);

console.log('a', response);

Check warning on line 684 in app/src/controllers/object.js

View workflow job for this annotation

GitHub Actions / Unit Tests (16.x)

Unexpected console statement

Check warning on line 684 in app/src/controllers/object.js

View workflow job for this annotation

GitHub Actions / Unit Tests (18.x)

Unexpected console statement

Check warning on line 684 in app/src/controllers/object.js

View workflow job for this annotation

GitHub Actions / Unit Tests (20.x)

Unexpected console statement

Check warning on line 684 in app/src/controllers/object.js

View workflow job for this annotation

GitHub Actions / Unit Tests (16.x)

Unexpected console statement

Check warning on line 684 in app/src/controllers/object.js

View workflow job for this annotation

GitHub Actions / Unit Tests (18.x)

Unexpected console statement

Check warning on line 684 in app/src/controllers/object.js

View workflow job for this annotation

GitHub Actions / Unit Tests (20.x)

Unexpected console statement

// TODO: Proper 304 caching logic (with If-Modified-Since header support)
// Consider looking around for express-based caching middleware
// Perhaps npm express-preconditions is sufficient?
Expand Down
76 changes: 14 additions & 62 deletions app/src/db/models/tables/objectModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,68 +79,20 @@ class ObjectModel extends Timestamps(Model) {
if (value !== undefined) query.where('object.active', value);
},
filterVersionAttributes(query, mimeType, deleteMarker, isLatest) {
query.modify(query => {
query
.withGraphJoined('version')
.leftJoinRelated('version')
.whereIn('version.id', query => {
query.select('version.id')
.modify(query => {
if (mimeType) {
query.where('version.mimeType', 'ilike', `%${mimeType}%`);
}
if (deleteMarker !== undefined) {
query.where('version.deleteMarker', deleteMarker);
}
if (isLatest !== undefined) {
query.where('version.isLatest', isLatest);
}
});
});
return query;
});

},
filterMimeType(query, value) {
if (value) {
query
.withGraphJoined('version')
.leftJoinRelated('version')
.whereIn('version.id', builder => {
builder.select('version.id')
.where('version.mimeType', 'ilike', `%${value}%`);
});
}
},
filterDeleteMarker(query, value) {
if (value !== undefined) {
query
.withGraphJoined('version')
.leftJoinRelated('version')
.where('version.deleteMarker', value);
}
},
filterLatest(query, value) {
if (value !== undefined) {

query.withGraphJoined('version');
if (value) {
// join on version where isLatest = true
query.modifyGraph('version', builder => {
builder
.select('version.*')
.where('version.isLatest', true);
});
} else {
// join on ALL versions where isLatest = false
const subquery = Version.query()
.select('version.id')
.where('version.isLatest', false);
query.whereIn('version.id', builder => {
builder.intersect(subquery);
});
}
}
query
.withGraphJoined('version')
.leftJoinRelated('version')
.modify(query => {
if (mimeType) {
query.where('version.mimeType', 'ilike', `%${mimeType}%`);
}
if (deleteMarker !== undefined) {
query.where('version.deleteMarker', deleteMarker);
}
if (isLatest !== undefined) {
query.where('version.isLatest', isLatest);
}
});
},
filterMetadataTag(query, value) {
const subqueries = [];
Expand Down
9 changes: 8 additions & 1 deletion app/src/services/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,27 @@ const service = {
response.data = await ObjectModel.query(trx)
.allowGraph('[bucketPermission, objectPermission, version]')
.groupBy('object.id')
// object
.modify('filterIds', params.id)
.modify('filterBucketIds', params.bucketId)
.modify('filterName', params.name)
.modify('filterPath', params.path)
.modify('filterPublic', params.public)
.modify('filterActive', params.active)
// version
.modify('filterVersionAttributes', params.mimeType, params.deleteMarker, params.latest)
// meta/tag
.modify('filterMetadataTag', {
metadata: params.metadata,
tag: params.tag
})
// permissions
.modify('hasPermission', params.userId, 'READ')
// pagination
.modify('pagination', params.page, params.limit)
// sort results
.modify('sortOrder', params.sort, params.order)
// format response
.then(result => {
let results = [];
if (Object.hasOwn(result, 'results')) {
Expand All @@ -163,7 +170,7 @@ const service = {
object.permissions = objectPermission.map(o => o.permCode);
}
}
return object;
return row;
})
);
});
Expand Down

0 comments on commit 779956f

Please sign in to comment.