@cognigy/rest-api-client
Version:
Cognigy REST-Client
161 lines • 5.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MONGO_AGGREGATE = void 0;
/* Custom modules */
const createNodeDescriptor_1 = require("../../../createNodeDescriptor");
const mongoDBConnection_1 = require("./mongoDBConnection");
const errors_1 = require("../../../../errors");
exports.MONGO_AGGREGATE = (0, createNodeDescriptor_1.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: mongoDBConnection_1.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: async ({ cognigy, config }) => {
const { api, input } = cognigy;
const { storeLocation, contextKey, inputKey, connection, collection, stopOnError } = config;
if (!connection) {
throw new errors_1.InvalidArgumentError("A connection has to be selected");
}
if (storeLocation === "context" && !contextKey) {
throw new errors_1.InvalidArgumentError("A context key has to be specified");
}
if (storeLocation === "input" && !inputKey) {
throw new errors_1.InvalidArgumentError("An input key has to be specified");
}
if (!collection) {
throw new errors_1.InvalidArgumentError("A collection has to be specified");
}
try {
if (storeLocation === "context")
api.deleteContext(contextKey);
const result = await 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