1
1
mirror of https://github.com/n8n-io/n8n.git synced 2024-11-10 12:35:46 +03:00

refactor: Trim down the response on the Workflows listing endpoint (#4726)

* fix: Avoid hashing workflows in the listing page

* stop returning full nodes data on the listings page when sharing is enabled

* fix the relations array for workflow listing

* add a comment explaining the hash skipping hack
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2022-11-25 14:20:28 +01:00 committed by GitHub
parent 39a5dc57a8
commit 1579d05fd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 32 deletions

View File

@ -101,6 +101,9 @@ export class WorkflowEntity extends AbstractEntity implements IWorkflowDb {
setHash(): void {
const { name, active, nodes, connections, settings, staticData, pinData } = this;
// Workflow listing page does not request the `connections` column, so we can use this for `undefined` to avoid generating hashes for all the workflows.
if (!connections) return;
const state = JSON.stringify({
name,
active,

View File

@ -97,10 +97,9 @@ EEWorkflowController.get(
);
}
return EEWorkflows.addCredentialsToWorkflow(
EEWorkflows.addOwnerAndSharings(workflow),
req.user,
);
EEWorkflows.addOwnerAndSharings(workflow);
await EEWorkflows.addCredentialsToWorkflow(workflow, req.user);
return workflow;
}),
);
@ -200,9 +199,12 @@ EEWorkflowController.get(
)) as unknown as WorkflowEntity[];
return Promise.all(
workflows.map(async (workflow) =>
EEWorkflows.addCredentialsToWorkflow(EEWorkflows.addOwnerAndSharings(workflow), req.user),
),
workflows.map(async (workflow) => {
EEWorkflows.addOwnerAndSharings(workflow);
await EEWorkflows.addCredentialsToWorkflow(workflow, req.user);
workflow.nodes = [];
return workflow;
}),
);
}),
);

View File

@ -85,9 +85,7 @@ export class EEWorkflowsService extends WorkflowsService {
return transaction.save(newSharedWorkflows);
}
static addOwnerAndSharings(
workflow: WorkflowWithSharingsAndCredentials,
): WorkflowWithSharingsAndCredentials {
static addOwnerAndSharings(workflow: WorkflowWithSharingsAndCredentials): void {
workflow.ownedBy = null;
workflow.sharedWith = [];
workflow.usedCredentials = [];
@ -104,14 +102,12 @@ export class EEWorkflowsService extends WorkflowsService {
});
delete workflow.shared;
return workflow;
}
static async addCredentialsToWorkflow(
workflow: WorkflowWithSharingsAndCredentials,
currentUser: User,
): Promise<WorkflowWithSharingsAndCredentials> {
): Promise<void> {
workflow.usedCredentials = [];
const userCredentials = await EECredentials.getAll(currentUser);
const credentialIdsUsedByWorkflow = new Set<number>();
@ -155,8 +151,6 @@ export class EEWorkflowsService extends WorkflowsService {
});
workflow.usedCredentials?.push(workflowCredential);
});
return workflow;
}
static validateCredentialPermissionsToUser(

View File

@ -140,26 +140,27 @@ export class WorkflowsService {
}
const fields: Array<keyof WorkflowEntity> = ['id', 'name', 'active', 'createdAt', 'updatedAt'];
const relations: string[] = [];
const query: FindManyOptions<WorkflowEntity> = {
select: config.get('enterprise.features.sharing') ? [...fields, 'nodes'] : fields,
relations: config.get('enterprise.features.sharing')
? ['tags', 'shared', 'shared.user', 'shared.role']
: ['tags'],
};
if (config.getEnv('workflowTagsDisabled')) {
delete query.relations;
if (!config.getEnv('workflowTagsDisabled')) {
relations.push('tags');
}
const workflows = await Db.collections.Workflow.find(
Object.assign(query, {
where: {
id: In(sharedWorkflowIds),
...filter,
},
}),
);
const isSharingEnabled = config.getEnv('enterprise.features.sharing');
if (isSharingEnabled) {
relations.push('shared', 'shared.user', 'shared.role');
}
const query: FindManyOptions<WorkflowEntity> = {
select: isSharingEnabled ? [...fields, 'nodes'] : fields,
relations,
where: {
id: In(sharedWorkflowIds),
...filter,
},
};
const workflows = await Db.collections.Workflow.find(query);
return workflows.map((workflow) => {
const { id, ...rest } = workflow;