Skip to content

Commit

Permalink
Merge pull request #73 from MongoCamp/scala3
Browse files Browse the repository at this point in the history
Scala3
  • Loading branch information
sfxcode authored Feb 7, 2025
2 parents db8112a + d433d73 commit b048977
Show file tree
Hide file tree
Showing 159 changed files with 5,033 additions and 3,127 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main_test_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
mongodb-version: [ '4.4', '5.0', '6.0', '7.0' , '8.0' ]
mongodb-version: ['4.4', '5.0', '6.0', '7.0', '8.0']
java: [ '21', '23' ]
steps:
- uses: actions/checkout@main
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/other_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
mongodb-version: [ '4.4', '5.0', '6.0', '7.0' , '8.0' ]
mongodb-version: [ '4.4', '5.0', '6.0', '7.0', '8.0' ]
java: [ '21', '23' ]
steps:
- uses: actions/checkout@main
Expand Down
30 changes: 11 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
A library for easy usage of the mongo-scala-driver (5.1.xa). Full MongoDB Functionality in Scala with a few lines of code.

## MongoDB Support

Support MongoDB 3.6 to 7.0.x.

We currently testing with '4.4', '5.0', '6.0', '7.0' and '8.0'.

## Features

Expand All @@ -28,7 +26,7 @@ Documentation can be found [here](https://mongodb-driver.mongocamp.dev/).

## Version

Scala Version is 2.13.x / 2.12.x.
Scala Version is 3.6.x / 2.13.x

## CI

Expand All @@ -46,7 +44,7 @@ Add following lines to your build.sbt (replace x.x with the actual Version)

```
libraryDependencies += "dev.mongocamp" %% "mongodb-driver" % "2.x.x"
libraryDependencies += "dev.mongocamp" %% "mongodb-driver" % "3.x.x"
```

Expand All @@ -61,6 +59,8 @@ import dev.mongocamp.driver.mongodb.database.DatabaseProvider
import org.bson.codecs.configuration.CodecRegistries._
import org.mongodb.scala.bson.ObjectId
import org.mongodb.scala.bson.codecs.Macros._
import dev.mongocamp.driver.mongodb.json._
import io.circe.generic.auto._

/**
* import mongodb restaurants sample data
Expand All @@ -71,12 +71,9 @@ object RestaurantDatabase {

case class Grade(date: Date, grade: String, score: Int)

case class Restaurant(restaurant_id: String, name: String, borough: String, cuisine: String,
grades: List[Grade], address: Address, _id: ObjectId = new ObjectId())

private val registry = fromProviders(classOf[Restaurant], classOf[Address], classOf[Grade])
case class Restaurant(restaurant_id: String, name: String, borough: String, cuisine: String, grades: List[Grade], address: Address, _id: ObjectId = new ObjectId())

val provider = DatabaseProvider("test", registry)
val provider = DatabaseProvider.fromPath("dev.mongocamp")

object RestaurantDAO extends MongoDAO[Restaurant](provider, "restaurants")

Expand All @@ -88,7 +85,6 @@ object RestaurantDatabase {
Import the database object and execute some find and CRUD functions on the DAO object ...

```scala

import dev.mongocamp.driver.mongodb.demo.restaurant.RestaurantDemoDatabase._
import dev.mongocamp.driver.mongodb._

Expand All @@ -97,17 +93,14 @@ trait RestaurantDemoDatabaseFunctions {
/**
* single result with implicit conversion to Entity Option
*/
def findRestaurantByName(name: String): Option[Restaurant] =
RestaurantDAO.find("name", name)
def findRestaurantByName(name: String): Option[Restaurant] = RestaurantDAO.find("name", name)

def restaurantsSize: Long = RestaurantDAO.count()

/**
* result with implicit conversion to List of Entities
*/
def findAllRestaurants(filterValues: Map[String, Any] = Map()): List[Restaurant] =
RestaurantDAO.find(filterValues)

def findAllRestaurants(filterValues: Map[String, Any] = Map()): List[Restaurant] = RestaurantDAO.find(filterValues)
```


Expand Down Expand Up @@ -137,7 +130,6 @@ Use the mongodb functions in your app ...
Write some spec tests ...

```scala

import dev.mongocamp.driver.mongodb.demo.restaurant.RestaurantDemoDatabase._
import org.specs2.mutable.Specification

Expand All @@ -159,9 +151,9 @@ class RestaurantDemoSpec extends Specification with RestaurantDemoDatabaseFuncti

## Run Tests
```shell
docker run -d --publish 27017:27017 --name mongodb mongocamp/mongodb:latest;
sbt test;
docker rm -f mongodb;
docker run -d --publish 27017:27017 --name mongodb mongocamp/mongodb:latest;
sbt +test
```

## Supporters
Expand Down
24 changes: 8 additions & 16 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ developers := List(

licenses += ("Apache-2.0", url("https://www.apache.org/licenses/LICENSE-2.0.html"))

crossScalaVersions := Seq("2.13.15", "2.12.20")
//crossScalaVersions := Seq("2.13.16")
crossScalaVersions := Seq("3.6.0", "2.13.16")

scalaVersion := crossScalaVersions.value.head

Expand All @@ -59,12 +60,6 @@ buildInfoOptions += BuildInfoOption.BuildTime

resolvers += "Sonatype OSS Snapshots".at("https://oss.sonatype.org/content/repositories/snapshots")

// Test

libraryDependencies += "org.specs2" %% "specs2-core" % "4.20.9" % Test

libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.5.12" % Test

libraryDependencies += "joda-time" % "joda-time" % "2.13.0"

val circeVersion = "0.14.10"
Expand All @@ -75,34 +70,31 @@ libraryDependencies ++= Seq(
"io.circe" %% "circe-parser"
).map(_ % circeVersion)

libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "5.1.4"
libraryDependencies += ("org.mongodb.scala" %% "mongo-scala-driver" % "5.3.1").cross(CrossVersion.for3Use2_13)

// MongoDB 5.2.0 not supported for de.bwaldvogel -> https://github.com/bwaldvogel/mongo-java-server/issues/233
val MongoJavaServerVersion = "1.45.0"
val MongoJavaServerVersion = "1.46.0"

libraryDependencies += "de.bwaldvogel" % "mongo-java-server" % MongoJavaServerVersion % Provided

libraryDependencies += "de.bwaldvogel" % "mongo-java-server-h2-backend" % MongoJavaServerVersion % Provided

libraryDependencies += "org.xerial.snappy" % "snappy-java" % "1.1.10.7" % Provided

libraryDependencies += "com.github.luben" % "zstd-jni" % "1.5.6-7" % Provided
libraryDependencies += "com.github.luben" % "zstd-jni" % "1.5.6-9" % Provided

libraryDependencies += "org.apache.lucene" % "lucene-queryparser" % "10.0.0"
libraryDependencies += "org.apache.lucene" % "lucene-queryparser" % "10.1.0"

libraryDependencies += "com.github.pathikrit" %% "better-files" % "3.9.2"

libraryDependencies += "com.typesafe" % "config" % "1.4.3"

libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.9.5"

libraryDependencies += "org.scala-lang.modules" %% "scala-collection-compat" % "2.12.0"
//libraryDependencies += "org.scala-lang.modules" %% "scala-collection-compat" % "2.12.0"

libraryDependencies += "com.vdurmont" % "semver4j" % "3.1.0"

libraryDependencies += "com.github.jsqlparser" % "jsqlparser" % "5.0"

libraryDependencies += "org.liquibase" % "liquibase-core" % "4.30.0" % Test
libraryDependencies += "com.github.jsqlparser" % "jsqlparser" % "5.1"

buildInfoPackage := "dev.mongocamp.driver.mongodb"

Expand Down
4 changes: 2 additions & 2 deletions build_release.sbt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import com.vdurmont.semver4j.Semver
import dev.quadstingray.sbt.json.JsonFile
import sbtrelease.ReleasePlugin.autoImport.ReleaseKeys.versions
import sbtrelease.ReleasePlugin.autoImport.ReleaseTransformations.*
import sbtrelease.ReleasePlugin.autoImport.ReleaseTransformations._
import sbtrelease.ReleasePlugin.runtimeVersion

import scala.sys.process.*
import scala.sys.process._

releaseVersionBump := sbtrelease.Version.Bump.NextStable

Expand Down
9 changes: 9 additions & 0 deletions build_test.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Test / parallelExecution := false

libraryDependencies += "org.liquibase" % "liquibase-core" % "4.31.0" % Test

// Test

libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.5.16" % Test

libraryDependencies += "org.scalameta" %% "munit" % "1.1.0"
8 changes: 4 additions & 4 deletions docs/documentation/collection/aggregation.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ MongoDB support an easy to use [Aggregation Handling](https://docs.mongodb.com/m
## Demo

### Setup imports
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/operation/AggregationSpec.scala#agg_imports
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/operation/AggregationSuite.scala#agg_imports

### Define stages
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/operation/AggregationSpec.scala#agg_stages
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/operation/AggregationSuite.scala#agg_stages

### Execute Aggregation

Expand All @@ -19,10 +19,10 @@ MongoDB support an easy to use [Aggregation Handling](https://docs.mongodb.com/m
:::


<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/operation/AggregationSpec.scala#agg_execute
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/operation/AggregationSuite.scala#agg_execute

### Convert Result

For easy result handling, using the implicit Document to Map conversion can be useful.

<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/operation/AggregationSpec.scala#agg_convert
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/operation/AggregationSuite.scala#agg_convert
4 changes: 2 additions & 2 deletions docs/documentation/collection/analyse-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The driver supports an automated detection of the schema of an existing collecti
### Schema Analysis
Analyse a collection to detect the values for each field and the percentage distribution of the types.

<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/schema/SchemaSpec.scala#schema-analysis
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/schema/SchemaSuite.scala#schema-analysis

### Detect Schema
The Schema Detector can be used to detect the schema of a collection and is based on [Schema Anaysis](analyse-schema.md#schema-analysis). The schema is used to detect the types of the columns and generate a [JSON Schema](https://json-schema.org) for the collection. In case of multiple types of a field the Generation of the JSON Schema use the type with the most elements.
Expand All @@ -15,4 +15,4 @@ The Schema Detector can be used to detect the schema of a collection and is base
The [JSON Schema](https://json-schema.org) format can be use to validate or generate data, as well to secure your [Mongo Collection](https://www.mongodb.com/docs/manual/core/schema-validation/).
:::

<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/schema/SchemaSpec.scala#schema-explorer
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/schema/SchemaSuite.scala#schema-explorer
8 changes: 4 additions & 4 deletions docs/documentation/collection/pagination.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ In many cases you want to have the possibility to paginate over the response of
The Pagination over an aggregation pipeline supports only the response of `Document`, also if you use an case class MongoDAO you will got an `Document` back.
:::

<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationAggregationSpec.scala#aggregation-pagination
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationAggregationSuite.scala#aggregation-pagination

## Find Pagination

<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationFilterSpec.scala#filter-pagination
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationFilterSuite.scala#filter-pagination

## Foreach over Pagination result

### With default row count
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationIterationSpec.scala#foreach-default-rows
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationIterationSuite.scala#foreach-default-rows

### With specific row count
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationIterationSpec.scala#foreach-with-rows
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/pagination/PaginationIterationSuite.scala#foreach-with-rows
27 changes: 16 additions & 11 deletions docs/documentation/database/bson.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,36 @@ BSON converter is used for bidirectional converting of BSON data.
### toBson

```scala
val result = BsonConverter.toBson(3) // result = BsonInt32(3)
val result = BsonConverter.toBson(3) // result = BsonInt32(3)
```


### fromBson

```scala
val result = BsonConverter.fromBson(BsonInt32(3)) // result = 3
val result = BsonConverter.fromBson(BsonInt32(3)) // result = 3
```

### toDocument

```scala
case class Base(int: Int, Long: Long, float: Float, double: Double,
string: String, date: Date = new Date())

object Base {
def apply(): Base = new Base(1, 2, 3, 4, "test")
}
case class Base(int: Int, Long: Long, float: Float, double: Double, string: String, date: Date = new Date())

val document = Converter.toDocument(Base())
object Base {
def apply(): Base = new Base(1, 2, 3, 4, "test")
}

// Document((float,BsonDouble{value=3.0}), (string,BsonString{value='test'}),
// (double,BsonDouble{value=4.0}), (Long,BsonInt64{value=2}), (date,
// BsonDateTime{value=1531166757627}), (int,BsonInt32{value=1}))
val document = Converter.toDocument(Base())

// Document(
// (float,BsonDouble{value=3.0}),
// (string,BsonString{value='test'}),
// (double,BsonDouble{value=4.0}),
// (Long,BsonInt64{value=2}),
// (date, BsonDateTime{value=1531166757627}),
// (int,BsonInt32{value=1})
// )
```

## Plugins
Expand Down
4 changes: 1 addition & 3 deletions docs/documentation/database/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ mongo.db.test {

Scala Code Snippet
```scala
val customConfig: MongoConfig = MongoConfig.fromPath("mongodb.db.prod")
val customConfig: MongoConfig = MongoConfig.fromPath("mongodb.db.prod")
```


Expand Down Expand Up @@ -76,5 +76,3 @@ It is used for DatabaseProvider creation.
| minSize | 0 |
| DefaultMaintenanceInitialDelay | 0 |

## Multiple databases access

8 changes: 4 additions & 4 deletions docs/documentation/database/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ DatabaseProvider is the central repository for MongoClient, registries, database
Every [Mongo DAO](../mongo-dao/index.md) / [GridFs DAO](../gridfs-dao/index.md) Instance needs this class.

```scala
val provider: DatabaseProvider = DatabaseProvider(MongoConfig.fromPath())
val provider: DatabaseProvider = DatabaseProvider(MongoConfig.fromPath())

val database: MongoDatabase = provider.database()
val database: MongoDatabase = provider.database()

// Infos for all collections in the default database
val collectionInfos: List[CollectionInfo] = provider.collectionInfos()
// Infos for all collections in the default database
val collectionInfos: List[CollectionInfo] = provider.collectionInfos()
```
6 changes: 3 additions & 3 deletions docs/documentation/database/lucene.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ MongoCamp Mongo Driver support the usage of [Lucene Query](https://lucene.apache
### Explicit Usage
The LuceneConverter has the methods to parse a String to and `Query` and a other to the document conversion.

<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/lucene/LuceneSearchSpec.scala#lucene-parser-with-explicit
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/lucene/LuceneSearchSuite.scala#lucene-parser-with-explicit

### Implicit Usage
Like the Map to Bson conversion there is also an implicit method to convert `Query` to find Bson.

<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/lucene/LuceneSearchSpec.scala#lucene-parser-with-implicit
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/lucene/LuceneSearchSuite.scala#lucene-parser-with-implicit

### Parse String to Query
We have an individual parser to parse an string to Lucene Query, because the default Lucene Analyser is case-insensitive and convert all search data into lower case. So the best way to seach in MongoDb with Lucene Query is to use this code.

<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/lucene/LuceneSearchSpec.scala#lucene-parser
<<< @/../src/test/scala/dev/mongocamp/driver/mongodb/lucene/LuceneSearchSuite.scala#lucene-parser

## Read More
[Lucene Cheatsheet](https://www.lucenetutorial.com/lucene-query-syntax.html)
Loading

0 comments on commit b048977

Please sign in to comment.