couchdb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Genereux <mgene...@gmail.com>
Subject Idea for View & Bulk Insert combination for ad-hoc database changes
Date Sun, 18 Apr 2010 09:51:27 GMT
Reposting this from the user list.  Probably belongs in the dev list.

I was doing some ad-hoc UPDATEs and DELETEs on a SQL database the
other day and it crossed my mind, how could I do the same on CouchDB?
I don't want to write an application to do something so simple.  It
seems to me that I should be able to produce a Javascript views, both
named and temporary, that in turn produce results that get imported
into the CouchDB Bulk Document API as a single transaction.

Here's the update example for a 10% increase in prices:
function( doc ) {
 doc.price = doc.price * 1.1;
 emit( null, doc )
}

Here's the delete example for old records:
function( doc ) {
 if( doc.year == 2009 ) {
   doc._deleted = true;
   emit( null, doc )
 }
}

Also, much like INSERT ... SELECT notation, this could be used to copy
records.  No need for the non-http compliant COPY method that does the
same on a single record level.  Changes can be made to the copy on the
fly.  Very efficient since the duplication and the update occur in one
transaction.

Example to duplicate all 'foo' widgets to a set of 'bar' widgets:
function( doc ) {
 if( doc.widget_type == 'foo' ) {
   reset( doc );  // helper function that does:
                  // delete( doc._id ); delete( doc._rev );
                  // and any other special vars in future
   doc.widget_type = 'bar'
   emit( null, doc )
 }
}

I like not having to learn another command, so I reused emit even
though bulk api won't use the first field.  If this feature existed,
this is how I would have expected to use it knowing that the key
parameter has no value in this use of a view.

I would love some feedback on this.

Michael

Mime
View raw message