@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
190 lines (189 loc) • 7.22 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 ChangeFeedForPartitionKey_exports = {};
__export(ChangeFeedForPartitionKey_exports, {
ChangeFeedForPartitionKey: () => ChangeFeedForPartitionKey
});
module.exports = __toCommonJS(ChangeFeedForPartitionKey_exports);
var import_ChangeFeedIteratorResponse = require("./ChangeFeedIteratorResponse.js");
var import_common = require("../../common/index.js");
var import_request = require("../../request/index.js");
var import_ContinuationTokenForPartitionKey = require("./ContinuationTokenForPartitionKey.js");
var import_documents = require("../../documents/index.js");
var import_diagnostics = require("../../utils/diagnostics.js");
var import_changeFeedUtils = require("./changeFeedUtils.js");
var import_ClientUtils = require("../ClientUtils.js");
class ChangeFeedForPartitionKey {
/**
* @internal
*/
constructor(clientContext, container, resourceId, resourceLink, partitionKey, changeFeedOptions) {
this.clientContext = clientContext;
this.container = container;
this.resourceId = resourceId;
this.resourceLink = resourceLink;
this.partitionKey = partitionKey;
this.changeFeedOptions = changeFeedOptions;
this.continuationToken = changeFeedOptions.continuationToken ? JSON.parse(changeFeedOptions.continuationToken) : void 0;
this.isInstantiated = false;
if (changeFeedOptions.startFromNow) {
this.startFromNow = true;
} else if (changeFeedOptions.startTime) {
this.startTime = changeFeedOptions.startTime.toUTCString();
}
}
continuationToken;
startTime;
rId;
isInstantiated;
startFromNow;
async instantiateIterator(diagnosticNode) {
await this.setIteratorRid(diagnosticNode);
if (this.clientContext.enableEncryption) {
await this.container.checkAndInitializeEncryption();
this.partitionKey = (0, import_common.copyObject)(this.partitionKey);
diagnosticNode.beginEncryptionDiagnostics(import_common.Constants.Encryption.DiagnosticsEncryptOperation);
const { partitionKeyList, encryptedCount } = await this.container.encryptionProcessor.getEncryptedPartitionKeyValue(
(0, import_documents.convertToInternalPartitionKey)(this.partitionKey)
);
this.partitionKey = partitionKeyList;
diagnosticNode.endEncryptionDiagnostics(
import_common.Constants.Encryption.DiagnosticsEncryptOperation,
encryptedCount
);
}
if (this.continuationToken) {
if (!this.continuationTokenRidMatchContainerRid()) {
throw new import_request.ErrorResponse("The continuation is not for the current container definition.");
}
} else {
this.continuationToken = new import_ContinuationTokenForPartitionKey.ContinuationTokenForPartitionKey(
this.rId,
this.partitionKey,
""
);
}
this.isInstantiated = true;
}
continuationTokenRidMatchContainerRid() {
if (this.continuationToken.rid !== this.rId) {
return false;
}
return true;
}
async setIteratorRid(diagnosticNode) {
const { resource } = await this.container.readInternal(diagnosticNode);
this.rId = resource._rid;
}
/**
* Change feed is an infinite feed. hasMoreResults is always true.
*/
get hasMoreResults() {
return true;
}
/**
* Gets an async iterator which will yield change feed results.
*/
async *getAsyncIterator() {
do {
const result = await this.readNext();
yield result;
} while (this.hasMoreResults);
}
/**
* Returns the result of change feed from Azure Cosmos DB.
*/
async readNext() {
return (0, import_diagnostics.withDiagnostics)(async (diagnosticNode) => {
if (!this.isInstantiated) {
await this.instantiateIterator(diagnosticNode);
}
const result = await this.fetchNext(diagnosticNode);
if (result.statusCode === import_common.StatusCodes.Ok) {
if (this.clientContext.enableEncryption) {
await (0, import_changeFeedUtils.decryptChangeFeedResponse)(
result,
diagnosticNode,
this.changeFeedOptions.changeFeedMode,
this.container.encryptionProcessor
);
}
}
return result;
}, this.clientContext);
}
/**
* Read feed and retrieves the next set of results in Azure Cosmos DB.
*/
async fetchNext(diagnosticNode) {
const response = await this.getFeedResponse(diagnosticNode);
this.continuationToken.Continuation = response.headers[import_common.Constants.HttpHeaders.ETag];
response.headers[import_common.Constants.HttpHeaders.ContinuationToken] = JSON.stringify(
this.continuationToken
);
return response;
}
async getFeedResponse(diagnosticNode) {
const feedOptions = (0, import_changeFeedUtils.buildFeedOptions)(
this.changeFeedOptions,
this.continuationToken?.Continuation,
this.startFromNow,
this.startTime
);
if (this.clientContext.enableEncryption) {
feedOptions.containerRid = this.container._rid;
}
try {
const isPartitionLevelFailOverEnabled = this.clientContext.isPartitionLevelFailOverEnabled();
const partitionKeyRangeId = await (0, import_ClientUtils.computePartitionKeyRangeId)(
diagnosticNode,
(0, import_documents.convertToInternalPartitionKey)(this.partitionKey),
this.clientContext.partitionKeyRangeCache,
isPartitionLevelFailOverEnabled,
this.container
);
const response = await this.clientContext.queryFeed({
path: this.resourceLink,
resourceType: import_common.ResourceType.item,
resourceId: this.resourceId,
resultFn: (result) => result ? result.Documents : [],
diagnosticNode,
query: void 0,
options: feedOptions,
partitionKey: this.partitionKey,
partitionKeyRangeId
});
return new import_ChangeFeedIteratorResponse.ChangeFeedIteratorResponse(
response.result,
response.result ? response.result.length : 0,
response.code,
response.headers,
(0, import_diagnostics.getEmptyCosmosDiagnostics)()
);
} catch (err) {
const errorResponse = new import_request.ErrorResponse(err.message);
errorResponse.code = err.code;
errorResponse.headers = err.headers;
throw errorResponse;
}
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ChangeFeedForPartitionKey
});