Skip to content

alonsodomin/enumeratum-mongodb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Enumeratum MongoDB

Build Status License Maven Central

Extension for Enumeratum and official MongoDB Scala Driver

Getting started

Add the following to your build.sbt:

libraryDependencies += "com.github.alonsodomin" %% "enumeratum-mongodb" % <version>

Usage

Define an enumerated value following the guidelines stated in the Enumeratum documentation:

import enumeratum._
import enumeratum.mongodb._

sealed trait ShirtSize extends EnumEntry
object ShirtSize extends Enum[ShirtSize] with MongoDBEnum[ShirtSize] {
  case object Small  extends ShirtSize
  case object Medium extends ShirtSize
  case object Large  extends ShirtSize

  val values = findValues
}

To make it compatible with MongoDB Driver, we need to mix-in the MongoDBEnum trait into our enum definition as shown above, that will provide with the MongoDB codec generation code required to be abe to use this enum.

To have the codec available when communicating with MongoDB, we need to define a CodecRegistry as follows:

val enumCodecRegistry = CodecRegistry.fromProviders(ShirtSize.bsonCodecProvider)

About

MongoDB Extension for Enumeratum

Resources

License

Stars

Watchers

Forks

Packages

No packages published