24ea83f0ff
* create-nocobase-app template from [develop] * change create-nocobase-app package.json config * feat: load configuration from directory * feat: configuration repository toObject * feat: create application from configuration dir * feat: application factory with plugins options * export type * feat: read application config & application with plugins options * feat: release command * fix: database release * chore: workflow package.json * feat: nocobase cli package * feat: console command * chore: load application in command * fix: load packages from process.cwd * feat: cli load env file * feat: create-nocobase-app * fix: gitignore create-nocobase-app lib * fix: sqlite path * feat: create plugin * chore: plugin files template * chore: move cli into application * chore: create-nocobase-app * fix: create plugin * chore: app-client && app-server * chore: package.json * feat: create-nocobase-app download template from npm * chore: create-nocobase-app template * fix: config of plugin-users * fix: yarn.lock * fix: database build error * fix: yarn.lock * fix: resourcer config * chore: cross-env * chore: app-client dependents * fix: env * chore: v0.6.0-alpha.1 * chore: verdaccio * chore(versions): 😊 publish v0.6.0 * chore(versions): 😊 publish v0.6.1-alpha.0 * chore(versions): 😊 publish v0.6.2-alpha.0 * chore(versions): 😊 publish v0.6.2-alpha.1 * chore: 0.6.2-alpha.2 * feat: workspaces * chore(versions): 😊 publish v0.6.2-alpha.3 * chore(versions): 😊 publish v0.6.2-alpha.4 * chore: create-nocobase-app * chore: create-nocobase-app lib * fix: update tsconfig.jest.json * chore: .env * chore(versions): 😊 publish v0.6.2-alpha.5 * chore(versions): 😊 publish v0.6.2-alpha.6 * feat: improve code * chore(versions): 😊 publish v0.6.2-alpha.7 * fix: cleanup * chore(versions): 😊 publish v0.6.2-alpha.8 * chore: tsconfig for app server package * fix: move files * fix: move files Co-authored-by: chenos <chenlinxh@gmail.com>
95 lines
2.6 KiB
TypeScript
95 lines
2.6 KiB
TypeScript
import { Database, Model } from '@nocobase/database';
|
|
import { HasManyCreateAssociationMixin, HasManyGetAssociationsMixin } from 'sequelize';
|
|
|
|
import triggers from '../triggers';
|
|
import { EXECUTION_STATUS } from '../constants';
|
|
import ExecutionModel from './Execution';
|
|
import FlowNodeModel from './FlowNode';
|
|
|
|
export default class WorkflowModel extends Model {
|
|
declare static database: Database;
|
|
|
|
declare id: number;
|
|
declare title: string;
|
|
declare enabled: boolean;
|
|
declare description?: string;
|
|
declare type: string;
|
|
declare config: any;
|
|
declare useTransaction: boolean;
|
|
|
|
declare createdAt: Date;
|
|
declare updatedAt: Date;
|
|
|
|
declare nodes: FlowNodeModel[];
|
|
declare getNodes: HasManyGetAssociationsMixin<FlowNodeModel>;
|
|
declare createNode: HasManyCreateAssociationMixin<FlowNodeModel>;
|
|
|
|
declare executions: ExecutionModel[];
|
|
declare getExecutions: HasManyGetAssociationsMixin<ExecutionModel>;
|
|
declare createExecution: HasManyCreateAssociationMixin<ExecutionModel>;
|
|
|
|
static async mount() {
|
|
const collection = this.database.getCollection('workflows');
|
|
const workflows = await collection.repository.find({
|
|
filter: { enabled: true },
|
|
});
|
|
|
|
workflows.forEach((workflow: WorkflowModel) => {
|
|
workflow.toggle();
|
|
});
|
|
|
|
this.addHook('afterCreate', (model: WorkflowModel) => model.toggle());
|
|
this.addHook('afterUpdate', (model: WorkflowModel) => model.toggle());
|
|
this.addHook('afterDestroy', (model: WorkflowModel) => model.toggle(false));
|
|
}
|
|
|
|
getHookId() {
|
|
return `workflow-${this.get('id')}`;
|
|
}
|
|
|
|
getTransaction(options) {
|
|
if (!this.useTransaction) {
|
|
return undefined;
|
|
}
|
|
|
|
return options.transaction && !options.transaction.finished
|
|
? options.transaction
|
|
: (<typeof WorkflowModel>this.constructor).database.sequelize.transaction();
|
|
}
|
|
|
|
async toggle(enable?: boolean) {
|
|
const type = this.get('type');
|
|
const { on, off } = triggers.get(type);
|
|
if (typeof enable !== 'undefined' ? enable : this.get('enabled')) {
|
|
on.call(this, this.trigger.bind(this));
|
|
} else {
|
|
off.call(this);
|
|
}
|
|
}
|
|
|
|
async trigger(context: Object, options) {
|
|
// `null` means not to trigger
|
|
if (context === null) {
|
|
return;
|
|
}
|
|
|
|
const transaction = await this.getTransaction(options);
|
|
|
|
const execution = await this.createExecution({
|
|
context,
|
|
status: EXECUTION_STATUS.STARTED,
|
|
useTransaction: this.useTransaction
|
|
}, { transaction });
|
|
|
|
execution.workflow = this;
|
|
|
|
await execution.start({ transaction });
|
|
|
|
if (transaction && (!options.transaction || options.transaction.finished)) {
|
|
await transaction.commit();
|
|
}
|
|
|
|
return execution;
|
|
}
|
|
}
|