ravendb
Version:
RavenDB client for Node.js
97 lines • 4.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StreamOperation = void 0;
const QueryStreamCommand_js_1 = require("../../Commands/QueryStreamCommand.js");
const index_js_1 = require("../../../Exceptions/index.js");
const StreamCommand_js_1 = require("../../Commands/StreamCommand.js");
const TypeUtil_js_1 = require("../../../Utility/TypeUtil.js");
const Pipelines_js_1 = require("../../../Mapping/Json/Streams/Pipelines.js");
const StringBuilder_js_1 = require("../../../Utility/StringBuilder.js");
const ObjectUtil_js_1 = require("../../../Utility/ObjectUtil.js");
const RavenCommandResponsePipeline_js_1 = require("../../../Http/RavenCommandResponsePipeline.js");
class StreamOperation {
_session;
_isQueryStream;
constructor(session) {
this._session = session;
}
createRequest(idPrefixOrQuery, opts) {
if (TypeUtil_js_1.TypeUtil.isString(idPrefixOrQuery)) {
return this._createRequestForIdPrefix(idPrefixOrQuery, opts);
}
return this._createRequestForQuery(idPrefixOrQuery);
}
_createRequestForQuery(query) {
if (!query) {
(0, index_js_1.throwError)("InvalidArgumentException", "Query cannot be null.");
}
this._isQueryStream = true;
if (query.waitForNonStaleResults) {
(0, index_js_1.throwError)("NotSupportedException", "Since stream() does not wait for indexing (by design), "
+ " streaming query with waitForNonStaleResults is not supported");
}
this._session.incrementRequestCount();
return new QueryStreamCommand_js_1.QueryStreamCommand(this._session.conventions, query);
}
_createRequestForIdPrefix(idPrefix, opts) {
const sb = new StringBuilder_js_1.StringBuilder(`streams/docs?format=jsonl&`);
if (idPrefix) {
sb.append("startsWith=")
.append(encodeURIComponent(idPrefix)).append("&");
}
if (opts) {
if ("matches" in opts) {
sb.append("matches=")
.append(encodeURIComponent(opts.matches)).append("&");
}
if ("exclude" in opts) {
sb.append("exclude=")
.append(encodeURIComponent(opts.exclude)).append("&");
}
if ("startAfter" in opts) {
sb.append("startAfter=")
.append(encodeURIComponent(opts.startAfter)).append("&");
}
if ("start" in opts) {
sb.append("start=").append(opts.start).append("&");
}
if ("pageSize" in opts && opts.pageSize !== Number.MAX_VALUE) {
sb.append("pageSize=").append(opts.pageSize).append("&");
}
}
return new StreamCommand_js_1.StreamCommand(sb.toString());
}
setResult(response) {
if (!response) {
(0, index_js_1.throwError)("IndexDoesNotExistException", "The index does not exists, failed to stream results.");
}
const result = (0, Pipelines_js_1.getDocumentResultsAsObjects)(this._session.conventions, !!this._isQueryStream)
.stream(response.stream);
if (this._isQueryStream) {
const pipeline = RavenCommandResponsePipeline_js_1.RavenCommandResponsePipeline.create();
pipeline.parseJsonlAsync(x => x["Stats"]);
pipeline.stream(response.stream)
.on("error", err => result.emit("error", err))
.on("data", data => {
const rawWithCamel = ObjectUtil_js_1.ObjectUtil.transformObjectKeys(data["value"], {
defaultTransform: ObjectUtil_js_1.ObjectUtil.camel
});
const statsResult = this._session.conventions.objectMapper
.fromObjectLiteral(rawWithCamel, {
nestedTypes: {
indexTimestamp: "date"
}
});
result.emit("stats", statsResult);
});
}
result.on("newListener", (event, listener) => {
if (event === "data") {
response.stream.resume();
}
});
return result;
}
}
exports.StreamOperation = StreamOperation;
//# sourceMappingURL=StreamOperation.js.map