/
StoredProcedures.ts
124 lines (116 loc) · 4.38 KB
/
StoredProcedures.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { ClientContext } from "../../ClientContext";
import { getIdFromLink, getPathFromLink, isResourceValid, ResourceType } from "../../common";
import { SqlQuerySpec } from "../../queryExecutionContext";
import { QueryIterator } from "../../queryIterator";
import { FeedOptions, RequestOptions } from "../../request";
import { Container } from "../Container";
import { Resource } from "../Resource";
import { StoredProcedure } from "./StoredProcedure";
import { StoredProcedureDefinition } from "./StoredProcedureDefinition";
import { StoredProcedureResponse } from "./StoredProcedureResponse";
/**
* Operations for creating, upserting, or reading/querying all Stored Procedures.
*
* For operations to read, replace, delete, or execute a specific, existing stored procedure by id, see `container.storedProcedure()`.
*/
export class StoredProcedures {
/**
* @param container The parent {@link Container}.
* @hidden
*/
constructor(
public readonly container: Container,
private readonly clientContext: ClientContext
) {}
/**
* Query all Stored Procedures.
* @param query Query configuration for the operation. See {@link SqlQuerySpec} for more info on how to configure a query.
* @param options
* @example Read all stored procedures to array.
* ```typescript
* const querySpec: SqlQuerySpec = {
* query: "SELECT * FROM root r WHERE r.id = @sproc",
* parameters: [
* {name: "@sproc", value: "Todo"}
* ]
* };
* const {body: sprocList} = await containers.storedProcedures.query(querySpec).fetchAll();
* ```
*/
public query(query: SqlQuerySpec, options?: FeedOptions): QueryIterator<any>;
/**
* Query all Stored Procedures.
* @param query Query configuration for the operation. See {@link SqlQuerySpec} for more info on how to configure a query.
* @param options
* @example Read all stored procedures to array.
* ```typescript
* const querySpec: SqlQuerySpec = {
* query: "SELECT * FROM root r WHERE r.id = @sproc",
* parameters: [
* {name: "@sproc", value: "Todo"}
* ]
* };
* const {body: sprocList} = await containers.storedProcedures.query(querySpec).fetchAll();
* ```
*/
public query<T>(query: SqlQuerySpec, options?: FeedOptions): QueryIterator<T>;
public query<T>(query: SqlQuerySpec, options?: FeedOptions): QueryIterator<T> {
const path = getPathFromLink(this.container.url, ResourceType.sproc);
const id = getIdFromLink(this.container.url);
return new QueryIterator(this.clientContext, query, options, (innerOptions) => {
return this.clientContext.queryFeed({
path,
resourceType: ResourceType.sproc,
resourceId: id,
resultFn: (result) => result.StoredProcedures,
query,
options: innerOptions
});
});
}
/**
* Read all stored procedures.
* @param options
* @example Read all stored procedures to array.
* ```typescript
* const {body: sprocList} = await containers.storedProcedures.readAll().fetchAll();
* ```
*/
public readAll(options?: FeedOptions): QueryIterator<StoredProcedureDefinition & Resource> {
return this.query<StoredProcedureDefinition & Resource>(undefined, options);
}
/**
* Create a StoredProcedure.
*
* Azure Cosmos DB allows stored procedures to be executed in the storage tier,
* directly against an item container. The script
* gets executed under ACID transactions on the primary storage partition of the
* specified container. For additional details,
* refer to the server-side JavaScript API documentation.
*/
public async create(
body: StoredProcedureDefinition,
options?: RequestOptions
): Promise<StoredProcedureResponse> {
if (body.body) {
body.body = body.body.toString();
}
const err = {};
if (!isResourceValid(body, err)) {
throw err;
}
const path = getPathFromLink(this.container.url, ResourceType.sproc);
const id = getIdFromLink(this.container.url);
const response = await this.clientContext.create<StoredProcedureDefinition>({
body,
path,
resourceType: ResourceType.sproc,
resourceId: id,
options
});
const ref = new StoredProcedure(this.container, response.result.id, this.clientContext);
return new StoredProcedureResponse(response.result, response.headers, response.code, ref);
}
}