slick

Assign dynamically injected database name in Play Slick

◇◆丶佛笑我妖孽 提交于 2019-12-24 05:35:08
问题 I have the following Play Slick DAO class. Note that the database configuration is a constant control0001 . The DAO has a function readUser that reads a user based on its user id: class UsersDAO @Inject()(@NamedDatabase("control0001") protected val dbConfigProvider: DatabaseConfigProvider) extends HasDatabaseConfigProvider[JdbcProfile] { import driver.api._ def readUser (userid: String) = { val users = TableQuery[UserDB] val action = users.filter(_.userid === userid).result val future = db

Scala - Slick - Getting a TypedType for a wrapped Option[T]

别说谁变了你拦得住时间么 提交于 2019-12-24 03:55:11
问题 It is usual to create custom IDs like this: case class CustomID(value: Int) extends MappedTo[Int] and to represent nullable custom IDs with types like Option[CustomID]. However, I would like to be able to move Option[_] into the case class, like this: case class OptCustomID(optValue: Option[Int]) To be more specific, I am looking for a TypedType[OptCustomId] that behaves like the built-in TypedType[Option[Int]] for what concerns the database DDL. Any ideas? 回答1: Actually you don't need

Slick dynamic groupby

最后都变了- 提交于 2019-12-24 03:23:35
问题 I have code like so: def query(buckets: List[String]): Future[Seq[(List[Option[String]], Option[Double])]] = { database.run { groupBy(row => buckets.map(bucket => customBucketer(row.metadata, bucket))) .map { grouping => val bucket = grouping._1 val group = grouping._2 (bucket, group.map(_.value).avg) } .result } } private def customBucketer(metadata: Rep[Option[String]], bucket: String): Rep[Option[String]] = { ... } I am wanting to be able to create queries in slick which groupby and

How can I extract results of aggregate queries in slick?

一笑奈何 提交于 2019-12-24 03:05:10
问题 I am simply trying to check if any rows that meet certain conditions exist: // Method defined on type T def exists(some_data : Long, other_data : Long) : Boolean = DB.withSession { implicit session : Session => (for { row <- table // table is a Table[T] if row.some_data =!= some_data if row.other_data === other_data } yield row).length > 0 } I am getting this error: polymorphic expression cannot be instantiated to expected type; [error] found : [R]scala.slick.lifted.Column[R] [error] required

Slick: Option column filtering

萝らか妹 提交于 2019-12-24 01:15:05
问题 I want to do something like this (this is a made-up example to simplify my actual problem): def findByGender(isMale: Option[Boolean]) = { People.filter(row => row.name.isNotNull && isMale match { case Some(true) => row.wife.isNotNull // find rows where wife column is not null case Some(false) => row.wife.isNull // find rows where wife column is null case None => true // select everything }) } This does not compile because of the last "true". Any better way to do this? 回答1: You have to make it

How to update a row omitting a column in Slick 3.x?

岁酱吖の 提交于 2019-12-24 00:35:34
问题 I have the following code in Slick that updates an object user: val users = TableQuery[UserDB] val action = users.filter(_.id === user.id).update(user) val future = db.run(action) val result = Await.result(future, Duration.Inf) But there's a field in the user object (password) that I don't want to update. How to omit it? 回答1: You should select columns using a map operation before an update operation: case class User(name: String, age: Int, password: String, id: Int) val updatedUser = User(

Cannot perform option-mapped operation with type: (Boolean, _57) => R

☆樱花仙子☆ 提交于 2019-12-23 23:18:37
问题 I have next filter type DatabaseID = Long val filter = moderators.filter(m => (m.created < before) && (m.userType inSet userTypeList) && (if(true) m.mcID === mcIDFilter else true) ) where m.mcID has Rep[Option[models.DatabaseID]] type and mcIDFilter Option[models.DatabaseID] . Why i'm getting next error? Cannot perform option-mapped operation with type: (Boolean, _57) => R for base type: (Boolean, Boolean) => Boolean _57 ? What is it? I have replaced condition with true for simplicity. If i

Slick 3 return custom case class from query

删除回忆录丶 提交于 2019-12-23 17:43:53
问题 Currently I have something like this: val q = for { department <- departments if department.id === x employee <- employees if employee.departmentId === department.id } yield (department, employee) which will give me: (sales, john) (sales, bob) (finance, william) (finance, helen) I then group the results by departments: val grouped = results.groupBy(_._1).mapValues(_.map(_._2)) to give me: (sales -> (john, bob)) (finance -> (wiliam, helen) I would like to avoid the tuples. Whilst it's quite

Scala & Play! & Slick & PostgreSQL auto increment

拟墨画扇 提交于 2019-12-23 09:58:06
问题 I have the following code in Scala: case class Product(id: Option[Long] = None, name: String, price: BigDecimal, description: String) object Products extends Table[Product]("product") { def id = column[Long]("id", O.AutoInc, O.PrimaryKey) def name = column[String]("name", O.NotNull) def price = column[BigDecimal]("price", O.NotNull) def description = column[String]("description", O.NotNull) def * = id.? ~ name ~ price ~ description <>(Product.apply _, Product.unapply _) def autoInc = *

Slick 3: How to implement repository pattern with transactions?

雨燕双飞 提交于 2019-12-23 08:51:23
问题 In my play framework (2.5) app, I need to write unit tests for services. I need to isolate data access logic to able to test service layer in isolation, for this I want to create repository interfaces and MOCK them in my unit tests: class UserService { def signUpNewUser(username: String, memberName: String): Future[Unit] { val userId = 1 // Set to 1 for demo val user = User(userId, username) val member = Member(memberName, userId) // ---- I NEED TO EXECUTE THIS BLOCK WITHIN TRANSACTION ----