@nocobase/plugin-workflow 0.7.0-alpha.9 → 0.7.1-alpha.6

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.
Files changed (159) hide show
  1. package/lib/Plugin.d.ts +18 -0
  2. package/lib/Plugin.js +285 -0
  3. package/lib/Processor.d.ts +40 -0
  4. package/lib/Processor.js +440 -0
  5. package/lib/actions/index.d.ts +3 -1
  6. package/lib/actions/index.js +33 -26
  7. package/{esm/actions/flow_nodes.d.ts → lib/actions/nodes.d.ts} +1 -0
  8. package/lib/actions/nodes.js +321 -0
  9. package/lib/actions/workflows.d.ts +2 -0
  10. package/lib/actions/workflows.js +197 -0
  11. package/lib/calculators/index.d.ts +2 -2
  12. package/lib/calculators/index.js +144 -92
  13. package/lib/collections/executions.js +32 -38
  14. package/lib/collections/flow_nodes.js +60 -72
  15. package/lib/collections/jobs.js +26 -47
  16. package/lib/collections/workflows.d.ts +1 -2
  17. package/lib/collections/workflows.js +70 -63
  18. package/lib/constants.js +22 -17
  19. package/lib/index.d.ts +4 -3
  20. package/lib/index.js +85 -22
  21. package/lib/instructions/calculation.d.ts +1 -1
  22. package/lib/instructions/calculation.js +32 -29
  23. package/lib/instructions/condition.d.ts +2 -2
  24. package/lib/instructions/condition.js +91 -88
  25. package/lib/instructions/create.d.ts +2 -2
  26. package/lib/instructions/create.js +39 -25
  27. package/lib/instructions/delay.d.ts +14 -0
  28. package/lib/instructions/delay.js +138 -0
  29. package/lib/instructions/destroy.d.ts +1 -1
  30. package/lib/instructions/destroy.js +38 -25
  31. package/lib/instructions/index.d.ts +11 -8
  32. package/lib/instructions/index.js +76 -25
  33. package/lib/instructions/parallel.d.ts +3 -3
  34. package/lib/instructions/parallel.js +95 -84
  35. package/lib/instructions/prompt.d.ts +2 -2
  36. package/lib/instructions/prompt.js +21 -13
  37. package/lib/instructions/query.d.ts +2 -1
  38. package/lib/instructions/query.js +42 -25
  39. package/lib/instructions/update.d.ts +2 -1
  40. package/lib/instructions/update.js +40 -25
  41. package/lib/models/Execution.d.ts +3 -33
  42. package/lib/models/Execution.js +18 -254
  43. package/lib/models/FlowNode.js +18 -5
  44. package/lib/models/Job.js +18 -5
  45. package/lib/models/Workflow.d.ts +5 -6
  46. package/lib/models/Workflow.js +18 -76
  47. package/lib/triggers/collection.d.ts +12 -0
  48. package/lib/triggers/collection.js +184 -0
  49. package/lib/triggers/index.d.ts +11 -7
  50. package/lib/triggers/index.js +64 -11
  51. package/lib/triggers/schedule.d.ts +38 -0
  52. package/lib/triggers/schedule.js +672 -0
  53. package/package.json +8 -12
  54. package/esm/actions/flow_nodes.js +0 -139
  55. package/esm/actions/flow_nodes.js.map +0 -1
  56. package/esm/actions/index.d.ts +0 -1
  57. package/esm/actions/index.js +0 -8
  58. package/esm/actions/index.js.map +0 -1
  59. package/esm/calculators/index.d.ts +0 -38
  60. package/esm/calculators/index.js +0 -128
  61. package/esm/calculators/index.js.map +0 -1
  62. package/esm/collections/executions.d.ts +0 -3
  63. package/esm/collections/executions.js +0 -38
  64. package/esm/collections/executions.js.map +0 -1
  65. package/esm/collections/flow_nodes.d.ts +0 -3
  66. package/esm/collections/flow_nodes.js +0 -72
  67. package/esm/collections/flow_nodes.js.map +0 -1
  68. package/esm/collections/jobs.d.ts +0 -3
  69. package/esm/collections/jobs.js +0 -47
  70. package/esm/collections/jobs.js.map +0 -1
  71. package/esm/collections/workflows.d.ts +0 -3
  72. package/esm/collections/workflows.js +0 -63
  73. package/esm/collections/workflows.js.map +0 -1
  74. package/esm/constants.d.ts +0 -17
  75. package/esm/constants.js +0 -18
  76. package/esm/constants.js.map +0 -1
  77. package/esm/index.d.ts +0 -5
  78. package/esm/index.js +0 -6
  79. package/esm/index.js.map +0 -1
  80. package/esm/instructions/calculation.d.ts +0 -8
  81. package/esm/instructions/calculation.js +0 -55
  82. package/esm/instructions/calculation.js.map +0 -1
  83. package/esm/instructions/condition.d.ts +0 -5
  84. package/esm/instructions/condition.js +0 -99
  85. package/esm/instructions/condition.js.map +0 -1
  86. package/esm/instructions/create.d.ts +0 -8
  87. package/esm/instructions/create.js +0 -25
  88. package/esm/instructions/create.js.map +0 -1
  89. package/esm/instructions/destroy.d.ts +0 -8
  90. package/esm/instructions/destroy.js +0 -25
  91. package/esm/instructions/destroy.js.map +0 -1
  92. package/esm/instructions/index.d.ts +0 -15
  93. package/esm/instructions/index.js +0 -20
  94. package/esm/instructions/index.js.map +0 -1
  95. package/esm/instructions/parallel.d.ts +0 -13
  96. package/esm/instructions/parallel.js +0 -88
  97. package/esm/instructions/parallel.js.map +0 -1
  98. package/esm/instructions/prompt.d.ts +0 -7
  99. package/esm/instructions/prompt.js +0 -13
  100. package/esm/instructions/prompt.js.map +0 -1
  101. package/esm/instructions/query.d.ts +0 -8
  102. package/esm/instructions/query.js +0 -25
  103. package/esm/instructions/query.js.map +0 -1
  104. package/esm/instructions/update.d.ts +0 -8
  105. package/esm/instructions/update.js +0 -25
  106. package/esm/instructions/update.js.map +0 -1
  107. package/esm/models/Execution.d.ts +0 -50
  108. package/esm/models/Execution.js +0 -250
  109. package/esm/models/Execution.js.map +0 -1
  110. package/esm/models/FlowNode.d.ts +0 -17
  111. package/esm/models/FlowNode.js +0 -4
  112. package/esm/models/FlowNode.js.map +0 -1
  113. package/esm/models/Job.d.ts +0 -15
  114. package/esm/models/Job.js +0 -4
  115. package/esm/models/Job.js.map +0 -1
  116. package/esm/models/Workflow.d.ts +0 -27
  117. package/esm/models/Workflow.js +0 -72
  118. package/esm/models/Workflow.js.map +0 -1
  119. package/esm/server.d.ts +0 -5
  120. package/esm/server.js +0 -62
  121. package/esm/server.js.map +0 -1
  122. package/esm/triggers/index.d.ts +0 -9
  123. package/esm/triggers/index.js +0 -6
  124. package/esm/triggers/index.js.map +0 -1
  125. package/esm/triggers/model.d.ts +0 -12
  126. package/esm/triggers/model.js +0 -47
  127. package/esm/triggers/model.js.map +0 -1
  128. package/lib/actions/flow_nodes.d.ts +0 -3
  129. package/lib/actions/flow_nodes.js +0 -163
  130. package/lib/actions/flow_nodes.js.map +0 -1
  131. package/lib/actions/index.js.map +0 -1
  132. package/lib/calculators/index.js.map +0 -1
  133. package/lib/collections/executions.js.map +0 -1
  134. package/lib/collections/flow_nodes.js.map +0 -1
  135. package/lib/collections/jobs.js.map +0 -1
  136. package/lib/collections/workflows.js.map +0 -1
  137. package/lib/constants.js.map +0 -1
  138. package/lib/index.js.map +0 -1
  139. package/lib/instructions/calculation.js.map +0 -1
  140. package/lib/instructions/condition.js.map +0 -1
  141. package/lib/instructions/create.js.map +0 -1
  142. package/lib/instructions/destroy.js.map +0 -1
  143. package/lib/instructions/index.js.map +0 -1
  144. package/lib/instructions/parallel.js.map +0 -1
  145. package/lib/instructions/prompt.js.map +0 -1
  146. package/lib/instructions/query.js.map +0 -1
  147. package/lib/instructions/update.js.map +0 -1
  148. package/lib/models/Execution.js.map +0 -1
  149. package/lib/models/FlowNode.js.map +0 -1
  150. package/lib/models/Job.js.map +0 -1
  151. package/lib/models/Workflow.js.map +0 -1
  152. package/lib/server.d.ts +0 -5
  153. package/lib/server.js +0 -68
  154. package/lib/server.js.map +0 -1
  155. package/lib/triggers/index.js.map +0 -1
  156. package/lib/triggers/model.d.ts +0 -12
  157. package/lib/triggers/model.js +0 -49
  158. package/lib/triggers/model.js.map +0 -1
  159. package/tsconfig.build.json +0 -9
@@ -1,15 +1,23 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const constants_1 = require("../constants");
4
- exports.default = {
5
- run(input, execution) {
6
- return {
7
- status: constants_1.JOB_STATUS.PENDING
8
- };
9
- },
10
- resume(job, execution) {
11
- job.set('status', constants_1.JOB_STATUS.RESOLVED);
12
- return job;
13
- }
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _constants = require("../constants");
9
+
10
+ var _default = {
11
+ run(node, input, processor) {
12
+ return {
13
+ status: _constants.JOB_STATUS.PENDING
14
+ };
15
+ },
16
+
17
+ resume(node, job, processor) {
18
+ job.set('status', _constants.JOB_STATUS.RESOLVED);
19
+ return job;
20
+ }
21
+
14
22
  };
15
- //# sourceMappingURL=prompt.js.map
23
+ exports.default = _default;
@@ -1,6 +1,7 @@
1
1
  import FlowNodeModel from "../models/FlowNode";
2
+ import Processor from "../Processor";
2
3
  declare const _default: {
3
- run(this: FlowNodeModel, input: any, execution: any): Promise<{
4
+ run(node: FlowNodeModel, input: any, processor: Processor): Promise<{
4
5
  result: any;
5
6
  status: number;
6
7
  }>;
@@ -1,27 +1,44 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _constants = require("../constants");
9
+
10
+ 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; }
11
+
12
+ 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; }
13
+
14
+ 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; }
15
+
16
+ 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); } }
17
+
18
+ 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); }); }; }
19
+
20
+ var _default = {
21
+ run(node, input, processor) {
22
+ return _asyncToGenerator(function* () {
23
+ const _node$config = node.config,
24
+ collection = _node$config.collection,
25
+ multiple = _node$config.multiple,
26
+ _node$config$params = _node$config.params,
27
+ params = _node$config$params === void 0 ? {} : _node$config$params;
28
+ const repo = node.constructor.database.getRepository(collection);
29
+ const options = processor.getParsedValue(params);
30
+ const result = yield (multiple ? repo.find : repo.findOne).call(repo, _objectSpread(_objectSpread({}, options), {}, {
31
+ transaction: processor.transaction
32
+ })); // NOTE: `toJSON()` to avoid getting undefined value from Proxied model instance (#380)
33
+ // e.g. Object.prototype.hasOwnProperty.call(result, 'id') // false
34
+ // so the properties can not be get by json-templates(object-path)
35
+
36
+ return {
37
+ result: multiple ? result.map(item => item.toJSON()) : result === null || result === void 0 ? void 0 : result.toJSON(),
38
+ status: _constants.JOB_STATUS.RESOLVED
39
+ };
40
+ })();
41
+ }
42
+
10
43
  };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const constants_1 = require("../constants");
13
- exports.default = {
14
- run(input, execution) {
15
- return __awaiter(this, void 0, void 0, function* () {
16
- const { collection, multiple, params = {} } = this.config;
17
- const repo = this.constructor.database.getRepository(collection);
18
- const options = execution.getParsedValue(params);
19
- const result = yield (multiple ? repo.find : repo.findOne).call(repo, Object.assign(Object.assign({}, options), { transaction: execution.transaction }));
20
- return {
21
- result,
22
- status: constants_1.JOB_STATUS.RESOLVED
23
- };
24
- });
25
- }
26
- };
27
- //# sourceMappingURL=query.js.map
44
+ exports.default = _default;
@@ -1,6 +1,7 @@
1
1
  import FlowNodeModel from "../models/FlowNode";
2
+ import Processor from "../Processor";
2
3
  declare const _default: {
3
- run(this: FlowNodeModel, input: any, execution: any): Promise<{
4
+ run(node: FlowNodeModel, input: any, processor: Processor): Promise<{
4
5
  result: import("@nocobase/database").Model<any, any> | import("@nocobase/database").Model<any, any>[];
5
6
  status: number;
6
7
  }>;
@@ -1,27 +1,42 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _constants = require("../constants");
9
+
10
+ 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; }
11
+
12
+ 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; }
13
+
14
+ 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; }
15
+
16
+ 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); } }
17
+
18
+ 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); }); }; }
19
+
20
+ var _default = {
21
+ run(node, input, processor) {
22
+ return _asyncToGenerator(function* () {
23
+ const _node$config = node.config,
24
+ collection = _node$config.collection,
25
+ _node$config$multiple = _node$config.multiple,
26
+ multiple = _node$config$multiple === void 0 ? false : _node$config$multiple,
27
+ _node$config$params = _node$config.params,
28
+ params = _node$config$params === void 0 ? {} : _node$config$params;
29
+ const repo = node.constructor.database.getRepository(collection);
30
+ const options = processor.getParsedValue(params);
31
+ const result = yield repo.update(_objectSpread(_objectSpread({}, options), {}, {
32
+ transaction: processor.transaction
33
+ }));
34
+ return {
35
+ result: multiple ? result : result[0] || null,
36
+ status: _constants.JOB_STATUS.RESOLVED
37
+ };
38
+ })();
39
+ }
40
+
10
41
  };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const constants_1 = require("../constants");
13
- exports.default = {
14
- run(input, execution) {
15
- return __awaiter(this, void 0, void 0, function* () {
16
- const { collection, multiple = false, params = {} } = this.config;
17
- const repo = this.constructor.database.getRepository(collection);
18
- const options = execution.getParsedValue(params);
19
- const result = yield repo.update(Object.assign(Object.assign({}, options), { transaction: execution.transaction }));
20
- return {
21
- result: multiple ? result : (result[0] || null),
22
- status: constants_1.JOB_STATUS.RESOLVED
23
- };
24
- });
25
- }
26
- };
27
- //# sourceMappingURL=update.js.map
42
+ exports.default = _default;
@@ -1,11 +1,7 @@
1
1
  import { Database, Model } from '@nocobase/database';
2
- import { BelongsToGetAssociationMixin, HasManyGetAssociationsMixin, Transaction } from 'sequelize';
2
+ import { BelongsToGetAssociationMixin, HasManyGetAssociationsMixin } from 'sequelize';
3
3
  import WorkflowModel from './Workflow';
4
- import FlowNodeModel from './FlowNode';
5
4
  import JobModel from './Job';
6
- export interface ExecutionOptions {
7
- transaction?: Transaction;
8
- }
9
5
  export default class ExecutionModel extends Model {
10
6
  static readonly database: Database;
11
7
  id: number;
@@ -13,38 +9,12 @@ export default class ExecutionModel extends Model {
13
9
  context: any;
14
10
  status: number;
15
11
  useTransaction: boolean;
12
+ transaction: string;
16
13
  createdAt: Date;
17
14
  updatedAt: Date;
15
+ key: string;
18
16
  workflow?: WorkflowModel;
19
17
  getWorkflow: BelongsToGetAssociationMixin<WorkflowModel>;
20
18
  jobs?: JobModel[];
21
19
  getJobs: HasManyGetAssociationsMixin<JobModel>;
22
- options: ExecutionOptions;
23
- transaction: Transaction;
24
- nodes: Array<FlowNodeModel>;
25
- nodesMap: Map<number, FlowNodeModel>;
26
- jobsMap: Map<number, JobModel>;
27
- jobsMapByNodeId: {
28
- [key: number]: any;
29
- };
30
- static StatusMap: {
31
- [x: number]: number;
32
- };
33
- makeNodes(nodes?: any[]): void;
34
- makeJobs(jobs: Array<JobModel>): void;
35
- getTransaction(): Transaction | Promise<Transaction>;
36
- prepare(options: any, commit?: boolean): Promise<void>;
37
- start(options: ExecutionOptions): Promise<void>;
38
- resume(job: JobModel, options: ExecutionOptions): Promise<void>;
39
- private commit;
40
- private exec;
41
- run(node: any, input?: any): any;
42
- end(node: any, job: any): any;
43
- recall(node: any, job: any): any;
44
- exit(job: JobModel | null): Promise<any>;
45
- saveJob(payload: any): Promise<JobModel>;
46
- findBranchStartNode(node: FlowNodeModel): FlowNodeModel | null;
47
- findBranchParentNode(node: FlowNodeModel): FlowNodeModel | null;
48
- findBranchParentJob(job: JobModel, node: FlowNodeModel): JobModel | null;
49
- getParsedValue(value: any, node?: any): any;
50
20
  }
@@ -1,256 +1,20 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- const database_1 = require("@nocobase/database");
16
- const json_templates_1 = __importDefault(require("json-templates"));
17
- const constants_1 = require("../constants");
18
- const instructions_1 = __importDefault(require("../instructions"));
19
- const calculators_1 = __importDefault(require("../calculators"));
20
- class ExecutionModel extends database_1.Model {
21
- constructor() {
22
- super(...arguments);
23
- this.nodes = [];
24
- this.nodesMap = new Map();
25
- this.jobsMap = new Map();
26
- this.jobsMapByNodeId = {};
27
- }
28
- // make dual linked nodes list then cache
29
- makeNodes(nodes = []) {
30
- this.nodes = nodes;
31
- nodes.forEach((node) => {
32
- this.nodesMap.set(node.id, node);
33
- });
34
- nodes.forEach((node) => {
35
- if (node.upstreamId) {
36
- node.upstream = this.nodesMap.get(node.upstreamId);
37
- }
38
- if (node.downstreamId) {
39
- node.downstream = this.nodesMap.get(node.downstreamId);
40
- }
41
- });
42
- }
43
- makeJobs(jobs) {
44
- jobs.forEach((job) => {
45
- this.jobsMap.set(job.id, job);
46
- // TODO: should consider cycle, and from previous job
47
- this.jobsMapByNodeId[job.nodeId] = job.result;
48
- });
49
- }
50
- getTransaction() {
51
- const { sequelize } = this.constructor.database;
52
- // @ts-ignore
53
- if (!this.useTransaction) {
54
- return undefined;
55
- }
56
- const { options } = this;
57
- // @ts-ignore
58
- return options.transaction && !options.transaction.finished
59
- ? options.transaction
60
- : sequelize.transaction();
61
- }
62
- prepare(options, commit = false) {
63
- return __awaiter(this, void 0, void 0, function* () {
64
- this.options = options || {};
65
- // @ts-ignore
66
- const transaction = yield this.getTransaction();
67
- this.transaction = transaction;
68
- if (!this.workflow) {
69
- this.workflow = yield this.getWorkflow({ transaction });
70
- }
71
- const nodes = yield this.workflow.getNodes({ transaction });
72
- this.makeNodes(nodes);
73
- const jobs = yield this.getJobs({
74
- order: [['id', 'ASC']],
75
- transaction,
76
- });
77
- this.makeJobs(jobs);
78
- if (commit) {
79
- yield this.commit();
80
- }
81
- });
82
- }
83
- start(options) {
84
- return __awaiter(this, void 0, void 0, function* () {
85
- if (this.status !== constants_1.EXECUTION_STATUS.STARTED) {
86
- throw new Error(`execution was ended with status ${this.status}`);
87
- }
88
- yield this.prepare(options);
89
- if (this.nodes.length) {
90
- const head = this.nodes.find(item => !item.upstream);
91
- yield this.run(head, { result: this.context });
92
- }
93
- else {
94
- yield this.exit(null);
95
- }
96
- yield this.commit();
97
- });
98
- }
99
- resume(job, options) {
100
- return __awaiter(this, void 0, void 0, function* () {
101
- if (this.status !== constants_1.EXECUTION_STATUS.STARTED) {
102
- throw new Error(`execution was ended with status ${this.status}`);
103
- }
104
- yield this.prepare(options);
105
- const node = this.nodesMap.get(job.nodeId);
106
- yield this.recall(node, job);
107
- yield this.commit();
108
- });
109
- }
110
- commit() {
111
- return __awaiter(this, void 0, void 0, function* () {
112
- // @ts-ignore
113
- if (this.transaction && (!this.options.transaction || this.options.transaction.finished)) {
114
- yield this.transaction.commit();
115
- }
116
- });
117
- }
118
- exec(instruction, node, prevJob) {
119
- return __awaiter(this, void 0, void 0, function* () {
120
- let job;
121
- try {
122
- // call instruction to get result and status
123
- job = yield instruction.call(node, prevJob, this);
124
- if (!job) {
125
- return null;
126
- }
127
- }
128
- catch (err) {
129
- // for uncaught error, set to rejected
130
- job = {
131
- result: err instanceof Error
132
- ? { message: err.message, stack: process.env.NODE_ENV === 'production' ? [] : err.stack }
133
- : err,
134
- status: constants_1.JOB_STATUS.REJECTED,
135
- };
136
- // if previous job is from resuming
137
- if (prevJob && prevJob.nodeId === node.id) {
138
- prevJob.set(job);
139
- job = prevJob;
140
- }
141
- }
142
- let savedJob;
143
- // TODO(optimize): many checking of resuming or new could be improved
144
- // could be implemented separately in exec() / resume()
145
- if (job instanceof database_1.Model) {
146
- savedJob = (yield job.save({ transaction: this.transaction }));
147
- }
148
- else {
149
- const upstreamId = prevJob instanceof database_1.Model ? prevJob.get('id') : null;
150
- savedJob = yield this.saveJob(Object.assign({ nodeId: node.id, upstreamId }, job));
151
- }
152
- if (savedJob.status === constants_1.JOB_STATUS.RESOLVED && node.downstream) {
153
- // run next node
154
- return this.run(node.downstream, savedJob);
155
- }
156
- // all nodes in scope have been executed
157
- return this.end(node, savedJob);
158
- });
159
- }
160
- run(node, input) {
161
- return __awaiter(this, void 0, void 0, function* () {
162
- const { run } = instructions_1.default.get(node.type);
163
- if (typeof run !== 'function') {
164
- return Promise.reject(new Error('`run` should be implemented for customized execution of the node'));
165
- }
166
- return this.exec(run, node, input);
167
- });
168
- }
169
- // parent node should take over the control
170
- end(node, job) {
171
- const parentNode = this.findBranchParentNode(node);
172
- // no parent, means on main flow
173
- if (parentNode) {
174
- return this.recall(parentNode, job);
175
- }
176
- // really done for all nodes
177
- // * should mark execution as done with last job status
178
- return this.exit(job);
179
- }
180
- recall(node, job) {
181
- return __awaiter(this, void 0, void 0, function* () {
182
- const { resume } = instructions_1.default.get(node.type);
183
- if (typeof resume !== 'function') {
184
- return Promise.reject(new Error('`resume` should be implemented because the node made branch'));
185
- }
186
- return this.exec(resume, node, job);
187
- });
188
- }
189
- exit(job) {
190
- return __awaiter(this, void 0, void 0, function* () {
191
- const status = job ? ExecutionModel.StatusMap[job.status] : constants_1.EXECUTION_STATUS.RESOLVED;
192
- yield this.update({ status }, { transaction: this.transaction });
193
- return null;
194
- });
195
- }
196
- // TODO(optimize)
197
- saveJob(payload) {
198
- return __awaiter(this, void 0, void 0, function* () {
199
- const { database } = this.constructor;
200
- const { model } = database.getCollection('jobs');
201
- const [job] = (yield model.upsert(Object.assign(Object.assign({}, payload), { executionId: this.id }), { transaction: this.transaction }));
202
- this.jobsMap.set(job.id, job);
203
- this.jobsMapByNodeId[job.nodeId] = job.result;
204
- return job;
205
- });
206
- }
207
- // find the first node in current branch
208
- findBranchStartNode(node) {
209
- for (let n = node; n; n = n.upstream) {
210
- if (n.branchIndex !== null) {
211
- return n;
212
- }
213
- }
214
- return null;
215
- }
216
- // find the node start current branch
217
- findBranchParentNode(node) {
218
- for (let n = node; n; n = n.upstream) {
219
- if (n.branchIndex !== null) {
220
- return n.upstream;
221
- }
222
- }
223
- return null;
224
- }
225
- findBranchParentJob(job, node) {
226
- for (let j = job; j; j = this.jobsMap.get(j.upstreamId)) {
227
- if (j.nodeId === node.id) {
228
- return j;
229
- }
230
- }
231
- return null;
232
- }
233
- getParsedValue(value, node) {
234
- const injectedFns = {};
235
- const scope = {
236
- execution: this,
237
- node
238
- };
239
- for (let [name, fn] of calculators_1.default.getEntities()) {
240
- injectedFns[name] = fn.bind(scope);
241
- }
242
- return (0, json_templates_1.default)(value)({
243
- $context: this.context,
244
- $jobsMapByNodeId: this.jobsMapByNodeId,
245
- $fn: injectedFns
246
- });
247
- }
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;
248
16
  }
249
- exports.default = ExecutionModel;
250
- ExecutionModel.StatusMap = {
251
- [constants_1.JOB_STATUS.PENDING]: constants_1.EXECUTION_STATUS.STARTED,
252
- [constants_1.JOB_STATUS.RESOLVED]: constants_1.EXECUTION_STATUS.RESOLVED,
253
- [constants_1.JOB_STATUS.REJECTED]: constants_1.EXECUTION_STATUS.REJECTED,
254
- [constants_1.JOB_STATUS.CANCELLED]: constants_1.EXECUTION_STATUS.CANCELLED,
255
- };
256
- //# sourceMappingURL=Execution.js.map
17
+
18
+ class ExecutionModel extends _database().Model {}
19
+
20
+ exports.default = ExecutionModel;
@@ -1,7 +1,20 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const database_1 = require("@nocobase/database");
4
- class FlowNodeModel extends database_1.Model {
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
- exports.default = FlowNodeModel;
7
- //# sourceMappingURL=FlowNode.js.map
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
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const database_1 = require("@nocobase/database");
4
- class JobModel extends database_1.Model {
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
- exports.default = JobModel;
7
- //# sourceMappingURL=Job.js.map
17
+
18
+ class JobModel extends _database().Model {}
19
+
20
+ exports.default = JobModel;
@@ -1,27 +1,26 @@
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 {
6
6
  static database: Database;
7
7
  id: number;
8
+ key: string;
8
9
  title: string;
9
10
  enabled: boolean;
11
+ current: boolean;
10
12
  description?: string;
11
13
  type: string;
12
14
  config: any;
13
15
  useTransaction: boolean;
16
+ executed: number;
14
17
  createdAt: Date;
15
18
  updatedAt: Date;
16
19
  nodes: FlowNodeModel[];
17
20
  getNodes: HasManyGetAssociationsMixin<FlowNodeModel>;
18
21
  createNode: HasManyCreateAssociationMixin<FlowNodeModel>;
19
22
  executions: ExecutionModel[];
23
+ countExecutions: HasManyCountAssociationsMixin;
20
24
  getExecutions: HasManyGetAssociationsMixin<ExecutionModel>;
21
25
  createExecution: HasManyCreateAssociationMixin<ExecutionModel>;
22
- static mount(): Promise<void>;
23
- getHookId(): string;
24
- getTransaction(options: any): any;
25
- toggle(enable?: boolean): Promise<void>;
26
- trigger(context: Object, options: any): Promise<ExecutionModel>;
27
26
  }