Is there a way to get attachments in a query response?

With CBL, when querying a view, I would like to get directly attachement’s url or base64 data for each document returned, without having to get the attachment in a second request. Is this possible ?

I don’t think there’s a query option for that but you can query a document with the attachment included in the JSON using the attachments=true query option!/document/get

Yes for sure, you can GET a document with attachments. But the question was when querying a view. Assume you want to Display a list view with a thumbnail for each entry, il would be interesting to have the image attachment directly in the query response without having to get each attachment…

How are you displaying the image on the screen? If it’s in HTML for example you can pass the url to the attachment in <img src="url_to_attachment" /> and have it loaded by the image tag.

But there is no option to query a view with the attachment inlined in the row result.

You haven’t said what platform this is for. And it sounds like you’re using the REST API, probably with PhoneGap?

Returning attachments as part of the view response could be really heavyweight. And it would either bloat the data by 33% (as base64) or we’d have to make the response MIME multipart, which is probably harder for you to parse.

From a PhoneGap type app, it’s much better to put the attachment URL in an IMG tag. That will allow the web-view to optimize the way it loads the data and manages memory.

Yes this is REST API for PhoneGap. I totally agree about returning attachement in the view response would be heavyweight.

But How can I output in the view requery response the attachment url ? What do I have to write in the map function ?


You’ll have to build the URL on the client rather than emitting it in the view. The URL to an attachment will always follow http://{host}:{port}/{db}/{id}/{attachment}. You should already have host, port and db in the client app code. The doc id can be emitted in the view. And you should also be able to emit the attachment name in the view. The document with an attachment has the following structure:

  "_id": 123,
  "_rev": "1-456",
  "_attachments": {
    "myattachmentname": { ... }

In this case the URL to the attachment is http://{host}:{port}/{db}/123/myattachmentname.


Mmmm. Yes this should work but as you know CouchbaseLite for phonegap uses the basic authentification scheme to access the REST api. I am not sure that writing

<img src="http://user:password@{host}:{port}/{db}/123/myattachmentname" />.

Will work… I will give it a try…