Index

DocumentDB server side JavaScript SDK

DocumentDB’s language integrated, transactional execution of JavaScript supports stored procedures, triggers and user defined functions (UDFs) written natively in JavaScript. This allows developers to write application logic which can be shipped and executed directly on the database storage partitions. JavaScript support at the server side has a number of intrinsic advantages that can be utilized to build rich applications.

Getting Started

Create a DocumentDB account using the Microsoft Azure Portal and setup databases/collections using REST API or one of the supported client SDKs (.NET, Node.js, Python). The JavaScript server-side SDK documented here has no downloads - it is available on every document collection created on DocumentDB. Stored procedures referencing the server side SDK can be written in plain text and registered against the DocumentDB service again either using the REST API or one of the supported clients.

  • Stored procedures can be executed directly with arguments passed in
  • Triggers can be included subsequently as pre or post operations in create, replace and delete document operations
  • User defined functions do not have access to the context object and are meant to be used as compute-only JavaScript.

To learn more, here is the DocumentDB JavaScript Tutorial.

Context Object

The Context object provides access the current collection against which database operations can be performed, as well as to the Request and Response to the JavaScript stored procedure and/or trigger.


    function () {
        /* the context method can be accessed inside stored procedures and triggers*/
        var context = getContext();
        /* access all database operations - CRUD, query against documents in the current collection */
        var collection = context.getCollection();
        /* access HTTP request body and headers for the procedure */
        var request = context.getRequest();
        /* access HTTP response body and headers from the procedure */
        var response = context.getResponse();
    } 

Request Object

Inside the trigger, the Request object can be used to manipulate the request message associated with the operation.

Note: this object not available in stored procedures

Response Object

The Response object can be similarly used to modify the response from a stored procedure or trigger.

Note: this object not available in pre-triggers

Collection Object

The Collection object supports create, read, update and delete (CRUD) against documents in the current collection.

References