/
Item.ts
208 lines (197 loc) · 5.98 KB
/
Item.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { ClientContext } from "../../ClientContext";
import {
createDocumentUri,
getIdFromLink,
getPathFromLink,
isResourceValid,
ResourceType,
StatusCodes,
} from "../../common";
import { PartitionKey } from "../../documents";
import { extractPartitionKey, undefinedPartitionKey } from "../../extractPartitionKey";
import { RequestOptions, Response } from "../../request";
import { Container } from "../Container";
import { Resource } from "../Resource";
import { ItemDefinition } from "./ItemDefinition";
import { ItemResponse } from "./ItemResponse";
/**
* Used to perform operations on a specific item.
*
* @see {@link Items} for operations on all items; see `container.items`.
*/
export class Item {
private partitionKey: PartitionKey;
/**
* Returns a reference URL to the resource. Used for linking in Permissions.
*/
public get url() {
return createDocumentUri(this.container.database.id, this.container.id, this.id);
}
/**
* @hidden
* @param container The parent {@link Container}.
* @param id The id of the given {@link Item}.
* @param partitionKey The primary key of the given {@link Item} (only for partitioned containers).
*/
constructor(
public readonly container: Container,
public readonly id: string,
partitionKey: PartitionKey,
private readonly clientContext: ClientContext
) {
this.partitionKey = partitionKey;
}
/**
* Read the item's definition.
*
* Any provided type, T, is not necessarily enforced by the SDK.
* You may get more or less properties and it's up to your logic to enforce it.
* If the type, T, is a class, it won't pass `typeof` comparisons, because it won't have a match prototype.
* It's recommended to only use interfaces.
*
* There is no set schema for JSON items. They may contain any number of custom properties.
*
* @param options Additional options for the request
*
* @example Using custom type for response
* ```typescript
* interface TodoItem {
* title: string;
* done: bool;
* id: string;
* }
*
* let item: TodoItem;
* ({body: item} = await item.read<TodoItem>());
* ```
*/
public async read<T extends ItemDefinition = any>(
options: RequestOptions = {}
): Promise<ItemResponse<T>> {
if (this.partitionKey === undefined) {
const {
resource: partitionKeyDefinition,
} = await this.container.readPartitionKeyDefinition();
this.partitionKey = undefinedPartitionKey(partitionKeyDefinition);
}
const path = getPathFromLink(this.url);
const id = getIdFromLink(this.url);
let response: Response<T & Resource>;
try {
response = await this.clientContext.read<T>({
path,
resourceType: ResourceType.item,
resourceId: id,
options,
partitionKey: this.partitionKey,
});
} catch (error) {
if (error.code !== StatusCodes.NotFound) {
throw error;
}
response = error;
}
return new ItemResponse(
response.result,
response.headers,
response.code,
response.substatus,
this
);
}
/**
* Replace the item's definition.
*
* There is no set schema for JSON items. They may contain any number of custom properties.
*
* @param body The definition to replace the existing {@link Item}'s definition with.
* @param options Additional options for the request
*/
public replace(
body: ItemDefinition,
options?: RequestOptions
): Promise<ItemResponse<ItemDefinition>>;
/**
* Replace the item's definition.
*
* Any provided type, T, is not necessarily enforced by the SDK.
* You may get more or less properties and it's up to your logic to enforce it.
*
* There is no set schema for JSON items. They may contain any number of custom properties.
*
* @param body The definition to replace the existing {@link Item}'s definition with.
* @param options Additional options for the request
*/
public replace<T extends ItemDefinition>(
body: T,
options?: RequestOptions
): Promise<ItemResponse<T>>;
public async replace<T extends ItemDefinition>(
body: T,
options: RequestOptions = {}
): Promise<ItemResponse<T>> {
if (this.partitionKey === undefined) {
const {
resource: partitionKeyDefinition,
} = await this.container.readPartitionKeyDefinition();
this.partitionKey = extractPartitionKey(body, partitionKeyDefinition);
}
const err = {};
if (!isResourceValid(body, err)) {
throw err;
}
const path = getPathFromLink(this.url);
const id = getIdFromLink(this.url);
const response = await this.clientContext.replace<T>({
body,
path,
resourceType: ResourceType.item,
resourceId: id,
options,
partitionKey: this.partitionKey,
});
return new ItemResponse(
response.result,
response.headers,
response.code,
response.substatus,
this
);
}
/**
* Delete the item.
*
* Any provided type, T, is not necessarily enforced by the SDK.
* You may get more or less properties and it's up to your logic to enforce it.
*
* @param options Additional options for the request
*/
public async delete<T extends ItemDefinition = any>(
options: RequestOptions = {}
): Promise<ItemResponse<T>> {
if (this.partitionKey === undefined) {
const {
resource: partitionKeyDefinition,
} = await this.container.readPartitionKeyDefinition();
this.partitionKey = undefinedPartitionKey(partitionKeyDefinition);
}
const path = getPathFromLink(this.url);
const id = getIdFromLink(this.url);
const response = await this.clientContext.delete<T>({
path,
resourceType: ResourceType.item,
resourceId: id,
options,
partitionKey: this.partitionKey,
});
return new ItemResponse(
response.result,
response.headers,
response.code,
response.substatus,
this
);
}
}