@cognigy/rest-api-client
Version:
Cognigy REST-Client
205 lines • 6.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MONGO_FIND = void 0;
/* Custom modules */
const createNodeDescriptor_1 = require("../../../createNodeDescriptor");
const mongoDBConnection_1 = require("./mongoDBConnection");
const errors_1 = require("../../../../errors");
exports.MONGO_FIND = (0, createNodeDescriptor_1.createNodeDescriptor)({
type: "mongoFind",
defaultLabel: 'Find',
summary: "UI__NODE_EDITOR__MONGO_FIND__SUMMARY",
fields: [
{
key: "connection",
type: "connection",
label: "UI__NODE_EDITOR__MONGO_FIND__CONNECTION__LABEL",
params: {
required: true,
connectionType: mongoDBConnection_1.MONGO_DB_CONNECTION.type
}
},
{
key: "collection",
type: "cognigyText",
label: "UI__NODE_EDITOR__MONGO_FIND__COLLECTION__LABEL",
params: {
required: true
}
},
{
key: "query",
type: "json",
label: "UI__NODE_EDITOR__MONGO_FIND__QUERY__LABEL",
defaultValue: '{}'
},
{
key: "options",
type: "json",
label: "UI__NODE_EDITOR__MONGO_FIND__OPTIONS__LABEL",
defaultValue: '{}'
},
{
key: "projection",
type: "json",
label: "UI__NODE_EDITOR__MONGO_FIND__PROJECTION__LABEL",
defaultValue: '{}'
},
{
key: "index",
type: "cognigyText",
label: "UI__NODE_EDITOR__MONGO_FIND__INDEX__LABEL"
},
{
key: "storeLocation",
type: "select",
label: "UI__NODE_EDITOR__MONGO_FIND__STORE_LOCATION__LABEL",
defaultValue: "input",
params: {
options: [
{
label: "UI__NODE_EDITOR__MONGO_FIND__STORE_LOCATION__INPUT__LABEL",
value: "input"
},
{
label: "UI__NODE_EDITOR__MONGO_FIND__STORE_LOCATION__CONTEXT__LABEL",
value: "context"
}
],
required: true
},
},
{
key: "inputKey",
type: "cognigyText",
label: "UI__NODE_EDITOR__MONGO_FIND__INPUT_KEY__LABEL",
defaultValue: "mongodb",
condition: {
key: "storeLocation",
value: "input",
}
},
{
key: "contextKey",
type: "cognigyText",
label: "UI__NODE_EDITOR__MONGO_FIND__CONTEXT_KEY__LABEL",
defaultValue: "mongodb",
condition: {
key: "storeLocation",
value: "context",
}
},
{
key: "stopOnError",
type: "toggle",
label: "UI__NODE_EDITOR__MONGO_FIND__STOP_ON_ERROR__LABEL",
defaultValue: false
},
{
key: "shouldCacheResult",
type: "toggle",
label: "UI__NODE_EDITOR__MONGO_FIND__SHOULD_CACHE_RESULT__LABEL",
defaultValue: false
},
{
key: "cacheExpiry",
type: "number",
label: "UI__NODE_EDITOR__MONGO_FIND__CACHE_EXPIRY__LABEL",
condition: {
key: "shouldCacheResult",
value: true
}
}
],
preview: {
key: "collection",
type: "text"
},
sections: [
{
key: "storage",
label: "UI__NODE_EDITOR__MONGO_FIND__STORAGE__LABEL",
defaultCollapsed: true,
fields: [
"storeLocation",
"inputKey",
"contextKey",
]
},
{
key: "advanced",
label: "UI__NODE_EDITOR__MONGO_FIND__ADVANCED__LABEL",
defaultCollapsed: true,
fields: [
"projection",
"options",
"index",
"stopOnError"
]
},
{
key: "cache",
label: "UI__NODE_EDITOR__MONGO_FIND__CACHE__LABEL",
defaultCollapsed: true,
fields: [
"shouldCacheResult",
"cacheExpiry"
]
}
],
form: [
{ type: "field", key: "connection" },
{ type: "field", key: "collection" },
{ type: "field", key: "query" },
{ type: "section", key: "storage" },
{ type: "section", key: "advanced" },
{ type: "section", key: "cache" }
],
tags: ["data", "service", "mongo", "database", "nosql"],
function: async ({ cognigy, config }) => {
const { input, api } = cognigy;
const { storeLocation, contextKey, inputKey, connection, collection, stopOnError, index } = 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");
}
const convertedIndex = index === "" ? null : Number(index);
try {
if (storeLocation === "context")
api.deleteContext(contextKey);
const result = await api.mongoFind({ config, organisation: input.organisation, traceId: input.traceId });
const resultObject = {
length: result.length,
index: convertedIndex !== null && convertedIndex !== void 0 ? convertedIndex : null,
result: (convertedIndex || convertedIndex === 0) ? result[convertedIndex] : result
};
if (storeLocation === "context")
api.addToContext(contextKey, resultObject, "simple");
else
api.addToInput(inputKey, resultObject);
}
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=find.js.map