Collection Item

Collection Item management can be done by using the following functions and properties.

Method/Property Description
monaca.cloud.CollectionItem.update() Update the data of a collection item
monaca.cloud.CollectionItem.getPermission() Get permission of a collection item
monaca.cloud.CollectionItem.updatePermission() Set permission of a collection item
monaca.cloud.CollectionItem.remove() Delete a collection item
monaca.cloud.CollectionItem._id Identifier of a collection item
monaca.cloud.CollectionItem._ownerUserOid The owner’s oid of the collection item
monaca.cloud.CollectionItem._createdAt The creation time of the collection item
monaca.cloud.CollectionItem._updatedAt The update time of the collection item

CollectionItem.update()

Send the CollectionItem’s current value to Backend server and save it.

CollectionItem.update() : $.Promise

Parameter

  • None

Return Value

Type Description
$.Promise object Use done(), fail() and always() methods to get results.

Within the done() callback, there is a permission JSON Object. For example: permission: {public: "rw"}.

Errors Code

Errors are returned as Error object.

Code Description
-32602 Invalid params
-14 Permission denied (When the user doesn’t have Write permission)

Example

The following example shows how to update an item of a collection.

var Diary = monaca.cloud.Collection("diary");
var newTitle = 'Updated: It’s snowing again';

Diary.findOne()
.done(function(item)
{
    item.title = newTitle;
    item.update()
    .done(function(result)
    {
        console.log('Updating success');
    })
    .fail(function(err)
    {
        console.log("Err#" + err.code +": " + err.message);
    });
});

CollectionItem.getPermission()

Get permission of the CollectionItem. The permission can be fetched only if the user/device has write-permission.

CollectionItem.getPermission() : $.Promise

Parameter

  • None

Return Value

Type Description
$.Promise object Use done(), fail() and always() methods to get results.

Within the done() callback, there is a permission JSON Object. For example: permission: {public: "rw"}.

Errors Code

Errors are returned as Error object.

Code Description
-14 Permission denied (When the user doesn’t have Write permission)

Example

The following example shows how to get permission of a CollectionItem.

var Diary = monaca.cloud.Collection("diary");

Diary.findOneMine()
.done(function(item)
{
    item.getPermission()
    .done(function(result)
    {
        console.log(result.permission);
    })
    .fail(function(err)
    {
        console.log("Err#" + err.code +": " + err.message);
    });
});

CollectionItem.updatePermission()

Update permissions of the CollectionItem. Permissions can be updated only if the user or device has write-permission.

CollectionItem.updatePermission(permission: Object) : $.Promise

Parameter

Name Type Description
permission JSON Object Permissions to be set. rw is read/write, r is read-only, w is write-only and empty string is no-permission.

Return Value

Type Description
$.Promise object Use done(), fail() and always() methods to get results.

Within the done() callback, there is a JSON Object which has the following properties:

  • numUpdates: the value is always 1.

Errors Code

Errors are returned as Error object.

Code Description
-32602 Invalid params
-14 Permission denied (When the user doesn’t have Write permission)

Example

The following example shows how to set permission of a CollectionItem.

var Diary = monaca.cloud.Collection("diary");

Diary.findOneMine()
.done(function(item)
{
    item.updatePermission({
        public: "r",
        "u00000000-xxxx-xxxx-xxxxxxxx": "rw",
        "u00000000-xxxx-xxxx-xxxxxxxx": ""
    })
    .done(function()
    {
        console.log("Permission updated!");
    })
    .fail(function(err)
    {
        console.log("Err#" + err.code +": " + err.message);
    });
});

And the following example illustrates how to add permission to specific CollectionItem.

var Diary = monaca.cloud.Collection("diary");

var oids = {
            userB: "x00000000-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
            userZ: "x00000000-yyyy-yyyy-yyyy-yyyyyyyyyyyy"
            };
var targetItem = null;

Diary.findOneMine()
.then(function(item)
{
    targetItem = item;
    return targetItem.getPermission();
})
.then(function(result)
{
    result.permission[oids.userB] = "rw";
    result.permission[oids.userZ] = "rw";
    return targetItem.updatePermission(result.permission);
})
.then(function()
{
    console.log("Permission updated!");
});

CollectionItem.remove()

Delete a collection item.

monaca.cloud.CollectionItem.remove() : $.Promise
This function replaces the old function CollectionItem.delete() which is obsolete. The CollectionItem.delete() still can be used, however it is recommended to use new function.

Parameter

  • None

Return Value

Type Description
$.Promise object Use done(), fail() and always() methods to get results.

Errors Code

Errors are returned as Error object.

Code Description
-32602 Invalid params
-14 Permission denied (When the user doesn’t have Write permission)

Example

The following example shows how to set permission of a CollectionItem.

var Diary = monaca.cloud.Collection("diary");

Diary.findOne('title == "I hate him"')
.done(function(item)
{
    item.remove()
    .done(function()
    {
        console.log("Yes indeed I like him");
    });
});

CollectionItem._id

Identifier of a collection item.

monaca.cloud.CollectionItem._id : String (read-only)

Return Value

  • The identifier (String) of the collection item

Example

The following code will display each item’s oid string which are found by the find() function in the console log.

/* find function */
.done(function(result)
{
    result.items.forEach(function(item)
    {
        console.log(item._id);
    });
});

CollectionItem._ownerUserOid

The user’s oid who created the data.

monaca.cloud.CollectionItem._ownerUserOid : String (read-only)

Return Value

  • The oid (String) of the data’s owner

Example

The following code will display each item’s oid of the data’s owner which are found by the find() function in the console log.

/* find function */
.done(function(result)
{
    result.items.forEach(function(item)
    {
        console.log(item._ownerUserOid);
    });
});

CollectionItem._createdAt

The creation time of the item.

_createdAt : Date (read-only)

Return Value

  • The creation date (Date object) of the item

Example

The following code will display each item’s creation time which are found by the find() function in the console log.

/* find function */
.done(function(result)
{
    result.items.forEach(function(item)
    {
        console.log(item._createdAt);
    });
});

CollectionItem._updatedAt

The update time of the item.

monaca.cloud.CollectionItem._updatedAt : Date (read-only)

Return Value

  • The updating date (Date object) of the item

Example

The following code will display each item’s update time which are found by the find() function in the console log.

/* find function */
.done(function(result)
{
    result.items.forEach(function(item)
    {
        console.log(item._updatedAt);
    });
});

See Also: