package eu.xeppaka.scalalearn

import org.mongodb.scala.bson.codecs.Macros._
import org.mongodb.scala.bson.codecs.DEFAULT_CODEC_REGISTRY
import org.bson.codecs.configuration.CodecRegistries.{fromCodecs, fromProviders, fromRegistries}
import org.mongodb.scala.MongoClient

import scala.concurrent.Await
import scala.concurrent.duration._

object Main13 {
  case class SomeData(field1: String, field2: Option[String])

  def main(args: Array[String]): Unit = {
    val codecRegistry = fromRegistries(fromProviders(classOf[SomeData]), DEFAULT_CODEC_REGISTRY)
    val mongo = MongoClient()
    val db = mongo.getDatabase("db-0003").withCodecRegistry(codecRegistry)
    val coll = db.getCollection[SomeData]("coll0001")

    Await.ready(coll.insertOne(SomeData("field1", Some("field2"))).toFuture(), 1.second)
    Await.ready(coll.insertOne(SomeData("field1", None)).toFuture(), 1.second)

    val res = Await.result(coll.find().toFuture(), 2.seconds)
    res.foreach(println)

    mongo.close()
  }
}
