InitDb.scala 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package com.weEat.migrations
  2. import org.mongodb.scala.MongoDatabase
  3. import com.weEat.models.{Authorization,User}
  4. import com.weEat.shared.models.FoodNodeId
  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(FoodNodeId.collectionName,
  35. new CreateCollectionOptions()
  36. ).head()
  37. def createUserCollection(db: MongoDatabase) =
  38. db.createCollection(User.collectionName,
  39. new CreateCollectionOptions().validationOptions(
  40. new ValidationOptions().validator(
  41. and(
  42. typ("email", STRING),
  43. typ("password", STRING),
  44. typ("created", DATE_TIME)
  45. )
  46. )
  47. )
  48. ).head().map(res => db.getCollection[User](User.collectionName)
  49. // TODO: this would be better as hashed
  50. // Currently ascending because hashed does not support unique
  51. .createIndex(ascending("email"), new IndexOptions().unique(true)).head()
  52. ).flatten
  53. def createTokenCollection(db: MongoDatabase) =
  54. db.createCollection(Authorization.collectionName,
  55. new CreateCollectionOptions().validationOptions(
  56. new ValidationOptions().validator(
  57. and(
  58. typ("email", STRING),
  59. typ("userId", OBJECT_ID),
  60. typ("accessToken", BINARY),
  61. typ("refreshToken", BINARY),
  62. typ("created", DATE_TIME)
  63. )
  64. )
  65. )
  66. ).head().map(res =>
  67. db.getCollection[Authorization](Authorization.collectionName)
  68. .createIndexes(Seq(
  69. // TODO: These two would be better as hashed
  70. // Currently ascending because hashed does not support unique
  71. new IndexModel(ascending("accessToken"), new IndexOptions()
  72. .unique(true)),
  73. new IndexModel(ascending("refreshToken"), new IndexOptions()
  74. .unique(true)),
  75. new IndexModel(ascending("created"),new IndexOptions()
  76. .expireAfter(Authorization.refreshFreshTime.toSeconds, SECONDS)
  77. )
  78. )).head()
  79. ).flatten
  80. }