本文整理汇总了Scala中reactivemongo.api.DefaultDB类的典型用法代码示例。如果您正苦于以下问题:Scala DefaultDB类的具体用法?Scala DefaultDB怎么用?Scala DefaultDB使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了DefaultDB类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Scala代码示例。
示例1: db
//设置package包名称以及导入依赖的类
package org.cristal.repository.dao
import reactivemongo.api.{DefaultDB, MongoDriver}
import reactivemongo.core.nodeset.Authenticate
import scala.concurrent.ExecutionContext
//import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future //FIXME change for the actor execution context
trait DBConnection {
val dbName = "cristal"
val userName = "cristal"
val password = "CristalRW2017"
val driver = new MongoDriver
val credentials = List(Authenticate(dbName, userName, password))
val connection = driver.connection(List("localhost"), authentications = credentials)
def db(implicit executor: ExecutionContext): Future[DefaultDB] = connection.database("cristal")
}
开发者ID:frecano,项目名称:cristal,代码行数:19,代码来源:DBConnection.scala
示例2: Database
//设置package包名称以及导入依赖的类
package de.choffmeister.microserviceutils.mongodb
import akka.Done
import com.typesafe.config.ConfigFactory
import reactivemongo.api.collections.bson.BSONCollection
import reactivemongo.api.{DefaultDB, MongoConnection, MongoDriver}
import scala.concurrent.{ExecutionContext, Future}
abstract class Database(val database: DefaultDB, collectionNamePrefix: String = "")(implicit ec: ExecutionContext) {
def collection(name: String): BSONCollection = database.collection[BSONCollection](collectionNamePrefix + name)
def init(): Future[Done]
def clear(): Future[Done]
}
object Database {
def open[DB <: Database](f: DefaultDB => DB)(implicit ec: ExecutionContext): Future[DB] = {
val config = ConfigFactory.load().getConfig("mongodb")
val uri = config.getString("uri")
val databaseName = config.getString("database-name")
val driver = MongoDriver()
val mongodbUri = MongoConnection.parseURI(uri)
val connection = Future.fromTry(mongodbUri.map(driver.connection))
connection
.flatMap(_.database(databaseName).map(f))
.flatMap(db => db.init().map(_ => db))
}
}
开发者ID:choffmeister,项目名称:microservice-utils,代码行数:32,代码来源:Database.scala
示例3: MongoCountAction
//设置package包名称以及导入依赖的类
package com.ringcentral.gatling.mongo.action
import com.ringcentral.gatling.mongo.command.MongoCountCommand
import com.ringcentral.gatling.mongo.response.MongoCountResponse
import io.gatling.commons.stats.KO
import io.gatling.commons.util.TimeHelper.nowMillis
import io.gatling.commons.validation._
import io.gatling.core.action.Action
import io.gatling.core.config.GatlingConfiguration
import io.gatling.core.session.{Expression, Session, _}
import io.gatling.core.stats.StatsEngine
import reactivemongo.api.DefaultDB
import reactivemongo.play.json.collection.JSONCollection
//TODO remove global context everywhere
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{Failure, Success}
class MongoCountAction(command: MongoCountCommand, database: DefaultDB, val statsEngine: StatsEngine, configuration: GatlingConfiguration, val next: Action) extends MongoAction(database) {
override def name: String = genName("Mongo count command")
override def commandName: Expression[String] = command.commandName
override def executeCommand(commandName: String, session: Session): Validation[Unit] =
for {
collectionName <- command.collection(session)
selectorDocument <- resolveOptionalExpression(command.selector, session)
hint <- resolveOptionalExpression(command.hint, session)
selector <- selectorDocument match {
case Some(d) => string2JsObject(d).map(Some.apply)
case None => NoneSuccess
}
} yield {
val sent = nowMillis
val collection: JSONCollection = database.collection[JSONCollection](collectionName)
collection.count(selector, command.limit, command.skip, hint).onComplete {
case Success(result) => processResult(session, sent, nowMillis, command.checks, MongoCountResponse(result), next, commandName)
case Failure(err) => executeNext(session, sent, nowMillis, KO, next, commandName, Some(err.getMessage))
}
}
}
开发者ID:RC-Platform-Disco-Team,项目名称:gatling-mongodb-protocol,代码行数:44,代码来源:MongoCountAction.scala
示例4: MongoUtils
//设置package包名称以及导入依赖的类
package com.ringcentral.gatling.mongo
import reactivemongo.api.MongoConnection.{ParsedURI, URIParsingException}
import reactivemongo.api.{DefaultDB, MongoConnection, MongoDriver}
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.{Await, Future}
import scala.util.{Failure, Success, Try}
// fixme remove global context
import scala.concurrent.ExecutionContext.Implicits.global
object MongoUtils {
private val defaultPort: Int = 27017
private lazy val mongoDriver = new MongoDriver()
private def establishConnection(uri: ParsedURI, dbName: String, connectionTimeout: FiniteDuration): DefaultDB = {
Await.result(establishConnection(uri, dbName), connectionTimeout)
}
private def establishConnection(uri: ParsedURI, dbName: String): Future[DefaultDB] =
Try(mongoDriver.connection(uri).database(dbName))
match {
case Success(db) => db
case Failure(err) =>
throw new IllegalStateException(s"Can't connect to database ${printHosts(uri.hosts)}: ${err.getMessage}", err)
}
private def printHosts(hosts: List[(String, Int)]): String = hosts.map { case (host, port) => s"$host:$port" }.mkString(", ")
def connectToDB(uri: ParsedURI, connectionTimeout: FiniteDuration): DefaultDB =
uri.db match {
case Some(dbName) => establishConnection(uri, dbName, connectionTimeout)
case None => throw new IllegalStateException(s"Can't connect to database $uri.")
}
def connectToDB(uri: String, connectionTimeout: FiniteDuration): DefaultDB = connectToDB(parseUri(uri), connectionTimeout)
def parseHosts(hosts: Seq[String]): Seq[(String, Int)] = hosts.map { hostAndPort =>
hostAndPort.split(':').toList match {
case host :: port :: Nil =>
host -> Try(port.toInt).filter(p => p > 0 && p < 65536)
.getOrElse(throw new URIParsingException(s"Could not parse hosts '$hosts' from URI: invalid port '$port'"))
case host :: Nil =>
host -> defaultPort
case _ => throw new URIParsingException(s"Could not parse hosts from URI: invalid definition '$hosts'")
}
}
def parseUri(uri: String): ParsedURI = {
MongoConnection.parseURI(uri) match {
case Success(parsedUri) => parsedUri
case Failure(err) => throw new IllegalStateException(s"Can't parse database uri. $err")
}
}
}
开发者ID:RC-Platform-Disco-Team,项目名称:gatling-mongodb-protocol,代码行数:58,代码来源:MongoUtils.scala
示例5:
//设置package包名称以及导入依赖的类
package com.themillhousegroup.mondrian.test
import org.specs2.mock.mockito.{MockitoStubs, MocksCreation}
import org.specs2.specification.Scope
import play.modules.reactivemongo.ReactiveMongoApi
import reactivemongo.api.commands._
import reactivemongo.api.{DefaultDB, MongoConnection, MongoConnectionOptions}
trait ScopedMockedReactiveApi extends Scope with MocksCreation with MockitoStubs with MockedConnection {
val mockDB = mock[DefaultDB]
val mockReactiveApi = mock[ReactiveMongoApi]
mockReactiveApi.db returns mockDB
mockDB.connection returns mockConnection
}
trait MockedConnection extends MocksCreation with MockitoStubs {
val mockConnection = mock[MongoConnection]
val mockConnectionOptions = mock[MongoConnectionOptions]
mockConnection.options returns mockConnectionOptions
mockConnectionOptions.writeConcern returns WriteConcern.Default
}
开发者ID:themillhousegroup,项目名称:mondrian,代码行数:24,代码来源:MockedReactiveApi.scala
示例6:
//设置package包名称以及导入依赖的类
package repository
import java.util.concurrent.Executors
import com.typesafe.config.ConfigFactory
import reactivemongo.api.{DefaultDB, MongoConnectionOptions, MongoDriver, ScramSha1Authentication}
import scala.collection.JavaConverters._
import scala.concurrent.{ExecutionContext, Future}
trait MongoConnection {
implicit val ec = ExecutionContext.fromExecutor(Executors.newCachedThreadPool())
private val config = ConfigFactory.load()
private val database = config.getString("mongodb.db")
private val servers = config.getStringList("mongodb.servers").asScala
private val driver = new MongoDriver
private lazy val connection = driver.connection(
nodes = servers,
options = MongoConnectionOptions(authMode = ScramSha1Authentication)
)
lazy val dbFuture: Future[DefaultDB] = connection
.authenticate(
db = database,
user = config.getString("mongodb.user"),
password = config.getString("mongodb.password")
)
.flatMap(authentication => connection.database(database))
}
开发者ID:Leonti,项目名称:receipts-rest-service,代码行数:32,代码来源:MongoConnection.scala
示例7: PublicPostsAccessor
//设置package包名称以及导入依赖的类
package me.lsbengine.api.public
import com.github.nscala_time.time.Imports.DateTime
import me.lsbengine.api.PostsAccessor
import me.lsbengine.database.DatabaseAccessor
import me.lsbengine.database.model.{MongoCollections, Post}
import me.lsbengine.database.model.MongoFormats._
import reactivemongo.api.DefaultDB
import reactivemongo.bson.{BSONDateTime, BSONDocument}
import scala.concurrent.Future
class PublicPostsAccessor(db: DefaultDB)
extends DatabaseAccessor[Post](db, MongoCollections.postsCollectionName)
with PostsAccessor {
def getPost(id: Int): Future[Option[Post]] = {
val now = DateTime.now
val query = BSONDocument("id" -> id,
"published" -> BSONDocument(
"$lte" -> BSONDateTime(now.getMillis)
))
super.getItem(query)
}
def listPosts: Future[List[Post]] = {
val now = DateTime.now
val sort = BSONDocument("published" -> -1)
val query = BSONDocument("published" -> BSONDocument(
"$lte" -> BSONDateTime(now.getMillis)
))
super.getItems(query = query, sort = sort)
}
}
开发者ID:jrabasco,项目名称:LSBEngine,代码行数:35,代码来源:PublicPostsAccessor.scala
注:本文中的reactivemongo.api.DefaultDB类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论