mapreduce

cozysdk Map Reduce functions

Queries in cozy are made using couchdb map/reduce view system. Read more in the tutorial.
Tutorials:

Methods

(static) defineView(docType, name, request, callbackopt)

Define a map/reduce request for a given doc type.
Parameters:
Name Type Attributes Description
docType string The doctype you want to create a view on.
name string The name of the view to create.
request string | function | object The request to define. it can either be a function, a string (function.toString()) or an object with map & reduce attributes.
callback callback <optional>
A node.js style callback
Examples

callback

byTitle = function(doc) { emit(doc.title); }
cozysdk.defineView('Note', 'all', byTitle, function(err){
    // view has been created
});

promise

byTitle = function(doc) { emit(doc.title); }
cozysdk.defineView('Note', 'all', byTitle)

(static) destroyByView(docType, name, params, callbackopt)

Destroy every documents that would have been returned by a call to queryView with the same parameters. Destroy all DocumentsQuery a map/reduce view. It accepts CouchDB like params.
Parameters:
Name Type Attributes Description
docType string The doctype you want to query a view on.
name string The name of the view to query.
params object The same query parameters than queryView.
Properties
Name Type Description
limit object Warning The limit param is ignored for deletion.
callback callback <optional>
A node.js style callback
Examples

callback

params = {startkey 'A', endkey: 'B'}
cozysdk.destroyByView('Note', 'byTitle', params, function(err){
    // destroy all notes with a title starting by A
});

promise

params = {startkey 'A', endkey: 'B'}
cozysdk.destroyByView('Note', 'byTitle', params)

(static) queryView(docType, name, params, callbackopt)

Query a map/reduce view. It accepts CouchDB like params.
Parameters:
Name Type Attributes Description
docType string The doctype you want to query a view on.
name string The name of the view to query.
params object The query parameters.
Properties
Name Type Attributes Default Description
key mixed <optional>
Get all entries with this key
keys Array.<mixed> <optional>
Get all entries with one of these keys
startkey mixed <optional>
Get all entries with key greater than this value
startkey_docid string <optional>
Document id to start with (to allow pagination for duplicate startkeys)
endkey mixed <optional>
Get all entries with key lesser than this value
endkey_docid string <optional>
Last document id to include in the output (to allow pagination for duplicate endkeys)
limit number <optional>
Infinity Limit the number of documents in the output
skip number <optional>
0 Skip n number of documents
descending boolean <optional>
false Change the direction of search
group boolean <optional>
false The group option controls whether the reduce function reduces to a set of distinct keys or to a single result row.
group_level number <optional>
See below
reduce boolean <optional>
true Use the reduce function of the view. It defaults to true, if a reduce function is defined and to false otherwise.
include_docs boolean <optional>
false Automatically fetch and include the document which emitted each view entry
inclusive_end boolean <optional>
true Controls whether the endkey is included in the result. It defaults to true.
update_seq= boolean <optional>
Response includes an update_seq value indicating which sequence id of the database the view reflects
callback callback <optional>
A node.js style callback
Examples

callback

params = {startkey 'A', endkey: 'B'}
cozysdk.queryView('Note', 'byTitle', params, function(err){
    // get all notes with a title starting by A
});

promise

params = {startkey 'A', endkey: 'B'}
cozysdk.queryView('Note', 'byTitle', params)