@nocobase/plugin-workflow-sql 1.0.0-alpha.2 → 1.0.0-alpha.4

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.
@@ -1,18 +0,0 @@
1
- import { Plugin } from '@nocobase/client';
2
- import WorkflowPlugin from '@nocobase/plugin-workflow/client';
3
-
4
- import SQLInstruction from './SQLInstruction';
5
-
6
- export default class extends Plugin {
7
- async afterAdd() {
8
- // await this.app.pm.add()
9
- }
10
-
11
- async beforeLoad() {}
12
-
13
- // You can get and modify the app instance here
14
- async load() {
15
- const workflow = this.app.pm.get('workflow') as WorkflowPlugin;
16
- workflow.registerInstruction('sql', SQLInstruction);
17
- }
18
- }
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './server';
2
- export { default } from './server';
@@ -1,5 +0,0 @@
1
- {
2
- "SQL action": "SQL action",
3
- "Execute a SQL statement in database": "Execute a SQL statement in database",
4
- "Usage of SQL query result is not supported yet.": "Usage of SQL query result is not supported yet."
5
- }
@@ -1,12 +0,0 @@
1
- import { useTranslation } from 'react-i18next';
2
-
3
- export const NAMESPACE = '@nocobase/plugin-workflow-sql';
4
-
5
- export function useLang(key: string, options = {}) {
6
- const { t } = usePluginTranslation(options);
7
- return t(key);
8
- }
9
-
10
- export function usePluginTranslation(options) {
11
- return useTranslation(NAMESPACE, options);
12
- }
@@ -1,5 +0,0 @@
1
- {
2
- "SQL action": "SQL 작업",
3
- "Execute a SQL statement in database": "데이터베이스에서 SQL 문을 실행합니다",
4
- "Usage of SQL query result is not supported yet.": "아직 SQL 쿼리 결과의 사용은 지원되지 않습니다."
5
- }
@@ -1,6 +0,0 @@
1
- {
2
- "SQL action": "SQL 操作",
3
- "Execute a SQL statement in database.": "在数据库中执行一个 SQL 语句",
4
- "Select a data source to execute SQL.": "选择一个数据源来执行 SQL",
5
- "SQL query result could be used through <1>JSON query node</1> (Commercial plugin).": "SQL 执行的结果可在 <1>JSON 解析节点</1> 中使用(商业插件)。"
6
- }
@@ -1,11 +0,0 @@
1
- import { Plugin } from '@nocobase/server';
2
- import WorkflowPlugin from '@nocobase/plugin-workflow';
3
-
4
- import SQLInstruction from './SQLInstruction';
5
-
6
- export default class extends Plugin {
7
- async load() {
8
- const workflowPlugin = this.app.getPlugin<WorkflowPlugin>(WorkflowPlugin);
9
- workflowPlugin.registerInstruction('sql', SQLInstruction);
10
- }
11
- }
@@ -1,31 +0,0 @@
1
- import { Processor, Instruction, JOB_STATUS, FlowNodeModel } from '@nocobase/plugin-workflow';
2
-
3
- export default class extends Instruction {
4
- async run(node: FlowNodeModel, input, processor: Processor) {
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();
13
- if (!sql) {
14
- return {
15
- status: JOB_STATUS.RESOLVED,
16
- };
17
- }
18
-
19
- // @ts-ignore
20
- const result = await db.sequelize.query(sql, {
21
- transaction: processor.transaction,
22
- // plain: true,
23
- // model: db.getCollection(node.config.collection).model
24
- });
25
-
26
- return {
27
- result,
28
- status: JOB_STATUS.RESOLVED,
29
- };
30
- }
31
- }
@@ -1,249 +0,0 @@
1
- import Database, { fn } from '@nocobase/database';
2
- import { Application } from '@nocobase/server';
3
- import { EXECUTION_STATUS, JOB_STATUS } from '@nocobase/plugin-workflow';
4
- import { getApp, sleep } from '@nocobase/plugin-workflow-test';
5
-
6
- import Plugin from '..';
7
-
8
- describe('workflow > instructions > sql', () => {
9
- let app: Application;
10
- let db: Database;
11
- let PostRepo;
12
- let PostCollection;
13
- let ReplyRepo;
14
- let WorkflowModel;
15
- let workflow;
16
-
17
- beforeEach(async () => {
18
- app = await getApp({
19
- plugins: [Plugin],
20
- });
21
-
22
- db = app.db;
23
- WorkflowModel = db.getCollection('workflows').model;
24
- PostCollection = db.getCollection('posts');
25
- PostRepo = PostCollection.repository;
26
- ReplyRepo = db.getCollection('replies').repository;
27
-
28
- workflow = await WorkflowModel.create({
29
- enabled: true,
30
- type: 'collection',
31
- config: {
32
- mode: 1,
33
- collection: 'posts',
34
- },
35
- });
36
- });
37
-
38
- afterEach(() => app.destroy());
39
-
40
- describe('invalid', () => {
41
- it('no sql', async () => {
42
- const n1 = await workflow.createNode({
43
- type: 'sql',
44
- config: {},
45
- });
46
-
47
- const post = await PostRepo.create({ values: { title: 't1' } });
48
-
49
- await sleep(500);
50
-
51
- const [execution] = await workflow.getExecutions();
52
- const [sqlJob] = await execution.getJobs({ order: [['id', 'ASC']] });
53
- expect(execution.status).toBe(EXECUTION_STATUS.RESOLVED);
54
- expect(sqlJob.status).toBe(JOB_STATUS.RESOLVED);
55
- });
56
-
57
- it('empty sql', async () => {
58
- const n1 = await workflow.createNode({
59
- type: 'sql',
60
- config: {
61
- sql: '',
62
- },
63
- });
64
-
65
- const post = await PostRepo.create({ values: { title: 't1' } });
66
-
67
- await sleep(500);
68
-
69
- const [execution] = await workflow.getExecutions();
70
- const [sqlJob] = await execution.getJobs({ order: [['id', 'ASC']] });
71
- expect(execution.status).toBe(EXECUTION_STATUS.RESOLVED);
72
- expect(sqlJob.status).toBe(JOB_STATUS.RESOLVED);
73
- });
74
-
75
- it('invalid sql', async () => {
76
- const n1 = await workflow.createNode({
77
- type: 'sql',
78
- config: {
79
- sql: '1',
80
- },
81
- });
82
-
83
- const post = await PostRepo.create({ values: { title: 't1' } });
84
-
85
- await sleep(500);
86
-
87
- const [execution] = await workflow.getExecutions();
88
- const [sqlJob] = await execution.getJobs({ order: [['id', 'ASC']] });
89
- expect(execution.status).toBe(EXECUTION_STATUS.ERROR);
90
- expect(sqlJob.status).toBe(JOB_STATUS.ERROR);
91
- });
92
- });
93
-
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
-
115
- it('update', async () => {
116
- const queryInterface = db.sequelize.getQueryInterface();
117
- const n1 = await workflow.createNode({
118
- type: 'sql',
119
- config: {
120
- sql: `update ${PostCollection.quotedTableName()} set ${queryInterface.quoteIdentifier(
121
- 'read',
122
- )}={{$context.data.id}} where ${queryInterface.quoteIdentifier('id')}={{$context.data.id}}`,
123
- },
124
- });
125
-
126
- const n2 = await workflow.createNode({
127
- type: 'query',
128
- config: {
129
- collection: 'posts',
130
- params: {
131
- filter: {
132
- id: '{{ $context.data.id }}',
133
- },
134
- },
135
- },
136
- upstreamId: n1.id,
137
- });
138
-
139
- await n1.setDownstream(n2);
140
-
141
- const post = await PostRepo.create({ values: { title: 't1' } });
142
-
143
- await sleep(500);
144
-
145
- const [execution] = await workflow.getExecutions();
146
- const [sqlJob, queryJob] = await execution.getJobs({ order: [['id', 'ASC']] });
147
- expect(sqlJob.status).toBe(JOB_STATUS.RESOLVED);
148
- expect(queryJob.status).toBe(JOB_STATUS.RESOLVED);
149
- expect(queryJob.result.read).toBe(post.id);
150
- });
151
-
152
- it('delete', async () => {
153
- const queryInterface = db.sequelize.getQueryInterface();
154
- const n1 = await workflow.createNode({
155
- type: 'sql',
156
- config: {
157
- sql: `delete from ${PostCollection.quotedTableName()} where ${queryInterface.quoteIdentifier(
158
- 'id',
159
- )}={{$context.data.id}};`,
160
- },
161
- });
162
-
163
- const n2 = await workflow.createNode({
164
- type: 'query',
165
- config: {
166
- collection: 'posts',
167
- params: {
168
- filter: {
169
- id: '{{ $context.data.id }}',
170
- },
171
- },
172
- },
173
- upstreamId: n1.id,
174
- });
175
-
176
- await n1.setDownstream(n2);
177
-
178
- const post = await PostRepo.create({ values: { title: 't1' } });
179
-
180
- await sleep(500);
181
-
182
- const [execution] = await workflow.getExecutions();
183
- const [sqlJob, queryJob] = await execution.getJobs({ order: [['id', 'ASC']] });
184
- expect(sqlJob.status).toBe(JOB_STATUS.RESOLVED);
185
- expect(queryJob.status).toBe(JOB_STATUS.RESOLVED);
186
- expect(queryJob.result).toBeNull();
187
- });
188
- });
189
-
190
- describe('run in sync mode', () => {
191
- it('sync workflow', async () => {
192
- const w2 = await WorkflowModel.create({
193
- enabled: true,
194
- sync: true,
195
- type: 'collection',
196
- config: {
197
- mode: 1,
198
- collection: 'categories',
199
- },
200
- });
201
-
202
- const n1 = await w2.createNode({
203
- type: 'sql',
204
- config: {
205
- sql: `select count(id) from ${PostCollection.quotedTableName()}`,
206
- },
207
- });
208
-
209
- const CategoryRepo = db.getCollection('categories').repository;
210
- const category = await CategoryRepo.create({ values: { title: 't1' } });
211
-
212
- const [execution] = await w2.getExecutions();
213
- expect(execution.status).toBe(EXECUTION_STATUS.RESOLVED);
214
- const [job] = await execution.getJobs();
215
- expect(job.status).toBe(JOB_STATUS.RESOLVED);
216
- });
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
- });
249
- });
@@ -1 +0,0 @@
1
- export { default } from './Plugin';