From 1b37e4ae9b7f74fd81434cb62e4e86f697095c36 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 15 Jun 2015 18:49:48 -0700 Subject: [PATCH] documentdb improvements --- documentdb/documentdb-tests.ts | 61 ++++++++++++++++++++++++ documentdb/documentdb.d.ts | 87 +++++++++++++++++++++++++++++----- 2 files changed, 137 insertions(+), 11 deletions(-) diff --git a/documentdb/documentdb-tests.ts b/documentdb/documentdb-tests.ts index 13927cd93..31f50624c 100644 --- a/documentdb/documentdb-tests.ts +++ b/documentdb/documentdb-tests.ts @@ -13,6 +13,22 @@ docDBClient.createDatabase({ id: 'foo' }, undefined, (error, result) => { } }); +var dbQuerySpec: docDB.SqlQuerySpec = {query: 'SELECT * FROM database d WHERE d.id = @id', parameters: [{name: 'id', value: 'foo'}]} +docDBClient.queryDatabases(dbQuerySpec).toArray((error, result) => { + + if (error) { + throw new Error(error.body); + } + else { + if (result.length < 1) { + throw new Error('Database foo not found'); + } + else { + console.log('Found database: ' + result[0].id); + } + } +}) + docDBClient.createCollection('database', { id: 'foo' }, undefined, (error, result) => { if (error) { @@ -40,6 +56,41 @@ docDBClient.createStoredProcedure('collection', procedure, undefined, (error, re } }); +var trigger: docDB.Trigger = { + id: 'trigger-one', + body: function () { + console.log('bar'); + }, + triggerType: 'pre', + triggerOperation: 'all' +} + +docDBClient.createTrigger('collection', trigger, undefined, (error, result) => { + + if (error) { + throw new Error(error.body); + } + else { + console.log('Created trigger: ' + result.id); + } +}); + +var triggerQuerySpec: docDB.SqlQuerySpec = {query: 'SELECT * FROM trigger t WHERE t.id = @id', parameters: [{name: 'id', value: 'trigger-foo'}]} +docDBClient.queryTriggers('collection', triggerQuerySpec).toArray((error, result) => { + + if (error) { + throw new Error(error.body); + } + else { + if (result.length < 1) { + throw new Error('Trigger trigger-foo not found'); + } + else { + console.log('Found trigger: ' + result[0].id); + } + } +}); + var document: docDB.NewDocument<{ val: string }> = { id: '10' }; @@ -51,6 +102,16 @@ docDBClient.createDocument('collection', document, undefined, (error, result) => } else { console.log('Created document: ' + result.id); + + docDBClient.replaceDocument(result._self, document, undefined, (subError, subResult) => { + + if (subError) { + throw new Error(subError.body); + } + else { + console.log('Replaced document: ' + subResult.id); + } + }) } }); diff --git a/documentdb/documentdb.d.ts b/documentdb/documentdb.d.ts index 581543508..6d064ef31 100644 --- a/documentdb/documentdb.d.ts +++ b/documentdb/documentdb.d.ts @@ -1,6 +1,6 @@ // Type definitions for DocumentDB // Project: https://github.com/Azure/azure-documentdb-node -// Definitions by: Noel Abrahams +// Definitions by: Noel Abrahams , Brett Gutstein // Definitions: https://github.com/borisyankov/DefinitelyTyped/documentdb declare module 'documentdb' { @@ -52,7 +52,25 @@ declare module 'documentdb' { /** Disables the automatic id generation. If id is missing in the body and this option is true, an error will be returned. */ disableAutomaticIdGeneration?: boolean; } - + + /** The Sql query parameter. */ + interface SqlParameter { + /** The name of the parameter. */ + name: string; + + /** The value of the parameter. */ + value: string; + } + + /** The Sql query specification. */ + interface SqlQuerySpec { + /** The body of the query. */ + query: string; + + /** The array of SqlParameters. */ + parameters: SqlParameter[]; + } + /** Represents the error object returned from a failed query. */ interface QueryError { @@ -130,6 +148,13 @@ declare module 'documentdb' { interface ProcedureMeta extends AbstractMeta { body: string; } + + /** Represents the meta data for a trigger. */ + interface TriggerMeta extends AbstractMeta { + body: string; + triggerType: string; + triggerOperation: string; + } /** An object that is used for authenticating requests and must contains one of the options. */ export interface AuthOptions { @@ -150,6 +175,18 @@ declare module 'documentdb' { /** The function representing the stored procedure. */ body(...params: any[]): void; } + + /** Represents a DocumentDB trigger. */ + export interface Trigger extends UniqueId { + /** The type of the trigger. Should be either 'pre' or 'post'. */ + triggerType: string; + + /** The trigger operation. Should be one of 'all', 'create', 'update', 'delete', or 'replace'. */ + triggerOperation: string; + + /** The function representing the trigger. */ + body(...params: any[]): void; + } /** Represents DocumentDB collection. */ export interface Collection extends UniqueId { @@ -195,12 +232,9 @@ declare module 'documentdb' { ExcludedPaths: string[]; } - - /** Provides a client-side logical representation of the Azure DocumentDB database account. This client is used to configure and execute requests against the service. */ export class DocumentClient { - /** * Constructs a DocumentClient. * @param urlConnection - The service endpoint to use to create the client. @@ -250,6 +284,19 @@ declare module 'documentdb' { * @param callback - The callback for the request. */ public createStoredProcedure(collectionLink: string, procedure: Procedure, options: RequestOptions, callback: RequestCallback): void; + + /** + * Create a trigger. + *

+ * DocumentDB supports pre and post triggers defined in JavaScript to be executed on creates, updates and deletes.
+ * For additional details, refer to the server-side JavaScript API documentation. + *

+ * @param collectionLink - The self-link of the collection. + * @param trigger - Represents the body of the trigger. + * @param [options] - The request options. + * @param callback - The callback for the request. + */ + public createTrigger(collectionLink: string, trigger: Trigger, options: RequestOptions, callback: RequestCallback): void; /** * Create a document. @@ -277,7 +324,7 @@ declare module 'documentdb' { * @param [options] - The feed options. * @returns - An instance of QueryIterator to handle reading feed. */ - public queryDatabases(query: string): QueryIterator; + public queryDatabases(query: string | SqlQuerySpec): QueryIterator; /** * Query the collections for the database. @@ -286,7 +333,7 @@ declare module 'documentdb' { * @param [options] - Represents the feed options. * @returns - An instance of queryIterator to handle reading feed. */ - public queryCollections(databaseLink: string, query: string): QueryIterator; + public queryCollections(databaseLink: string, query: string | SqlQuerySpec): QueryIterator; /** * Query the storedProcedures for the collection. @@ -295,7 +342,7 @@ declare module 'documentdb' { * @param [options] - Represents the feed options. * @returns - An instance of queryIterator to handle reading feed. */ - public queryStoredProcedures(collectionLink: string, query: string): QueryIterator; + public queryStoredProcedures(collectionLink: string, query: string | SqlQuerySpec): QueryIterator; /** * Query the documents for the collection. @@ -304,8 +351,17 @@ declare module 'documentdb' { * @param [options] - Represents the feed options. * @returns - An instance of queryIterator to handle reading feed. */ - public queryDocuments(collectionLink: string, query: string, options?: FeedOptions): QueryIterator>; + public queryDocuments(collectionLink: string, query: string | SqlQuerySpec, options?: FeedOptions): QueryIterator>; + /** + * Query the triggers for the collection. + * @param {string} collectionLink - The self-link of the collection. + * @param {SqlQuerySpec | string} query - A SQL query. + * @param {FeedOptions} [options] - Represents the feed options. + * @returns {QueryIterator} - An instance of queryIterator to handle reading feed. + */ + public queryTriggers(collectionLink: string, query: string | SqlQuerySpec, options?: FeedOptions): QueryIterator; + /** * Delete the document object. * @param documentLink - The self-link of the document. @@ -337,7 +393,16 @@ declare module 'documentdb' { * @param callback - The callback for the request. */ public deleteStoredProcedure(procedureLink: string, options: RequestOptions, callback: RequestCallback): void; - + + /** + * Replace the document object. + * @param {string} documentLink - The self-link of the document. + * @param {object} document - Represent the new document body. + * @param {RequestOptions} [options] - The request options. + * @param {RequestCallback} callback - The callback for the request. + */ + public replaceDocument(documentLink: string, document: NewDocument, options: RequestOptions, callback: RequestCallback>): void; + /** * Replace the StoredProcedure object. * @param procedureLink - The self-link of the stored procedure. @@ -347,4 +412,4 @@ declare module 'documentdb' { */ public replaceStoredProcedure(procedureLink: string, procedure: Procedure, options: RequestOptions, callback: RequestCallback): void; } -} \ No newline at end of file +}