-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Mongo db driver Added ReactiveMongo Zlayer Added Mongo repo and collection update and query Updated the docker-compose to create the collection and indexes required. Co-authored-by: Shailesh Patil <[email protected]> Signed-off-by: Fabio Pinheiro <[email protected]> Signed-off-by: Shailesh Patil <[email protected]>
- Loading branch information
1 parent
f97efe5
commit fdbf96a
Showing
20 changed files
with
932 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package fmgp.did | ||
|
||
import fmgp.crypto.error._ | ||
import fmgp.did._ | ||
import fmgp.did.comm._ | ||
import zio.json._ | ||
|
||
trait MediatorError | ||
|
||
case class MediatorException(fail: MediatorError) extends Exception(fail.toString()) | ||
|
||
final case class MediatorDidError(val error: DidFail) extends MediatorError | ||
object MediatorDidError { | ||
def apply(error: DidFail) = new MediatorDidError(error) | ||
} | ||
|
||
final case class MediatorThrowable(val error: String) extends StorageError | ||
object MediatorThrowable { | ||
def apply(throwable: Throwable) = new MediatorThrowable(throwable.getClass.getName() + ":" + throwable.getMessage) | ||
} | ||
|
||
// Storage | ||
|
||
trait StorageError extends MediatorError { | ||
def error: String | ||
} | ||
|
||
final case class StorageCollection(val error: String) extends StorageError | ||
object StorageCollection { | ||
def apply(throwable: Throwable) = new StorageCollection(throwable.getClass.getName() + ":" + throwable.getMessage) | ||
} | ||
|
||
final case class StorageThrowable(val error: String) extends StorageError | ||
object StorageThrowable { | ||
def apply(throwable: Throwable) = new StorageThrowable(throwable.getClass.getName() + ":" + throwable.getMessage) | ||
} | ||
|
||
sealed trait ProtocolError extends MediatorError { | ||
def piuri: PIURI | ||
} | ||
|
||
// Protocol | ||
|
||
object ProtocolError { | ||
given decoder: JsonDecoder[ProtocolError] = DeriveJsonDecoder.gen[ProtocolError] | ||
given encoder: JsonEncoder[ProtocolError] = DeriveJsonEncoder.gen[ProtocolError] | ||
} | ||
|
||
case class MissingProtocolError(piuri: PIURI) extends ProtocolError | ||
case class FailToEncodeMessage(piuri: PIURI, error: String) extends ProtocolError |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package fmgp.did | ||
|
||
import scala.concurrent.{ExecutionContext, Future} | ||
|
||
import reactivemongo.api.{Cursor, DB, MongoConnection, AsyncDriver} | ||
import reactivemongo.api.bson.{BSONDocumentWriter, BSONDocumentReader, Macros, document} | ||
|
||
object MongoDriver { | ||
// My settings (see available connection options) | ||
// val connectionString = "mongodb://localhost:27017/mydb?authMode=scram-sha1" | ||
val connectionString = | ||
// "mongodb+srv://mediator:@fmgp-db.orfjsdi.mongodb.net/?retryWrites=true&w=majority" | ||
"mongodb+srv://mediator:[email protected]" | ||
|
||
import ExecutionContext.Implicits.global // use any appropriate context | ||
|
||
// Connect to the database: Must be done only once per application | ||
val driver = AsyncDriver() | ||
val parsedUri = MongoConnection.fromString(connectionString) | ||
|
||
// Database and collections: Get references | ||
val futureConnection = parsedUri.flatMap(driver.connect(_)) | ||
def db1: Future[DB] = futureConnection.flatMap(_.database("firstdb")) | ||
def db2: Future[DB] = futureConnection.flatMap(_.database("anotherdb")) | ||
def personCollection = db1.map(_.collection("person")) | ||
|
||
// Write Documents: insert or update | ||
|
||
implicit def personWriter: BSONDocumentWriter[Person] = Macros.writer[Person] | ||
// or provide a custom one | ||
|
||
// use personWriter | ||
def createPerson(person: Person): Future[Unit] = | ||
personCollection.flatMap(_.insert.one(person).map(_ => {})) | ||
|
||
def updatePerson(person: Person): Future[Int] = { | ||
val selector = document( | ||
"firstName" -> person.firstName, | ||
"lastName" -> person.lastName | ||
) | ||
|
||
// Update the matching person | ||
personCollection.flatMap(_.update.one(selector, person).map(_.n)) | ||
} | ||
|
||
implicit def personReader: BSONDocumentReader[Person] = Macros.reader[Person] | ||
// or provide a custom one | ||
|
||
def findPersonByAge(age: Int): Future[List[Person]] = | ||
personCollection.flatMap( | ||
_.find(document("age" -> age)) | ||
.cursor[Person]() | ||
.collect[List](-1, Cursor.FailOnError[List[Person]]()) | ||
) | ||
// ... deserializes the document using personReader | ||
|
||
// Custom persistent types | ||
case class Person(firstName: String, lastName: String, age: Int) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.