Xenioo Database Collection Methods

The Connect connection object represent a connection instance to your chatbot database. The following methods are exposed by the connection object to interact with your data.

Connect

Returns a virtual Connect database instance that can be further used to manipulate remote data.

var connection = conversation.Connect();

Save

This method will insert or update a record in the specified collection. The collection must first be created in the Database Collections interface. Objects should be supplied as standard JS objects.

connection.Save( collection, object );

If the object contains an _id field, Connect will attempto to update the record, overwriting it.

The following example will save a new record inside the users collection:

var id = connection.Save( "users", {
    first_name:'Mario',
    last_name:'Rossi',
    email:'mario.rossi@someserver.com',
    enabled:true
} );

/* the id var now contains the unique id of the object */

Update

This method will update a record in the specified collection. The collection must first be created in the Database Collections interface. Objects should be supplied as standard JS objects.

connection.Update( collection, documentid, object );
connection.Update( collection, documentid, object, updatemode );

This method will use all of the fields of the supplied object ot update the target record. If the field does not exist in the target record, it will be created.

Update mode can be one of the following values:

The following example will update a field for a user in the example users collection:

connection.Update( "users", { enabled:false }, "<record id>", "OVW" );

Delete

Deletes one or more records from a collection, using the specified filters.

connection.Delete( collection, filter );

If filter contains a record unique id, only that specific record will be deleted. If filter contains instead a supported URL Filter it will delete all the affected records.

The following example deletes a single record from the example users collection:

var deletedcount = connection.Delete( "users", "<record id>" );

The following example instead will delete all users who are not enabled from the example users collection:

var deletedcount = connection.Delete( "users", "enabled[EQL]false" );

Load

This method will load a single record from a collection

connection.Load( collection, filter );

If filter contains a record unique id, only that specific record will be loaded. If filter contains instead a supported URL Filter the first record that matches the filter will be returned

Query

This method will open a cursor pointing to a list of all the objects that are currently stored in the specified collection that matches the given filter

connection.Query( collection, filter );

Filter can contain a valid URL Filter or be empty. An empty filter will return a cursor pointing to all records in the collection.

This method does not return any actual data. It must be used together with a loop with Read.

Read

This method will read the next available record in the current cursor opened with Query.

var obj = connection.Read();

In the following example, our cloud script will loop on all records inside the users collection that are not enabled:

connection.Query( "users", "enabled[EQL]false" );
var record = connection.Read();
while( record != null ){
    //Do something with the record.
    conversation.Log( JSON.stringify( record ) );    
    //Read the next one
    record = database.Read();
}

Last updated