Create a client connection by creating a WindowsAzure.MobileServiceClient
object. Replace appUrl
with the URL to your Mobile App.
var client = WindowsAzure.MobileServiceClient(appUrl);
To access or update data, create a reference to the backend table. Replace tableName
with the name of your table
var table = client.getTable(tableName);
Once you have a table reference, you can work further with your table:
###How to: Query a Table Reference
Once you have a table reference, you can use it to query for data on the server. Queries are made in a "LINQ-like" language. To return all data from the table, use the following:
/**
* Process the results that are received by a call to table.read()
*
* @param {Object} results the results as a pseudo-array
* @param {int} results.length the length of the results array
* @param {Object} results[] the individual results
*/
function success(results) {
var numItemsRead = results.length;
for (var i = 0 ; i < results.length ; i++) {
var row = results[i];
// Each row is an object - the properties are the columns
}
}
function failure(error) {
throw new Error('Error loading data: ', error);
}
table
.read()
.then(success, failure);
The success function is called with the results. Do not use for (var i in results)
in
the success function as that will iterate over information that is included in the results
when other query functions (such as .includeTotalCount()
) are used.
For more information on the Query syntax, refer to the [Query object documentation].
####Filtering Data on the server
You can use a where
clause on the table reference:
table
.where({ userId: user.userId, complete: false })
.read()
.then(success, failure);
You can also use a function that filters the object. In this case the this
variable is assigned to the
current object being filtered. The following is functionally equivalent to the prior example:
function filterByUserId(currentUserId) {
return this.userId === currentUserId && this.complete === false;
}
table
.where(filterByUserId, user.userId)
.read()
.then(success, failure);
Utilize the take() and skip() methods. For example, if you wish to split the table into 100-row records:
var totalCount = 0, pages = 0;
// Step 1 - get the total number of records
table.includeTotalCount().take(0).read(function (results) {
totalCount = results.totalCount;
pages = Math.floor(totalCount/100) + 1;
loadPage(0);
}, failure);
function loadPage(pageNum) {
let skip = pageNum * 100;
table.skip(skip).take(100).read(function (results) {
for (var i = 0 ; i < results.length ; i++) {
var row = results[i];
// Process each row
}
}
}
The .includeTotalCount()
method is used to add a totalCount field to the results object. The
totalCount field is filled with the total number of records that would be returned if no paging
is used.
You can then use the pages variable and some UI buttons to provide a page list; use loadPage() to load the new records for each page. You should implement some sort of caching to speed access to records that have already been loaded.
####How to: Return data sorted
Use the .orderBy() or .orderByDescending() query methods:
table
.orderBy('name')
.read()
.then(success, failure);
For more information on the Query object, refer to the [Query object documentation].
Create a JavaScript object with the appropriate date and call table.insert() asynchronously:
var newItem = {
name: 'My Name',
signupDate: new Date()
};
table
.insert(newItem)
.done(function (insertedItem) {
var id = insertedItem.id;
}, failure);
On successful insertion, the inserted item is returned with the additional fields that are required for sync operations. You should update your own cache with this information for later updates.
Note that the Azure Mobile Apps Node.js Server SDK supports dynamic schema for development purposes. In the case of dynamic schema, the schema of the table is updated on the fly, allowing you to add columns to the table just by specifying them in an insert or update operation. We recommend that you turn off dynamic schema before moving your application to production.
Similar to the .insert() method, you should create an Update object and then call .update(). The update object must contain the ID of the record to be updated - this is obtained when reading the record or when calling .insert().
var updateItem = {
id: '7163bc7a-70b2-4dde-98e9-8818969611bd',
name: 'My New Name'
};
table
.update(updateItem)
.done(function (updatedItem) {
// You can now update your cached copy
}, failure);
Call the .del() method to delete a record. Pass the ID in an object reference:
table
.del({ id: '7163bc7a-70b2-4dde-98e9-8818969611bd' })
.done(function () {
// Record is now deleted - update your cache
}, failure);