Eksportuj (0) Drukuj
Rozwiń wszystko
EN
Ta zawartość nie jest dostępna w wymaganym języku. Wersja w języku angielskim znajduje się tutaj.

table object

Updated: January 22, 2013

Provides functionality for working with specific tables.

This object contains the following members:

del

Deletes a specified item from the table.

Syntax

Table.del(itemOrId, options)

Parameters

 

Parameter Description

itemID

Either the item a row with the specified ID.

Returns

Undefined.

 

Syntax Returns Description

del

 

undefined

 

insert

Table.insert(item, options)

undefined

Inserts the specified item into the specified tableName.

orderBy

Table.orderBy(arg1, arg2, …)

Query object

Returns a Query object instance where the query is ordered by the supplied column name arguments, in ascending order.

orderByDescending

Table.orderByDescending(arg1, arg2, …)

Query object

Returns a Query object instance where the query is ordered by the supplied column name arguments, in descending order.

read

Table.read(options)

undefined

Reads all data from the table and invokes the success handler specified on the options parameter passing in an array of results.

ImportantImportant
You should not call the read method on tables of unbounded size.

select

Table.select(string)

Query object

Returns a Query object instance with the requested string projection applied.

Table.select(function)

Query object

Returns a Query object instance with the requested function projection applied.

skip

Table.skip(recordCount)

Query object

Returns a Query object instance that skips the first recordCount number of records.

take

Table.take(recordCount)

Query object

Returns a Query object instance that returns the recordCount number of records.

where

Table.where(object)

Query object

Returns a Query object instance that is filtered based on the property values of the supplied JSON object.

Table.where(function)

Query object

Returns a Query object instance that is filtered based on the supplied function.

update

Table.update(item, options)

undefined

Updates the specified item in into the specified tableName.

insert, update, and del methods accept an options object, which can have success or error handlers defined.

Query methods (orderBy, orderByDescending, select, skip, take and where all return a Query object. This object exposes these same methods, which enables you to compose queries as a series of method calls.

The following script calls the where method to filter the returned rows by the supplied object values. When at least one record is returned, it is assumed that the user has the necessary permission to submit an order and the insert is executed; otherwise an error is returned.

function insert(item, user, request) {
    var permissionsTable = tables.getTable('permissions');

    permissionsTable.where({
        userId: user.userId,
        permission: 'submit order'
    }).read({
        success: function(results) {
            if (results.length > 0) {
                // Permission record was found. Continue normal execution.
                request.execute();
            } else {
                console.log('User %s attempted to submit an order without permissions.', user.userId);
                request.respond(statusCodes.FORBIDDEN, 'You do not have permission to submit orders.');
            }
        }
    });
}

The following function, from a scheduled job, executes Transact-SQL that returns duplicate rows and then uses the del method to remove the duplicates.

function cleanup_channels() {
    var sql = "SELECT MAX(Id) as Id, Uri FROM Channel " + 
        "GROUP BY Uri HAVING COUNT(*) > 1";
    var channelTable = tables.getTable('Channel');

    mssql.query(sql, {
        success: function(results) {
            if (results.length > 0) {
                for (var i = 0; i < results.length; i++) {
                    channelTable.del(results[i].Id);
                    console.log('Deleted duplicate channel:' + 
                    results[i].Uri);
                }
            } else {
                console.log('No duplicate rows found.');
            }
        }
    });
}

Pokaż:
© 2014 Microsoft