Data persistence services are provided by MongoDB on the server and accessed using the dojo.data interfaces on the client.
uow.data.getDatabase(args) → dojo.Deferred
This method opens a database collection for use in your application. You can call this function any number of times to open connections to different databases and collections and with different modes of access (e.g., read, write, etc.)
The args parameter must have the following attributes configuring your access to the database:
The return value is a dojo.Deferred. You should invoke then on the deferred to register a callback function that will receive a uow.data.MongoStore instance for your use.
Note: The modes granted may not match the modes requested. The intersection depends on the permissions allowed for the user role using uow.data.setAccess or an equivalent function. You can invoke the getMode method on the uow.data.MongoStore instance to determine what permissions were actually granted.
Create operations record the current user's id in the _owner field. Delete and Update operations fail unless the _owner is equal to the current user id or _owner is null or Override has been granted.
Records are checked in two ways on create and update operations. First they are checked against the schema if one is provided. Then, any strings values are filtered to protect against common XSS attacks. If the field name ends with HTML
, the content is filtered against a white list that only allows a subset of tags and attributes. In all other string valued fields, the characters <, >, ", ' and & (outside of entities) are replaced with HTML entities. If you have some very rare case where these transformations will be a problem, you should encode your data before saving it to the db.
uow.data.manageDatabase(args) → dojo.Deferred
This method allows you to list and delete the collections in a database. You can call this function any number of times to manage different databases.
The args parameter must have the following attribute identifying which database you want to manage:
The return value is a dojo.Deferred. You should invoke then on the deferred to register a callback function that will receive a uow.data.MongoStore instance with delete and read permissions for your use in listing and dropping collections in the database.
uow.data.setAccess(args) → dojo.Deferred
This function sets the permissions allowed for a user role when accessing a given database and collection. Only those permissions allowed here will be granted to a user in the named role when accessing the database / collection pair using uow.data.getDatabase.
The args parameter must have the following attributes identifying the database, collection, and user role to configure:
The return value is a dojo.Deferred. You should invoke then on the deferred to register a callback function that will receive the record created, updated, or deleted in the Admin:AccessModes collection defining the allowed permission.
uow.data.setRole(args) → dojo.Deferred
This function assigns a associates a user's Open ID with a role. The association exists across all databases and collections on the server.
The args parameter must have the following attributes identifying the user and role:
The return value is a dojo.Deferred. You should invoke then on the deferred to register a callback function that will receive the record created, updated, or deleted in the Admin:AccessUsers collection defining the role association.
uow.data.setSchema(args) → dojo.Deferred
This function sets the JSON Schema for all future records in a given collection. When a schema exists, only those operations that produce items deemed valid by the schema are allowed. All other operations are denied.
The args parameter must have the following attributes identifying the database, collection, and schema:
The return value is a dojo.Deferred. You should invoke then on the deferred to register a callback function that will receive the record created, updated, or deleted in the Admin:Schemas collection defining the schema.
uow.data.touchCollection(args) → dojo.Deferred
This function creates a new, empty collection in a database. Normally, you do not need to invoke this function as the collection is brought into existence when you write the first record to it. This function is useful, however, if you need a collection to appear when fetched using uow.data.manageDatabase.
The args parameter must have the following attributes identifying the collection to create:
The return value is a dojo.Deferred. You should invoke then on the deferred to register a callback function that will receive a uow.data.MongoStore instance for the new collection with crud permissions.
MongoStore is a subclass of dojox.data.JsonRestStore which implements the read, write, identity, and notification interfaces of dojo.data. MongoStore instances expose additional methods beyond those defined by the basic dojo.data interfaces. These convenience methods are listed below.
deleteOne(args) → dojo.Deferred
This method deletes one and only one record matching a query from the open collection.
The args parameter must have the following attributes identifying the item to delete:
The return value is a dojo.Deferred. You should invoke then on the deferred to register a callback function that will receive the deleted item or an error if zero or more than one items match the query.
fetchOne(args) → dojo.Deferred
This method gets one and only one record matching a query from the open collection.
The args parameter must have the following attributes identifying the item to fetch:
The return value is a dojo.Deferred. You should invoke then on the deferred to register a callback function that will receive the matching item or an error if zero or more than one items match the query.
getMode() → string
This method returns the permissions actually granted on the opened collection. It takes no parameters.
The return value is a string containing a subset of the mode characters crRudO defined in the uow.data.getDatabase documentation above.
putOne(args) → dojo.Deferred
This method updates one and only one record matching a query in the open collection or creates it if no records match.
The args parameter must have the following attributes identifying the item to update or create:
The return value is a dojo.Deferred. You should invoke then on the deferred to register a callback function that will receive the updated or created item or an error if more than one items match the query.
updateOne(args) → dojo.Deferred
This method updates one and only one record matching a query from the open collection.
The args parameter must have the following attributes identifying the item to update:
The return value is a dojo.Deferred. You should invoke then on the deferred to register a callback function that will receive the updated item or an error if zero or more than one items match the query.
var def = uow.data.getDatabase({
database: 'catalog',
collection : 'rolodex',
mode : 'c'
});
def.then(function(db) {
var fn = dojo.byId('db_ex1first');
var ln = dojo.byId('db_ex1last');
db.newItem({firstName : fn.value, lastName : ln.value});
db.save();
fn.value = ln.value = '';
});
var def = uow.data.getDatabase({
database: 'catalog',
collection : 'rolodex',
mode : 'r'
});
def.then(function(db) {
var i = 1;
db.fetch({
count: 5,
onBegin: function() {
dojo.byId('db_ex1log').innerHTML = 'Fetching.\n';
},
onItem: function(item) {
var text = dojo.replace('{i}. {item.lastName}, {item.firstName}\n',
{i: i++, item : item});
dojo.byId('db_ex1log').innerHTML += text;
},
onComplete: function(items) {
dojo.byId('db_ex1log').innerHTML += 'Done.\n';
},
onError: function(e) {
dojo.byId('db_ex1log').innerHTML += 'Kaboom. Bad things happened.\n';
}
});
});