@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
104 lines (103 loc) • 4.24 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var extractPartitionKey_exports = {};
__export(extractPartitionKey_exports, {
extractPartitionKeys: () => extractPartitionKeys,
setPartitionKeyIfUndefined: () => setPartitionKeyIfUndefined,
undefinedPartitionKey: () => undefinedPartitionKey
});
module.exports = __toCommonJS(extractPartitionKey_exports);
var import_logger = require("@azure/logger");
var import_common = require("./common/index.js");
var import_documents = require("./documents/index.js");
var import_partitionKeys = require("./common/partitionKeys.js");
var import_ClientUtils = require("./client/ClientUtils.js");
const logger = (0, import_logger.createClientLogger)("extractPartitionKey");
function extractPartitionKeys(document, partitionKeyDefinition) {
if (!partitionKeyDefinition || !partitionKeyDefinition.paths || partitionKeyDefinition.paths.length <= 0) {
logger.error("Unexpected Partition Key Definition Found.");
return void 0;
}
if (partitionKeyDefinition.paths.length === 1 && partitionKeyDefinition.paths[0] === import_partitionKeys.DEFAULT_PARTITION_KEY_PATH) {
const defaultKey = extractPartitionKey(import_partitionKeys.DEFAULT_PARTITION_KEY_PATH, document);
if (defaultKey === void 0) {
if (partitionKeyDefinition.systemKey === true) {
return [];
}
logger.warning("Unsupported PartitionKey found.");
return void 0;
} else if (defaultKey === import_documents.NullPartitionKeyLiteral || defaultKey === import_documents.NonePartitionKeyLiteral) {
if (partitionKeyDefinition.systemKey === true) {
return [];
}
}
return [defaultKey];
}
if (partitionKeyDefinition.systemKey === true) {
return [];
}
const partitionKeys = [];
partitionKeyDefinition.paths.forEach((path) => {
const obj = extractPartitionKey(path, document);
if (obj === void 0) {
logger.warning("Unsupported PartitionKey found.");
return void 0;
}
partitionKeys.push(obj);
});
return partitionKeys;
}
function extractPartitionKey(path, obj) {
const pathParts = (0, import_common.parsePath)(path);
for (const part of pathParts) {
if (typeof obj === "object" && obj !== null && part in obj) {
obj = obj[part];
} else {
obj = void 0;
break;
}
}
if (typeof obj === "string" || typeof obj === "number" || typeof obj === "boolean") {
return obj;
} else if (obj === import_documents.NullPartitionKeyLiteral) {
return import_documents.NullPartitionKeyLiteral;
} else if (obj === void 0 || JSON.stringify(obj) === JSON.stringify(import_documents.NonePartitionKeyLiteral)) {
return import_documents.NonePartitionKeyLiteral;
}
return void 0;
}
function undefinedPartitionKey(partitionKeyDefinition) {
if (partitionKeyDefinition?.systemKey) {
return [];
} else {
return partitionKeyDefinition?.paths.map(() => import_documents.NonePartitionKeyLiteral);
}
}
async function setPartitionKeyIfUndefined(diagnosticNode, container, partitionKey) {
if (partitionKey === void 0) {
const partitionKeyDefinition = await (0, import_ClientUtils.readPartitionKeyDefinition)(diagnosticNode, container);
partitionKey = undefinedPartitionKey(partitionKeyDefinition);
}
return (0, import_documents.convertToInternalPartitionKey)(partitionKey);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
extractPartitionKeys,
setPartitionKeyIfUndefined,
undefinedPartitionKey
});