HTML5 (Javascript) Client Reference
Class bigDB
Language: Javascript (HTML5)
The BigDB service. This class is used to create, load, and delete database objects. All database objects are stored in tables and have a unique key. You can set up tables in your admin panel, and you can also set up indexes there for when you want to load objects by properties or ranges of properties.
Example
Here's how to store and update an object:
Example
This is how you load an object:
Example
In case you always want to modify an object, you can use the LoadOrCreate method to ensure you get an object back:
Example
BigDB also supports indexes for retrieving objects by a specific property, a range of properties, or to sort objects by properties. Indexes need to be set up in the admin panel for each table, each index needs a name, and a list of properties, and for each property you also need to specify a sort order.
Imagine that we have objects that look like this:
That we have defined an index called "ByUsername" that looks like this:
- {Property:"username", Type:String, Order:Ascending}
And an index called "ByCreated" that looks like this:
- {Property:"created", Type:Datetime, Order:Descending}
Then we can do lookups like these:
Example
BigDB also supports compound indexes, that is indexes with more than one property. Given our example object above, we can create an index called "ByLocationAgeCreated" that looks like this:
- {Property:"location", Type:String, Order:Ascending}
- {Property:"age", Type:Integer, Order:Ascending}
- {Property:"created", Type:Datetime, Order:Descending}
With this index, we can then lookup on either location, or location and age, or location and age and created. If we use more than one property in the lookup, we can only specify the range for the last one, the preceding ones have to be fixed and are sent in via the path parameter.
Example
Finally, deleting objects is as easy as calling the DeleteKeys method, or DeleteRange if you want to delete by an index.
Methods | ||||
---|---|---|---|---|
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
bigDB.createObject
|
Creates a new database object in the given table with the specified key. If no key is specified (null), the object will receive an autogenerated id.
Arguments
table:string | |
The name of the table to create the database object in. | |
key:string | |
The key to assign to the database object. | |
obj:object | |
The database object to create in the table. | |
successCallback:function(databaseobject) | |
Callback function that will be called with the newly created databaseobject instance | |
errorCallback:function(PlayerIOError) | |
Callback function that will be called if an error occurs |
bigDB.deleteKeys
|
Delete a set of database objects from a table.
Arguments
table:string | |
The table to delete the database objects from. | |
keys:string[] | |
The keys of the database objects to delete. | |
successCallback:function() | |
Callback function that will be called when the objects have been successfully deleted | |
errorCallback:function(PlayerIOError) | |
Callback function that will be called if an error occurs |
bigDB.deleteRange
|
Delete a range of database objects from a table using an index.
Arguments
table:string | |
The table to delete the database object from. | |
index:string | |
The name of the index to query for the database objects to delete. | |
indexPath:array | |
Where in the index to start the range delete: An array of objects of the same types as the index properties, specifying where in the index to start loading database objects from. For instance, in the index [Mode,Map,Score] you might use new object[]{"expert","skyland"} as the indexPath and use the start and stop arguments to determine the range of scores you wish to delete. IndexPath can be set to null instead of an empty array. | |
start:object | |
Where to start the range delete. For instance, if the index is [Mode,Map,Score] and indexPath is ["expert","skyland"], then start defines the minimum score to delete. | |
successCallback:function() | |
Callback function that will be called when the objects have been successfully deleted | |
errorCallback:function(PlayerIOError) | |
Callback function that will be called if an error occurs | |
errorCallback: | |
bigDB.load
|
Load the database object with the given key from the given table.
Arguments
table:string | |
The table to load the database object from. | |
key:string | |
The key of the database object to load. | |
successCallback:function(databaseobject) | |
Callback function that will be called with the databaseobject found, or null if no object exists for the given key | |
errorCallback:function(PlayerIOError) | |
Callback function that will be called if an error occurs |
bigDB.loadKeys
|
Loads the database objects with the given keys from the given table.
Arguments
table:string | |
The table to load the database objects from. | |
keys:string[] | |
The keys of the database objects to load. | |
successCallback:function(databaseobjects) | |
Callback function that will be called with an array of databaseobjects found, or an empty array if no objects exist for the given keys | |
errorCallback:function(PlayerIOError) | |
Callback function that will be called if an error occurs |
bigDB.loadKeysOrCreate
|
Loads or creates database objects with the given keys from the given table. New objects are created if there are no existing objects for the given keys.
Arguments
table:string | |
The table from which to load or create the database objects. | |
keys:string[] | |
The keys of the database objects to load or create. | |
successCallback:function(databaseobjects) | |
Callback function that will be called with an array of databaseobjects found or created | |
errorCallback:function(PlayerIOError) | |
Callback function that will be called if an error occurs |
bigDB.loadMyPlayerObject
|
Loads the database object corresponding to the player from the PlayerObjects table.
Arguments
successCallback:function(databaseobject) | |
Callback function that will be called with the databaseobject | |
errorCallback:function(PlayerIOError) | |
Callback function that will be called if an error occurs |
bigDB.loadOrCreate
|
Loads or creates the database object with the given key from the given table.
Arguments
table:string | |
The table from which to load or create the database object | |
key:string | |
The key of the database object to load or create | |
successCallback:function(databaseobject) | |
Callback function that will be called with the databaseobject found or created | |
errorCallback:function(PlayerIOError) | |
Callback function that will be called if an error occurs |
bigDB.loadRange
|
Load a range of database objects from a table using the specified index.
Arguments
table:string | |
The table to load the database objects from. | |
index:string | |
The name of the index to query for the database objects. | |
indexPath:array | |
Where in the index to start the range search: An array of objects of the same types as the index properties, specifying where in the index to start loading database objects from. For instance, in the index [Mode,Map,Score] you might use new object[]{"expert","skyland"} as the indexPath and use the start and stop arguments to determine the range of scores you wish to return. IndexPath can be set to null if there is only one property in the index. | |
start:object | |
Where to start the range search. For instance, if the index is [Mode,Map,Score] and indexPath is ["expert","skyland"], then start defines the minimum score to include in the results. | |
stop:object | |
Where to stop the range search. For instance, if the index is [Mode,Map,Score] and indexPath is ["expert","skyland"], then stop defines the maximum score to include in the results. | |
limit:int | |
The max amount of objects to return. | |
successCallback:function(databaseobjects) | |
Callback function that will be called with an array of databaseobjects found, or an empty array if no objects are found. | |
errorCallback:function(PlayerIOError) | |
Callback function that will be called if an error occurs |
bigDB.loadSingle
|
Load a database object from a table using the specified index.
Arguments
table:string | |
The table to load the database object from. | |
index:string | |
The name of the index to query for the database object. | |
indexValue:array | |
An array of objects of the same types as the index properties, specifying which object to load. | |
successCallback:function(databaseobject) | |
Callback function that will be called with the databaseobject found, or null if no object was found | |
errorCallback:function(PlayerIOError) | |
Callback function that will be called if an error occurs |
bigDB.saveChanges
|
Save changes to one or more database objects in one go.
Arguments
useOptimisticLock:bool | |
Should the save only go through, if no other process has modified the object since it was loaded? | |
fullOverwrite:bool | |
Will completely overwrite the database object in BigDB with the properties in this instance, instead of just sending the changed properties to the server. | |
objects:array | |
The objects with changes to save. | |
successCallback:function() | |
Callback function that will be called when the objects have been successfully saved | |
errorCallback:function(PlayerIOError) | |
Callback function that will be called if an error occurs |