/
batch.ts
163 lines (142 loc) · 4.55 KB
/
batch.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
import { JSONObject } from "../queryExecutionContext";
import { extractPartitionKey } from "../extractPartitionKey";
import { PartitionKeyDefinition } from "../documents";
import { RequestOptions } from "..";
import { v4 as uuid } from "uuid";
export type Operation =
| CreateOperation
| UpsertOperation
| ReadOperation
| DeleteOperation
| ReplaceOperation;
export interface Batch {
min: string;
max: string;
rangeId: string;
indexes: number[];
operations: Operation[];
}
export interface OperationResponse {
statusCode: number;
requestCharge: number;
eTag?: string;
resourceBody?: JSONObject;
}
export function isKeyInRange(min: string, max: string, key: string) {
const isAfterMinInclusive = key.localeCompare(min) >= 0;
const isBeforeMax = key.localeCompare(max) < 0;
return isAfterMinInclusive && isBeforeMax;
}
export interface OperationBase {
partitionKey?: string;
ifMatch?: string;
ifNoneMatch?: string;
}
export const BulkOperationType = {
Create: "Create",
Upsert: "Upsert",
Read: "Read",
Delete: "Delete",
Replace: "Replace"
} as const;
// TODO Make operationInput CreateOperationInput | ...
export type OperationInput =
| CreateOperationInput
| UpsertOperationInput
| ReadOperationInput
| DeleteOperationInput
| ReplaceOperationInput;
export interface CreateOperationInput {
partitionKey?: string | number | null | {} | undefined;
ifMatch?: string;
ifNoneMatch?: string;
operationType: typeof BulkOperationType.Create;
resourceBody: JSONObject;
}
export interface UpsertOperationInput {
partitionKey?: string | number | null | {} | undefined;
ifMatch?: string;
ifNoneMatch?: string;
operationType: typeof BulkOperationType.Upsert;
resourceBody: JSONObject;
}
export interface ReadOperationInput {
partitionKey?: string | number | null | {} | undefined;
operationType: typeof BulkOperationType.Read;
id: string;
}
export interface DeleteOperationInput {
partitionKey?: string | number | null | {} | undefined;
operationType: typeof BulkOperationType.Delete;
id: string;
}
export interface ReplaceOperationInput {
partitionKey?: string | number | null | {} | undefined;
ifMatch?: string;
ifNoneMatch?: string;
operationType: typeof BulkOperationType.Replace;
resourceBody: JSONObject;
}
export type OperationWithItem = OperationBase & {
resourceBody: JSONObject;
};
export type CreateOperation = OperationWithItem & {
operationType: typeof BulkOperationType.Create;
};
export type UpsertOperation = OperationWithItem & {
operationType: typeof BulkOperationType.Upsert;
};
export type ReadOperation = OperationBase & {
operationType: typeof BulkOperationType.Read;
id: string;
};
export type DeleteOperation = OperationBase & {
operationType: typeof BulkOperationType.Delete;
id: string;
};
export type ReplaceOperation = OperationWithItem & {
operationType: typeof BulkOperationType.Replace;
id: string;
};
export function hasResource(
operation: Operation
): operation is CreateOperation | UpsertOperation | ReplaceOperation {
return (operation as OperationWithItem).resourceBody !== undefined;
}
export function getPartitionKeyToHash(operation: Operation, partitionProperty: string) {
const toHashKey = hasResource(operation)
? (operation.resourceBody as any)[partitionProperty]
: operation.partitionKey.replace(/[\[\]\"\']/g, "");
// We check for empty object since replace will stringify the value
// The second check avoids cases where the partitionKey value is actually the string '{}'
if (toHashKey === "{}" && operation.partitionKey === "[{}]") {
return {};
}
return toHashKey;
}
export function decorateOperation(
operation: OperationInput,
definition: PartitionKeyDefinition,
options: RequestOptions = {}
): Operation {
if (operation.operationType === BulkOperationType.Create) {
if (
(operation.resourceBody.id === undefined || operation.resourceBody.id === "") &&
!options.disableAutomaticIdGeneration
) {
operation.resourceBody.id = uuid();
}
}
if (operation.partitionKey) {
const extracted = extractPartitionKey(operation, { paths: ["/partitionKey"] });
return { ...operation, partitionKey: JSON.stringify(extracted) } as Operation;
} else if (
operation.operationType === BulkOperationType.Create ||
operation.operationType === BulkOperationType.Replace ||
operation.operationType === BulkOperationType.Upsert
) {
const pk = extractPartitionKey(operation.resourceBody, definition);
return { ...operation, partitionKey: JSON.stringify(pk) } as Operation;
}
return operation as Operation;
}