1 |
|
2 |
|
3 | import { parsePath } from "./common";
|
4 |
|
5 |
|
6 |
|
7 | export function extractPartitionKey(document, partitionKeyDefinition) {
|
8 | if (partitionKeyDefinition &&
|
9 | partitionKeyDefinition.paths &&
|
10 | partitionKeyDefinition.paths.length > 0) {
|
11 | const partitionKey = [];
|
12 | partitionKeyDefinition.paths.forEach((path) => {
|
13 | const pathParts = parsePath(path);
|
14 | let obj = document;
|
15 | for (const part of pathParts) {
|
16 | if (typeof obj === "object" && part in obj) {
|
17 | obj = obj[part];
|
18 | }
|
19 | else {
|
20 | obj = undefined;
|
21 | break;
|
22 | }
|
23 | }
|
24 | partitionKey.push(obj);
|
25 | });
|
26 | if (partitionKey.length === 1 && partitionKey[0] === undefined) {
|
27 | return undefinedPartitionKey(partitionKeyDefinition);
|
28 | }
|
29 | return partitionKey;
|
30 | }
|
31 | }
|
32 |
|
33 |
|
34 |
|
35 | export function undefinedPartitionKey(partitionKeyDefinition) {
|
36 | if (partitionKeyDefinition.systemKey === true) {
|
37 | return [];
|
38 | }
|
39 | else {
|
40 | return [{}];
|
41 | }
|
42 | }
|
43 |
|
\ | No newline at end of file |