beloglazov / couchdb-scala

A purely functional Scala client for CouchDB
Apache License 2.0
65 stars 19 forks source link

Deleting a doc should not require a full CouchDoc instance #77

Closed giftig closed 5 years ago

giftig commented 6 years ago

This seems overkill, as I know the id and rev of the document I want to delete but don't have a full CouchDoc reference as I discarded it to avoid leaking db layer details into my code. id and rev is all that's required, and indeed your implemention is:

  def delete[D](obj: CouchDoc[D]): Task[Res.DocOk] = {
    if (obj._id.isEmpty)
      Res.Error("cannot_delete", "Document ID must not be empty").toTask
    else {
      client.delete[Res.DocOk](
        s"/$db/${obj._id}?rev=${obj._rev}",
        Status.Ok)
    }
  }

so any other details shouldn't be required. As a workaround I'm simply doing this:

val doc = CouchDoc[String](_id = id, _rev = rev, kind = "", doc = "")
giftig commented 6 years ago

I guess this matches the rest of the API design since to read, modify, and update a document you'd need to pass around the full CouchDoc instance or you'd lose kind details mapped to types. The API feels a little invasive in that you potentially have to expose multiple layers of application code to the database's types, though, whereas just an id and rev is (relatively) generic and portable.