Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(dsl): Support create and upsert requests #6

Merged
merged 16 commits into from
Nov 28, 2022
Merged
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](
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add both:
create(index: IndexName, id: DocumentId, doc: A...)
and
create(index: IndexName, doc: A).

We are seeking to provide simpler public API and make our library usage easier, so it makes sense to provide rich public API.

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.