项目作者: scalajs-io

项目描述 :
Mongoose MongoDB ODM
高级语言: Scala
项目地址: git://github.com/scalajs-io/mongoose.git
创建时间: 2017-02-09T20:28:20Z
项目社区:https://github.com/scalajs-io/mongoose

开源协议:Apache License 2.0

下载


Mongoose API for Scala.js

mongoose - Mongoose MongoDB ODM.

Description

Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment.

NOTE: This is a work-in-progress binding.

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._
  3. import io.scalajs.npm.mongodb.doc
  4. import io.scalajs.npm.mongoose._
  5. import scalajs.concurrent.JSExecutionContext.Implicits.queue
  6. import scalajs.js
  7. // define the type-safe schema trait
  8. @js.native
  9. trait ContactLike extends js.Object {
  10. var name: String = js.native
  11. var age: js.UndefOr[Int] = js.native
  12. var bio: js.UndefOr[String] = js.native
  13. var date: js.UndefOr[Double] = js.native
  14. var buff: js.UndefOr[buffer.Buffer] = js.native
  15. }
  16. // define the schema
  17. val contactSchema = {
  18. import Mongoose.Schema.Types._
  19. Schema[ContactLike](
  20. "name" -> SchemaField(`type` = String, default = "John Doe"),
  21. "age" -> SchemaField(`type` = Number, min = 18, c = true),
  22. "bio" -> SchemaField(`type` = String, `match` = js.RegExp("[a-z]")),
  23. "date" -> SchemaField(`type` = Date, default = js.Date.now),
  24. "buff" -> Buffer)
  25. }
  26. // register the model
  27. val Contacts = Mongoose.model("Contact", contactSchema)
  28. // create and populate an instance of the model
  29. val newContact = Contacts()
  30. newContact.name = "John Doe"
  31. newContact.age = 21
  32. newContact.bio = "Lover of life"
  33. newContact.date = js.Date.now()
  34. for {
  35. // connect to the database
  36. _ <- Mongoose.connectAsync("mongodb://localhost:27017/test").future
  37. // save the contact
  38. saved <- newContact.save().toFuture
  39. _ = println(s"saved contact: ${JSON.stringify(saved)}")
  40. // retrieve the contact by ID
  41. contactId = saved._id.orNull
  42. contactById <- Contacts.findById(contactId).exec().toFuture
  43. _ = println(s"contact-by-Id: ${JSON.stringify(contactById)}")
  44. // update the contact
  45. updateResult <- saved.increment().update(doc("name" -> "John Travolta", "age" -> 63)).toFuture
  46. _ = println(s"updated contact: ${JSON.stringify(saved)}")
  47. _ = Assert.ok(updateResult.nModified == 1 && updateResult.isOk, JSON.stringify(updateResult))
  48. // retrieve a contact via where clause
  49. oneContactWhere <- Contacts.findOne(doc()).where("age").gt(60).exec().toFuture
  50. _ = println(s"one-contact-where: ${JSON.stringify(oneContactWhere)}")
  51. // delete the contact
  52. deleted <- oneContactWhere.remove().toFuture
  53. _ = println(s"deleted contact: ${JSON.stringify(deleted)}")
  54. } {
  55. println("Done.")
  56. }

Artifacts and Resolvers

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

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

Optionally, you may add the Sonatype Repository resolver:

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