Xenioo Database Collection Methods

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

To avoid type mismatch errors ensure that the parameters you're supplying to the following methods contain the expected type.

Connect

Returns a virtual Xenioo 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:string, object:any );

If the object contains an _id field, Xenioo 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:string, documentid:string, object:any );
connection.Update( collection:string, documentid:string, object:any, updatemode:string );

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:

Value

Description

OVW

Overwrites the contents of field with the new value

ADD

Adds a value to the current field value

SUB

Subtracts a value from the current field value

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:string, filter:string );

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:string, filter:string );

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:string, filter:string );

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();
}

Count

This method will return the number of records returned by the active query.

connection.Count( );

Sum

This method will return the value of the sum of all the values contained in each record for the specified field.

connection.Sum( "Price" );

UniqueValues

This method will return an array of strings containing the unique values of all rows for the specified field.

connection.UniqueValues( "Category" );

Last updated