Skip to content

Commit

Permalink
Adjust sorting in pipeline for GET /mappings (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefandesu committed Jun 27, 2022
1 parent 2d3fbf4 commit a3a5d30
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions services/mappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ class MappingService {
if (!annotatedWith && !annotatedBy && !annotatedFor) {
// Simply match mapping query
pipeline.push({ $match: query })
pipeline.push({ $sort: sorting })
}
// 2. Filter by annotation, and from/to/creator is defined
else if (from || to || creator || annotatedFor === "none" || (annotatedFor || "").startsWith("!")) {
Expand All @@ -252,20 +253,22 @@ class MappingService {
annotationQuery["annotations.bodyValue"] = annotatedWith
}
if (annotatedFor) {
let annotatedForQuery = annotatedFor
if (annotatedFor === "none") {
annotatedFor = { $exists: false }
annotatedForQuery = { $exists: false }
} else if (annotatedFor === "any") {
annotatedFor = { $exists: true }
annotatedForQuery = { $exists: true }
} else if (annotatedFor.startsWith("!")) {
annotatedFor = { $ne: annotatedFor.slice(1) }
annotatedForQuery = { $ne: annotatedFor.slice(1) }
}
annotationQuery["annotations.motivation"] = annotatedFor
annotationQuery["annotations.motivation"] = annotatedForQuery
}
if (annotatedBy) {
annotationQuery["annotations.creator.id"] = { $in: annotatedBy.split("|") }
}
pipeline = [
{ $match: query },
{ $sort: sorting },
{
$lookup: {
from: "annotations",
Expand All @@ -289,10 +292,11 @@ class MappingService {
annotationQuery["bodyValue"] = annotatedWith
}
if (annotatedFor) {
let annotatedForQuery = annotatedFor
if (annotatedFor === "any") {
annotatedFor = { $exists: true }
annotatedForQuery = { $exists: true }
}
annotationQuery["motivation"] = annotatedFor
annotationQuery["motivation"] = annotatedForQuery
}
if (annotatedBy) {
annotationQuery["creator.id"] = { $in: annotatedBy.split("|") }
Expand All @@ -319,6 +323,8 @@ class MappingService {
{ $addFields: { mapping: { $arrayElemAt: ["$data", 0] } } },
// Replace root with mapping
{ $replaceRoot: { newRoot: "$mapping" } },
// Sort
{ $sort: sorting },
// Match mappings
{ $match: query },
]
Expand All @@ -329,7 +335,7 @@ class MappingService {
return model.aggregate(pipeline).cursor()
} else {
// Otherwise, return results
const mappings = await model.aggregate(pipeline).sort(sorting).skip(offset).limit(limit).exec()
const mappings = await model.aggregate(pipeline).skip(offset).limit(limit).exec()
mappings.totalCount = await utils.count(model, pipeline)
return mappings
}
Expand Down

0 comments on commit a3a5d30

Please sign in to comment.