InitDb.scala 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package com.weEat.migrations
  2. import org.mongodb.scala.MongoDatabase
  3. import com.weEat.models.{Authorization,User}
  4. import com.weEat.shared.models.FoodNode
  5. import com.mongodb.client.model._
  6. import org.mongodb.scala.model.Filters._
  7. import org.mongodb.scala.model.Indexes._
  8. import java.util.concurrent.TimeUnit._
  9. import org.bson.BsonType._
  10. import scala.concurrent.{ExecutionContext,Future}
  11. object InitDb extends Migration {
  12. // Must be multiple of 256
  13. val MAX_SIZE_META_DOCUMENT = 256*4
  14. implicit val ec: ExecutionContext = ExecutionContext.global
  15. private def typ = `type` _
  16. def execute(db: MongoDatabase) = Future.sequence(Seq(
  17. initMetadataCollection(db),
  18. createFoodCollection(db),
  19. createUserCollection(db),
  20. createTokenCollection(db)
  21. ))
  22. def initMetadataCollection(db: MongoDatabase) = {
  23. db.createCollection(Metadata.collectionName, new CreateCollectionOptions()
  24. .capped(true)
  25. .maxDocuments(1)
  26. .sizeInBytes(MAX_SIZE_META_DOCUMENT)
  27. ).head().map(res =>
  28. db.getCollection[Metadata](Metadata.collectionName)
  29. .insertOne(Metadata())
  30. .head()
  31. ).flatten
  32. }
  33. def createFoodCollection(db: MongoDatabase) =
  34. db.createCollection(FoodNode.collectionName,
  35. new CreateCollectionOptions().validationOptions(
  36. new ValidationOptions().validator(
  37. typ("defaultUnit", STRING)
  38. )
  39. )
  40. ).head()
  41. def createUserCollection(db: MongoDatabase) =
  42. db.createCollection(User.collectionName,
  43. new CreateCollectionOptions().validationOptions(
  44. new ValidationOptions().validator(
  45. and(
  46. typ("email", STRING),
  47. typ("password", STRING),
  48. typ("created", DATE_TIME)
  49. )
  50. )
  51. )
  52. ).head().map(res => db.getCollection[User](User.collectionName)
  53. // TODO: this would be better as hashed
  54. // Currently ascending because hashed does not support unique
  55. .createIndex(ascending("email"), new IndexOptions().unique(true)).head()
  56. ).flatten
  57. def createTokenCollection(db: MongoDatabase) =
  58. db.createCollection(Authorization.collectionName,
  59. new CreateCollectionOptions().validationOptions(
  60. new ValidationOptions().validator(
  61. and(
  62. typ("email", STRING),
  63. typ("userId", OBJECT_ID),
  64. typ("accessToken", BINARY),
  65. typ("refreshToken", BINARY),
  66. typ("created", DATE_TIME)
  67. )
  68. )
  69. )
  70. ).head().map(res =>
  71. db.getCollection[Authorization](Authorization.collectionName)
  72. .createIndexes(Seq(
  73. // TODO: These two would be better as hashed
  74. // Currently ascending because hashed does not support unique
  75. new IndexModel(ascending("accessToken"), new IndexOptions()
  76. .unique(true)),
  77. new IndexModel(ascending("refreshToken"), new IndexOptions()
  78. .unique(true)),
  79. new IndexModel(ascending("created"),new IndexOptions()
  80. .expireAfter(Authorization.refreshFreshTime.toSeconds, SECONDS)
  81. )
  82. )).head()
  83. ).flatten
  84. }