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

Add docs, operators, improve semantics #4

Merged
merged 2 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 28 additions & 24 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ThisBuild / scalaVersion := "2.13.12"
ThisBuild / organization := "com.github.dapperware"
ThisBuild / organizationName := "Dapperware"
ThisBuild / organizationHomepage := Some(url("https://dappermongo.github.io"))
ThisBuild / name := "dappermongo"
ThisBuild / semanticdbEnabled := true
ThisBuild / semanticdbVersion := scalafixSemanticdb.revision
Expand Down Expand Up @@ -42,14 +43,15 @@ lazy val core = (project in file("modules/core"))
.settings(
name := "dappermongo-core",
libraryDependencies ++= Seq(
"dev.zio" %% "zio" % "2.0.21",
"dev.zio" %% "zio-streams" % "2.0.21",
"dev.zio" %% "zio-interop-reactivestreams" % "2.0.2",
"org.mongodb" % "mongodb-driver-reactivestreams" % "4.11.0",
"org.reactivemongo" %% "reactivemongo-bson-msb-compat" % "1.1.0-RC12",
"dev.zio" %% "zio-test" % "2.0.21" % Test,
"dev.zio" %% "zio-test-sbt" % "2.0.21" % Test,
"com.dimafeng" %% "testcontainers-scala-mongodb" % "0.40.12" % Test
"dev.zio" %% "zio" % "2.0.21",
"dev.zio" %% "zio-streams" % "2.0.21",
"dev.zio" %% "zio-interop-reactivestreams" % "2.0.2",
"org.mongodb" % "mongodb-driver-reactivestreams" % "4.11.0",
"org.reactivemongo" %% "reactivemongo-bson-msb-compat" % "1.1.0-RC12",
"org.scala-lang.modules" %% "scala-collection-compat" % "2.11.0",
"dev.zio" %% "zio-test" % "2.0.21" % Test,
"dev.zio" %% "zio-test-sbt" % "2.0.21" % Test,
"com.dimafeng" %% "testcontainers-scala-mongodb" % "0.40.12" % Test
),
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework"),
Test / fork := true,
Expand All @@ -65,24 +67,26 @@ lazy val core = (project in file("modules/core"))
}
)

//lazy val docs = project
// .in(file("zio-mongo-docs"))
// .settings(
// scalacOptions --= List("-Yno-imports", "-Xfatal-warnings"),
// publish / skip := true
// )
// .settings(
// moduleName := "zio-mongo-docs",
// projectName := (ThisBuild / name).value,
// mainModuleName := (core / moduleName).value,
// projectStage := ProjectStage.Development,
// ScalaUnidoc / unidoc / unidocProjectFilter := inProjects(core)
// )
// .dependsOn(core)
// .enablePlugins(WebsitePlugin)
lazy val microsite = project
.enablePlugins(MicrositesPlugin, MdocPlugin)
.settings(
publish / skip := true,
name := "dappermongo-microsite",
micrositeName := "DapperMongo",
micrositeDescription := "A ZIO-friendly MongoDB client",
micrositeFooterText := Some("DapperMongo is maintained by the Dapperware team."),
micrositeDocumentationUrl := "docs"
)

lazy val examples = project
.dependsOn(core)
.settings(
publish / skip := true,
scalacOptions := scalacOptionsVersion(scalaVersion.value)
)

lazy val root = (project in file("."))
.aggregate(core)
.aggregate(core, microsite, examples)
.settings(
publish / skip := true
)
5 changes: 0 additions & 5 deletions docs/index.md

This file was deleted.

5 changes: 0 additions & 5 deletions docs/package.json

This file was deleted.

15 changes: 0 additions & 15 deletions docs/sidebars.js

This file was deleted.

6 changes: 6 additions & 0 deletions examples/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
mongo:
image: mongo:latest
container_name: mongo
ports:
- "27017:27017"
32 changes: 32 additions & 0 deletions examples/src/main/scala/dapperware/examples/InsertAndQuery.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dapperware.examples

import dappermongo.{Collection, Database, MongoClient}
import reactivemongo.api.bson.{BSONDocumentHandler, Macros, document}
import zio.{ZIO, ZIOAppDefault}

object InsertAndQuery extends ZIOAppDefault {
case class Cookie(name: String, price: Double, quantity: Int)

object Cookie {
implicit val handler: BSONDocumentHandler[Cookie] = Macros.handler[Cookie]
}

val program = for {
db <- ZIO.service[Database]
_ <- db.insert.many(
List(
Cookie("oreo", 1.5, 10),
Cookie("chips ahoy", 2.0, 5),
Cookie("nutter butter", 1.75, 7)
)
)
_ <- db.find(document("price" -> document("$lt" -> 2.0))).stream[Cookie]().runCollect.debug
} yield ()

val run = program.provide(
MongoClient.local,
Collection.named("cookies"),
Database.named("example")
)

}
60 changes: 60 additions & 0 deletions microsite/docs/docs/getting-started.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
layout: docs
title: Getting Started
permalink: docs/getting-started/
---

# Getting started

You'll need to first make sure that you have access to a mongo instance or cluster.

There are a few ways to do this:
- Install mongo on your local machine or get a hosted version of mongo through Atlas.
- You can find more information on that [here](https://www.mongodb.com/docs/manual/installation/).

- Alternatively, if you want to develop locally you can also use docker to run a mongo instance. See the examples project for reference.

- Finally, you can use the mongodb-atlas CLI tool to create a cluster either locally or on atlas.

## Installation

```scala
libraryDependencies += "com.github.dapperware" %% "dappermongo-core" % "0.0.1"
```

## Setting up the client

After adding the above dependency you can start a `MongoClient`

```scala
import dappermongo._

// ZLayer
MongoClient.local // Uses the default local settings
MongoClient.configured // By default, it looks for a config key of "mongodb"
MongoClient.configured(NonEmptyChunk("mongo", "database")) // You can also specify the config key
MongoClient.live // Accepts `MongoSettings` as an environmental parameter

// ZIO
MongoClient.scoped // Returns a ZIO that can be configured
MongoClient.fromSettings(settings) // Accepts settings passed as an argument
```

### Example

```scala
import dappermongo._
import zio._

object Example1 extends ZIOAppDefault {

val program = for {
db <- MongoClient.database("test")
_ <- db.diagnostics.ping
} yield ()

val run = program.provide(
MongoClient.local
)
}
```
25 changes: 25 additions & 0 deletions microsite/docs/docs/intro.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
layout: docs
title: Intro
permalink: docs/
---

# Introduction

Welcome to DapperMongo, a ZIO-friendly MongoDB client. This project is designed to provide an easy-to-use, efficient, and reliable interface for interacting with MongoDB databases in Scala.

## Why DapperMongo?

You might be wondering, why another wrapper library for MongoDB in Scala? The answer is simple: DapperMongo takes the best from each library and combines them into a single "dapper" library.

Here are some of the reasons why you should consider using DapperMongo:

- **Ease of Use**: DapperMongo is designed with simplicity in mind. It provides a straightforward and intuitive API that makes it easy to perform common database operations.

- **Efficiency**: By leveraging the power of ZIO and other high-performance libraries, DapperMongo ensures that your database operations are as efficient as possible.

- **Reliability**: DapperMongo is built on top of reliable libraries and uses ZIO's powerful error handling capabilities to ensure that your database operations are safe and reliable.

- **Best of All Worlds**: DapperMongo takes the best features from various MongoDB libraries and combines them into a single, easy-to-use package. This means you get the benefits of all these libraries without having to deal with their individual quirks and complexities.

So, if you're looking for a MongoDB library that's easy to use, efficient, reliable, and takes the best from each library, look no further than DapperMongo. We hope you'll find it to be a valuable tool in your Scala development toolkit.
9 changes: 9 additions & 0 deletions microsite/docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
layout: home
title: "Home"
section: "home"
technologies:
- first: ["ZIO", ""]
- second: ["Reactivemongo-BSON", ""]
- third: ["mongodb-java-sdk", ""]
---
7 changes: 7 additions & 0 deletions microsite/src/main/resources/microsite/data/menu.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
options:
- title: Intro
url: docs/

- title: Getting Started
url: docs/getting-started/
section: intro
13 changes: 5 additions & 8 deletions modules/core/src/main/scala/dappermongo/AggregateOps.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package dappermongo
import scala.jdk.CollectionConverters._

import com.mongodb.reactivestreams.client.{ClientSession, MongoDatabase}
import dappermongo.aggregate.Pipeline
import dappermongo.internal.{CollectionConversionsVersionSpecific, _}
import dappermongo.internal._
import org.bson.RawBsonDocument
import reactivemongo.api.bson.msb._
import reactivemongo.api.bson.{BSON, BSONDocumentReader, BSONDocumentWriter}
Expand Down Expand Up @@ -57,8 +59,7 @@ object AggregateBuilder {
pipeline: Pipeline,
options: AggregateBuilderOptions,
sessionStorage: SessionStorage[ClientSession]
) extends AggregateBuilder[Collection]
with CollectionConversionsVersionSpecific {
) extends AggregateBuilder[Collection] {

override def allowDiskUse(allowDiskUse: Boolean): AggregateBuilder[Collection] =
copy(options = options.copy(allowDiskUse = Some(allowDiskUse)))
Expand Down Expand Up @@ -115,11 +116,7 @@ object AggregateBuilder {
session: Option[ClientSession]
) = ZIO.attempt {
val underlying = database.getCollection(collection.name, classOf[RawBsonDocument])
val encoded = listAsJava(
pipeline.stages.reduceMapLeft(stage => List(fromDocument(BSON.writeDocument(stage).get))) { (acc, stage) =>
acc ++ List(fromDocument(BSON.writeDocument(stage).get))
}
)
val encoded = pipeline.toList.map(_.map(fromDocument).asJava).get

val hint = options.hint.map(_.apply()).map(_.map(fromDocument).get)
val comment = options.comment.map(_.apply()).map(_.map(fromDocument).get)
Expand Down
24 changes: 24 additions & 0 deletions modules/core/src/main/scala/dappermongo/BuildInfo.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dappermongo

import reactivemongo.api.bson.{BSONDocument, BSONDocumentHandler, Macros}

case class BuildInfo(
version: String,
gitVersion: String,
sysInfo: String,
loaderFlags: String,
compilerFlags: String,
allocator: String,
versionArray: List[Int],
openssl: BSONDocument,
javascriptEngine: String,
bits: Int,
debug: Boolean,
maxBsonObjectSize: Int,
storageEngines: List[String],
ok: Int
)

object BuildInfo {
implicit val bsonHandler: BSONDocumentHandler[BuildInfo] = Macros.handler[BuildInfo]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package dappermongo

case class ChangeStreamPreAndPostImagesOptions(
enabled: Boolean = false
) {
def toJava: com.mongodb.client.model.ChangeStreamPreAndPostImagesOptions =
new com.mongodb.client.model.ChangeStreamPreAndPostImagesOptions(enabled)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dappermongo

import reactivemongo.api.bson.BSONDocument
import reactivemongo.api.bson.msb._

case class ClusteredIndexOptions(
key: BSONDocument,
unique: Boolean,
name: Option[String] = None
) {
def toJava: com.mongodb.client.model.ClusteredIndexOptions = {
val builder = new com.mongodb.client.model.ClusteredIndexOptions(key, unique)
name.foreach(builder.name)
builder
}
}
10 changes: 9 additions & 1 deletion modules/core/src/main/scala/dappermongo/Collection.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dappermongo

import zio.ZIO
import zio.stream.ZStream
import zio.{Trace, ZIO, ZLayer}

trait Collection { self =>
def name: String
Expand Down Expand Up @@ -30,6 +30,14 @@ trait Collection { self =>

object Collection {

def named(
name: String,
readPreference: Option[ReadPreference] = None,
writeConcern: Option[WriteConcern] = None,
readConcern: Option[ReadConcern] = None
)(implicit trace: Trace): ZLayer[Any, Nothing, Collection] =
ZLayer.succeed(Impl(name, readPreference, writeConcern, readConcern))

def apply(
name: String,
readPreference: Option[ReadPreference] = None,
Expand Down
Loading
Loading