Skip to content

Commit

Permalink
Working to simplify model references
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Dec 25, 2023
1 parent a56b659 commit 9e254f3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class DocumentCollection[D <: Document[D]](protected[arango] val graph: Graph,

override protected def afterRetrieval(value: Json): Json = model.allMutations.foldLeft(value)((v, m) => m.retrieve(v))

lazy val update: UpdateBuilder[D] = UpdateBuilder(this)
lazy val upsert: UpsertBuilder[D] = UpsertBuilder(this)
def update[M <: DocumentModel[D]](model: M): UpdateBuilder[D, M] = UpdateBuilder(this, model)
def upsert[M <: DocumentModel[D]](model: M): UpsertBuilder[D, M] = UpsertBuilder(this, model)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.outr.arango.query.{Query, QueryPart}
import fabric.rw._

case class UpdateBuilder[D <: Document[D], M <: DocumentModel[D]](collection: DocumentCollection[D],
model: M,
ignoreErrors: Boolean = false,
keepNull: Boolean = true,
mergeObjects: Boolean = true,
Expand Down Expand Up @@ -34,7 +35,8 @@ case class UpdateBuilder[D <: Document[D], M <: DocumentModel[D]](collection: Do

def toQuery(f: Update,
applyReturn: => Unit): Query = noConsumingRefs {
val v = collection.ref
val v = DocumentRef[D, M](model, None)
// val v: DocumentRef[D, M] = model.ref

def opt(name: String, value: Boolean, default: Boolean): Option[Query] = if (value != default) {
Some(Query.static(s"$name: $value"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ trait PaginationSupport extends Graph { graph =>
(implicit rw: RW[R]): IO[Option[Page[R]]] = {
val offset = page * pageSize
for {
countFiber <- pagedResults.query.byFilter(ref => ref.queryId === queryId).count.start
countFiber <- pagedResults.query.byFilter[PagedResult.type](ref => ref.queryId === queryId).count.start
resultsFiber <- pagedResults.query(
aql"""
FOR pr IN $pagedResults
Expand Down
33 changes: 17 additions & 16 deletions driver/src/main/scala/com/outr/arango/upsert/UpsertBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import fabric._
import fabric.io.JsonFormatter
import fabric.rw._

case class UpsertBuilder[D <: Document[D]](collection: DocumentCollection[D],
case class UpsertBuilder[D <: Document[D], M <: DocumentModel[D]](collection: DocumentCollection[D],
model: M,
list: Option[() => (Ref, List[Json], List[QueryPart])] = None,
search: List[QueryPart] = Nil,
insert: Option[Json] = None,
Expand All @@ -24,16 +25,16 @@ case class UpsertBuilder[D <: Document[D]](collection: DocumentCollection[D],
forceIndexHint: Boolean = false) {
private implicit def rw: RW[D] = collection.model.rw

def withSearch(f: FieldAndValue[_]): UpsertBuilder[D] =
def withSearch(f: FieldAndValue[_]): UpsertBuilder[D, M] =
withSearch(f.field.fieldName, QueryPart.Variable(f.value))
def withSearch(entry: (String, QueryPart)): UpsertBuilder[D] = {
def withSearch(entry: (String, QueryPart)): UpsertBuilder[D, M] = {
val part = QueryPart.Container(List(QueryPart.Static(entry._1), QueryPart.Static(": "), entry._2))
copy(
search = part :: search
)
}
def withListSearch[T <: Document[T]](collection: DocumentCollection[T], list: List[T])
(f: DocumentRef[T] => List[Searchable]): UpsertBuilder[D] = {
def withListSearch[T <: Document[T], TM <: DocumentModel[T]](collection: DocumentCollection[T, TM], list: List[T])
(f: DocumentRef[T, TM] => List[Searchable]): UpsertBuilder[D, M] = {
copy(
list = Some(() => {
val ref = collection.ref("doc")
Expand All @@ -42,18 +43,18 @@ case class UpsertBuilder[D <: Document[D]](collection: DocumentCollection[D],
})
)
}
def withListSearch(list: List[D])(f: DocumentRef[D] => List[Searchable]): UpsertBuilder[D] =
withListSearch[D](collection, list)(f)
def withInsert(doc: D): UpsertBuilder[D] = withInsert(doc.json(collection.model.rw))
def withInsert(json: Json): UpsertBuilder[D] = withInsert(JsonFormatter.Compact(json))
def withInsert(insert: String): UpsertBuilder[D] = copy(insert = Some(insert))
def withListSearch(list: List[D])(f: DocumentRef[D] => List[Searchable]): UpsertBuilder[D, M] =
withListSearch[D, M](collection, list)(f)
def withInsert(doc: D): UpsertBuilder[D, M] = withInsert(doc.json(collection.model.rw))
def withInsert(json: Json): UpsertBuilder[D, M] = withInsert(JsonFormatter.Compact(json))
def withInsert(insert: String): UpsertBuilder[D, M] = copy(insert = Some(insert))

def withUpdate(doc: D): UpsertBuilder[D] = withUpdate(doc.json(collection.model.rw))
def withUpdate(json: Json): UpsertBuilder[D] = withUpdate(JsonFormatter.Compact(json))
def withUpdate(update: String): UpsertBuilder[D] = copy(upsert = Some(Upsert.Update(update)))
def withNoUpdate: UpsertBuilder[D] = withUpdate(obj())
def withUpdate(doc: D): UpsertBuilder[D, M] = withUpdate(doc.json(collection.model.rw))
def withUpdate(json: Json): UpsertBuilder[D, M] = withUpdate(JsonFormatter.Compact(json))
def withUpdate(update: String): UpsertBuilder[D, M] = copy(upsert = Some(Upsert.Update(update)))
def withNoUpdate: UpsertBuilder[D, M] = withUpdate(obj())

def withReplace(doc: D): UpsertBuilder[D] = copy(upsert = Some(Upsert.Replace(doc)))
def withReplace(doc: D): UpsertBuilder[D, M] = copy(upsert = Some(Upsert.Replace(doc)))

def withOptions(ignoreErrors: Boolean = false,
keepNull: Boolean = true,
Expand All @@ -62,7 +63,7 @@ case class UpsertBuilder[D <: Document[D]](collection: DocumentCollection[D],
ignoreRevs: Boolean = true,
exclusive: Boolean = false,
indexHint: Option[String] = None,
forceIndexHint: Boolean = false): UpsertBuilder[D] = copy(
forceIndexHint: Boolean = false): UpsertBuilder[D, M] = copy(
ignoreErrors = ignoreErrors,
keepNull = keepNull,
mergeObjects = mergeObjects,
Expand Down

0 comments on commit 9e254f3

Please sign in to comment.