From 95bd599bc879a80a389b3f43fd132737a004ce72 Mon Sep 17 00:00:00 2001 From: Alvaro Dias Date: Tue, 24 Jan 2017 20:47:04 -0800 Subject: [PATCH] documentdb: Add typings for user-defined functions --- documentdb/documentdb-tests.ts | 25 +++++++++++++++++++++++++ documentdb/index.d.ts | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/documentdb/documentdb-tests.ts b/documentdb/documentdb-tests.ts index 34d6b5236b..f4a06d3fb7 100644 --- a/documentdb/documentdb-tests.ts +++ b/documentdb/documentdb-tests.ts @@ -55,6 +55,31 @@ docDBClient.createStoredProcedure('collection', procedure, undefined, (error, re } }); +var userDefinedFunction: docDB.UserDefinedFunction = { + id: 'udf1', + body: function () { + console.log('foo'); + } +}; + +docDBClient.createUserDefinedFunction('collection', userDefinedFunction, undefined, (error, result) => { + if (error) { + throw new Error(error.body); + } + else { + console.log('Created function: ' + result.id); + } +}); + +docDBClient.queryUserDefinedFunctions('collection', "SELECT * FROM root", undefined).toArray((error, results) => { + if (error) { + throw new Error(error.body); + } + else { + console.log('Number of functions in collection: ' + results.length); + } +}); + var trigger: docDB.Trigger = { id: 'trigger-one', body: function () { diff --git a/documentdb/index.d.ts b/documentdb/index.d.ts index 406affe2c8..048825b492 100644 --- a/documentdb/index.d.ts +++ b/documentdb/index.d.ts @@ -154,6 +154,10 @@ interface ProcedureMeta extends AbstractMeta { body: string; } +/** Represents the meta data for a user-defined function. */ +interface UserDefinedFunctionMeta extends AbstractMeta { +} + /** Represents the meta data for a trigger. */ interface TriggerMeta extends AbstractMeta { body: string; @@ -181,6 +185,13 @@ export interface Procedure extends UniqueId { body(...params: any[]): void; } +/** Represents a DocumentDB user-defined function. */ +export interface UserDefinedFunction extends UniqueId { + + /** The function representing the user-defined function. */ + body(...params: any[]): void; +} + /** Represents a DocumentDB trigger. */ export interface Trigger extends UniqueId { /** The type of the trigger. Should be either 'pre' or 'post'. */ @@ -290,6 +301,19 @@ export declare class DocumentClient { */ public createStoredProcedure(collectionLink: string, procedure: Procedure, options: RequestOptions, callback: RequestCallback): void; + /** + * Create a UserDefinedFunction. + *

+ * DocumentDB supports JavaScript UDFs which can be used inside queries, stored procedures and triggers.
+ * For additional details, refer to the server-side JavaScript API documentation. + *

+ * @param collectionLink - The self-link of the collection. + * @param udf - Represents the body of the userDefinedFunction. + * @param [options] - The request options. + * @param callback - The callback for the request. + */ + public createUserDefinedFunction(collectionLink: string, udf: UserDefinedFunction, options: RequestOptions, callback: RequestCallback): void; + /** * Create a trigger. *

@@ -349,6 +373,15 @@ export declare class DocumentClient { */ public queryStoredProcedures(collectionLink: string, query: string | SqlQuerySpec): QueryIterator; + /** + * Query the user-defined functions for the collection. + * @param collectionLink - The self-link of the collection. + * @param query - A SQL query string. + * @param [options] - Represents the feed options. + * @returns - An instance of queryIterator to handle reading feed. + */ + public queryUserDefinedFunctions(collectionLink: string, query: string | SqlQuerySpec, options?: FeedOptions): QueryIterator; + /** * Query the documents for the collection. * @param collectionLink - The self-link of the collection.