1 |
|
2 |
|
3 | import { extractPartitionKey } from "../extractPartitionKey";
|
4 | import { v4 } from "uuid";
|
5 | const uuid = v4;
|
6 | export function isKeyInRange(min, max, key) {
|
7 | const isAfterMinInclusive = key.localeCompare(min) >= 0;
|
8 | const isBeforeMax = key.localeCompare(max) < 0;
|
9 | return isAfterMinInclusive && isBeforeMax;
|
10 | }
|
11 | export const BulkOperationType = {
|
12 | Create: "Create",
|
13 | Upsert: "Upsert",
|
14 | Read: "Read",
|
15 | Delete: "Delete",
|
16 | Replace: "Replace",
|
17 | Patch: "Patch",
|
18 | };
|
19 | export function hasResource(operation) {
|
20 | return (operation.operationType !== "Patch" &&
|
21 | operation.resourceBody !== undefined);
|
22 | }
|
23 | export 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 |
|
29 |
|
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 | }
|
41 | export 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 | }
|
65 | export 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 |
|
77 |
|
78 |
|
79 | export 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 |
|
\ | No newline at end of file |