diff --git a/modules/library/src/main/scala/zio/elasticsearch/Document.scala b/modules/library/src/main/scala/zio/elasticsearch/Document.scala new file mode 100644 index 000000000..af8ad4763 --- /dev/null +++ b/modules/library/src/main/scala/zio/elasticsearch/Document.scala @@ -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 + ) +} diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticError.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticError.scala index f6a84b4ed..c774838fd 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/ElasticError.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticError.scala @@ -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 + } } diff --git a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala index 33f3a4d8f..70226ad76 100644 --- a/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala +++ b/modules/library/src/main/scala/zio/elasticsearch/ElasticRequest.scala @@ -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, @@ -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] } diff --git a/modules/library/src/main/scala/zio/elasticsearch/package.scala b/modules/library/src/main/scala/zio/elasticsearch/package.scala deleted file mode 100644 index c948a18b3..000000000 --- a/modules/library/src/main/scala/zio/elasticsearch/package.scala +++ /dev/null @@ -1,11 +0,0 @@ -package zio - -import zio.schema.Schema -import zio.schema.codec.DecodeError -import zio.schema.codec.JsonCodec.JsonDecoder - -package object elasticsearch { - private[elasticsearch] final case class Document(json: String) { - def decode[A](implicit schema: Schema[A]): Either[DecodeError, A] = JsonDecoder.decode(schema, json) - } -}