项目作者: scalajs-io

项目描述 :
Embedded Node.js database upward compatible with MongoDB
高级语言: Scala
项目地址: git://github.com/scalajs-io/tingodb.git
创建时间: 2017-02-05T19:04:05Z
项目社区:https://github.com/scalajs-io/tingodb

开源协议:Apache License 2.0

下载


TingoDB API for Scala.js

tingodb - Embedded Node.js database upward compatible with MongoDB.

Description

TingoDB is an embedded JavaScript in-process filesystem or in-memory database upwards compatible with MongoDB at the API level.

Upwards compatible means that if you build an app that uses functionality implemented by TingoDB
you can switch to MongoDB almost without code changes. This greatly reduces implementation risks
and give you freedom to switch to a mature solution at any moment.

As a proof for upward compatibility, all tests designed to run against both MongoDB and TingoDB.
Moreover, significant parts of tests contributed from MongoDB nodejs driver projects and are used
as is without modifications.

For those folks who familiar with the Mongoose.js ODM, we suggest to look at Tungus, an experimental
driver that allows using the famous ODM tool with our database.

TingoDB can be dropin replacement for existing apps and frameworks that are based on MongoDB.

Build Dependencies

Build/publish the SDK locally

  1. $ sbt clean publish-local

Running the tests

Before running the tests the first time, you must ensure the npm packages are installed:

  1. $ npm install

Then you can run the tests:

  1. $ sbt test

Examples

  1. import io.scalajs.JSON
  2. import io.scalajs.nodejs.console
  3. import io.scalajs.nodejs.fs.Fs
  4. import io.scalajs.npm.mongodb.{MongoError, doc}
  5. import io.scalajs.npm.tingodb._
  6. import scala.scalajs.js
  7. val dbPath = "./src/test/resources/"
  8. val collName = "actresses"
  9. val collPath = dbPath + "/" + collName
  10. // clear the test
  11. Fs.writeFileSync(collPath, "")
  12. // open a connection
  13. val tingoDB = TingoDB(new TingoDbOptions())
  14. val db = tingoDB.Db.apply(dbPath, new TingoDbOptions())
  15. val collection = db.collection(collName)
  16. // define some actors
  17. val actors = js.Array(
  18. new Actor(firstName = "Drew", lastName = "Barrymore", age = 41),
  19. new Actor(firstName = "Halle", lastName = "Berry", age = 50),
  20. new Actor(firstName = "Grace", lastName = "Park", age = 42)
  21. )
  22. // insert the actors into the database
  23. collection.insert(actors, (err, result) => {
  24. if (err != null) console.log("err =>", err)
  25. else {
  26. console.log(s"result => ${JSON.stringify(result)}")
  27. // read the data from the database
  28. collection.findOne(doc("firstName" -> "Grace"), (err: MongoError, actor: Actor) => {
  29. if (err != null) console.log("err =>", err)
  30. else {
  31. console.log(s"actor => ${JSON.stringify(actor)}")
  32. }
  33. })
  34. }
  35. })
  36. class Actor(var firstName: js.UndefOr[String] = js.undefined,
  37. var lastName: js.UndefOr[String] = js.undefined,
  38. var age: js.UndefOr[Int] = js.undefined)
  39. extends js.Object

Artifacts and Resolvers

To add the tingodb binding to your project, add the following to your build.sbt:

  1. libraryDependencies += "io.scalajs.npm" %%% "tingodb" % "0.5.0"

Optionally, you may add the Sonatype Repository resolver:

  1. resolvers += Resolver.sonatypeRepo("releases")