2

I there! I'm following Play-Auth-Slick-Seed example that works excellent! and I'm trying to propose the following model mimicking the User model in there (User.scala, DBTableDefinitions.scala, UserDAOImpl.scala, UserDAO.scala) The model:

 Node.scala
  case class Node(
   nodeID: Option[Long],
   name: Option[String],
   nodeCode: Option[String],
  nodeStamp: Option[Date]
 )

In the NodeDAOImpl class I defined the following functions: find, count and create, just to test if I can create the data in my postgres as the app does with users!

def find(nodeID: Long) = {
    val nodeQuery = for {
        dbNode <- slickNodes.filter(_.id === nodeID)
    } yield dbNode
    db.run(nodeQuery.result.headOption).map { dbNodeOption =>
        dbNodeOption.map { node =>
            Node(node.nodeID, node.name,node.nodeCode,node.nodeStamp)
         }
        }
    }


def count(): Future[Int] = {
    db.run(slickNodes.length.result)
}

def create(node: Node) = {
    val dbNode = DBNode(node.nodeID, node.name, node.nodeCode,         node.nodeStamp)
    val actions = (for {
      _ <- slickNodes.insertOrUpdate(dbNode)
    } yield ()).transactionally
    db.run(actions).map(_ => node)
  }

Now, How can I push some data to the database when app starts? I tried to create a Database.scala in module (similar to SilhouetteModule.scala) and I made the following

    class DatabaseModule extends AbstractModule {
      def configure() = {
        bind[NodeDAO].to[NodeDAOImpl]
      }
    } 

But I can't realize where I can add a kind of function like

   StartData.insert()

defined as (many examples suggest)

   object StartData {
     private def nodeDAOImpl = new NodeDAOImpl
     def insert(): Unit = {
       nodeDAOImpl.count map { size =>
         if (size == 0) {
           Logger.info("Insertando nodes");
           val nodes = Seq(
             Node(Option(1L), Option("Node1"), Option("XMZ12RT"), Option(new java.util.Date)))
             )
           nodes.map(nodeDAOImpl.create)
         }
       }
     }
   }

Thnks for any suggestion or guidance! ReneX

  • I could solve the problem modifying the computer-database sample from play-slick-3.0. I move the db to postgres but I got the ERROR: null value in column "id" violates not-null constraint message. I solved the problem changing the id column (primary key) type to SERIAL in the evolution file /conf/evolutions/default/1.sql (originally was in BIGINT). Take a look at https://groups.google.com/forum/?fromgroups=#%21topic/scalaquery/OEOF8HNzn2U for the whole discussion. I upload the modified sample ( no so DRY) to https://github.com/renexdev/play-slick-computer-database-mod. Cheers ReneX – Rene Cejas Bolecek Sep 27 '15 at 04:49

0 Answers0