UNPKG

n8n

Version:

n8n Workflow Automation Tool

179 lines 8.02 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WorkflowRepository = void 0; const typedi_1 = require("typedi"); const config_1 = require("@n8n/config"); const typeorm_1 = require("@n8n/typeorm"); const utils_1 = require("../../utils"); const config_2 = __importDefault(require("../../config")); const WorkflowEntity_1 = require("../entities/WorkflowEntity"); const WebhookEntity_1 = require("../entities/WebhookEntity"); let WorkflowRepository = class WorkflowRepository extends typeorm_1.Repository { constructor(dataSource, globalConfig) { super(WorkflowEntity_1.WorkflowEntity, dataSource.manager); this.globalConfig = globalConfig; } async get(where, options) { return await this.findOne({ where, relations: options === null || options === void 0 ? void 0 : options.relations, }); } async getAllActive() { return await this.find({ where: { active: true }, relations: { shared: { project: { projectRelations: true } } }, }); } async getActiveIds() { const activeWorkflows = await this.find({ select: ['id'], where: { active: true }, }); return activeWorkflows.map((workflow) => workflow.id); } async findById(workflowId) { return await this.findOne({ where: { id: workflowId }, relations: { shared: { project: { projectRelations: true } } }, }); } async findByIds(workflowIds, { fields } = {}) { const options = { where: { id: (0, typeorm_1.In)(workflowIds) }, }; if (fields === null || fields === void 0 ? void 0 : fields.length) options.select = fields; return await this.find(options); } async getActiveTriggerCount() { const totalTriggerCount = await this.sum('triggerCount', { active: true, }); return totalTriggerCount !== null && totalTriggerCount !== void 0 ? totalTriggerCount : 0; } async updateWorkflowTriggerCount(id, triggerCount) { const qb = this.createQueryBuilder('workflow'); const dbType = this.globalConfig.database.type; return await qb .update() .set({ triggerCount, updatedAt: () => { if (['mysqldb', 'mariadb'].includes(dbType)) { return 'updatedAt'; } return '"updatedAt"'; }, }) .where('id = :id', { id }) .execute(); } async getMany(sharedWorkflowIds, options) { var _a, _b, _c, _d, _e; if (sharedWorkflowIds.length === 0) return { workflows: [], count: 0 }; if (typeof ((_a = options === null || options === void 0 ? void 0 : options.filter) === null || _a === void 0 ? void 0 : _a.projectId) === 'string' && options.filter.projectId !== '') { options.filter.shared = { projectId: options.filter.projectId }; delete options.filter.projectId; } const where = { ...options === null || options === void 0 ? void 0 : options.filter, id: (0, typeorm_1.In)(sharedWorkflowIds), }; const reqTags = (_b = options === null || options === void 0 ? void 0 : options.filter) === null || _b === void 0 ? void 0 : _b.tags; if ((0, utils_1.isStringArray)(reqTags)) { where.tags = reqTags.map((tag) => ({ name: tag })); } const select = (options === null || options === void 0 ? void 0 : options.select) ? { ...options.select } : { name: true, active: true, createdAt: true, updatedAt: true, versionId: true, shared: { role: true }, }; select === null || select === void 0 ? true : delete select.ownedBy; const relations = []; const areTagsEnabled = !config_2.default.getEnv('workflowTagsDisabled'); const isDefaultSelect = (options === null || options === void 0 ? void 0 : options.select) === undefined; const areTagsRequested = isDefaultSelect || ((_c = options === null || options === void 0 ? void 0 : options.select) === null || _c === void 0 ? void 0 : _c.tags) === true; const isOwnedByIncluded = isDefaultSelect || ((_d = options === null || options === void 0 ? void 0 : options.select) === null || _d === void 0 ? void 0 : _d.ownedBy) === true; if (areTagsEnabled && areTagsRequested) { relations.push('tags'); select.tags = { id: true, name: true }; } if (isOwnedByIncluded) relations.push('shared', 'shared.project'); if (typeof where.name === 'string' && where.name !== '') { where.name = (0, typeorm_1.Like)(`%${where.name}%`); } const findManyOptions = { select: { ...select, id: true }, where, }; if (isDefaultSelect || ((_e = options === null || options === void 0 ? void 0 : options.select) === null || _e === void 0 ? void 0 : _e.updatedAt) === true) { findManyOptions.order = { updatedAt: 'ASC' }; } if (relations.length > 0) { findManyOptions.relations = relations; } if (options === null || options === void 0 ? void 0 : options.take) { findManyOptions.skip = options.skip; findManyOptions.take = options.take; } const [workflows, count] = (await this.findAndCount(findManyOptions)); return { workflows, count }; } async findStartingWith(workflowName) { return await this.find({ select: ['name'], where: { name: (0, typeorm_1.Like)(`${workflowName}%`) }, }); } async findIn(workflowIds) { return await this.find({ select: ['id', 'name'], where: { id: (0, typeorm_1.In)(workflowIds) }, }); } async findWebhookBasedActiveWorkflows() { return await this.createQueryBuilder('workflow') .select('DISTINCT workflow.id, workflow.name') .innerJoin(WebhookEntity_1.WebhookEntity, 'webhook_entity', 'workflow.id = webhook_entity.workflowId') .execute(); } async updateActiveState(workflowId, newState) { return await this.update({ id: workflowId }, { active: newState }); } async deactivateAll() { return await this.update({ active: true }, { active: false }); } async activateAll() { return await this.update({ active: false }, { active: true }); } async findByActiveState(activeState) { return await this.findBy({ active: activeState }); } }; exports.WorkflowRepository = WorkflowRepository; exports.WorkflowRepository = WorkflowRepository = __decorate([ (0, typedi_1.Service)(), __metadata("design:paramtypes", [typeorm_1.DataSource, config_1.GlobalConfig]) ], WorkflowRepository); //# sourceMappingURL=workflow.repository.js.map