UNPKG

@nocobase/plugin-workflow-sql

Version:

Execute SQL statements in workflow.

140 lines (138 loc) 5.33 kB
/** * This file is part of the NocoBase (R) project. * Copyright (c) 2020-2024 NocoBase Co., Ltd. * Authors: NocoBase Team. * * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. * For more information, please refer to: https://www.nocobase.com/agreement. */ var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var SQLInstruction_exports = {}; __export(SQLInstruction_exports, { default: () => SQLInstruction_default }); module.exports = __toCommonJS(SQLInstruction_exports); var import_data_source_manager = require("@nocobase/data-source-manager"); var import_plugin_workflow = require("@nocobase/plugin-workflow"); var import_joi = __toESM(require("joi")); class SQLInstruction_default extends import_plugin_workflow.Instruction { configSchema = import_joi.default.object({ dataSource: import_joi.default.string(), sql: import_joi.default.string(), withMeta: import_joi.default.boolean().default(false), unsafeInjection: import_joi.default.boolean().default(false), variables: import_joi.default.array().items( import_joi.default.object({ name: import_joi.default.string().required(), value: import_joi.default.any() }) ) }); async run(node, input, processor) { const dataSourceName = node.config.dataSource || "main"; const { collectionManager } = this.workflow.app.dataSourceManager.dataSources.get(dataSourceName); if (!(collectionManager instanceof import_data_source_manager.SequelizeCollectionManager)) { throw new Error(`type of data source "${node.config.dataSource}" is not database`); } const { unsafeInjection = false, variables = [] } = node.config; let sql = ""; let replacements = null; if (unsafeInjection) { sql = processor.getParsedValue(node.config.sql || "", node.id).trim(); } else { sql = (node.config.sql || "").trim(); const parameters = processor.getParsedValue(variables, node.id); replacements = {}; for (const { name, value } of parameters) { if (name) { replacements[name] = value; } } } if (!sql) { return { status: import_plugin_workflow.JOB_STATUS.RESOLVED }; } const [result = null, meta = null] = await collectionManager.db.sequelize.query(sql, { transaction: this.workflow.useDataSourceTransaction(dataSourceName, processor.transaction), replacements // plain: true, // model: db.getCollection(node.config.collection).model }) ?? []; return { result: node.config.withMeta ? [result, meta] : result, status: import_plugin_workflow.JOB_STATUS.RESOLVED }; } async test({ dataSource, sql: sqlConfig, withMeta, unsafeInjection = false, variables = [] } = {}) { if (!sqlConfig) { return { result: null, status: import_plugin_workflow.JOB_STATUS.RESOLVED }; } const dataSourceName = dataSource || "main"; const { collectionManager } = this.workflow.app.dataSourceManager.dataSources.get(dataSourceName); if (!(collectionManager instanceof import_data_source_manager.SequelizeCollectionManager)) { throw new Error(`type of data source "${dataSource}" is not database`); } try { let sql = ""; let replacements = null; if (unsafeInjection) { sql = sqlConfig.trim(); } else { sql = sqlConfig.trim(); replacements = {}; for (const { name, value } of variables) { if (name) { replacements[name] = value; } } } const [result = null, meta = null] = await collectionManager.db.sequelize.query(sql, { replacements }) ?? []; return { result: withMeta ? [result, meta] : result, status: import_plugin_workflow.JOB_STATUS.RESOLVED }; } catch (error) { return { result: error.message, status: import_plugin_workflow.JOB_STATUS.ERROR }; } } }