crud

Cozysdk CRUD functions

They are basic functions to manipulate data documents from the Cozy:
Tutorials:

Methods

(static) create(docType, attributes, callbackopt)

Creates a new document for given doc type with fields given in the attributes object.
Parameters:
Name Type Attributes Description
docType string The doctype you want to create.
attributes object The attributes your document should have.
callback callback <optional>
A node.js style callback
Examples

callback

var attributes = {title:"hello", content:"world"}
cozysdk.create('Note', attributes, function(err, obj){
    console.log(obj.id)
});

promise

var attributes = {title:"hello", content:"world"}
cozysdk.create('Note', attributes)
    .then(function(obj){ console.log(obj.id) } );

(static) destroy(docType, id, callbackopt)

Delete a document by its ID.
Parameters:
Name Type Attributes Description
docType string The doctype you want to destroy.
id string The id of the document you want to destroy.
callback callback <optional>
A node.js style callback
Examples

callback

cozysdk.destroy('Note', '732732832832' function(err){
    // note has been destroyed
});

promise

cozysdk.destroy('Note', '732732832832')
.then( function(note){
    // note has been destroyed
})

(static) find(docType, id, callbackopt)

Retrieve a document by its ID.
Parameters:
Name Type Attributes Description
docType string The doctype you want to retrieve.
id string The id of the document you want to retrieve.
callback callback <optional>
A node.js style callback
Examples

callback

cozysdk.find('Note', '732732832832', function(err, note){ note.title });

promise

cozysdk.find('Note', '732732832832').then( function(note){ note.title } );

(static) updateAttributes(docType, id, attrs, callbackopt)

Update attributes of the document that matches given doc type and given ID..
Parameters:
Name Type Attributes Description
docType string The doctype of the document you want to change.
id string The id of the document you want to change.
attrs object The changes you want to make.
callback callback <optional>
A node.js style callback
Examples

callback

cozysdk.find('Note', '732732832832', function(err, note){
    console.log(note) // {title: "hello", content: "world"}
    var changes = {title: "Hola"};
    cozysdk.updateAttributes('Note', '732732832832', changes, function(){
        // note now is {title: "Hola", content: "world"}
    });
});

promise

cozysdk.find('Note', '732732832832')
.then( function(note){
    var changes = {title: "Hola"};
    return cozysdk.updateAttributes('Note', '732732832832', changes)
} )
.then( function(){
    // note now is {title: "Hola", content: "world"}
})