@cognigy/rest-api-client
Version:
Cognigy REST-Client
159 lines • 5.35 kB
JavaScript
import { __awaiter } from "tslib";
/* Custom modules */
import { createNodeDescriptor } from "../../../createNodeDescriptor";
import { MONGO_DB_CONNECTION } from "./mongoDBConnection";
import { InvalidArgumentError } from "../../../../errors";
export const MONGO_AGGREGATE = createNodeDescriptor({
type: "mongoAggregate",
defaultLabel: 'MongoDB Aggregate',
summary: "UI__NODE_EDITOR__MONGO_AGGREGATE__SUMMARY",
fields: [
{
key: "connection",
type: "connection",
label: "UI__NODE_EDITOR__MONGO_AGGREGATE__CONNECTION__LABEL",
params: {
required: true,
connectionType: MONGO_DB_CONNECTION.type
}
},
{
key: "collection",
type: "cognigyText",
label: "UI__NODE_EDITOR__MONGO_AGGREGATE__COLLECTION__LABEL",
params: {
required: true
}
},
{
key: "query",
type: "json",
label: "UI__NODE_EDITOR__MONGO_AGGREGATE__QUERY__LABEL",
defaultValue: '[{}]'
},
{
key: "options",
type: "json",
label: "UI__NODE_EDITOR__MONGO_AGGREGATE__OPTIONS__LABEL",
defaultValue: '{}'
},
{
key: "storeLocation",
type: "select",
label: "UI__NODE_EDITOR__MONGO_AGGREGATE__STORE_LOCATION__LABEL",
params: {
options: [
{
label: "UI__NODE_EDITOR__MONGO_AGGREGATE__STORE_LOCATION__INPUT__LABEL",
value: "input"
},
{
label: "UI__NODE_EDITOR__MONGO_AGGREGATE__STORE_LOCATION__CONTEXT__LABEL",
value: "context"
}
],
required: true
},
defaultValue: "input"
},
{
key: "inputKey",
type: "cognigyText",
label: "UI__NODE_EDITOR__MONGO_AGGREGATE__INPUT_KEY__LABEL",
defaultValue: "mongodb",
condition: {
key: "storeLocation",
value: "input",
}
},
{
key: "contextKey",
type: "cognigyText",
label: "UI__NODE_EDITOR__MONGO_AGGREGATE__CONTEXT_KEY__LABEL",
defaultValue: "mongodb",
condition: {
key: "storeLocation",
value: "context",
}
},
{
key: "stopOnError",
type: "toggle",
label: "UI__NODE_EDITOR__MONGO_AGGREGATE__STOP_ON_ERROR__LABEL",
defaultValue: false
}
],
sections: [
{
key: "storage",
label: "UI__NODE_EDITOR__MONGO_AGGREGATE__STORAGE__LABEL",
defaultCollapsed: true,
fields: [
"storeLocation",
"inputKey",
"contextKey",
]
},
{
key: "advanced",
label: "UI__NODE_EDITOR__MONGO_AGGREGATE__ADVANCED__LABEL",
defaultCollapsed: true,
fields: [
"stopOnError"
]
}
],
form: [
{ type: "field", key: "connection" },
{ type: "field", key: "collection" },
{ type: "field", key: "query" },
{ type: "field", key: "options" },
{ type: "section", key: "storage" },
{ type: "section", key: "advanced" }
],
preview: {
key: "collection",
type: "text"
},
tags: ["data", "service", "mongo", "database", "nosql"],
function: ({ cognigy, config }) => __awaiter(void 0, void 0, void 0, function* () {
const { api, input } = cognigy;
const { storeLocation, contextKey, inputKey, connection, collection, stopOnError } = config;
if (!connection) {
throw new InvalidArgumentError("A connection has to be selected");
}
if (storeLocation === "context" && !contextKey) {
throw new InvalidArgumentError("A context key has to be specified");
}
if (storeLocation === "input" && !inputKey) {
throw new InvalidArgumentError("An input key has to be specified");
}
if (!collection) {
throw new InvalidArgumentError("A collection has to be specified");
}
try {
if (storeLocation === "context")
api.deleteContext(contextKey);
const result = yield api.mongoAggregate({ config, traceId: input.traceId });
if (storeLocation === "context")
api.addToContext(contextKey, { result }, "simple");
else
api.addToInput(inputKey, { result });
}
catch (err) {
if (stopOnError) {
throw err;
}
const resultObject = {
length: 0,
result: null,
error: err.message
};
if (storeLocation === "context")
api.addToContext(contextKey, resultObject, "simple");
else
api.addToInput(inputKey, resultObject);
}
})
});
//# sourceMappingURL=aggregate.js.map