@nocobase/plugin-workflow 0.7.0-alpha.6 → 0.7.0-alpha.62
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/lib/actions/index.d.ts +3 -1
- package/lib/actions/index.js +33 -26
- package/{esm/actions/flow_nodes.d.ts → lib/actions/nodes.d.ts} +1 -0
- package/lib/actions/nodes.js +321 -0
- package/lib/actions/workflows.d.ts +3 -0
- package/lib/actions/workflows.js +271 -0
- package/lib/calculators/index.js +143 -91
- package/lib/collections/executions.js +40 -38
- package/lib/collections/flow_nodes.js +60 -72
- package/lib/collections/jobs.js +45 -47
- package/lib/collections/workflows.js +70 -63
- package/lib/constants.js +22 -17
- package/lib/index.js +71 -22
- package/lib/instructions/calculation.js +34 -29
- package/lib/instructions/condition.js +94 -87
- package/lib/instructions/create.d.ts +1 -1
- package/lib/instructions/create.js +43 -25
- package/lib/instructions/destroy.js +42 -25
- package/lib/instructions/index.js +46 -25
- package/lib/instructions/parallel.js +99 -84
- package/lib/instructions/prompt.js +21 -13
- package/lib/instructions/query.js +46 -25
- package/lib/instructions/update.d.ts +2 -1
- package/lib/instructions/update.js +44 -25
- package/lib/models/Execution.d.ts +3 -2
- package/lib/models/Execution.js +400 -241
- package/lib/models/FlowNode.js +18 -5
- package/lib/models/Job.js +18 -5
- package/lib/models/Workflow.d.ts +4 -5
- package/lib/models/Workflow.js +94 -72
- package/lib/server.d.ts +7 -2
- package/lib/server.js +114 -65
- package/lib/triggers/collection.d.ts +16 -0
- package/lib/triggers/collection.js +162 -0
- package/lib/triggers/index.d.ts +3 -6
- package/lib/triggers/index.js +14 -11
- package/package.json +7 -12
- package/esm/actions/flow_nodes.js +0 -139
- package/esm/actions/flow_nodes.js.map +0 -1
- package/esm/actions/index.d.ts +0 -1
- package/esm/actions/index.js +0 -8
- package/esm/actions/index.js.map +0 -1
- package/esm/calculators/index.d.ts +0 -38
- package/esm/calculators/index.js +0 -128
- package/esm/calculators/index.js.map +0 -1
- package/esm/collections/executions.d.ts +0 -3
- package/esm/collections/executions.js +0 -38
- package/esm/collections/executions.js.map +0 -1
- package/esm/collections/flow_nodes.d.ts +0 -3
- package/esm/collections/flow_nodes.js +0 -72
- package/esm/collections/flow_nodes.js.map +0 -1
- package/esm/collections/jobs.d.ts +0 -3
- package/esm/collections/jobs.js +0 -47
- package/esm/collections/jobs.js.map +0 -1
- package/esm/collections/workflows.d.ts +0 -3
- package/esm/collections/workflows.js +0 -63
- package/esm/collections/workflows.js.map +0 -1
- package/esm/constants.d.ts +0 -17
- package/esm/constants.js +0 -18
- package/esm/constants.js.map +0 -1
- package/esm/index.d.ts +0 -5
- package/esm/index.js +0 -6
- package/esm/index.js.map +0 -1
- package/esm/instructions/calculation.d.ts +0 -8
- package/esm/instructions/calculation.js +0 -55
- package/esm/instructions/calculation.js.map +0 -1
- package/esm/instructions/condition.d.ts +0 -5
- package/esm/instructions/condition.js +0 -99
- package/esm/instructions/condition.js.map +0 -1
- package/esm/instructions/create.d.ts +0 -8
- package/esm/instructions/create.js +0 -25
- package/esm/instructions/create.js.map +0 -1
- package/esm/instructions/destroy.d.ts +0 -8
- package/esm/instructions/destroy.js +0 -25
- package/esm/instructions/destroy.js.map +0 -1
- package/esm/instructions/index.d.ts +0 -15
- package/esm/instructions/index.js +0 -20
- package/esm/instructions/index.js.map +0 -1
- package/esm/instructions/parallel.d.ts +0 -13
- package/esm/instructions/parallel.js +0 -88
- package/esm/instructions/parallel.js.map +0 -1
- package/esm/instructions/prompt.d.ts +0 -7
- package/esm/instructions/prompt.js +0 -13
- package/esm/instructions/prompt.js.map +0 -1
- package/esm/instructions/query.d.ts +0 -8
- package/esm/instructions/query.js +0 -25
- package/esm/instructions/query.js.map +0 -1
- package/esm/instructions/update.d.ts +0 -8
- package/esm/instructions/update.js +0 -25
- package/esm/instructions/update.js.map +0 -1
- package/esm/models/Execution.d.ts +0 -50
- package/esm/models/Execution.js +0 -250
- package/esm/models/Execution.js.map +0 -1
- package/esm/models/FlowNode.d.ts +0 -17
- package/esm/models/FlowNode.js +0 -4
- package/esm/models/FlowNode.js.map +0 -1
- package/esm/models/Job.d.ts +0 -15
- package/esm/models/Job.js +0 -4
- package/esm/models/Job.js.map +0 -1
- package/esm/models/Workflow.d.ts +0 -27
- package/esm/models/Workflow.js +0 -72
- package/esm/models/Workflow.js.map +0 -1
- package/esm/server.d.ts +0 -5
- package/esm/server.js +0 -62
- package/esm/server.js.map +0 -1
- package/esm/triggers/index.d.ts +0 -9
- package/esm/triggers/index.js +0 -6
- package/esm/triggers/index.js.map +0 -1
- package/esm/triggers/model.d.ts +0 -12
- package/esm/triggers/model.js +0 -47
- package/esm/triggers/model.js.map +0 -1
- package/lib/actions/flow_nodes.d.ts +0 -3
- package/lib/actions/flow_nodes.js +0 -163
- package/lib/actions/flow_nodes.js.map +0 -1
- package/lib/actions/index.js.map +0 -1
- package/lib/calculators/index.js.map +0 -1
- package/lib/collections/executions.js.map +0 -1
- package/lib/collections/flow_nodes.js.map +0 -1
- package/lib/collections/jobs.js.map +0 -1
- package/lib/collections/workflows.js.map +0 -1
- package/lib/constants.js.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/instructions/calculation.js.map +0 -1
- package/lib/instructions/condition.js.map +0 -1
- package/lib/instructions/create.js.map +0 -1
- package/lib/instructions/destroy.js.map +0 -1
- package/lib/instructions/index.js.map +0 -1
- package/lib/instructions/parallel.js.map +0 -1
- package/lib/instructions/prompt.js.map +0 -1
- package/lib/instructions/query.js.map +0 -1
- package/lib/instructions/update.js.map +0 -1
- package/lib/models/Execution.js.map +0 -1
- package/lib/models/FlowNode.js.map +0 -1
- package/lib/models/Job.js.map +0 -1
- package/lib/models/Workflow.js.map +0 -1
- package/lib/server.js.map +0 -1
- package/lib/triggers/index.js.map +0 -1
- package/lib/triggers/model.d.ts +0 -12
- package/lib/triggers/model.js +0 -49
- package/lib/triggers/model.js.map +0 -1
- package/tsconfig.build.json +0 -9
package/lib/models/FlowNode.js
CHANGED
|
@@ -1,7 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
function _database() {
|
|
9
|
+
const data = require("@nocobase/database");
|
|
10
|
+
|
|
11
|
+
_database = function _database() {
|
|
12
|
+
return data;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
return data;
|
|
5
16
|
}
|
|
6
|
-
|
|
7
|
-
|
|
17
|
+
|
|
18
|
+
class FlowNodeModel extends _database().Model {}
|
|
19
|
+
|
|
20
|
+
exports.default = FlowNodeModel;
|
package/lib/models/Job.js
CHANGED
|
@@ -1,7 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
function _database() {
|
|
9
|
+
const data = require("@nocobase/database");
|
|
10
|
+
|
|
11
|
+
_database = function _database() {
|
|
12
|
+
return data;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
return data;
|
|
5
16
|
}
|
|
6
|
-
|
|
7
|
-
|
|
17
|
+
|
|
18
|
+
class JobModel extends _database().Model {}
|
|
19
|
+
|
|
20
|
+
exports.default = JobModel;
|
package/lib/models/Workflow.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Database, Model } from '@nocobase/database';
|
|
2
|
-
import { HasManyCreateAssociationMixin, HasManyGetAssociationsMixin } from 'sequelize';
|
|
2
|
+
import { HasManyCountAssociationsMixin, HasManyCreateAssociationMixin, HasManyGetAssociationsMixin } from 'sequelize';
|
|
3
3
|
import ExecutionModel from './Execution';
|
|
4
4
|
import FlowNodeModel from './FlowNode';
|
|
5
5
|
export default class WorkflowModel extends Model {
|
|
@@ -11,17 +11,16 @@ export default class WorkflowModel extends Model {
|
|
|
11
11
|
type: string;
|
|
12
12
|
config: any;
|
|
13
13
|
useTransaction: boolean;
|
|
14
|
+
executed: number;
|
|
14
15
|
createdAt: Date;
|
|
15
16
|
updatedAt: Date;
|
|
16
17
|
nodes: FlowNodeModel[];
|
|
17
18
|
getNodes: HasManyGetAssociationsMixin<FlowNodeModel>;
|
|
18
19
|
createNode: HasManyCreateAssociationMixin<FlowNodeModel>;
|
|
19
20
|
executions: ExecutionModel[];
|
|
21
|
+
countExecutions: HasManyCountAssociationsMixin;
|
|
20
22
|
getExecutions: HasManyGetAssociationsMixin<ExecutionModel>;
|
|
21
23
|
createExecution: HasManyCreateAssociationMixin<ExecutionModel>;
|
|
22
|
-
static mount(): Promise<void>;
|
|
23
|
-
getHookId(): string;
|
|
24
24
|
getTransaction(options: any): any;
|
|
25
|
-
|
|
26
|
-
trigger(context: Object, options: any): Promise<ExecutionModel>;
|
|
25
|
+
trigger: (context: Object, options?: {}) => Promise<ExecutionModel>;
|
|
27
26
|
}
|
package/lib/models/Workflow.js
CHANGED
|
@@ -1,78 +1,100 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
return
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
getTransaction(options) {
|
|
37
|
-
if (!this.useTransaction) {
|
|
38
|
-
return undefined;
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
function _database() {
|
|
9
|
+
const data = require("@nocobase/database");
|
|
10
|
+
|
|
11
|
+
_database = function _database() {
|
|
12
|
+
return data;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
return data;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
var _constants = require("../constants");
|
|
19
|
+
|
|
20
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
21
|
+
|
|
22
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
23
|
+
|
|
24
|
+
class WorkflowModel extends _database().Model {
|
|
25
|
+
constructor(...args) {
|
|
26
|
+
var _this;
|
|
27
|
+
|
|
28
|
+
super(...args);
|
|
29
|
+
_this = this;
|
|
30
|
+
|
|
31
|
+
this.trigger = /*#__PURE__*/function () {
|
|
32
|
+
var _ref = _asyncToGenerator(function* (context, options = {}) {
|
|
33
|
+
// `null` means not to trigger
|
|
34
|
+
if (context === null) {
|
|
35
|
+
return;
|
|
39
36
|
}
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
37
|
+
|
|
38
|
+
const transaction = yield _this.getTransaction(options);
|
|
39
|
+
|
|
40
|
+
if (_this.useTransaction) {
|
|
41
|
+
const existed = yield _this.countExecutions({
|
|
42
|
+
where: {
|
|
43
|
+
transaction: transaction.id
|
|
44
|
+
},
|
|
45
|
+
transaction
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
if (existed) {
|
|
49
|
+
console.warn(`workflow ${_this.id} has already been triggered in same execution (${transaction.id}), and newly triggering will be skipped.`);
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const execution = yield _this.createExecution({
|
|
55
|
+
context,
|
|
56
|
+
status: _constants.EXECUTION_STATUS.STARTED,
|
|
57
|
+
useTransaction: _this.useTransaction,
|
|
58
|
+
transaction: transaction.id
|
|
59
|
+
}, {
|
|
60
|
+
transaction
|
|
54
61
|
});
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
context,
|
|
65
|
-
status: constants_1.EXECUTION_STATUS.STARTED,
|
|
66
|
-
useTransaction: this.useTransaction
|
|
67
|
-
}, { transaction });
|
|
68
|
-
execution.workflow = this;
|
|
69
|
-
yield execution.start({ transaction });
|
|
70
|
-
if (transaction && (!options.transaction || options.transaction.finished)) {
|
|
71
|
-
yield transaction.commit();
|
|
72
|
-
}
|
|
73
|
-
return execution;
|
|
62
|
+
const executed = yield _this.countExecutions({
|
|
63
|
+
transaction
|
|
64
|
+
}); // NOTE: not to trigger afterUpdate hook here
|
|
65
|
+
|
|
66
|
+
yield _this.update({
|
|
67
|
+
executed
|
|
68
|
+
}, {
|
|
69
|
+
transaction,
|
|
70
|
+
hooks: false
|
|
74
71
|
});
|
|
72
|
+
execution.workflow = _this;
|
|
73
|
+
yield execution.start({
|
|
74
|
+
transaction
|
|
75
|
+
}); // @ts-ignore
|
|
76
|
+
|
|
77
|
+
if (transaction && (!options.transaction || options.transaction.finished)) {
|
|
78
|
+
yield transaction.commit();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return execution;
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
return function (_x) {
|
|
85
|
+
return _ref.apply(this, arguments);
|
|
86
|
+
};
|
|
87
|
+
}();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
getTransaction(options) {
|
|
91
|
+
if (!this.useTransaction) {
|
|
92
|
+
return null;
|
|
75
93
|
}
|
|
94
|
+
|
|
95
|
+
return options.transaction && !options.transaction.finished ? options.transaction : this.constructor.database.sequelize.transaction();
|
|
96
|
+
}
|
|
97
|
+
|
|
76
98
|
}
|
|
77
|
-
|
|
78
|
-
|
|
99
|
+
|
|
100
|
+
exports.default = WorkflowModel;
|
package/lib/server.d.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { Plugin } from '@nocobase/server';
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
import WorkflowModel from './models/Workflow';
|
|
3
|
+
import { Trigger } from './triggers';
|
|
4
|
+
import { Registry } from '@nocobase/utils';
|
|
5
|
+
export default class extends Plugin {
|
|
6
|
+
triggers: Registry<Trigger>;
|
|
4
7
|
getName(): string;
|
|
8
|
+
load(options?: {}): Promise<void>;
|
|
9
|
+
toggle(workflow: WorkflowModel, enable?: boolean): Promise<void>;
|
|
5
10
|
}
|
package/lib/server.js
CHANGED
|
@@ -1,68 +1,117 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
return
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
function _path() {
|
|
9
|
+
const data = _interopRequireDefault(require("path"));
|
|
10
|
+
|
|
11
|
+
_path = function _path() {
|
|
12
|
+
return data;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
return data;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function _server() {
|
|
19
|
+
const data = require("@nocobase/server");
|
|
20
|
+
|
|
21
|
+
_server = function _server() {
|
|
22
|
+
return data;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
return data;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
var _Workflow = _interopRequireDefault(require("./models/Workflow"));
|
|
29
|
+
|
|
30
|
+
var _Execution = _interopRequireDefault(require("./models/Execution"));
|
|
31
|
+
|
|
32
|
+
var _actions = _interopRequireDefault(require("./actions"));
|
|
33
|
+
|
|
34
|
+
var _triggers = _interopRequireDefault(require("./triggers"));
|
|
35
|
+
|
|
36
|
+
function _utils() {
|
|
37
|
+
const data = require("@nocobase/utils");
|
|
38
|
+
|
|
39
|
+
_utils = function _utils() {
|
|
40
|
+
return data;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
return data;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
47
|
+
|
|
48
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
49
|
+
|
|
50
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
|
|
51
|
+
|
|
52
|
+
class _default extends _server().Plugin {
|
|
53
|
+
constructor(...args) {
|
|
54
|
+
super(...args);
|
|
55
|
+
this.triggers = new (_utils().Registry)();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
getName() {
|
|
59
|
+
return this.getPackageName(__dirname);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
load(options = {}) {
|
|
63
|
+
var _this = this;
|
|
64
|
+
|
|
65
|
+
return _asyncToGenerator(function* () {
|
|
66
|
+
const db = _this.app.db;
|
|
67
|
+
db.registerModels({
|
|
68
|
+
WorkflowModel: _Workflow.default,
|
|
69
|
+
ExecutionModel: _Execution.default
|
|
70
|
+
});
|
|
71
|
+
yield db.import({
|
|
72
|
+
directory: _path().default.resolve(__dirname, 'collections')
|
|
73
|
+
});
|
|
74
|
+
(0, _actions.default)(_this);
|
|
75
|
+
(0, _triggers.default)(_this); // [Life Cycle]:
|
|
76
|
+
// * load all workflows in db
|
|
77
|
+
// * add all hooks for enabled workflows
|
|
78
|
+
// * add hooks for create/update[enabled]/delete workflow to add/remove specific hooks
|
|
79
|
+
|
|
80
|
+
_this.app.on('beforeStart', /*#__PURE__*/_asyncToGenerator(function* () {
|
|
81
|
+
const collection = db.getCollection('workflows');
|
|
82
|
+
const workflows = yield collection.repository.find({
|
|
83
|
+
filter: {
|
|
84
|
+
enabled: true
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
workflows.forEach(workflow => {
|
|
88
|
+
_this.toggle(workflow);
|
|
61
89
|
});
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
90
|
+
db.on('workflows.afterCreate', model => _this.toggle(model));
|
|
91
|
+
db.on('workflows.afterUpdate', model => _this.toggle(model));
|
|
92
|
+
db.on('workflows.afterDestroy', model => _this.toggle(model, false));
|
|
93
|
+
})); // [Life Cycle]: initialize all necessary seed data
|
|
94
|
+
// this.app.on('db.init', async () => {});
|
|
95
|
+
|
|
96
|
+
})();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
toggle(workflow, enable) {
|
|
100
|
+
var _this2 = this;
|
|
101
|
+
|
|
102
|
+
return _asyncToGenerator(function* () {
|
|
103
|
+
const type = workflow.get('type');
|
|
104
|
+
|
|
105
|
+
const trigger = _this2.triggers.get(type);
|
|
106
|
+
|
|
107
|
+
if (typeof enable !== 'undefined' ? enable : workflow.get('enabled')) {
|
|
108
|
+
yield trigger.on(workflow);
|
|
109
|
+
} else {
|
|
110
|
+
yield trigger.off(workflow);
|
|
111
|
+
}
|
|
112
|
+
})();
|
|
113
|
+
}
|
|
114
|
+
|
|
66
115
|
}
|
|
67
|
-
|
|
68
|
-
|
|
116
|
+
|
|
117
|
+
exports.default = _default;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Trigger } from ".";
|
|
2
|
+
import WorkflowModel from "../models/Workflow";
|
|
3
|
+
export interface CollectionChangeTriggerConfig {
|
|
4
|
+
collection: string;
|
|
5
|
+
mode: number;
|
|
6
|
+
condition: any;
|
|
7
|
+
}
|
|
8
|
+
export default class CollectionTrigger implements Trigger {
|
|
9
|
+
db: any;
|
|
10
|
+
events: Map<any, any>;
|
|
11
|
+
constructor({ app }: {
|
|
12
|
+
app: any;
|
|
13
|
+
});
|
|
14
|
+
on(workflow: WorkflowModel): void;
|
|
15
|
+
off(workflow: WorkflowModel): void;
|
|
16
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
9
|
+
|
|
10
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
11
|
+
|
|
12
|
+
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
13
|
+
|
|
14
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
15
|
+
|
|
16
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e2) { throw _e2; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e3) { didErr = true; err = _e3; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
17
|
+
|
|
18
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
19
|
+
|
|
20
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
21
|
+
|
|
22
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
23
|
+
|
|
24
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
25
|
+
|
|
26
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
27
|
+
|
|
28
|
+
const MODE_BITMAP = {
|
|
29
|
+
CREATE: 1,
|
|
30
|
+
UPDATE: 2,
|
|
31
|
+
DESTROY: 4
|
|
32
|
+
};
|
|
33
|
+
const MODE_BITMAP_EVENTS = new Map();
|
|
34
|
+
MODE_BITMAP_EVENTS.set(MODE_BITMAP.CREATE, 'afterCreateWithAssociations');
|
|
35
|
+
MODE_BITMAP_EVENTS.set(MODE_BITMAP.UPDATE, 'afterUpdateWithAssociations');
|
|
36
|
+
MODE_BITMAP_EVENTS.set(MODE_BITMAP.DESTROY, 'afterDestroy');
|
|
37
|
+
|
|
38
|
+
function getHookId(workflow, type) {
|
|
39
|
+
return `${type}#${workflow.id}`;
|
|
40
|
+
} // async function, should return promise
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
function handler(data, options) {
|
|
44
|
+
const _this$config = this.config,
|
|
45
|
+
condition = _this$config.condition,
|
|
46
|
+
changed = _this$config.changed; // NOTE: if no configured fields changed, do not trigger
|
|
47
|
+
|
|
48
|
+
if (changed && changed.length && changed.every(name => !data.changed(name))) {
|
|
49
|
+
return;
|
|
50
|
+
} // NOTE: if no configured condition match, do not trigger
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
if (condition && condition.$and.length) {// TODO: check all conditions in condition against data
|
|
54
|
+
// const calculation = toCalculation(condition);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return this.trigger({
|
|
58
|
+
data: data.get()
|
|
59
|
+
}, options);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
class CollectionTrigger {
|
|
63
|
+
constructor({
|
|
64
|
+
app
|
|
65
|
+
}) {
|
|
66
|
+
this.db = void 0;
|
|
67
|
+
this.events = new Map();
|
|
68
|
+
this.db = app.db;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
on(workflow) {
|
|
72
|
+
// NOTE: remove previous listener if config updated
|
|
73
|
+
const prev = workflow.previous();
|
|
74
|
+
|
|
75
|
+
if (prev.config) {
|
|
76
|
+
this.off(_objectSpread(_objectSpread({}, workflow.get()), prev));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const _workflow$config = workflow.config,
|
|
80
|
+
collection = _workflow$config.collection,
|
|
81
|
+
mode = _workflow$config.mode;
|
|
82
|
+
const Collection = this.db.getCollection(collection);
|
|
83
|
+
|
|
84
|
+
if (!Collection) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
var _iterator = _createForOfIteratorHelper(MODE_BITMAP_EVENTS.entries()),
|
|
89
|
+
_step;
|
|
90
|
+
|
|
91
|
+
try {
|
|
92
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
93
|
+
let _step$value = _slicedToArray(_step.value, 2),
|
|
94
|
+
key = _step$value[0],
|
|
95
|
+
type = _step$value[1];
|
|
96
|
+
|
|
97
|
+
const event = `${collection}.${type}`;
|
|
98
|
+
const name = getHookId(workflow, event);
|
|
99
|
+
|
|
100
|
+
if (mode & key) {
|
|
101
|
+
if (!this.events.has(name)) {
|
|
102
|
+
const listener = handler.bind(workflow);
|
|
103
|
+
this.events.set(name, listener);
|
|
104
|
+
this.db.on(event, listener);
|
|
105
|
+
}
|
|
106
|
+
} else {
|
|
107
|
+
const listener = this.events.get(name);
|
|
108
|
+
|
|
109
|
+
if (listener) {
|
|
110
|
+
this.db.off(event, listener);
|
|
111
|
+
this.events.delete(name);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
} catch (err) {
|
|
116
|
+
_iterator.e(err);
|
|
117
|
+
} finally {
|
|
118
|
+
_iterator.f();
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
off(workflow) {
|
|
123
|
+
const _workflow$config2 = workflow.config,
|
|
124
|
+
collection = _workflow$config2.collection,
|
|
125
|
+
mode = _workflow$config2.mode;
|
|
126
|
+
const Collection = this.db.getCollection(collection);
|
|
127
|
+
|
|
128
|
+
if (!Collection) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
var _iterator2 = _createForOfIteratorHelper(MODE_BITMAP_EVENTS.entries()),
|
|
133
|
+
_step2;
|
|
134
|
+
|
|
135
|
+
try {
|
|
136
|
+
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
137
|
+
let _step2$value = _slicedToArray(_step2.value, 2),
|
|
138
|
+
key = _step2$value[0],
|
|
139
|
+
type = _step2$value[1];
|
|
140
|
+
|
|
141
|
+
const event = `${collection}.${type}`;
|
|
142
|
+
const name = getHookId(workflow, event);
|
|
143
|
+
|
|
144
|
+
if (mode & key) {
|
|
145
|
+
const listener = this.events.get(name);
|
|
146
|
+
|
|
147
|
+
if (listener) {
|
|
148
|
+
this.db.off(event, listener);
|
|
149
|
+
this.events.delete(name);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
} catch (err) {
|
|
154
|
+
_iterator2.e(err);
|
|
155
|
+
} finally {
|
|
156
|
+
_iterator2.f();
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
exports.default = CollectionTrigger;
|
package/lib/triggers/index.d.ts
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import { Registry } from '@nocobase/utils';
|
|
2
1
|
import WorkflowModel from '../models/Workflow';
|
|
3
2
|
export interface Trigger {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
off(this: WorkflowModel): void;
|
|
3
|
+
on(workflow: WorkflowModel): void;
|
|
4
|
+
off(workflow: WorkflowModel): void;
|
|
7
5
|
}
|
|
8
|
-
export
|
|
9
|
-
export default triggers;
|
|
6
|
+
export default function (plugin: any): void;
|