/
Container.ts
250 lines (230 loc) · 8.88 KB
/
Container.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { ClientContext } from "../../ClientContext";
import {
createDocumentCollectionUri,
getIdFromLink,
getPathFromLink,
isResourceValid,
ResourceType
} from "../../common";
import { PartitionKeyDefinition } from "../../documents";
import { SqlQuerySpec } from "../../queryExecutionContext";
import { QueryIterator } from "../../queryIterator";
import { FeedOptions, RequestOptions, ResourceResponse, Response } from "../../request";
import { PartitionedQueryExecutionInfo } from "../../request/ErrorResponse";
import { Conflict, Conflicts } from "../Conflict";
import { Database } from "../Database";
import { Item, Items } from "../Item";
import { Scripts } from "../Script/Scripts";
import { ContainerDefinition } from "./ContainerDefinition";
import { ContainerResponse } from "./ContainerResponse";
import { PartitionKeyRange } from "./PartitionKeyRange";
import { Offer, OfferDefinition } from "../Offer";
import { OfferResponse } from "../Offer/OfferResponse";
import { Resource } from "../Resource";
/**
* Operations for reading, replacing, or deleting a specific, existing container by id.
*
* @see {@link Containers} for creating new containers, and reading/querying all containers; use `.containers`.
*
* Note: all these operations make calls against a fixed budget.
* You should design your system such that these calls scale sublinearly with your application.
* For instance, do not call `container(id).read()` before every single `item.read()` call, to ensure the container exists;
* do this once on application start up.
*/
export class Container {
private $items: Items;
/**
* Operations for creating new items, and reading/querying all items
*
* For reading, replacing, or deleting an existing item, use `.item(id)`.
*
* @example Create a new item
* ```typescript
* const {body: createdItem} = await container.items.create({id: "<item id>", properties: {}});
* ```
*/
public get items(): Items {
if (!this.$items) {
this.$items = new Items(this, this.clientContext);
}
return this.$items;
}
private $scripts: Scripts;
/**
* All operations for Stored Procedures, Triggers, and User Defined Functions
*/
public get scripts(): Scripts {
if (!this.$scripts) {
this.$scripts = new Scripts(this, this.clientContext);
}
return this.$scripts;
}
private $conflicts: Conflicts;
/**
* Opertaions for reading and querying conflicts for the given container.
*
* For reading or deleting a specific conflict, use `.conflict(id)`.
*/
public get conflicts(): Conflicts {
if (!this.$conflicts) {
this.$conflicts = new Conflicts(this, this.clientContext);
}
return this.$conflicts;
}
/**
* Returns a reference URL to the resource. Used for linking in Permissions.
*/
public get url() {
return createDocumentCollectionUri(this.database.id, this.id);
}
/**
* Returns a container instance. Note: You should get this from `database.container(id)`, rather than creating your own object.
* @param database The parent {@link Database}.
* @param id The id of the given container.
* @hidden
*/
constructor(
public readonly database: Database,
public readonly id: string,
private readonly clientContext: ClientContext
) {}
/**
* Used to read, replace, or delete a specific, existing {@link Item} by id.
*
* Use `.items` for creating new items, or querying/reading all items.
*
* @param id The id of the {@link Item}.
* @param partitionKeyValue The value of the {@link Item} partition key
* @example Replace an item
* const {body: replacedItem} = await container.item("<item id>", "<partition key value>").replace({id: "<item id>", title: "Updated post", authorID: 5});
*/
public item(id: string, partitionKeyValue?: any): Item {
return new Item(this, id, partitionKeyValue, this.clientContext);
}
/**
* Used to read, replace, or delete a specific, existing {@link Conflict} by id.
*
* Use `.conflicts` for creating new conflicts, or querying/reading all conflicts.
* @param id The id of the {@link Conflict}.
*/
public conflict(id: string): Conflict {
return new Conflict(this, id, this.clientContext);
}
/** Read the container's definition */
public async read(options?: RequestOptions): Promise<ContainerResponse> {
const path = getPathFromLink(this.url);
const id = getIdFromLink(this.url);
const response = await this.clientContext.read<ContainerDefinition>({
path,
resourceType: ResourceType.container,
resourceId: id,
options
});
this.clientContext.partitionKeyDefinitionCache[this.url] = response.result.partitionKey;
return new ContainerResponse(response.result, response.headers, response.code, this);
}
/** Replace the container's definition */
public async replace(
body: ContainerDefinition,
options?: RequestOptions
): Promise<ContainerResponse> {
const err = {};
if (!isResourceValid(body, err)) {
throw err;
}
const path = getPathFromLink(this.url);
const id = getIdFromLink(this.url);
const response = await this.clientContext.replace<ContainerDefinition>({
body,
path,
resourceType: ResourceType.container,
resourceId: id,
options
});
return new ContainerResponse(response.result, response.headers, response.code, this);
}
/** Delete the container */
public async delete(options?: RequestOptions): Promise<ContainerResponse> {
const path = getPathFromLink(this.url);
const id = getIdFromLink(this.url);
const response = await this.clientContext.delete<ContainerDefinition>({
path,
resourceType: ResourceType.container,
resourceId: id,
options
});
return new ContainerResponse(response.result, response.headers, response.code, this);
}
/**
* Gets the partition key definition first by looking into the cache otherwise by reading the collection.
* @deprecated This method has been renamed to readPartitionKeyDefinition.
* @param {string} collectionLink - Link to the collection whose partition key needs to be extracted.
* @param {function} callback - \
* The arguments to the callback are(in order): error, partitionKeyDefinition, response object and response headers
*/
public async getPartitionKeyDefinition(): Promise<ResourceResponse<PartitionKeyDefinition>> {
return this.readPartitionKeyDefinition();
}
/**
* Gets the partition key definition first by looking into the cache otherwise by reading the collection.
* @ignore
* @param {string} collectionLink - Link to the collection whose partition key needs to be extracted.
* @param {function} callback - \
* The arguments to the callback are(in order): error, partitionKeyDefinition, response object and response headers
*/
public async readPartitionKeyDefinition(): Promise<ResourceResponse<PartitionKeyDefinition>> {
// $ISSUE-felixfan-2016-03-17: Make name based path and link based path use the same key
// $ISSUE-felixfan-2016-03-17: Refresh partitionKeyDefinitionCache when necessary
if (this.url in this.clientContext.partitionKeyDefinitionCache) {
return new ResourceResponse<PartitionKeyDefinition>(
this.clientContext.partitionKeyDefinitionCache[this.url],
{},
0
);
}
const { headers, statusCode } = await this.read();
return new ResourceResponse<PartitionKeyDefinition>(
this.clientContext.partitionKeyDefinitionCache[this.url],
headers,
statusCode
);
}
/**
* Gets offer on container. If none exists, returns an OfferResponse with undefined.
* @param options
*/
public async readOffer(options: RequestOptions = {}): Promise<OfferResponse> {
const { resource: container } = await this.read();
const path = "/offers";
const url = container._self;
const response = await this.clientContext.queryFeed<OfferDefinition & Resource[]>({
path,
resourceId: "",
resourceType: ResourceType.offer,
query: `SELECT * from root where root.resource = "${url}"`,
resultFn: (result) => result.Offers,
options
});
const offer = response.result[0]
? new Offer(this.database.client, response.result[0].id, this.clientContext)
: undefined;
return new OfferResponse(response.result[0], response.headers, response.code, offer);
}
public async getQueryPlan(
query: string | SqlQuerySpec
): Promise<Response<PartitionedQueryExecutionInfo>> {
const path = getPathFromLink(this.url);
return this.clientContext.getQueryPlan(
path + "/docs",
ResourceType.item,
getIdFromLink(this.url),
query
);
}
public readPartitionKeyRanges(feedOptions?: FeedOptions): QueryIterator<PartitionKeyRange> {
feedOptions = feedOptions || {};
return this.clientContext.queryPartitionKeyRanges(this.url, undefined, feedOptions);
}
}