Skip to content

Commit ab8153a

Browse files
committed
wrap up count implementation using the aggregation framework
1 parent 8af7dad commit ab8153a

File tree

2 files changed

+18
-25
lines changed

2 files changed

+18
-25
lines changed

server/connectors/api-connector-mongo/src/main/scala/com/prisma/api/connector/mongo/database/NodeManyQueries.scala

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -116,29 +116,16 @@ trait NodeManyQueries extends FilterConditionBuilder with AggregationQueryBuilde
116116

117117
//Fixme this does not use all queryarguments
118118
def countFromModel(model: Model, queryArguments: QueryArguments) = SimpleMongoAction { database =>
119-
// val queryArgFilter = queryArguments match {
120-
// case Some(arg) => arg.filter
121-
// case None => None
122-
// }
123-
//
124-
// val skipAndLimit = LimitClauseHelper.skipAndLimitValues(queryArguments)
125-
//
126-
// val cursorCondition = CursorConditionBuilder.buildCursorCondition(queryArguments)
127-
// We could try passing the other args into countoptions, but not sure about order
128-
// val baseQuery2 = collection.countDocuments(Filters.and(buildConditionForFilter(queryArgFilter), cursorCondition)).toFuture()
129-
//
130-
// val baseQuery: FindObservable[Document] = collection(Filters.and(buildConditionForFilter(queryArgFilter), cursorCondition))
131-
// val queryWithOrder: FindObservable[Document] = OrderByClauseBuilder.queryWithOrder(baseQuery, queryArguments)
132-
// val queryWithSkip: FindObservable[Document] = queryWithOrder.skip(skipAndLimit.skip)
133-
//
134-
// val queryWithLimit = skipAndLimit.limit match {
135-
// case Some(limit) => queryWithSkip.limit(limit)
136-
// case None => queryWithSkip
137-
// }
138-
//
139-
// queryWithLimit.collect().toFuture
140-
141-
database.getCollection(model.dbName).countDocuments(buildConditionForFilter(queryArguments.filter)).toFuture.map(_.toInt)
119+
if (needsAggregation(queryArguments.filter)) {
120+
aggregationQueryForId(database, model, queryArguments).map { x =>
121+
x.length match {
122+
case 0 => 0
123+
case x => x - 1 // we always fetch one more item for the page info we need to subtract that
124+
}
125+
}
126+
} else {
127+
database.getCollection(model.dbName).countDocuments(buildConditionForFilter(queryArguments.filter)).toFuture.map(_.toInt)
128+
}
142129
}
143130

144131
def countFromTable(table: String, filter: Option[Filter]) = SimpleMongoAction { database =>

server/servers/api/src/test/scala/com/prisma/api/queries/MultiItemConnectionQuerySpec.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ class MultiItemConnectionQuerySpec extends FlatSpec with Matchers with ApiSpecBa
136136
| name: "x"
137137
| },
138138
| }, first: 2, after: "${a.pathAsString("data.createUser.id")}") {
139+
| aggregate {
140+
| count
141+
| }
139142
| edges {
140143
| node {
141144
| name
@@ -146,7 +149,7 @@ class MultiItemConnectionQuerySpec extends FlatSpec with Matchers with ApiSpecBa
146149
project2
147150
)
148151

149-
result.toString should be("""{"data":{"usersConnection":{"edges":[{"node":{"name":"d"}},{"node":{"name":"g"}}]}}}""")
152+
result.toString should be("""{"data":{"usersConnection":{"aggregate":{"count":2},"edges":[{"node":{"name":"d"}},{"node":{"name":"g"}}]}}}""")
150153

151154
}
152155

@@ -175,6 +178,9 @@ class MultiItemConnectionQuerySpec extends FlatSpec with Matchers with ApiSpecBa
175178
| name: "x"
176179
| },
177180
| }, first: 2, after: "${a.pathAsString("data.createUser.id")}") {
181+
| aggregate {
182+
| count
183+
| }
178184
| edges {
179185
| node {
180186
| name
@@ -185,7 +191,7 @@ class MultiItemConnectionQuerySpec extends FlatSpec with Matchers with ApiSpecBa
185191
project2
186192
)
187193

188-
result.toString should be("""{"data":{"usersConnection":{"edges":[{"node":{"name":"d"}},{"node":{"name":"g"}}]}}}""")
194+
result.toString should be("""{"data":{"usersConnection":{"aggregate":{"count":2},"edges":[{"node":{"name":"d"}},{"node":{"name":"g"}}]}}}""")
189195

190196
}
191197

0 commit comments

Comments
 (0)