Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

4.7 KB
Raw Blame
import {
    Flow,
    FlowOperationStatus,
    FlowRun,
    FlowStatus,
    FlowVersion,
    Folder,
    Project,
    TableWebhook,
    TriggerEvent,
    User,
} from '@activepieces/shared'
import { EntitySchema } from 'typeorm'
import {
    ApIdSchema,
    BaseColumnSchemaPart,
} from '../../database/database-common'

export type FlowSchema = Flow & {
    versions: FlowVersion[]
    project: Project
    runs: FlowRun[]
    folder?: Folder
    owner?: User
    events: TriggerEvent[]
    publishedVersion?: FlowVersion
    tableWebhooks: TableWebhook[]
}

export const FlowEntity = new EntitySchema<FlowSchema>({
    name: 'flow',
    columns: {
        ...BaseColumnSchemaPart,
        projectId: {
            ...ApIdSchema,
            nullable: false,
        },
        folderId: {
            ...ApIdSchema,
            nullable: true,
        },
        status: {
            type: String,
            enum: FlowStatus,
            nullable: false,
            default: FlowStatus.DISABLED,
        },
        externalId: {
            type: String,
            nullable: false,
        },
        publishedVersionId: {
            ...ApIdSchema,
            nullable: true,
            unique: true,
        },
        metadata: {
            type: 'jsonb',
            nullable: true,
        },
        operationStatus: {
            type: String,
            nullable: false,
            default: FlowOperationStatus.NONE,
        },
        timeSavedPerRun: {
            type: Number,
            nullable: true,
        },
        ownerId: {
            type: String,
            nullable: true,
        },
        templateId: {
            type: String,
            nullable: true,
        },
        createdBy: {
            type: 'jsonb',
            nullable: true,
        },
        workerGroupId: {
            type: String,
            nullable: true,
        },
    },
    indices: [
        {
            name: 'idx_flow_project_id',
            columns: ['projectId'],
            unique: false,
        },
        {
            name: 'idx_flow_owner_id',
            columns: ['ownerId'],
            unique: false,
        },
        {
            name: 'idx_flow_folder_id',
            columns: ['folderId'],
            unique: false,
        },
        {
            name: 'idx_flow_project_id_status',
            columns: ['projectId', 'status'],
            unique: false,
        },
        {
            name: 'idx_flow_project_id_external_id',
            columns: ['projectId', 'externalId'],
            unique: true,
            where: `"operationStatus" != '${FlowOperationStatus.DELETING}'`,
        },
        {
            name: 'idx_flow_deleting_updated',
            columns: ['updated'],
            where: `"operationStatus" = '${FlowOperationStatus.DELETING}'`,
        },
        {
            name: 'idx_flow_worker_group',
            columns: ['workerGroupId'],
            unique: false,
        },
    ],
    relations: {
        runs: {
            type: 'one-to-many',
            target: 'flow_run',
            inverseSide: 'flow',
        },
        owner: {
            type: 'many-to-one',
            target: 'user',
            cascade: true,
            onDelete: 'SET NULL',
            nullable: false,
            joinColumn: {
                name: 'ownerId',
                foreignKeyConstraintName: 'fk_flow_owner_id',
            },
        },
        folder: {
            type: 'many-to-one',
            target: 'folder',
            onDelete: 'SET NULL',
            nullable: true,
            joinColumn: {
                name: 'folderId',
                foreignKeyConstraintName: 'fk_flow_folder_id',
            },
        },
        events: {
            type: 'one-to-many',
            target: 'trigger_event',
            inverseSide: 'flow',
        },
        versions: {
            type: 'one-to-many',
            target: 'flow_version',
            inverseSide: 'flow',
        },
        project: {
            type: 'many-to-one',
            target: 'project',
            cascade: true,
            onDelete: 'CASCADE',
            joinColumn: {
                name: 'projectId',
                foreignKeyConstraintName: 'fk_flow_project_id',
            },
        },
        publishedVersion: {
            type: 'one-to-one',
            target: 'flow_version',
            nullable: true,
            onDelete: 'RESTRICT',
            joinColumn: {
                name: 'publishedVersionId',
                referencedColumnName: 'id',
                foreignKeyConstraintName: 'fk_flow_published_version',
            },
        },
        tableWebhooks: {
            type: 'one-to-many',
            target: 'table_webhook',
            inverseSide: 'flow',
        },
    },
})