Skip to content

Commit

Permalink
(dsl): Support create and upsert requests (#6)
Browse files Browse the repository at this point in the history
Co-authored-by: Dragutin Marjanović <[email protected]>

Co-authored-by: markaya <[email protected]>
  • Loading branch information
dbulaja98 and markaya authored Nov 28, 2022
1 parent 5f40fda commit 85ee86f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 15 deletions.
15 changes: 15 additions & 0 deletions modules/library/src/main/scala/zio/elasticsearch/Document.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package zio.elasticsearch

import zio.schema.Schema
import zio.schema.codec.JsonCodec.JsonDecoder
import zio.schema.codec.{DecodeError, JsonCodec}

private[elasticsearch] final case class Document(json: String) {
def decode[A](implicit schema: Schema[A]): Either[DecodeError, A] = JsonDecoder.decode(schema, json)
}

private[elasticsearch] object Document {
def from[A](doc: A)(implicit schema: Schema[A]): Document = Document(
JsonCodec.jsonEncoder(schema).encodeJson(doc, indent = None).toString
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ object ElasticError {

object DocumentRetrievingError {

final case object DocumentNotFound extends DocumentRetrievingError

final case class DecoderError(reason: String) extends DocumentRetrievingError

final case object DocumentNotFound extends DocumentRetrievingError

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,20 @@ sealed trait ElasticRequest[+A] { self =>

object ElasticRequest {

private[elasticsearch] final case class Map[A, B](request: ElasticRequest[A], mapper: A => B)
extends ElasticRequest[B]
def create[A: Schema](
index: IndexName,
id: DocumentId,
doc: A,
routing: Option[Routing] = None
): ElasticRequest[Unit] =
Create(index, Some(id), Document.from(doc), routing)

def create[A: Schema](
index: IndexName,
doc: A,
routing: Option[Routing]
): ElasticRequest[Unit] =
Create(index, None, Document.from(doc), routing)

def getById[A: Schema](
index: IndexName,
Expand All @@ -24,10 +36,34 @@ object ElasticRequest {
case None => Left(DocumentNotFound)
}

def upsert[A: Schema](
index: IndexName,
id: DocumentId,
doc: A,
routing: Option[Routing] = None
): ElasticRequest[Unit] =
CreateOrUpdate(index, id, Document.from(doc), routing)

private[elasticsearch] final case class Create(
index: IndexName,
id: Option[DocumentId],
document: Document,
routing: Option[Routing] = None
) extends ElasticRequest[Unit]

private[elasticsearch] final case class CreateOrUpdate(
index: IndexName,
id: DocumentId,
document: Document,
routing: Option[Routing] = None
) extends ElasticRequest[Unit]

private[elasticsearch] final case class GetById(
index: IndexName,
id: DocumentId,
routing: Option[Routing] = None
) extends ElasticRequest[Option[Document]]

private[elasticsearch] final case class Map[A, B](request: ElasticRequest[A], mapper: A => B)
extends ElasticRequest[B]
}
11 changes: 0 additions & 11 deletions modules/library/src/main/scala/zio/elasticsearch/package.scala

This file was deleted.

0 comments on commit 85ee86f

Please sign in to comment.