@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
133 lines (132 loc) • 5.02 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 ChangeFeedIterator_exports = {};
__export(ChangeFeedIterator_exports, {
ChangeFeedIterator: () => ChangeFeedIterator
});
module.exports = __toCommonJS(ChangeFeedIterator_exports);
var import_ChangeFeedResponse = require("./ChangeFeedResponse.js");
var import_common = require("./common/index.js");
var import_diagnostics = require("./utils/diagnostics.js");
class ChangeFeedIterator {
/**
* @internal
*/
constructor(clientContext, resourceId, resourceLink, partitionKey, changeFeedOptions) {
this.clientContext = clientContext;
this.resourceId = resourceId;
this.resourceLink = resourceLink;
this.partitionKey = partitionKey;
this.changeFeedOptions = changeFeedOptions;
const partitionKeyValid = partitionKey !== void 0;
this.isPartitionSpecified = partitionKeyValid;
let canUseStartFromBeginning = true;
if (changeFeedOptions.continuation) {
this.nextIfNoneMatch = changeFeedOptions.continuation;
canUseStartFromBeginning = false;
}
if (changeFeedOptions.startTime) {
this.ifModifiedSince = changeFeedOptions.startTime.toUTCString();
canUseStartFromBeginning = false;
}
if (canUseStartFromBeginning && !changeFeedOptions.startFromBeginning) {
this.nextIfNoneMatch = ChangeFeedIterator.IfNoneMatchAllHeaderValue;
}
}
static IfNoneMatchAllHeaderValue = "*";
nextIfNoneMatch;
ifModifiedSince;
lastStatusCode;
isPartitionSpecified;
/**
* Gets a value indicating whether there are potentially additional results that can be retrieved.
*
* Initially returns true. This value is set based on whether the last execution returned a continuation token.
*
* @returns Boolean value representing if whether there are potentially additional results that can be retrieved.
*/
get hasMoreResults() {
return this.lastStatusCode !== import_common.StatusCodes.NotModified;
}
/**
* Gets an async iterator which will yield pages of results from Azure Cosmos DB.
*/
async *getAsyncIterator() {
do {
const result = await this.fetchNext();
if (result.count > 0) {
yield result;
}
} while (this.hasMoreResults);
}
/**
* Read feed and retrieves the next page of results in Azure Cosmos DB.
*/
async fetchNext() {
return (0, import_diagnostics.withDiagnostics)(async (diagnosticNode) => {
const response = await this.getFeedResponse(diagnosticNode);
this.lastStatusCode = response.statusCode;
this.nextIfNoneMatch = response.headers[import_common.Constants.HttpHeaders.ETag];
return response;
}, this.clientContext);
}
async getFeedResponse(diagnosticNode) {
if (!this.isPartitionSpecified) {
throw new Error(
"Container is partitioned, but no partition key or partition key range id was specified."
);
}
const feedOptions = { initialHeaders: {}, useIncrementalFeed: true };
if (typeof this.changeFeedOptions.maxItemCount === "number") {
feedOptions.maxItemCount = this.changeFeedOptions.maxItemCount;
}
if (this.changeFeedOptions.sessionToken) {
feedOptions.sessionToken = this.changeFeedOptions.sessionToken;
}
if (this.nextIfNoneMatch) {
feedOptions.accessCondition = {
type: import_common.Constants.HttpHeaders.IfNoneMatch,
condition: this.nextIfNoneMatch
};
}
if (this.ifModifiedSince) {
feedOptions.initialHeaders[import_common.Constants.HttpHeaders.IfModifiedSince] = this.ifModifiedSince;
}
const response = await this.clientContext.queryFeed({
path: this.resourceLink,
resourceType: import_common.ResourceType.item,
resourceId: this.resourceId,
resultFn: (result) => result ? result.Documents : [],
query: void 0,
options: feedOptions,
partitionKey: this.partitionKey,
diagnosticNode
});
return new import_ChangeFeedResponse.ChangeFeedResponse(
response.result,
response.result ? response.result.length : 0,
response.code,
response.headers,
(0, import_diagnostics.getEmptyCosmosDiagnostics)()
);
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ChangeFeedIterator
});