Created
July 8, 2015 17:21
-
-
Save pjazdzewski1990/537835c0e6d73f3620c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val suppliers = TableQuery[Suppliers] | |
val products = TableQuery[Products] | |
val tablesExist: DBIO[Boolean] = MTable.getTables.map { tables => | |
val names = Vector(suppliers.baseTableRow.tableName, products.baseTableRow.tableName) | |
names.intersect(tables.map(_.name.name)) == names | |
} | |
val create: DBIO[Unit] = (suppliers.schema ++ products.schema).create | |
val createIfNotExist: DBIO[Unit] = tablesExist.flatMap(exist => if (!exist) create else SuccessAction{}) | |
val insertSuppliers: DBIO[Option[Int]] = suppliers.map(s => (s.id, s.name)) ++= Seq((1, "Fruits Co."), (2, "Birds R Us")) | |
val insertProducts: DBIO[Option[Int]] = products.map(p => (p.supplierId, p.name)) ++= Seq((1, "Bananas"), (1, "Oranges"), (2, "Norwegian Blue")) | |
val reloadData: DBIO[Option[Int]] = products.delete >> suppliers.delete >> insertSuppliers >> insertProducts | |
val listSuppliers: DBIO[Seq[String]] = suppliers.map(_.name).result | |
val suppliersNames: Future[Seq[String]] = db.run(createIfNotExist >> reloadData >> listSuppliers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment