metricq / aiocouch

🛋 An asynchronous client library for CouchDB 2.x and 3.x
https://aiocouch.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
29 stars 10 forks source link

Access database._security document #13

Closed adrienverge closed 4 years ago

adrienverge commented 4 years ago

Currently, trying to read or write _security document using db["_security"] fails, because aiocouch tries to access it as a regular document. But _security is special: it doesn't have _id not _rev (it's one of the only documents without revisions).

I propose to implement wrappers to get and set this document:

async with CouchDB() as couch:
    db = await couch["db"]
    print(await db.get_security())
    await db.set_security({
        "members": {"names": ["user1"],
                    "roles": ["db_reader", "db_writer"]},
    })

PS: About the wording get_security() / set_security(), your feedback is welcome!

bmario commented 4 years ago

My aim is that everything that "feels" like a document, should eventually have at least a similar interface like the Document class. Thus, 18ba9a10c27c97ff418d49000e290ad999526076 adds a new SecurityDocument, which is available as data.security().

adrienverge commented 4 years ago

Again, your solution is pretty neat, I like it!

However, I'm not a fan of these 2 lines inside SecurityDocument.fetch():

        self.setdefault("members", {"names": [], "roles": []})
        self.setdefault("admins", {"names": [], "roles": []})

because the user can choose to have just {members: {names: ["a", "b"]}} or even {}. It's supported by CouchDB, it's the default (a fresh database comes with nothing), and we use this to have lightweight shards (again, performance -- we operate 100k+ databases).

Do you think we could remove these lines, and adapt the helpers you write, e.g.

     @property
     def members(self):
-        return self["members"]["names"]
+        return self.get("members", {}).get("names", [])

?

bmario commented 4 years ago

I see the idea, but the problem with that specific code is that its behavior is inconsistent and users can easily run into issues.

I think 4e3f045f3796aad7f9e10ee6534761dfd3024d6a is a good comprimise.

adrienverge commented 4 years ago

I think 4e3f045 is a good comprimise.

:+1: