@cognigy/rest-api-client
Version:
Cognigy REST-Client
143 lines • 4.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SQL_RUN_QUERY = void 0;
/* Custom modules */
const createNodeDescriptor_1 = require("../../../createNodeDescriptor");
const errors_1 = require("../../../../errors");
const sqlConnection_1 = require("./sqlConnection");
exports.SQL_RUN_QUERY = (0, createNodeDescriptor_1.createNodeDescriptor)({
type: "sqlRunQuery",
defaultLabel: 'Run SQL Query',
summary: "UI__NODE_EDITOR__SQL__SQL_RUN_QUERY__SUMMARY",
fields: [
{
key: "connection",
type: "connection",
label: "UI__NODE_EDITOR__SQL__SQL_RUN_QUERY__CONNECTION__LABEL",
params: {
required: true,
connectionType: sqlConnection_1.SQL_CONNECTION.type
}
},
{
key: "query",
type: "cognigyText",
label: "UI__NODE_EDITOR__SQL__SQL_RUN_QUERY__QUERY__LABEL",
params: {
multiline: true,
required: true
}
},
{
key: "storeLocation",
type: "select",
label: "UI__NODE_EDITOR__SQL__SQL_RUN_QUERY__STORE_LOCATION__LABEL",
params: {
options: [
{
label: "UI__NODE_EDITOR__SQL__SQL_RUN_QUERY__STORE_LOCATION__INPUT__LABEL",
value: "input"
},
{
label: "UI__NODE_EDITOR__SQL__SQL_RUN_QUERY__STORE_LOCATION__CONTEXT__LABEL",
value: "context"
}
],
required: true
},
defaultValue: "input"
},
{
key: "inputKey",
type: "cognigyText",
label: "UI__NODE_EDITOR__SQL__SQL_RUN_QUERY__INPUT_KEY__LABEL",
defaultValue: "sqlresult",
condition: {
key: "storeLocation",
value: "input",
}
},
{
key: "contextKey",
type: "cognigyText",
label: "UI__NODE_EDITOR__SQL__SQL_RUN_QUERY__CONTEXT_KEY__LABEL",
condition: {
key: "storeLocation",
value: "context",
}
},
{
key: "stopOnError",
type: "toggle",
label: "UI__NODE_EDITOR__SQL__SQL_RUN_QUERY__STOP_ON_ERROR__LABEL",
defaultValue: false
}
],
sections: [
{
key: "storage",
label: "UI__NODE_EDITOR__SQL__SQL_RUN_QUERY__STORAGE__LABEL",
defaultCollapsed: true,
fields: [
"storeLocation",
"inputKey",
"contextKey",
]
},
{
key: "advanced",
label: "UI__NODE_EDITOR__SQL__SQL_RUN_QUERY__ADVANCED__LABEL",
defaultCollapsed: true,
fields: [
"stopOnError"
]
}
],
form: [
{ type: "field", key: "connection" },
{ type: "field", key: "query" },
{ type: "section", key: "storage" },
{ type: "section", key: "advanced" }
],
preview: {
key: "query",
type: "text"
},
tags: ["data", "service", "sql"],
function: async ({ cognigy, config }) => {
const { api, input } = cognigy;
const { connection = {}, storeLocation, contextKey, inputKey, query, 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");
}
try {
if (storeLocation === "context")
api.deleteContext(contextKey);
const result = await api.runSQLQuery({ connection, query, traceId: input.traceId });
if (storeLocation === "context")
api.addToContext(contextKey, result, "simple");
else
input[inputKey] = result;
}
catch (err) {
if (stopOnError) {
throw err;
}
const resultObject = {
result: null,
error: err.message
};
if (storeLocation === "context")
api.addToContext(contextKey, resultObject, "simple");
else
api.addToInput(inputKey, resultObject);
}
}
});
//# sourceMappingURL=runQuery.js.map