You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by TAE JIN KIM <sn...@hotmail.com> on 2014/12/11 15:42:00 UTC

Enter multiline filter function in source editor?

Hello,

In Fulton admin interface, you click source editor to edit directly...
I was trying to add one filter function like this..

       "filters": {
             "by_country": "function(doc ,req) { blahblah...... }  "
       }

The thing is that when I tried to enter the function with new line for more readable format,
(ex, function (doc,req) {
          blah1
          blah2
         })
 I saw validation error like saying..
"JSON.parse: bad control character in string literal at line 6 column 23 of the JSON data"...

1. I don't think I have to enter the function in one line...is there any escaped character I need to enter to format the function?

2. I know I can query the filter over http rest api, but is there any way I can run the filter function in fulton web interface?

Thanks,

 		 	   		  

Re: Enter multiline filter function in source editor?

Posted by Alexander Shorin <kx...@gmail.com>.
No. Futon tries to work with as with JSON values. JSON strings lacks
multiline support.
--
,,,^..^,,,


On Thu, Dec 11, 2014 at 5:52 PM, TAE JIN KIM <sn...@hotmail.com> wrote:
> So I can't format the function in Futon?
>
>> Date: Thu, 11 Dec 2014 18:45:06 +0400
>> Subject: Re: Enter multiline filter function in source editor?
>> From: kxepal@gmail.com
>> To: user@couchdb.apache.org
>>
>> Futon isn't suitable for design function edition (except views). Try
>> erica: it has nice web editor and is made specially to simplify work
>> on design documents.
>> --
>> ,,,^..^,,,
>>
>>
>> On Thu, Dec 11, 2014 at 5:42 PM, TAE JIN KIM <sn...@hotmail.com> wrote:
>> > Hello,
>> >
>> > In Fulton admin interface, you click source editor to edit directly...
>> > I was trying to add one filter function like this..
>> >
>> >        "filters": {
>> >              "by_country": "function(doc ,req) { blahblah...... }  "
>> >        }
>> >
>> > The thing is that when I tried to enter the function with new line for more readable format,
>> > (ex, function (doc,req) {
>> >           blah1
>> >           blah2
>> >          })
>> >  I saw validation error like saying..
>> > "JSON.parse: bad control character in string literal at line 6 column 23 of the JSON data"...
>> >
>> > 1. I don't think I have to enter the function in one line...is there any escaped character I need to enter to format the function?
>> >
>> > 2. I know I can query the filter over http rest api, but is there any way I can run the filter function in fulton web interface?
>> >
>> > Thanks,
>> >
>> >
>

RE: Validate_Doc_Update

Posted by TAE JIN KIM <sn...@hotmail.com>.
T
Yes, that's absolutely true that I can provide my own _id ..then it will return conflict staust if it is duplicated. HOWEVER, what I was asking,  "Other than _id"..



Subject: RE: Validate_Doc_Update
From: conormacaoidh@gmail.com
Date: Thu, 18 Dec 2014 20:21:00 +0000
To: user@couchdb.apache.org; snowebang@hotmail.com

You could assign your own ids that describe the contents of the document. ie. POST your own '_id' field when creating the document. If the '_id' exists it will fail with an update conflict - stopping duplicate data from being inserted.



Conor

On 18 December 2014 20:12:51 GMT+00:00, TAE JIN KIM <sn...@hotmail.com> wrote:
So other than _id(which is uuid CouchDB provides automatically if not specified explicitly for the entire database), there is no some elegant(?) way  to prevent creating duplicate data inside CouchDB?

Thanks,

 Date: Thu, 18 Dec 2014 23:41:50 +0400
 Subject: Re: Validate_Doc_Update
 From: kxepal@gmail.com
 To: user@couchdb.apache.org
 
 No, that's not possible. Validate doc update function only operates
 with document's new version candidate and the "old", current one. It
 cannot not access to others since this will slow down whole the
 process and break eventual consistency. You should run this check
 outside CouchDB in your client code before PUT the document.
 --
 ,,,^..^,,,
 
 
 On Thu, Dec 18, 2014 at 10:36 PM, TAE JIN KIM <sn...@hotmail.com>
wrote:
 Hello,

 Since CouchDB provides validate_doc_update, I thought that I might be able to provide some to check duplication here..
 like...

 function(newDoc,oldDoc,userCtx) {
       if(newDoc.type == "myCustomType") {
               // here..something like..
               // check newDoc against the existing documents of "myCustomType" to see whether there was already same data or not..
              // for example,
             //  getExistingDocumentsByType("myCustomType").forEach(function(doc) {
            //               if(newDoc.mydata == doc.mydata) {
             //                       ///Boom..throw error..
              //              }
               //    });
            //

 Is this something possible? Well..obviously there is no provided
getExistingDocumentsByType function from couchdb.....just was trying to show what I was trying to achieve...

 So ultimately, my question is that..is there any way I can somehow access the existing documents from validate_doc_update function?

 Thanks,



            

-- 

Sent from my Android device with K-9 Mail. Please excuse my brevity. 		 	   		  

RE: Validate_Doc_Update

Posted by Conor Mac Aoidh <co...@gmail.com>.
You could assign your own ids that describe the contents of the document. ie. POST your own '_id' field when creating the document. If the '_id' exists it will fail with an update conflict - stopping duplicate data from being inserted.

Conor

On 18 December 2014 20:12:51 GMT+00:00, TAE JIN KIM <sn...@hotmail.com> wrote:
>So other than _id(which is uuid CouchDB provides automatically if not
>specified explicitly for the entire database), there is no some
>elegant(?) way  to prevent creating duplicate data inside CouchDB?
>
>Thanks,
>
>> Date: Thu, 18 Dec 2014 23:41:50 +0400
>> Subject: Re: Validate_Doc_Update
>> From: kxepal@gmail.com
>> To: user@couchdb.apache.org
>> 
>> No, that's not possible. Validate doc update function only operates
>> with document's new version candidate and the "old", current one. It
>> cannot not access to others since this will slow down whole the
>> process and break eventual consistency. You should run this check
>> outside CouchDB in your client code before PUT the document.
>> --
>> ,,,^..^,,,
>> 
>> 
>> On Thu, Dec 18, 2014 at 10:36 PM, TAE JIN KIM <sn...@hotmail.com>
>wrote:
>> > Hello,
>> >
>> > Since CouchDB provides validate_doc_update, I thought that I might
>be able to provide some to check duplication here..
>> > like...
>> >
>> > function(newDoc,oldDoc,userCtx) {
>> >       if(newDoc.type == "myCustomType") {
>> >               // here..something like..
>> >               // check newDoc against the existing documents of
>"myCustomType" to see whether there was already same data or not..
>> >              // for example,
>> >             // 
>getExistingDocumentsByType("myCustomType").forEach(function(doc) {
>> >            //               if(newDoc.mydata == doc.mydata) {
>> >             //                       ///Boom..throw error..
>> >              //              }
>> >               //    });
>> >            //
>> >
>> > Is this something possible? Well..obviously there is no provided
>getExistingDocumentsByType function from couchdb.....just was trying to
>show what I was trying to achieve...
>> >
>> > So ultimately, my question is that..is there any way I can somehow
>access the existing documents from validate_doc_update function?
>> >
>> > Thanks,
>> >
>> >
>> >
>> >
> 		 	   		  

-- 
Sent from my Android device with K-9 Mail. Please excuse my brevity.

RE: Validate_Doc_Update

Posted by TAE JIN KIM <sn...@hotmail.com>.
So other than _id(which is uuid CouchDB provides automatically if not specified explicitly for the entire database), there is no some elegant(?) way  to prevent creating duplicate data inside CouchDB?

Thanks,

> Date: Thu, 18 Dec 2014 23:41:50 +0400
> Subject: Re: Validate_Doc_Update
> From: kxepal@gmail.com
> To: user@couchdb.apache.org
> 
> No, that's not possible. Validate doc update function only operates
> with document's new version candidate and the "old", current one. It
> cannot not access to others since this will slow down whole the
> process and break eventual consistency. You should run this check
> outside CouchDB in your client code before PUT the document.
> --
> ,,,^..^,,,
> 
> 
> On Thu, Dec 18, 2014 at 10:36 PM, TAE JIN KIM <sn...@hotmail.com> wrote:
> > Hello,
> >
> > Since CouchDB provides validate_doc_update, I thought that I might be able to provide some to check duplication here..
> > like...
> >
> > function(newDoc,oldDoc,userCtx) {
> >       if(newDoc.type == "myCustomType") {
> >               // here..something like..
> >               // check newDoc against the existing documents of "myCustomType" to see whether there was already same data or not..
> >              // for example,
> >             //  getExistingDocumentsByType("myCustomType").forEach(function(doc) {
> >            //               if(newDoc.mydata == doc.mydata) {
> >             //                       ///Boom..throw error..
> >              //              }
> >               //    });
> >            //
> >
> > Is this something possible? Well..obviously there is no provided getExistingDocumentsByType function from couchdb.....just was trying to show what I was trying to achieve...
> >
> > So ultimately, my question is that..is there any way I can somehow access the existing documents from validate_doc_update function?
> >
> > Thanks,
> >
> >
> >
> >
 		 	   		  

Re: Validate_Doc_Update

Posted by Alexander Shorin <kx...@gmail.com>.
No, that's not possible. Validate doc update function only operates
with document's new version candidate and the "old", current one. It
cannot not access to others since this will slow down whole the
process and break eventual consistency. You should run this check
outside CouchDB in your client code before PUT the document.
--
,,,^..^,,,


On Thu, Dec 18, 2014 at 10:36 PM, TAE JIN KIM <sn...@hotmail.com> wrote:
> Hello,
>
> Since CouchDB provides validate_doc_update, I thought that I might be able to provide some to check duplication here..
> like...
>
> function(newDoc,oldDoc,userCtx) {
>       if(newDoc.type == "myCustomType") {
>               // here..something like..
>               // check newDoc against the existing documents of "myCustomType" to see whether there was already same data or not..
>              // for example,
>             //  getExistingDocumentsByType("myCustomType").forEach(function(doc) {
>            //               if(newDoc.mydata == doc.mydata) {
>             //                       ///Boom..throw error..
>              //              }
>               //    });
>            //
>
> Is this something possible? Well..obviously there is no provided getExistingDocumentsByType function from couchdb.....just was trying to show what I was trying to achieve...
>
> So ultimately, my question is that..is there any way I can somehow access the existing documents from validate_doc_update function?
>
> Thanks,
>
>
>
>

Validate_Doc_Update

Posted by TAE JIN KIM <sn...@hotmail.com>.
Hello,

Since CouchDB provides validate_doc_update, I thought that I might be able to provide some to check duplication here..
like...

function(newDoc,oldDoc,userCtx) {
      if(newDoc.type == "myCustomType") {
              // here..something like..
              // check newDoc against the existing documents of "myCustomType" to see whether there was already same data or not..
             // for example,
            //  getExistingDocumentsByType("myCustomType").forEach(function(doc) {
           //               if(newDoc.mydata == doc.mydata) {
            //                       ///Boom..throw error..
             //              }
              //    });
           // 

Is this something possible? Well..obviously there is no provided getExistingDocumentsByType function from couchdb.....just was trying to show what I was trying to achieve...

So ultimately, my question is that..is there any way I can somehow access the existing documents from validate_doc_update function?

Thanks,



 		 	   		  

Re: Anyway I can define my javascript function that can be accessed in every design?

Posted by James Dingwall <ja...@zynstra.com>.
 > My question is ..is there any way I can define some common function 
that this can be accessed from every design doc? Thanks,
Short answer: no.

Longer answer:  Yes, I manage my design docs using couchapp and symlink 
common code between design documents in to views/lib/commoncode.js which 
can then be require()d in the view function.  i.e. something like:

views/lib/commoncode.js:

function api() {
   var _afunc = function(a1, a2) {
     ...
   };

   var _api = {
     afunc: _afunc
   };

   return(_api);
}

exports.cc = api();

// views/lib/commoncode.js

My view map.js:

function(doc) {
   var cc = require('views/lib/commoncode').cc;

   if(cc.afunc(doc, 'banana') === true) {
      emit(doc._id);
   }
}

Notes:
  - you cannot require() in the reduce function but there is an open bug 
for that feature.
  - shared code for views *must* be kept under the views key in the 
design document. The content of the views key is hashed to determine if 
view indexes need to be rebuilt after a design document update.  There 
is no problem with other design doc operations doing a require() of code 
from views/lib (show/list/update etc).

Regards,
James


Anyway I can define my javascript function that can be accessed in every design?

Posted by TAE JIN KIM <sn...@hotmail.com>.
Hello,

In mydesign1/firstview,

my function has been defiend like..
function mycommon() {
   ..
 ...
}

in mydesigin1/secondview
I am able to use the mycommon javascript function that has been defined in firstview of same design doc, which is mydesign1.

Say I created another design doc, which is mydesign2..

in mydesign2/firstview, I can't access the mycommon function because this is different design doc...

My question is ..is there any way I can define some common function that this can be accessed from every design doc?

Thanks,
 		 	   		  

RE: Enter multiline filter function in source editor?

Posted by TAE JIN KIM <sn...@hotmail.com>.
So I can't format the function in Futon?

> Date: Thu, 11 Dec 2014 18:45:06 +0400
> Subject: Re: Enter multiline filter function in source editor?
> From: kxepal@gmail.com
> To: user@couchdb.apache.org
> 
> Futon isn't suitable for design function edition (except views). Try
> erica: it has nice web editor and is made specially to simplify work
> on design documents.
> --
> ,,,^..^,,,
> 
> 
> On Thu, Dec 11, 2014 at 5:42 PM, TAE JIN KIM <sn...@hotmail.com> wrote:
> > Hello,
> >
> > In Fulton admin interface, you click source editor to edit directly...
> > I was trying to add one filter function like this..
> >
> >        "filters": {
> >              "by_country": "function(doc ,req) { blahblah...... }  "
> >        }
> >
> > The thing is that when I tried to enter the function with new line for more readable format,
> > (ex, function (doc,req) {
> >           blah1
> >           blah2
> >          })
> >  I saw validation error like saying..
> > "JSON.parse: bad control character in string literal at line 6 column 23 of the JSON data"...
> >
> > 1. I don't think I have to enter the function in one line...is there any escaped character I need to enter to format the function?
> >
> > 2. I know I can query the filter over http rest api, but is there any way I can run the filter function in fulton web interface?
> >
> > Thanks,
> >
> >
 		 	   		  

Re: Enter multiline filter function in source editor?

Posted by Alexander Shorin <kx...@gmail.com>.
Futon isn't suitable for design function edition (except views). Try
erica: it has nice web editor and is made specially to simplify work
on design documents.
--
,,,^..^,,,


On Thu, Dec 11, 2014 at 5:42 PM, TAE JIN KIM <sn...@hotmail.com> wrote:
> Hello,
>
> In Fulton admin interface, you click source editor to edit directly...
> I was trying to add one filter function like this..
>
>        "filters": {
>              "by_country": "function(doc ,req) { blahblah...... }  "
>        }
>
> The thing is that when I tried to enter the function with new line for more readable format,
> (ex, function (doc,req) {
>           blah1
>           blah2
>          })
>  I saw validation error like saying..
> "JSON.parse: bad control character in string literal at line 6 column 23 of the JSON data"...
>
> 1. I don't think I have to enter the function in one line...is there any escaped character I need to enter to format the function?
>
> 2. I know I can query the filter over http rest api, but is there any way I can run the filter function in fulton web interface?
>
> Thanks,
>
>