UNPKG

@cognigy/rest-api-client

Version:

Cognigy REST-Client

175 lines 5.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MONGO_UPDATE_MANY = void 0; /* Custom modules */ const createNodeDescriptor_1 = require("../../../createNodeDescriptor"); const mongoDBConnection_1 = require("./mongoDBConnection"); const errors_1 = require("../../../../errors"); exports.MONGO_UPDATE_MANY = (0, createNodeDescriptor_1.createNodeDescriptor)({ type: "mongoUpdateMany", defaultLabel: 'Update Many', summary: "UI__NODE_EDITOR__MONGO_UPDATE_MANY__SUMMARY", fields: [ { key: "connection", type: "connection", label: "UI__NODE_EDITOR__MONGO_UPDATE_MANY__CONNECTION__LABEL", params: { required: true, connectionType: mongoDBConnection_1.MONGO_DB_CONNECTION.type } }, { key: "collection", type: "cognigyText", label: "UI__NODE_EDITOR__MONGO_UPDATE_MANY__COLLECTION__LABEL", params: { required: true } }, { key: "query", type: "json", label: "UI__NODE_EDITOR__MONGO_UPDATE_MANY__QUERY__LABEL", defaultValue: '{}' }, { key: "update", type: "json", label: "UI__NODE_EDITOR__MONGO_UPDATE_MANY__UPDATE__LABEL", defaultValue: '{}' }, { key: "options", type: "json", label: "UI__NODE_EDITOR__MONGO_UPDATE_MANY__OPTIONS__LABEL", defaultValue: '{}' }, { key: "storeLocation", type: "select", label: "UI__NODE_EDITOR__MONGO_UPDATE_MANY__STORE_LOCATION__LABEL", params: { options: [ { label: "UI__NODE_EDITOR__MONGO_UPDATE_MANY__STORE_LOCATION__INPUT__LABEL", value: "input" }, { label: "UI__NODE_EDITOR__MONGO_UPDATE_MANY__STORE_LOCATION__CONTEXT__LABEL", value: "context" } ], required: true }, defaultValue: "input" }, { key: "inputKey", type: "cognigyText", label: "UI__NODE_EDITOR__MONGO_UPDATE_MANY__INPUT_KEY__LABEL", defaultValue: "mongodb", condition: { key: "storeLocation", value: "input", } }, { key: "contextKey", type: "cognigyText", label: "UI__NODE_EDITOR__MONGO_UPDATE_MANY__CONTEXT_KEY__LABEL", defaultValue: "mongodb", condition: { key: "storeLocation", value: "context", } }, { key: "useOperators", type: "toggle", label: "UI__NODE_EDITOR__MONGO_UPDATE_MANY__USE_OPERATORS__LABEL", defaultValue: false }, { key: "stopOnError", type: "toggle", label: "UI__NODE_EDITOR__MONGO_UPDATE_MANY__STOP_ON_ERROR__LABEL", defaultValue: false } ], sections: [ { key: "storage", label: "UI__NODE_EDITOR__MONGO_UPDATE_MANY__STORAGE__LABEL", defaultCollapsed: true, fields: [ "storeLocation", "inputKey", "contextKey", ] }, { key: "advanced", label: "UI__NODE_EDITOR__MONGO_UPDATE_MANY__ADVANCED__LABEL", defaultCollapsed: true, fields: [ "useOperators", "options", "stopOnError" ] } ], form: [ { type: "field", key: "connection" }, { type: "field", key: "collection" }, { type: "field", key: "query" }, { type: "field", key: "update" }, { 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 } = cognigy; const { storeLocation, contextKey, inputKey, collection, connection, 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.mongoUpdateMany({ config }); 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=updateMany.js.map