couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Eiri <e...@eiri.ca>
Subject Re: Howto edit design documents
Date Mon, 14 Aug 2017 17:36:30 GMT

Yes, jq is json manipulation tool, so it can’t format javascript function in “map” attribute,
it treats it just as a string.

The thing with line-breaks is that even while document with multiline formatted function is
a valid design document (i.e. the function can be evaluated as javascript) it is not a valid
json, because json does not allow un-escaped line-breaks. Curl and Fauxtron stripping\escaping
line-breaks during upload, so formatting doc in a file and then uploading it works.

For the reverse operation in just a command line, from top of my head, something like this
should do the trick:

$ curl http://localhost:5984/koi/_design/ddoc | jq . | perl -pe 's/\\n/\n/g'

Here curl pulls the ddoc, jq formats it to a pretty printed json, perl replaces escaped line-breaks
with the real ones. This could be done with different tools, obviously, just a question of
personal preferences.


Regards,
Eric.


> On Aug 14, 2017, at 12:41, Ronny Berndt <ronny@kioskkinder.com> wrote:
> 
> @Stefan:
> Thanks for the hint, couchdb-compile looks nice!
> 
> @Eric
> If i search for that, i saw jq as json tool.
> 
> jq formats the key/value pairs of the json well, but i doesn’t see a possibility to
> format the line breaks in the functions (e.g. map) itself? 
> So after download \n has to be converted to a real linebreak. couchdb-compile will escape
to linebreak to \n…
> 
> Regards, Ronny
> 
>> Am 14.08.2017 um 17:24 schrieb Eiri <eiri@eiri.ca>:
>> 
>> To the first one any pretty printing command line utility will work, I really like
jq (https://stedolan.github.io/jq/)
>> 
>> So with my example just piping to jq with no arguments gives formatted json which
then could be sent to file:
>> 
>> $ curl http://localhost:5984/koi/_design/ddoc -s | jq .
>> {
>> "_id": "_design/ddoc",
>> "_rev": "2-969b8f6ac9911b15c7884d9d5d527093",
>> "views": {
>>   "name": {
>>     "map": "function(doc) { emit(doc.name, doc.age); }"
>>   }
>> }
>> }
>> $ curl http://localhost:5984/koi/_design/ddoc -s | jq . > ddoc2.json
>> 
>> In reality jq is much more powerful tool, so check it out if you haven’t seen it
before.
>> 
>> 
>> For the second one, if I understand the question correctly, no, a design document
doesn’t need to be no-linebreaks json. As long as its values are strings and evaluates to
a proper javascript it could be formatted as necessary, json and javascript function parts
alike, i.e. the following is perfectly valid design document:
>> 
>> {
>> "_id": "_design/ddoc",
>> "_rev": "2-969b8f6ac9911b15c7884d9d5d527093",
>> "views": {
>>   "name": {
>>     "map": "function(doc) {
>>        var name = doc.name;
>>        var age = doc.age;
>>        emit(name, age); 
>>     }"
>>   }
>> }
>> }
>> 
>> 
>> 
>> Regards,
>> Eric
>> 
>> 
>>> On Aug 14, 2017, at 11:50, Ronny Berndt <ronny@kioskkinder.com> wrote:
>>> 
>>> Hi Eric,
>>> 
>>> thank you for your answer.
>>> 
>>> My problem is, if you get the design doc, you get one line of json (no linebreaks,
etc…).
>>> How to reformat to 
>>> 1. a doc with linebreaks to easily edit the file
>>> 2. reformat two one line (or isn’T it necessary for the upload)?
>>> 
>>> all with command line tools (or automatic after downloading/uploading)?
>>> 
>>> Regards, Ronny
>>> 
>>>> Am 14.08.2017 um 16:13 schrieb Eiri <eiri@eiri.ca>:
>>>> 
>>>> Hi Ronny,
>>>> 
>>>> For me “a-ha” moment was to realise that design document is just an ordinary
CouchDB document, so it can be created and updated with any http client: curl, httpie, whatnot.
I’ve writed up a gist with basic demo on using curl for that: https://gist.github.com/eiri/70e3a44421ade7f01d4394ef08f80091
>>>> 
>>>> Key moments to notice there that while updating design document it must have
revision and that value of “map” is a string, but it need to be a proper javascript, as
it’ll be evaluated as such.
>>>> 
>>>> So yes, it is easily possible to manage design documents without third party
tools.
>>>> 
>>>> Sorry, I don’t know the answer on your third question, I’m more of a
command line guy.
>>>> 
>>>> 
>>>> Regards,
>>>> Eric 
>>>> 
>>>> 
>>>>> On Aug 11, 2017, at 15:44, Ronny Berndt <ronny@kioskkinder.com>
wrote:
>>>>> 
>>>>> Hi,
>>>>> 
>>>>> I have some questions regarding the edit of design documents:
>>>>> 
>>>>> 
>>>>> 1. What is the best way to edit design documents locally and upload it
to couchdb?
>>>>> 2. Is it easily possible without third party tools like couchapp or couchdb-bootstrap?
>>>>> 3. How can I add a list function in Fauxton?
>>>>> 
>>>>> Cheers, Ronny
>>>> 
>>> 
>> 
> 


Mime
View raw message