How do I handle my json writes to update existing objects in ReactiveMongo?

1.1k Views Asked by At

In my current Play2 project I have implemented ReactiveMongo to store my user objects.

User:

case class User(override var _id: Option[BSONObjectID] = None,
                override var created: Option[DateTime] = None,
                override var updated: Option[DateTime] = None,
                firstName: String,
                lastName: String,
                email: String,
                emailValidated: Boolean,
                phoneNumber: String,
                lastLogin: DateTime,
                linkedProviders: Seq[LinkedProvider],
                userRoles: Seq[UserRole.UserRole]) extends TemporalModel {
}

import EnumUtils.enumReads
implicit val userRoleReads = enumReads(UserRole)
import mongo_models.LinkedProvider.linkedProviderReads
implicit val userReads: Reads[User] = (
    (__ \ "_id").read[Option[BSONObjectID]] and
      (__ \ "created").read[Option[DateTime]] and
      (__ \ "updated").read[Option[DateTime]] and
      (__ \ "firstName").read[String] and
      (__ \ "lastName").read[String] and
      (__ \ "email").read[String] and
      (__ \ "emailValidated").read[Boolean] and
      (__ \ "phoneNumber").read[String] and
      (__ \ "lastLogin").read[DateTime] and
      (__ \ "linkedProviders").read(list[LinkedProvider](linkedProviderReads)) and
      (__ \ "userRoles").read(list[UserRole.UserRole])
    )(User.apply _)


  import EnumUtils.enumWrites
  import mongo_models.LinkedProvider.linkedProviderWrites
  import play.api.libs.json.Writes._

  implicit val userWrites: Writes[User] = (
    (__ \ "_id").write[Option[BSONObjectID]] and
      (__ \ "created").write[Option[DateTime]] and
      (__ \ "updated").write[Option[DateTime]] and
      (__ \ "firstName").write[String] and
      (__ \ "lastName").write[String] and
      (__ \ "email").write[String] and
      (__ \ "emailValidated").write[Boolean] and
      (__ \ "phoneNumber").write[String] and
      (__ \ "lastLogin").write[DateTime] and
      (__ \ "linkedProviders").write(Writes.traversableWrites[LinkedProvider](linkedProviderWrites)) and
      (__ \ "userRoles").write(Writes.traversableWrites[UserRole.UserRole])
    )(unlift(User.unapply))

LinkedProvider:

case class LinkedProvider(userId: String,
                          providerId: String,
                          authMethod: String,
                          avatarUrl: String,
                          oAuth1Info: Option[OAuth1Info] = None,
                          oAuth2Info: Option[OAuth2Info] = None,
                          passwordInfo: Option[PasswordInfo] = None) {

}

object LinkedProvider {

  implicit val o1 = Json.format[OAuth1Info]
  implicit val o2 = Json.format[OAuth2Info]
  implicit val p = Json.format[PasswordInfo]

  implicit val linkedProviderReads: Reads[LinkedProvider] = (
    (__ \ "userId").read[String] and
      (__ \ "providerId").read[String] and
      (__ \ "authMethod").read[String] and
      (__ \ "avatarUrl").read[String] and
        (__ \ "oAuth1Info").read[Option[OAuth1Info]] and
        (__ \ "oAuth2Info").read[Option[OAuth2Info]] and
          (__ \ "passwordInfo").read[Option[PasswordInfo]]
    )(LinkedProvider.apply _)

  implicit val linkedProviderWrites: Writes[LinkedProvider] = (
    (__ \ "userId").write[String] and
      (__ \ "providerId").write[String] and
      (__ \ "authMethod").write[String] and
      (__ \ "avatarUrl").write[String] and
      (__ \ "oAuth1Info").write[Option[OAuth1Info]] and
      (__ \ "oAuth2Info").write[Option[OAuth2Info]] and
      (__ \ "passwordInfo").write[Option[PasswordInfo]]
    )(unlift(LinkedProvider.unapply))
}

As you can see I have implemented reads and writes so that the objects are correctly stored in the mongoDB instance. When doing the insert with a new object everything is working like a charm and the object structure is correctly saved and retrieved from mongoDB.

What I'm having issues with is figuring out how to handle the updates. So let say I want to update some values on my user object like this:

val userForUpdate = user.copy(
       firstName = identity.firstName,
       lastName = identity.lastName,
       email = identity.email.getOrElse(""),
       lastLogin = DateTime.now()
       )

And then call my update method:

UserDAO.update(user._id.get.stringify, userForUpdate, false)

updateMethod:

def update(id: String, document: T, upsert: Boolean)(implicit writer: Writes[T]): Future[Either[ServiceException, T]] = {
    document.updated = Some(new DateTime())
    Logger.debug(s"Updating document: [collection=$collectionName, id=$id, document=$document]")
    Recover(collection.update(DBQueryBuilder.id(id), DBQueryBuilder.set(document))) {
      document
    }
  }

DBQueryBuilder.set() method:

def set[T](data: T)(implicit writer: Writes[T]): JsObject = Json.obj("$set" -> data)

This will cause this error:

[error] application - DB operation failed: [message=DatabaseException['Mod on _id not allowed' (code = 10148)]]
[error] application - DatabaseException: [code=10148, isNotAPrimaryError=false]

since the writes, (__ \ "_id").write[Option[BSONObjectID]], states that also the _id field should be serialized when calling the DBQueryBuilder.set() method. Updating _id is as we know not allowed and should definitely not be done in this case.

So my question is: How should I handle this? I guess there some smart Scala way that do not involve me writing the entire "$set" -> query? Maybe creating a better DBQueryBuilder? Maybe defining another writes definition?
Please gimmie your best shot and remember that I'm a Scala newbie so be gentle =) !

2

There are 2 best solutions below

1
On

How about this?

def set[T](data: T)(implicit writer: Writes[T]): JsObject = {
    val jsonTransformer = (__ \ '$set \ '_id).json.prune
    val dataWithId = Json.obj("$set" -> data)
    val withoutId = dataWithId.transform(jsonTransformer).get
    withoutId
}
1
On

Instead of

def set[T](data: T)(implicit writer: Writes[T]): JsObject = Json.obj("$set" -> data)

You could use something like that

def set[T](data: T)(implicit writer: Writes[T]): JsObject = {
    val data = Json.obj(data)
    val dataWithoutId = data.remove("_id")
    Json.obj("$set" -> dataWithoutId)
}

There should be something in the Play Json library to remove the _id field (but it may not be "remove(...)"...)


Since you use option you would probably write something like:

val userForUpdate = user.copy(
       id = None,
       firstName = identity.firstName,
       lastName = identity.lastName,
       email = identity.email.getOrElse(""),
       lastLogin = DateTime.now()
       )

The best way is probably to use a common trait for all classes representing Mongo documents (maybe the T you use?) and add on the trait the possibility to remove the id (set it to none).

This way you could call:

def set[T](data: T)(implicit writer: Writes[T]): JsObject = Json.obj("$set" -> data.copy(_id = None))