/
StoredProcedure.ts
134 lines (125 loc) · 4.38 KB
/
StoredProcedure.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
125
126
127
128
129
130
131
132
133
134
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { ClientContext } from "../../ClientContext";
import {
createStoredProcedureUri,
getIdFromLink,
getPathFromLink,
isResourceValid,
ResourceType
} from "../../common";
import { undefinedPartitionKey } from "../../extractPartitionKey";
import { RequestOptions, ResourceResponse } from "../../request";
import { Container } from "../Container";
import { StoredProcedureDefinition } from "./StoredProcedureDefinition";
import { StoredProcedureResponse } from "./StoredProcedureResponse";
/**
* Operations for reading, replacing, deleting, or executing a specific, existing stored procedure by id.
*
* For operations to create, upsert, read all, or query Stored Procedures,
*/
export class StoredProcedure {
/**
* Returns a reference URL to the resource. Used for linking in Permissions.
*/
public get url() {
return createStoredProcedureUri(this.container.database.id, this.container.id, this.id);
}
/**
* Creates a new instance of {@link StoredProcedure} linked to the parent {@link Container}.
* @param container The parent {@link Container}.
* @param id The id of the given {@link StoredProcedure}.
* @hidden
*/
constructor(
public readonly container: Container,
public readonly id: string,
private readonly clientContext: ClientContext
) {}
/**
* Read the {@link StoredProcedureDefinition} for the given {@link StoredProcedure}.
* @param options
*/
public async read(options?: RequestOptions): Promise<StoredProcedureResponse> {
const path = getPathFromLink(this.url);
const id = getIdFromLink(this.url);
const response = await this.clientContext.read<StoredProcedureDefinition>({
path,
resourceType: ResourceType.sproc,
resourceId: id,
options
});
return new StoredProcedureResponse(response.result, response.headers, response.code, this);
}
/**
* Replace the given {@link StoredProcedure} with the specified {@link StoredProcedureDefinition}.
* @param body The specified {@link StoredProcedureDefinition} to replace the existing definition.
* @param options
*/
public async replace(
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.url);
const id = getIdFromLink(this.url);
const response = await this.clientContext.replace<StoredProcedureDefinition>({
body,
path,
resourceType: ResourceType.sproc,
resourceId: id,
options
});
return new StoredProcedureResponse(response.result, response.headers, response.code, this);
}
/**
* Delete the given {@link StoredProcedure}.
* @param options
*/
public async delete(options?: RequestOptions): Promise<StoredProcedureResponse> {
const path = getPathFromLink(this.url);
const id = getIdFromLink(this.url);
const response = await this.clientContext.delete<StoredProcedureDefinition>({
path,
resourceType: ResourceType.sproc,
resourceId: id,
options
});
return new StoredProcedureResponse(response.result, response.headers, response.code, this);
}
/**
* Execute the given {@link StoredProcedure}.
*
* The specified type, T, is not enforced by the client.
* Be sure to validate the response from the stored procedure matches the type, T, you provide.
*
* @param partitionKey The partition key to use when executing the stored procedure
* @param params Array of parameters to pass as arguments to the given {@link StoredProcedure}.
* @param options Additional options, such as the partition key to invoke the {@link StoredProcedure} on.
*/
public async execute<T = any>(
partitionKey: any,
params?: any[],
options?: RequestOptions
): Promise<ResourceResponse<T>> {
if (partitionKey === undefined) {
const {
resource: partitionKeyDefinition
} = await this.container.readPartitionKeyDefinition();
partitionKey = undefinedPartitionKey(partitionKeyDefinition);
}
const response = await this.clientContext.execute<T>({
sprocLink: this.url,
params,
options,
partitionKey
});
return new ResourceResponse<T>(response.result, response.headers, response.code);
}
}