@azure/cosmos
Version:
Microsoft Azure Cosmos DB Service Node.js SDK for NOSQL API
115 lines (114 loc) • 5.47 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 OrderByQueryContinuationToken_exports = {};
__export(OrderByQueryContinuationToken_exports, {
createOrderByQueryContinuationToken: () => createOrderByQueryContinuationToken,
parseOrderByQueryContinuationToken: () => parseOrderByQueryContinuationToken,
serializeOrderByQueryContinuationToken: () => serializeOrderByQueryContinuationToken
});
module.exports = __toCommonJS(OrderByQueryContinuationToken_exports);
var import_CompositeQueryContinuationToken = require("./CompositeQueryContinuationToken.js");
var import_ErrorResponse = require("../../request/ErrorResponse.js");
function createOrderByQueryContinuationToken(rangeMappings, orderByItems, rid, skipCount, documentRid, offset, limit, hashedLastResult) {
if (!rangeMappings || rangeMappings.length === 0) {
throw new Error("rangeMappings must contain at least one element");
}
if (!orderByItems || orderByItems.length === 0) {
throw new Error("orderByItems must contain at least one element");
}
return {
rangeMappings,
orderByItems,
rid,
skipCount,
documentRid,
offset,
limit,
hashedLastResult
};
}
function serializeOrderByQueryContinuationToken(token) {
return JSON.stringify(token);
}
function parseOrderByQueryContinuationToken(tokenString) {
const token = (0, import_CompositeQueryContinuationToken.parseBaseContinuationToken)(tokenString);
if (!Array.isArray(token.orderByItems) || token.orderByItems.length === 0) {
throw new import_ErrorResponse.ErrorResponse(
"Invalid continuation token. ORDER BY tokens require non-empty orderByItems."
);
}
if (token.orderByItems.some((item) => !item || typeof item !== "object" || Array.isArray(item))) {
throw new import_ErrorResponse.ErrorResponse("Invalid continuation token. ORDER BY items must be objects.");
}
if (typeof token.skipCount !== "number" || !Number.isSafeInteger(token.skipCount) || token.skipCount < 0) {
throw new import_ErrorResponse.ErrorResponse(
"Invalid continuation token. ORDER BY tokens require a non-negative safe integer skipCount."
);
}
if (token.documentRid !== void 0 && typeof token.documentRid !== "string") {
throw new import_ErrorResponse.ErrorResponse(
"Invalid continuation token. ORDER BY documentRid must be a string when present."
);
}
for (const [index, mapping] of token.rangeMappings.entries()) {
const filter = mapping.orderByFilter;
if (filter === void 0) {
continue;
}
if (!filter || typeof filter !== "object" || Array.isArray(filter)) {
throw new import_ErrorResponse.ErrorResponse(
`Invalid continuation token. ORDER BY rangeMappings[${index}].orderByFilter must be an object.`
);
}
if (!Array.isArray(filter.orderByItems) || filter.orderByItems.length === 0 || filter.orderByItems.some((item) => !item || typeof item !== "object" || Array.isArray(item))) {
throw new import_ErrorResponse.ErrorResponse(
`Invalid continuation token. ORDER BY rangeMappings[${index}].orderByFilter requires non-empty object-valued orderByItems.`
);
}
if (typeof filter.skipCount !== "number" || !Number.isSafeInteger(filter.skipCount) || filter.skipCount < 0) {
throw new import_ErrorResponse.ErrorResponse(
`Invalid continuation token. ORDER BY rangeMappings[${index}].orderByFilter requires a non-negative safe integer skipCount.`
);
}
if (typeof filter.rid !== "string") {
throw new import_ErrorResponse.ErrorResponse(
`Invalid continuation token. ORDER BY rangeMappings[${index}].orderByFilter requires a string rid.`
);
}
}
const logicallyOrderedMappings = [...token.rangeMappings].sort(
(left, right) => left.queryRange.min.localeCompare(right.queryRange.min)
);
const checkpointIndexes = logicallyOrderedMappings.map((mapping, index) => mapping.isOrderByCheckpoint === true ? index : -1).filter((index) => index >= 0);
const hasCheckpointMetadata = logicallyOrderedMappings.some(
(mapping) => mapping.isOrderByCheckpoint !== void 0
);
if (hasCheckpointMetadata && (checkpointIndexes.length === 0 || checkpointIndexes[checkpointIndexes.length - 1] - checkpointIndexes[0] + 1 !== checkpointIndexes.length)) {
throw new import_ErrorResponse.ErrorResponse(
"Invalid continuation token. ORDER BY checkpoint owners must form one contiguous logical interval."
);
}
return token;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createOrderByQueryContinuationToken,
parseOrderByQueryContinuationToken,
serializeOrderByQueryContinuationToken
});
//# sourceMappingURL=OrderByQueryContinuationToken.js.map