UNPKG

@nocobase/flow-engine

Version:

A standalone flow engine for NocoBase, managing workflows, models, and actions.

295 lines (293 loc) 8.51 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 __name = (target, value) => __defProp(target, "name", { value, configurable: true }); 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 sqlResource_exports = {}; __export(sqlResource_exports, { FlowSQLRepository: () => FlowSQLRepository, SQLResource: () => SQLResource }); module.exports = __toCommonJS(sqlResource_exports); var import_reactive = require("@formily/reactive"); var import_lodash = __toESM(require("lodash")); var import_flowContext = require("../flowContext"); var import_baseRecordResource = require("./baseRecordResource"); function transformSQL(template) { let index = 1; const bind = {}; const sql = template.replace(/{{\s*([^}]+)\s*}}/g, (_2, expr) => { const key = `__var${index}`; bind[key] = `{{${expr.trim()}}}`; index++; return `$${key}`; }); return { sql, bind }; } __name(transformSQL, "transformSQL"); const _FlowSQLRepository = class _FlowSQLRepository { ctx; constructor(ctx) { this.ctx = new import_flowContext.FlowContext(); this.ctx.addDelegate(ctx); this.ctx.defineProperty("offset", { get: /* @__PURE__ */ __name(() => 0, "get"), cache: false }); this.ctx.defineProperty("limit", { get: /* @__PURE__ */ __name(() => 20, "get"), cache: false }); } async run(sql, options = {}) { const result = transformSQL(sql); const bind = await this.ctx.resolveJsonTemplate(result.bind); const { data } = await this.ctx.api.request({ method: "POST", url: "flowSql:run", data: { sql: result.sql, ...options, bind: { ...bind, ...options.bind } } }); return data == null ? void 0 : data.data; } async save(data) { await this.ctx.api.request({ method: "POST", url: "flowSql:save", data: { ...data } }); } async runById(uid, options) { const response = await this.ctx.api.request({ method: "GET", url: "flowSql:getBind", params: { uid } }); const bind = await this.ctx.resolveJsonTemplate(response.data.data || {}); const { data } = await this.ctx.api.request({ method: "POST", url: "flowSql:runById", data: { uid, ...options, bind: { ...bind, ...options.bind } } }); return data == null ? void 0 : data.data; } async destroy(uid) { await this.ctx.api.request({ url: "flowSql:destroy", params: { filterByTk: uid } }); } }; __name(_FlowSQLRepository, "FlowSQLRepository"); let FlowSQLRepository = _FlowSQLRepository; const _SQLResource = class _SQLResource extends import_baseRecordResource.BaseRecordResource { _data = import_reactive.observable.ref(null); _meta = import_reactive.observable.ref({}); refreshTimer = null; _debugEnabled = false; _sql; // 请求配置 - 与 APIClient 接口保持一致 request = { // url: null as string | null, method: "post", params: {}, data: {}, headers: {} }; get refreshActionName() { return this._debugEnabled ? "run" : "runById"; } get supportsFilter() { return true; } constructor(context) { super(context); context.defineProperty("limit", { get: /* @__PURE__ */ __name(() => this.getPageSize(), "get"), cache: false }); context.defineProperty("offset", { get: /* @__PURE__ */ __name(() => { const page = this.getPage(); const pageSize = this.getPageSize(); return (page - 1) * pageSize; }, "get"), cache: false }); } buildURL(action) { return `flowSql:${action || this.refreshActionName}`; } setPage(page) { this.addRequestParameter("page", page); return this.setMeta({ page }); } getPage() { return this.getMeta("page") || 1; } setPageSize(pageSize) { this.addRequestParameter("pageSize", pageSize); return this.setMeta({ pageSize }); } setDebug(enabled) { this._debugEnabled = enabled; return this; } getPageSize() { return this.getMeta("pageSize") || 20; } async next() { this.setPage(this.getPage() + 1); await this.refresh(); } async previous() { if (this.getPage() > 1) { this.setPage(this.getPage() - 1); await this.refresh(); } } async goto(page) { if (page > 0) { this.request.params.page = page; await this.refresh(); } } setDataSourceKey(dataSourceKey) { this.request.data.dataSourceKey = dataSourceKey; return this; } setSQLType(type) { this.request.data.type = type; return this; } setSQL(sql) { this._sql = sql; return this; } setFilterByTk(filterByTk) { this.request.data.uid = filterByTk; return this; } setFilter(filter) { this.request.data.filter = filter; return this; } setBind(bind) { this.request.data.bind = bind; return this; } async run() { return this._debugEnabled ? await this.runBySQL() : await this.runById(); } async runBySQL() { const sql = this._sql; const result = transformSQL(sql); const bind = await this.context.resolveJsonTemplate(result.bind); const options = import_lodash.default.cloneDeep({ method: "post", ...this.getRefreshRequestOptions() }); options.data.bind = bind; options.data.sql = result.sql; return await this.runAction("run", options); } async runById() { const { data } = await this.runAction("getBind", { method: "get", params: { uid: this.request.data.uid } }); const bind = await this.context.resolveJsonTemplate(data); this.setBind(bind); return await this.runAction("runById", { method: "post", ...this.getRefreshRequestOptions() }); } /** * 在同一个事件循环内多次调用 refresh 方法时,只有最后一次调用会生效。避免触发多次相同的接口请求。 * @returns */ async refresh() { if (this.refreshTimer) { clearTimeout(this.refreshTimer); } return new Promise((resolve, reject) => { this.refreshTimer = setTimeout(async () => { try { this.clearError(); this.loading = true; const { data, meta } = await this.run(); this.setData(data).setMeta(meta); this.emit("refresh"); this.loading = false; resolve(); } catch (error) { this.setError(error); reject(error instanceof Error ? error : new Error(String(error))); } finally { this.refreshTimer = null; this.loading = false; } }); }); } }; __name(_SQLResource, "SQLResource"); let SQLResource = _SQLResource; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { FlowSQLRepository, SQLResource });