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

Don't raise exception if one doc in ids list doesn't exist #27

Closed H--o-l closed 4 years ago

H--o-l commented 4 years ago

Hey! Thanks for this extra-cool library.

Sometime it's needed to fetch a precise list of documents, but without knowing if they exist or not. CouchDB itself allow that, with POST /_all_docs. For each returned row, there is either:

But aiocouch API catch the error, raise an exception, and stop its async generator: https://github.com/metricq/aiocouch/blob/9d9ae99ba6b3b44b11358c59822529d3982de0d6/aiocouch/view.py#L109-L112

It then become really hard to further process the result, and do something with documents that does exists.

It also seems impossible to workaround it, without calling RemoteView itself.

One example of what could be the usage:

# List of ids which may or may not exists.
ids = ['toto', 'titi']

docs = await db.docs(ids)  # Get all data in one database call.
ok_doc = [doc for doc in docs if not doc.error]
nok_doc = [doc for doc in docs if doc.error]

Do you have an idea how it could or should be implemented? I'd be happy to contribute!

bmario commented 4 years ago

If I understand you correctly, you can achieve the wanted behavior with the create parameter.

# List of ids that may or may not exists.
ids = ['toto', 'titi']

ok_doc = []
nok_doc = []
async for doc in db.docs(ids=ids, create=True):
    if doc.exists:
        ok_doc.append(doc)
    else:
        nok_doc.append(doc)
H--o-l commented 4 years ago

Thanks @bmario it's a nice idea! I have just one question just to be sure, I don't want to create the document, can you confirm it will not create it as long as I don't save it, is that it?

H--o-l commented 4 years ago

I have tested and I can answer to myself: as long as I don't save the un-existing document it does not create it. I'm closing this issue, thanks @bmario !

bmario commented 4 years ago

I have just one question just to be sure, I don't want to create the document, can you confirm it will not create it as long as I don't save it, is that it?

Yes, indeed. Documents will only ever be created on the server, when you call the save() method.