UNPKG

3.65 kBJavaScriptView Raw
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT license.
3import { extractPartitionKey } from "../extractPartitionKey";
4import { v4 } from "uuid";
5const uuid = v4;
6export function isKeyInRange(min, max, key) {
7 const isAfterMinInclusive = key.localeCompare(min) >= 0;
8 const isBeforeMax = key.localeCompare(max) < 0;
9 return isAfterMinInclusive && isBeforeMax;
10}
11export const BulkOperationType = {
12 Create: "Create",
13 Upsert: "Upsert",
14 Read: "Read",
15 Delete: "Delete",
16 Replace: "Replace",
17 Patch: "Patch",
18};
19export function hasResource(operation) {
20 return (operation.operationType !== "Patch" &&
21 operation.resourceBody !== undefined);
22}
23export function getPartitionKeyToHash(operation, partitionProperty) {
24 const toHashKey = hasResource(operation)
25 ? deepFind(operation.resourceBody, partitionProperty)
26 : (operation.partitionKey && operation.partitionKey.replace(/[[\]"']/g, "")) ||
27 operation.partitionKey;
28 // We check for empty object since replace will stringify the value
29 // The second check avoids cases where the partitionKey value is actually the string '{}'
30 if (toHashKey === "{}" && operation.partitionKey === "[{}]") {
31 return {};
32 }
33 if (toHashKey === "null" && operation.partitionKey === "[null]") {
34 return null;
35 }
36 if (toHashKey === "0" && operation.partitionKey === "[0]") {
37 return 0;
38 }
39 return toHashKey;
40}
41export function decorateOperation(operation, definition, options = {}) {
42 if (operation.operationType === BulkOperationType.Create ||
43 operation.operationType === BulkOperationType.Upsert) {
44 if ((operation.resourceBody.id === undefined || operation.resourceBody.id === "") &&
45 !options.disableAutomaticIdGeneration) {
46 operation.resourceBody.id = uuid();
47 }
48 }
49 if ("partitionKey" in operation) {
50 const extracted = extractPartitionKey(operation, { paths: ["/partitionKey"] });
51 return Object.assign(Object.assign({}, operation), { partitionKey: JSON.stringify(extracted) });
52 }
53 else if (operation.operationType === BulkOperationType.Create ||
54 operation.operationType === BulkOperationType.Replace ||
55 operation.operationType === BulkOperationType.Upsert) {
56 const pk = extractPartitionKey(operation.resourceBody, definition);
57 return Object.assign(Object.assign({}, operation), { partitionKey: JSON.stringify(pk) });
58 }
59 else if (operation.operationType === BulkOperationType.Read ||
60 operation.operationType === BulkOperationType.Delete) {
61 return Object.assign(Object.assign({}, operation), { partitionKey: "[{}]" });
62 }
63 return operation;
64}
65export function decorateBatchOperation(operation, options = {}) {
66 if (operation.operationType === BulkOperationType.Create ||
67 operation.operationType === BulkOperationType.Upsert) {
68 if ((operation.resourceBody.id === undefined || operation.resourceBody.id === "") &&
69 !options.disableAutomaticIdGeneration) {
70 operation.resourceBody.id = uuid();
71 }
72 }
73 return operation;
74}
75/**
76 * Util function for finding partition key values nested in objects at slash (/) separated paths
77 * @hidden
78 */
79export function deepFind(document, path) {
80 const apath = path.split("/");
81 let h = document;
82 for (const p of apath) {
83 if (p in h)
84 h = h[p];
85 else {
86 console.warn(`Partition key not found, using undefined: ${path} at ${p}`);
87 return "{}";
88 }
89 }
90 return h;
91}
92//# sourceMappingURL=batch.js.map
\No newline at end of file