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../queryExecutionContext\";\nimport { extractPartitionKey } from \"../extractPartitionKey\";\nimport { PartitionKeyDefinition } from \"../documents\";\nimport { RequestOptions } from \"..\";\nimport { PatchRequestBody } from \"./patch\";\nimport { v4 } from \"uuid\";\nconst uuid = v4;\n\nexport type Operation =\n | CreateOperation\n | UpsertOperation\n | ReadOperation\n | DeleteOperation\n | ReplaceOperation\n | BulkPatchOperation;\n\nexport interface Batch {\n min: string;\n max: string;\n rangeId: string;\n indexes: number[];\n operations: Operation[];\n}\n\nexport interface OperationResponse {\n statusCode: number;\n requestCharge: number;\n eTag?: string;\n resourceBody?: JSONObject;\n}\n\n/**\n * Options object used to modify bulk execution.\n * continueOnError (Default value: false) - Continues bulk execution when an operation fails ** NOTE THIS WILL DEFAULT TO TRUE IN the 4.0 RELEASE\n */\nexport interface BulkOptions {\n continueOnError?: boolean;\n}\n\nexport function isKeyInRange(min: string, max: string, key: string): boolean {\n const isAfterMinInclusive = key.localeCompare(min) >= 0;\n const isBeforeMax = key.localeCompare(max) < 0;\n return isAfterMinInclusive && isBeforeMax;\n}\n\nexport interface OperationBase {\n partitionKey?: string;\n ifMatch?: string;\n ifNoneMatch?: string;\n}\n\nexport const BulkOperationType = {\n Create: \"Create\",\n Upsert: \"Upsert\",\n Read: \"Read\",\n Delete: \"Delete\",\n Replace: \"Replace\",\n Patch: \"Patch\",\n} as const;\n\nexport type OperationInput =\n | CreateOperationInput\n | UpsertOperationInput\n | ReadOperationInput\n | DeleteOperationInput\n | ReplaceOperationInput\n | PatchOperationInput;\n\nexport interface CreateOperationInput {\n partitionKey?: string | number | null | Record<string, unknown> | undefined;\n ifMatch?: string;\n ifNoneMatch?: string;\n operationType: typeof BulkOperationType.Create;\n resourceBody: JSONObject;\n}\n\nexport interface UpsertOperationInput {\n partitionKey?: string | number | null | Record<string, unknown> | undefined;\n ifMatch?: string;\n ifNoneMatch?: string;\n operationType: typeof BulkOperationType.Upsert;\n resourceBody: JSONObject;\n}\n\nexport interface ReadOperationInput {\n partitionKey?: string | number | boolean | null | Record<string, unknown> | undefined;\n operationType: typeof BulkOperationType.Read;\n id: string;\n}\n\nexport interface DeleteOperationInput {\n partitionKey?: string | number | null | Record<string, unknown> | undefined;\n operationType: typeof BulkOperationType.Delete;\n id: string;\n}\n\nexport interface ReplaceOperationInput {\n partitionKey?: string | number | null | Record<string, unknown> | undefined;\n ifMatch?: string;\n ifNoneMatch?: string;\n operationType: typeof BulkOperationType.Replace;\n resourceBody: JSONObject;\n id: string;\n}\n\nexport interface PatchOperationInput {\n partitionKey?: string | number | null | Record<string, unknown> | undefined;\n ifMatch?: string;\n ifNoneMatch?: string;\n operationType: typeof BulkOperationType.Patch;\n resourceBody: PatchRequestBody;\n id: string;\n}\n\nexport type OperationWithItem = OperationBase & {\n resourceBody: JSONObject;\n};\n\nexport type CreateOperation = OperationWithItem & {\n operationType: typeof BulkOperationType.Create;\n};\n\nexport type UpsertOperation = OperationWithItem & {\n operationType: typeof BulkOperationType.Upsert;\n};\n\nexport type ReadOperation = OperationBase & {\n operationType: typeof BulkOperationType.Read;\n id: string;\n};\n\nexport type DeleteOperation = OperationBase & {\n operationType: typeof BulkOperationType.Delete;\n id: string;\n};\n\nexport type ReplaceOperation = OperationWithItem & {\n operationType: typeof BulkOperationType.Replace;\n id: string;\n};\n\nexport type BulkPatchOperation = OperationBase & {\n operationType: typeof BulkOperationType.Patch;\n id: string;\n};\n\nexport function hasResource(\n operation: Operation\n): operation is CreateOperation | UpsertOperation | ReplaceOperation {\n return (\n operation.operationType !== \"Patch\" &&\n (operation as OperationWithItem).resourceBody !== undefined\n );\n}\n\nexport function getPartitionKeyToHash(operation: Operation, partitionProperty: string): any {\n const toHashKey = hasResource(operation)\n ? deepFind(operation.resourceBody, partitionProperty)\n : (operation.partitionKey && operation.partitionKey.replace(/[[\\]\"']/g, \"\")) ||\n operation.partitionKey;\n // We check for empty object since replace will stringify the value\n // The second check avoids cases where the partitionKey value is actually the string '{}'\n if (toHashKey === \"{}\" && operation.partitionKey === \"[{}]\") {\n return {};\n }\n if (toHashKey === \"null\" && operation.partitionKey === \"[null]\") {\n return null;\n }\n if (toHashKey === \"0\" && operation.partitionKey === \"[0]\") {\n return 0;\n }\n return toHashKey;\n}\n\nexport function decorateOperation(\n operation: OperationInput,\n definition: PartitionKeyDefinition,\n options: RequestOptions = {}\n): Operation {\n if (\n operation.operationType === BulkOperationType.Create ||\n operation.operationType === BulkOperationType.Upsert\n ) {\n if (\n (operation.resourceBody.id === undefined || operation.resourceBody.id === \"\") &&\n !options.disableAutomaticIdGeneration\n ) {\n operation.resourceBody.id = uuid();\n }\n }\n if (\"partitionKey\" in operation) {\n const extracted = extractPartitionKey(operation, { paths: [\"/partitionKey\"] });\n return { ...operation, partitionKey: JSON.stringify(extracted) } as Operation;\n } else if (\n operation.operationType === BulkOperationType.Create ||\n operation.operationType === BulkOperationType.Replace ||\n operation.operationType === BulkOperationType.Upsert\n ) {\n const pk = extractPartitionKey(operation.resourceBody, definition);\n return { ...operation, partitionKey: JSON.stringify(pk) } as Operation;\n } else if (\n operation.operationType === BulkOperationType.Read ||\n operation.operationType === BulkOperationType.Delete\n ) {\n return { ...operation, partitionKey: \"[{}]\" };\n }\n return operation as Operation;\n}\n\nexport function decorateBatchOperation(\n operation: OperationInput,\n options: RequestOptions = {}\n): Operation {\n if (\n operation.operationType === BulkOperationType.Create ||\n operation.operationType === BulkOperationType.Upsert\n ) {\n if (\n (operation.resourceBody.id === undefined || operation.resourceBody.id === \"\") &&\n !options.disableAutomaticIdGeneration\n ) {\n operation.resourceBody.id = uuid();\n }\n }\n return operation as Operation;\n}\n/**\n * Util function for finding partition key values nested in objects at slash (/) separated paths\n * @hidden\n */\nexport function deepFind<T, P extends string>(document: T, path: P): string | JSONObject {\n const apath = path.split(\"/\");\n let h: any = document;\n for (const p of apath) {\n if (p in h) h = h[p];\n else {\n console.warn(`Partition key not found, using undefined: ${path} at ${p}`);\n return \"{}\";\n }\n }\n return h;\n}\n"]}
\No newline at end of file