GithubHelp home page GithubHelp logo

isabella232 / play-mongo-bson Goto Github PK

View Code? Open in Web Editor NEW

This project forked from snipsco/play-mongo-bson

0.0 0.0 0.0 1.08 MB

Scala client for MongoDB using macros for case class serialization/deserialization

License: MIT License

Scala 100.00%

play-mongo-bson's Introduction

Play Mongo BSON

Build Status License: MIT

A Scala MongoDB Object-Document-Mapper for Play Framework using macros to serialize/deserialize case class as BSON documents. The idea is to get something similar to salat.

Salat was good in that it was a "small" library and not a full blown driver, but is ageing bad: it was based on bytecode analysis and bytecode generation whereas Scala compiler macros can be used now (simpler build, no more issues with type elision). Also it was still bound (by the BSON types) to the "old" synchronous MongoDB API.

Alternative projects are offering full driver and object mapping solution but now that the official driver is asynchronous, they feel less relevant. Also, they are re-inventing the whole API instead of sticking to the official MongoDB line: same API for all drivers. This is getting frustrating when switching between languages constantly.

build.sbt

Import play-mongo-bson in your project. Only Scala 2.12 and 2.13 is supported.

You will need to provide also Scala mongo driver.

resolvers ++= Seq(
  ...
  "Sonatype OSS" at "https://oss.sonatype.org/content/groups/public"
)

libraryDependencies ++= Seq(
  ...
  "ai.snips" %% "play-mongo-bson" % "0.5.1",
  "org.mongodb.scala" %% "mongo-scala-driver" % "2.6.0"
)

DAO

  • extend BaseDAO[_] by specifying you data class.
  • register you case case in the codec registry using CodecGen.
  • declare your mongo collection.
class SampleDataDAO @Inject()(val dbContext: DatabaseContext)
                             (implicit ec: ExecutionContext) extends BaseDAO[SampleData] {

  CodecGen[SampleData](dbContext.codecRegistry)

  val db = dbContext.database("sample_db")

  override val collection = db.getCollection[SampleData]("sample_data")
}

Configuration

Provide "mongodb.uri" in application.conf, if not provided "mongodb://localhost" will be used.

Sample

Go to sample project (HomeController, SampleDataDAO) on how to use it

Supported types

  • String
  • Boolean
  • Int
  • Long
  • Double
  • ObjectId
  • UUID
  • Enumeration
  • java.time.Instant
  • Option[_]
  • Either[_,_]
  • Seq[_]
  • Set[_]
  • List[_]
  • Map[_,_]
  • Simple monomorphic and non recursive ADTs
case class Point(x: Int, y: Int)
CodecGen[Point](registry)

sealed trait Shape
final case class Rectangle(bottomLeft: Point, topRight: Point) extends Shape
final case class Circle(center: Point, radius: Int) extends Shape
final case object Empty extends Shape
CodecGen[Shape](registry)

Default values are not yet supported (eg. case class Foo(bar: Int = 12))

Publishing release

export SONATYPE_USERNAME=...
export SONATYPE_PASSWORD=...
sbt "project lib" release

This will publish artifacts in release and create a new snapshot.

License

Licensed under MIT license (LICENSE or http://opensource.org/licenses/MIT)

play-mongo-bson's People

Contributors

gstraymond avatar guillaumesaintraymondsonos avatar kali avatar mkondratek avatar nadenf avatar rby avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.