@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
226 lines (225 loc) • 8.7 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 batch_exports = {};
__export(batch_exports, {
BulkOperationType: () => BulkOperationType,
TaskCompletionSource: () => TaskCompletionSource,
calculateObjectSizeInBytes: () => calculateObjectSizeInBytes,
decorateBatchOperation: () => decorateBatchOperation,
encryptOperationInput: () => encryptOperationInput,
hasResource: () => hasResource,
isKeyInRange: () => isKeyInRange,
isSuccessStatusCode: () => isSuccessStatusCode,
prepareOperations: () => prepareOperations,
splitBatchBasedOnBodySize: () => splitBatchBasedOnBodySize
});
module.exports = __toCommonJS(batch_exports);
var import_extractPartitionKey = require("../extractPartitionKey.js");
var import_documents = require("../documents/index.js");
var import_typeChecks = require("./typeChecks.js");
var import_request = require("../request/request.js");
var import_constants = require("../common/constants.js");
var import_core_util = require("@azure/core-util");
function isKeyInRange(min, max, key) {
const isAfterMinInclusive = key.localeCompare(min) >= 0;
const isBeforeMax = key.localeCompare(max) < 0;
return isAfterMinInclusive && isBeforeMax;
}
const BulkOperationType = {
Create: "Create",
Upsert: "Upsert",
Read: "Read",
Delete: "Delete",
Replace: "Replace",
Patch: "Patch"
};
function hasResource(operation) {
return operation.operationType !== "Patch" && operation.resourceBody !== void 0;
}
function prepareOperations(operationInput, definition, options = {}) {
populateIdsIfNeeded(operationInput, options);
let partitionKey;
if (Object.prototype.hasOwnProperty.call(operationInput, "partitionKey")) {
if (operationInput.partitionKey === void 0) {
partitionKey = definition.paths.map(() => import_documents.NonePartitionKeyLiteral);
} else {
partitionKey = (0, import_documents.convertToInternalPartitionKey)(operationInput.partitionKey);
}
} else {
switch (operationInput.operationType) {
case BulkOperationType.Create:
case BulkOperationType.Replace:
case BulkOperationType.Upsert:
partitionKey = (0, import_typeChecks.assertNotUndefined)(
(0, import_extractPartitionKey.extractPartitionKeys)(operationInput.resourceBody, definition),
"Unexpected undefined Partition Key Found."
);
break;
case BulkOperationType.Read:
case BulkOperationType.Delete:
case BulkOperationType.Patch:
partitionKey = (0, import_extractPartitionKey.undefinedPartitionKey)(definition);
break;
}
}
return {
operation: { ...operationInput, partitionKey: JSON.stringify(partitionKey) },
partitionKey
};
}
function populateIdsIfNeeded(operationInput, options) {
if (operationInput.operationType === BulkOperationType.Create || operationInput.operationType === BulkOperationType.Upsert) {
if ((operationInput.resourceBody.id === void 0 || operationInput.resourceBody.id === "") && !options.disableAutomaticIdGeneration) {
operationInput.resourceBody.id = (0, import_core_util.randomUUID)();
}
}
}
function splitBatchBasedOnBodySize(originalBatch) {
if (originalBatch?.operations === void 0 || originalBatch.operations.length < 1) return [];
let currentBatchSize = calculateObjectSizeInBytes(originalBatch.operations[0]);
let currentBatch = {
...originalBatch,
operations: [originalBatch.operations[0]],
indexes: [originalBatch.indexes[0]]
};
const processedBatches = [];
processedBatches.push(currentBatch);
for (let index = 1; index < originalBatch.operations.length; index++) {
const operation = originalBatch.operations[index];
const currentOpSize = calculateObjectSizeInBytes(operation);
if (currentBatchSize + currentOpSize > import_constants.Constants.DefaultMaxBulkRequestBodySizeInBytes) {
currentBatch = {
...originalBatch,
operations: [],
indexes: []
};
processedBatches.push(currentBatch);
currentBatchSize = 0;
}
currentBatch.operations.push(operation);
currentBatch.indexes.push(originalBatch.indexes[index]);
currentBatchSize += currentOpSize;
}
return processedBatches;
}
function calculateObjectSizeInBytes(obj) {
return new TextEncoder().encode((0, import_request.bodyFromData)(obj)).length;
}
function decorateBatchOperation(operation, options = {}) {
if (operation.operationType === BulkOperationType.Create || operation.operationType === BulkOperationType.Upsert) {
if ((operation.resourceBody.id === void 0 || operation.resourceBody.id === "") && !options.disableAutomaticIdGeneration) {
operation.resourceBody.id = (0, import_core_util.randomUUID)();
}
}
return operation;
}
function isSuccessStatusCode(statusCode) {
return statusCode >= 200 && statusCode <= 299;
}
class TaskCompletionSource {
promise;
resolveFn;
rejectFn;
constructor() {
this.promise = new Promise((resolve, reject) => {
this.resolveFn = resolve;
this.rejectFn = reject;
});
}
get task() {
return this.promise;
}
setResult(value) {
this.resolveFn(value);
}
setException(error) {
this.rejectFn(error);
}
}
async function encryptOperationInput(encryptionProcessor, operation, totalPropertiesEncryptedCount) {
if (Object.prototype.hasOwnProperty.call(operation, "partitionKey")) {
const partitionKeyInternal = (0, import_documents.convertToInternalPartitionKey)(operation.partitionKey);
const { partitionKeyList, encryptedCount } = await encryptionProcessor.getEncryptedPartitionKeyValue(partitionKeyInternal);
operation.partitionKey = partitionKeyList;
totalPropertiesEncryptedCount += encryptedCount;
}
switch (operation.operationType) {
case BulkOperationType.Create:
case BulkOperationType.Upsert: {
const { body, propertiesEncryptedCount } = await encryptionProcessor.encrypt(
operation.resourceBody
);
operation.resourceBody = body;
totalPropertiesEncryptedCount += propertiesEncryptedCount;
break;
}
case BulkOperationType.Read:
case BulkOperationType.Delete:
if (await encryptionProcessor.isPathEncrypted("/id")) {
operation.id = await encryptionProcessor.getEncryptedId(operation.id);
totalPropertiesEncryptedCount++;
}
break;
case BulkOperationType.Replace: {
if (await encryptionProcessor.isPathEncrypted("/id")) {
operation.id = await encryptionProcessor.getEncryptedId(operation.id);
totalPropertiesEncryptedCount++;
}
const { body, propertiesEncryptedCount } = await encryptionProcessor.encrypt(
operation.resourceBody
);
operation.resourceBody = body;
totalPropertiesEncryptedCount += propertiesEncryptedCount;
break;
}
case BulkOperationType.Patch: {
if (await encryptionProcessor.isPathEncrypted("/id")) {
operation.id = await encryptionProcessor.getEncryptedId(operation.id);
totalPropertiesEncryptedCount++;
}
const body = operation.resourceBody;
const patchRequestBody = Array.isArray(body) ? body : body.operations;
for (const patchOperation of patchRequestBody) {
if ("value" in patchOperation) {
if (await encryptionProcessor.isPathEncrypted(patchOperation.path)) {
patchOperation.value = await encryptionProcessor.encryptProperty(
patchOperation.path,
patchOperation.value
);
totalPropertiesEncryptedCount++;
}
}
}
break;
}
}
return { operation, totalPropertiesEncryptedCount };
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
BulkOperationType,
TaskCompletionSource,
calculateObjectSizeInBytes,
decorateBatchOperation,
encryptOperationInput,
hasResource,
isKeyInRange,
isSuccessStatusCode,
prepareOperations,
splitBatchBasedOnBodySize
});