@nocobase/plugin-workflow-sql 0.20.0-alpha.15 → 0.20.0-alpha.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client/SQLInstruction.d.ts +13 -0
- package/dist/client/index.js +2 -2
- package/dist/externalVersion.js +6 -6
- package/dist/locale/zh-CN.json +1 -0
- package/dist/server/SQLInstruction.d.ts +1 -1
- package/dist/server/SQLInstruction.js +8 -3
- package/package.json +2 -2
- package/src/client/SQLInstruction.tsx +15 -0
- package/src/locale/zh-CN.json +1 -0
- package/src/server/SQLInstruction.ts +10 -3
- package/src/server/__tests__/instruction.test.ts +51 -0
|
@@ -6,6 +6,19 @@ export default class extends Instruction {
|
|
|
6
6
|
group: string;
|
|
7
7
|
description: string;
|
|
8
8
|
fieldset: {
|
|
9
|
+
dataSource: {
|
|
10
|
+
type: string;
|
|
11
|
+
required: boolean;
|
|
12
|
+
title: string;
|
|
13
|
+
description: string;
|
|
14
|
+
'x-decorator': string;
|
|
15
|
+
'x-component': string;
|
|
16
|
+
'x-component-props': {
|
|
17
|
+
className: string;
|
|
18
|
+
filter(item: any): any;
|
|
19
|
+
};
|
|
20
|
+
default: string;
|
|
21
|
+
};
|
|
9
22
|
sql: {
|
|
10
23
|
type: string;
|
|
11
24
|
required: boolean;
|
package/dist/client/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
(function(e,o){typeof exports=="object"&&typeof module!="undefined"?o(exports,require("@nocobase/client"),require("react/jsx-runtime"),require("@nocobase/plugin-workflow/client"),require("react-i18next")):typeof define=="function"&&define.amd?define(["exports","@nocobase/client","react/jsx-runtime","@nocobase/plugin-workflow/client","react-i18next"],o):(e=typeof globalThis!="undefined"?globalThis:e||self,o(e["@nocobase/plugin-workflow-sql"]={},e["@nocobase/client"],e.jsxRuntime,e["@nocobase/plugin-workflow"],e["react-i18next"]))})(this,function(e,o,
|
|
1
|
+
(function(e,o){typeof exports=="object"&&typeof module!="undefined"?o(exports,require("@nocobase/client"),require("react/jsx-runtime"),require("@nocobase/plugin-workflow/client"),require("react-i18next")):typeof define=="function"&&define.amd?define(["exports","@nocobase/client","react/jsx-runtime","@nocobase/plugin-workflow/client","react-i18next"],o):(e=typeof globalThis!="undefined"?globalThis:e||self,o(e["@nocobase/plugin-workflow-sql"]={},e["@nocobase/client"],e.jsxRuntime,e["@nocobase/plugin-workflow"],e["react-i18next"]))})(this,function(e,o,t,a,l){"use strict";var m=Object.defineProperty;var x=(e,o,t)=>o in e?m(e,o,{enumerable:!0,configurable:!0,writable:!0,value:t}):e[o]=t;var r=(e,o,t)=>(x(e,typeof o!="symbol"?o+"":o,t),t);var u=(e,o,t)=>new Promise((a,l)=>{var s=n=>{try{i(t.next(n))}catch(c){l(c)}},d=n=>{try{i(t.throw(n))}catch(c){l(c)}},i=n=>n.done?a(n.value):Promise.resolve(n.value).then(s,d);i((t=t.apply(e,o)).next())});const s="@nocobase/plugin-workflow-sql";class d extends a.Instruction{constructor(){super(...arguments);r(this,"title",`{{t("SQL action", { ns: "${s}" })}}`);r(this,"type","sql");r(this,"group","collection");r(this,"description",`{{t("Execute a SQL statement in database.", { ns: "${s}" })}}`);r(this,"fieldset",{dataSource:{type:"string",required:!0,title:'{{t("Data source")}}',description:`{{t("Select a data source to execute SQL.", { ns: "${s}" })}}`,"x-decorator":"FormItem","x-component":"DataSourceSelect","x-component-props":{className:"auto-width",filter(p){return p.options.isDBInstance}},default:"main"},sql:{type:"string",required:!0,title:"SQL",description:"{{sqlDescription()}}","x-decorator":"FormItem","x-component":"WorkflowVariableRawTextArea","x-component-props":{rows:20,className:o.css`
|
|
2
2
|
font-size: 80%;
|
|
3
3
|
font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
|
|
4
|
-
`}}});r(this,"scope",{sqlDescription(){return
|
|
4
|
+
`}}});r(this,"scope",{sqlDescription(){return t.jsxs(l.Trans,{ns:s,children:["SQL query result could be used through ",t.jsx("a",{href:"https://docs-cn.nocobase.com/handbook/workflow-json-query",target:"_blank",rel:"noreferrer",children:"JSON query node"})," (Commercial plugin)."]})}});r(this,"components",{WorkflowVariableRawTextArea:a.WorkflowVariableRawTextArea})}useVariables({key:p,title:w},{types:b,fieldNames:f=a.defaultFieldNames}){return{[f.value]:p,[f.label]:w}}}class i extends o.Plugin{afterAdd(){return u(this,null,function*(){})}beforeLoad(){return u(this,null,function*(){})}load(){return u(this,null,function*(){this.app.pm.get("workflow").registerInstruction("sql",d)})}}e.default=i,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
package/dist/externalVersion.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
module.exports = {
|
|
2
|
-
"@nocobase/client": "0.20.0-alpha.
|
|
3
|
-
"@nocobase/plugin-workflow": "0.20.0-alpha.
|
|
2
|
+
"@nocobase/client": "0.20.0-alpha.17",
|
|
3
|
+
"@nocobase/plugin-workflow": "0.20.0-alpha.17",
|
|
4
4
|
"react": "18.2.0",
|
|
5
5
|
"react-i18next": "11.18.6",
|
|
6
|
-
"@nocobase/server": "0.20.0-alpha.
|
|
7
|
-
"@nocobase/plugin-workflow-test": "0.20.0-alpha.
|
|
8
|
-
"@nocobase/test": "0.20.0-alpha.
|
|
9
|
-
"@nocobase/utils": "0.20.0-alpha.
|
|
6
|
+
"@nocobase/server": "0.20.0-alpha.17",
|
|
7
|
+
"@nocobase/plugin-workflow-test": "0.20.0-alpha.17",
|
|
8
|
+
"@nocobase/test": "0.20.0-alpha.17",
|
|
9
|
+
"@nocobase/utils": "0.20.0-alpha.17"
|
|
10
10
|
};
|
package/dist/locale/zh-CN.json
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"SQL action": "SQL 操作",
|
|
3
3
|
"Execute a SQL statement in database.": "在数据库中执行一个 SQL 语句",
|
|
4
|
+
"Select a data source to execute SQL.": "选择一个数据源来执行 SQL",
|
|
4
5
|
"SQL query result could be used through <1>JSON query node</1> (Commercial plugin).": "SQL 执行的结果可在 <1>JSON 解析节点</1> 中使用(商业插件)。"
|
|
5
6
|
}
|
|
@@ -23,14 +23,19 @@ module.exports = __toCommonJS(SQLInstruction_exports);
|
|
|
23
23
|
var import_plugin_workflow = require("@nocobase/plugin-workflow");
|
|
24
24
|
class SQLInstruction_default extends import_plugin_workflow.Instruction {
|
|
25
25
|
async run(node, input, processor) {
|
|
26
|
-
const {
|
|
27
|
-
|
|
26
|
+
const { db } = this.workflow.app.dataSourceManager.dataSources.get(
|
|
27
|
+
node.config.dataSource || "main"
|
|
28
|
+
).collectionManager;
|
|
29
|
+
if (!db) {
|
|
30
|
+
throw new Error(`type of data source "${node.config.dataSource}" is not database`);
|
|
31
|
+
}
|
|
32
|
+
const sql = processor.getParsedValue(node.config.sql || "", node.id).trim();
|
|
28
33
|
if (!sql) {
|
|
29
34
|
return {
|
|
30
35
|
status: import_plugin_workflow.JOB_STATUS.RESOLVED
|
|
31
36
|
};
|
|
32
37
|
}
|
|
33
|
-
const result = await sequelize.query(sql, {
|
|
38
|
+
const result = await db.sequelize.query(sql, {
|
|
34
39
|
transaction: processor.transaction
|
|
35
40
|
// plain: true,
|
|
36
41
|
// model: db.getCollection(node.config.collection).model
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"displayName.zh-CN": "工作流:SQL 节点",
|
|
5
5
|
"description": "Execute SQL statements in workflow.",
|
|
6
6
|
"description.zh-CN": "可用于在工作流中对数据库执行任意 SQL 语句。",
|
|
7
|
-
"version": "0.20.0-alpha.
|
|
7
|
+
"version": "0.20.0-alpha.17",
|
|
8
8
|
"license": "AGPL-3.0",
|
|
9
9
|
"main": "./dist/server/index.js",
|
|
10
10
|
"homepage": "https://docs.nocobase.com/handbook/workflow-sql",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
"@nocobase/server": "0.x",
|
|
22
22
|
"@nocobase/test": "0.x"
|
|
23
23
|
},
|
|
24
|
-
"gitHead": "
|
|
24
|
+
"gitHead": "a2460c222bc0b8a3bcb783b5c856499d756efa82",
|
|
25
25
|
"keywords": [
|
|
26
26
|
"Workflow"
|
|
27
27
|
]
|
|
@@ -12,6 +12,21 @@ export default class extends Instruction {
|
|
|
12
12
|
group = 'collection';
|
|
13
13
|
description = `{{t("Execute a SQL statement in database.", { ns: "${NAMESPACE}" })}}`;
|
|
14
14
|
fieldset = {
|
|
15
|
+
dataSource: {
|
|
16
|
+
type: 'string',
|
|
17
|
+
required: true,
|
|
18
|
+
title: `{{t("Data source")}}`,
|
|
19
|
+
description: `{{t("Select a data source to execute SQL.", { ns: "${NAMESPACE}" })}}`,
|
|
20
|
+
'x-decorator': 'FormItem',
|
|
21
|
+
'x-component': 'DataSourceSelect',
|
|
22
|
+
'x-component-props': {
|
|
23
|
+
className: 'auto-width',
|
|
24
|
+
filter(item) {
|
|
25
|
+
return item.options.isDBInstance;
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
default: 'main',
|
|
29
|
+
},
|
|
15
30
|
sql: {
|
|
16
31
|
type: 'string',
|
|
17
32
|
required: true,
|
package/src/locale/zh-CN.json
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"SQL action": "SQL 操作",
|
|
3
3
|
"Execute a SQL statement in database.": "在数据库中执行一个 SQL 语句",
|
|
4
|
+
"Select a data source to execute SQL.": "选择一个数据源来执行 SQL",
|
|
4
5
|
"SQL query result could be used through <1>JSON query node</1> (Commercial plugin).": "SQL 执行的结果可在 <1>JSON 解析节点</1> 中使用(商业插件)。"
|
|
5
6
|
}
|
|
@@ -2,15 +2,22 @@ import { Processor, Instruction, JOB_STATUS, FlowNodeModel } from '@nocobase/plu
|
|
|
2
2
|
|
|
3
3
|
export default class extends Instruction {
|
|
4
4
|
async run(node: FlowNodeModel, input, processor: Processor) {
|
|
5
|
-
|
|
6
|
-
const
|
|
5
|
+
// @ts-ignore
|
|
6
|
+
const { db } = this.workflow.app.dataSourceManager.dataSources.get(
|
|
7
|
+
node.config.dataSource || 'main',
|
|
8
|
+
).collectionManager;
|
|
9
|
+
if (!db) {
|
|
10
|
+
throw new Error(`type of data source "${node.config.dataSource}" is not database`);
|
|
11
|
+
}
|
|
12
|
+
const sql = processor.getParsedValue(node.config.sql || '', node.id).trim();
|
|
7
13
|
if (!sql) {
|
|
8
14
|
return {
|
|
9
15
|
status: JOB_STATUS.RESOLVED,
|
|
10
16
|
};
|
|
11
17
|
}
|
|
12
18
|
|
|
13
|
-
|
|
19
|
+
// @ts-ignore
|
|
20
|
+
const result = await db.sequelize.query(sql, {
|
|
14
21
|
transaction: processor.transaction,
|
|
15
22
|
// plain: true,
|
|
16
23
|
// model: db.getCollection(node.config.collection).model
|
|
@@ -92,6 +92,26 @@ describe('workflow > instructions > sql', () => {
|
|
|
92
92
|
});
|
|
93
93
|
|
|
94
94
|
describe('sql with variables', () => {
|
|
95
|
+
it('$system.now', async () => {
|
|
96
|
+
const queryInterface = db.sequelize.getQueryInterface();
|
|
97
|
+
const n1 = await workflow.createNode({
|
|
98
|
+
type: 'sql',
|
|
99
|
+
config: {
|
|
100
|
+
sql: `select '{{$system.now}}' as a`,
|
|
101
|
+
},
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
const post = await PostRepo.create({ values: { title: 't1' } });
|
|
105
|
+
|
|
106
|
+
await sleep(500);
|
|
107
|
+
|
|
108
|
+
const [execution] = await workflow.getExecutions();
|
|
109
|
+
const [sqlJob] = await execution.getJobs({ order: [['id', 'ASC']] });
|
|
110
|
+
expect(sqlJob.status).toBe(JOB_STATUS.RESOLVED);
|
|
111
|
+
// expect(queryJob.status).toBe(JOB_STATUS.RESOLVED);
|
|
112
|
+
// expect(queryJob.result.read).toBe(post.id);
|
|
113
|
+
});
|
|
114
|
+
|
|
95
115
|
it('update', async () => {
|
|
96
116
|
const queryInterface = db.sequelize.getQueryInterface();
|
|
97
117
|
const n1 = await workflow.createNode({
|
|
@@ -195,4 +215,35 @@ describe('workflow > instructions > sql', () => {
|
|
|
195
215
|
expect(job.status).toBe(JOB_STATUS.RESOLVED);
|
|
196
216
|
});
|
|
197
217
|
});
|
|
218
|
+
|
|
219
|
+
describe('multiple data source', () => {
|
|
220
|
+
it('query on another data source', async () => {
|
|
221
|
+
const anotherSource = app.dataSourceManager.dataSources.get('another');
|
|
222
|
+
const PostCollection = anotherSource.collectionManager.getCollection('posts');
|
|
223
|
+
const { repository: AnotherPostRepo } = PostCollection;
|
|
224
|
+
const post = await AnotherPostRepo.create({ values: { title: 't1' } });
|
|
225
|
+
const p1s = await AnotherPostRepo.find();
|
|
226
|
+
expect(p1s.length).toBe(1);
|
|
227
|
+
|
|
228
|
+
const n1 = await workflow.createNode({
|
|
229
|
+
type: 'sql',
|
|
230
|
+
config: {
|
|
231
|
+
dataSource: 'another',
|
|
232
|
+
sql: `select * from ${PostCollection.quotedTableName()}`,
|
|
233
|
+
},
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
await PostRepo.create({ values: { title: 't1' } });
|
|
237
|
+
|
|
238
|
+
await sleep(500);
|
|
239
|
+
|
|
240
|
+
const [execution] = await workflow.getExecutions();
|
|
241
|
+
expect(execution.status).toBe(EXECUTION_STATUS.RESOLVED);
|
|
242
|
+
const [job] = await execution.getJobs();
|
|
243
|
+
expect(job.result.length).toBe(2);
|
|
244
|
+
expect(job.result[0].length).toBe(1);
|
|
245
|
+
// @ts-ignore
|
|
246
|
+
expect(job.result[0][0].id).toBe(post.id);
|
|
247
|
+
});
|
|
248
|
+
});
|
|
198
249
|
});
|