You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Conor Mac Aoidh <co...@gmail.com> on 2014/09/26 10:57:16 UTC

Post Insert Validation

Hi All,

I was previously using the validate_doc_update function for document 
validation. However, now I need to be able to change the contents of a 
document if it is invalid.

I was wondering what the best way to go about this is? Is there any 
design function that executes after a document insert and that will 
allow me to edit the document contents. I understand this will result in 
an additional revision.

Thanks

Conor

Re: Post Insert Validation

Posted by Alexander Shorin <kx...@gmail.com>.
There isn't such. Only on document pre-saving also known as update function.
--
,,,^..^,,,


On Fri, Sep 26, 2014 at 12:57 PM, Conor Mac Aoidh
<co...@gmail.com> wrote:
> Hi All,
>
> I was previously using the validate_doc_update function for document
> validation. However, now I need to be able to change the contents of a
> document if it is invalid.
>
> I was wondering what the best way to go about this is? Is there any design
> function that executes after a document insert and that will allow me to
> edit the document contents. I understand this will result in an additional
> revision.
>
> Thanks
>
> Conor

Re: Post Insert Validation

Posted by Conor Mac Aoidh <co...@gmail.com>.
Hi Mike,

Oh I may have misunderstood you. So, you mean use a design function to 
filter the changes feed so that only validated documents are emitted? 
That's a good idea will give it a go.

Thanks

Conor

On 26/09/14 11:54, Conor Mac Aoidh wrote:
> Hi Mike,
>
> The issue with that method is that I have other process listening for 
> changes also. I don't want these processes to pick up documents that 
> aren't validated. So, I'll need some kind of identifier to tell 
> whether they have passed validation yet.
>
> Conor
>
> On 26/09/14 11:27, Mike Marino wrote:
>>>
>>> That's likely the solution I'll go for. I'll insert documents with a
>>> 'validation' field set to 0. Then have a process watching for 
>>> changes and
>>> get it to validate the documents, mark them as validated.
>>>
>> You probably don't even need to explicitly set another field. Just write
>> your view to essentially "validate" the documents and emit them if 
>> they're
>> not valid.  You can filter your changes feed based on these results and
>> then call an update function on those documents that are invalid from 
>> the
>> daemon.  This keeps as much as possible within couch, limits what the
>> daemon needs to do and keeps you from having to change your app code.
>>
>> Cheers,
>> Mike
>>
>


Re: Post Insert Validation

Posted by Conor Mac Aoidh <co...@gmail.com>.
Hi Mike,

The issue with that method is that I have other process listening for 
changes also. I don't want these processes to pick up documents that 
aren't validated. So, I'll need some kind of identifier to tell whether 
they have passed validation yet.

Conor

On 26/09/14 11:27, Mike Marino wrote:
>>
>> That's likely the solution I'll go for. I'll insert documents with a
>> 'validation' field set to 0. Then have a process watching for changes and
>> get it to validate the documents, mark them as validated.
>>
> You probably don't even need to explicitly set another field.  Just write
> your view to essentially "validate" the documents and emit them if they're
> not valid.  You can filter your changes feed based on these results and
> then call an update function on those documents that are invalid from the
> daemon.  This keeps as much as possible within couch, limits what the
> daemon needs to do and keeps you from having to change your app code.
>
> Cheers,
> Mike
>


Re: Post Insert Validation

Posted by Mike Marino <mm...@gmail.com>.
>
>
>>
> That's likely the solution I'll go for. I'll insert documents with a
> 'validation' field set to 0. Then have a process watching for changes and
> get it to validate the documents, mark them as validated.
>

You probably don't even need to explicitly set another field.  Just write
your view to essentially "validate" the documents and emit them if they're
not valid.  You can filter your changes feed based on these results and
then call an update function on those documents that are invalid from the
daemon.  This keeps as much as possible within couch, limits what the
daemon needs to do and keeps you from having to change your app code.

Cheers,
Mike

Re: Post Insert Validation

Posted by Conor Mac Aoidh <co...@gmail.com>.
Hi Mike,

On 26/09/14 10:26, Mike Marino wrote:
> Ok, that's too bad. This generally however may be implemented so that 
> it only changes the path where you POST, nothing else (i.e. POST to 
> db/design/_update/update_name instead of POST to db). 

This is not an option for me as I have multiple applications on multiple 
devices posting to the server so it wouldn't be ideal to have them 
change how they do things.

>> Just wondering, is there a particular reason that you can't update
>> documents in the validate_doc_update function? Or is there a reason that
>> there is no design document with this functionality?
> Some of the developers may chime in here, but I believe the main reason is
> to keep the validation as a clean yes or no.  Also, forcing an insertion to
> use an update function means you know that the inserted document may change.
>
> Do you need to do this as soon as a document comes in, or could you also
> allow for some small amount of buffer time?  In principle, the update
> function is your only option if you want essentially an atomic operation,
> but if you can allow for some time between changes you may be able to do
> the following:
>
> - write a view function to give you "incorrect" documents
> - write a daemon to listen for these inserts via the changes feed.  This
> daemon could be run automatically by couch at startup (e.g. via os_daemon
> configuration).
> - Handle the updates to the documents with this daemon.

That's likely the solution I'll go for. I'll insert documents with a 
'validation' field set to 0. Then have a process watching for changes 
and get it to validate the documents, mark them as validated.

It's just I'd hoped to keep this within couchdb, but this solution 
should work.

Thanks for the help!

Conor




>> Thanks
>>
>> Conor
>>
>>
>> On 26/09/14 10:02, Mike Marino wrote:
>>
>>> Hi Conor,
>>>
>>> On Fri, Sep 26, 2014 at 10:57 AM, Conor Mac Aoidh <
>>> conormacaoidh@gmail.com>
>>> wrote:
>>>
>>>   Hi All,
>>>> I was previously using the validate_doc_update function for document
>>>> validation. However, now I need to be able to change the contents of a
>>>> document if it is invalid.
>>>>
>>>>   validate_doc_update only gives you a yes or no as you point out.
>>>
>>>   I was wondering what the best way to go about this is? Is there any
>>>> design
>>>> function that executes after a document insert and that will allow me to
>>>> edit the document contents. I understand this will result in an
>>>> additional
>>>> revision.
>>>>
>>>>   You can change the contents of a document (either in place or newly
>>> submitted) using a update function.  Your update function could check to
>>> make sure something is correct and modify if necessary.  The only caveat
>>> is
>>> that you must always explicitly POST or PUT to the update function, so
>>> this
>>> would require a change in your user app.  I think, however, this is likely
>>> the best solution for you.  (see
>>> http://docs.couchdb.org/en/latest/couchapp/ddocs.html#update-functions)
>>>
>>> Cheers,
>>> Mike
>>>
>>>


Re: Post Insert Validation

Posted by Mike Marino <mm...@gmail.com>.
Hi Conor,

On Fri, Sep 26, 2014 at 11:10 AM, Conor Mac Aoidh <co...@gmail.com>
wrote:

> Hi Mike,
>
> Thanks. I'd looked into that option but unfortunately I need to use the
> standard database insertion method.
>

Ok, that's too bad.  This generally however may be implemented so that it
only changes the path where you POST, nothing else (i.e. POST to
db/design/_update/update_name instead of POST to db).


> Just wondering, is there a particular reason that you can't update
> documents in the validate_doc_update function? Or is there a reason that
> there is no design document with this functionality?
>

Some of the developers may chime in here, but I believe the main reason is
to keep the validation as a clean yes or no.  Also, forcing an insertion to
use an update function means you know that the inserted document may change.

Do you need to do this as soon as a document comes in, or could you also
allow for some small amount of buffer time?  In principle, the update
function is your only option if you want essentially an atomic operation,
but if you can allow for some time between changes you may be able to do
the following:

- write a view function to give you "incorrect" documents
- write a daemon to listen for these inserts via the changes feed.  This
daemon could be run automatically by couch at startup (e.g. via os_daemon
configuration).
- Handle the updates to the documents with this daemon.

Cheers,
Mike


> Thanks
>
> Conor
>
>
> On 26/09/14 10:02, Mike Marino wrote:
>
>> Hi Conor,
>>
>> On Fri, Sep 26, 2014 at 10:57 AM, Conor Mac Aoidh <
>> conormacaoidh@gmail.com>
>> wrote:
>>
>>  Hi All,
>>>
>>> I was previously using the validate_doc_update function for document
>>> validation. However, now I need to be able to change the contents of a
>>> document if it is invalid.
>>>
>>>  validate_doc_update only gives you a yes or no as you point out.
>>
>>
>>  I was wondering what the best way to go about this is? Is there any
>>> design
>>> function that executes after a document insert and that will allow me to
>>> edit the document contents. I understand this will result in an
>>> additional
>>> revision.
>>>
>>>  You can change the contents of a document (either in place or newly
>> submitted) using a update function.  Your update function could check to
>> make sure something is correct and modify if necessary.  The only caveat
>> is
>> that you must always explicitly POST or PUT to the update function, so
>> this
>> would require a change in your user app.  I think, however, this is likely
>> the best solution for you.  (see
>> http://docs.couchdb.org/en/latest/couchapp/ddocs.html#update-functions)
>>
>> Cheers,
>> Mike
>>
>>
>

Re: Post Insert Validation

Posted by Conor Mac Aoidh <co...@gmail.com>.
Hi Mike,

Thanks. I'd looked into that option but unfortunately I need to use the 
standard database insertion method.

Just wondering, is there a particular reason that you can't update 
documents in the validate_doc_update function? Or is there a reason that 
there is no design document with this functionality?

Thanks

Conor

On 26/09/14 10:02, Mike Marino wrote:
> Hi Conor,
>
> On Fri, Sep 26, 2014 at 10:57 AM, Conor Mac Aoidh <co...@gmail.com>
> wrote:
>
>> Hi All,
>>
>> I was previously using the validate_doc_update function for document
>> validation. However, now I need to be able to change the contents of a
>> document if it is invalid.
>>
> validate_doc_update only gives you a yes or no as you point out.
>
>
>> I was wondering what the best way to go about this is? Is there any design
>> function that executes after a document insert and that will allow me to
>> edit the document contents. I understand this will result in an additional
>> revision.
>>
> You can change the contents of a document (either in place or newly
> submitted) using a update function.  Your update function could check to
> make sure something is correct and modify if necessary.  The only caveat is
> that you must always explicitly POST or PUT to the update function, so this
> would require a change in your user app.  I think, however, this is likely
> the best solution for you.  (see
> http://docs.couchdb.org/en/latest/couchapp/ddocs.html#update-functions)
>
> Cheers,
> Mike
>


Re: Post Insert Validation

Posted by Mike Marino <mm...@gmail.com>.
Hi Conor,

On Fri, Sep 26, 2014 at 10:57 AM, Conor Mac Aoidh <co...@gmail.com>
wrote:

> Hi All,
>
> I was previously using the validate_doc_update function for document
> validation. However, now I need to be able to change the contents of a
> document if it is invalid.
>

validate_doc_update only gives you a yes or no as you point out.


> I was wondering what the best way to go about this is? Is there any design
> function that executes after a document insert and that will allow me to
> edit the document contents. I understand this will result in an additional
> revision.
>

You can change the contents of a document (either in place or newly
submitted) using a update function.  Your update function could check to
make sure something is correct and modify if necessary.  The only caveat is
that you must always explicitly POST or PUT to the update function, so this
would require a change in your user app.  I think, however, this is likely
the best solution for you.  (see
http://docs.couchdb.org/en/latest/couchapp/ddocs.html#update-functions)

Cheers,
Mike