lila/modules/db/src/main/CursorExt.scala

33 lines
923 B
Scala
Raw Permalink Normal View History

package lila.db
2019-11-29 11:08:13 -07:00
import scala.collection.Factory
import reactivemongo.api._
2019-11-29 11:08:13 -07:00
import reactivemongo.api.bson._
trait CursorExt { self: dsl =>
// Can be refactor as CursorProducer
2020-05-05 22:11:15 -06:00
implicit final class ExtendCursor[A: BSONDocumentReader](val c: Cursor[A])(implicit
ec: scala.concurrent.ExecutionContext
) {
// like collect, but with stopOnError defaulting to false
2019-11-29 11:08:13 -07:00
def gather[M[_]](upTo: Int = Int.MaxValue)(implicit cbf: Factory[A, M[A]]): Fu[M[A]] =
c.collect[M](upTo, Cursor.ContOnError[M[A]]())
2021-08-17 15:19:45 -06:00
private def list(limit: Option[Int]): Fu[List[A]] = gather[List](limit | Int.MaxValue)
def list(limit: Int): Fu[List[A]] = list(limit.some)
def list(): Fu[List[A]] = list(none)
// like headOption, but with stopOnError defaulting to false
2019-12-13 07:30:20 -07:00
def uno: Fu[Option[A]] =
c.collect[Iterable](
2020-06-24 03:37:18 -06:00
1,
Cursor.ContOnError[Iterable[A]]()
).map(_.headOption)
}
}