refactor(core): Continue moving typeorm operators to repositories (no-changelog) (#8212)

Follow-up to: https://github.com/n8n-io/n8n/pull/8186
This commit is contained in:
Iván Ovejero
2024-01-05 13:06:24 +01:00
committed by GitHub
parent bed04ec122
commit 23a4ac96c0
10 changed files with 69 additions and 47 deletions

View File

@@ -1,5 +1,5 @@
import { Service } from 'typedi';
import { DataSource, Repository } from 'typeorm';
import { DataSource, In, Repository } from 'typeorm';
import type { RoleNames, RoleScopes } from '../entities/Role';
import { Role } from '../entities/Role';
import { User } from '../entities/User';
@@ -32,4 +32,11 @@ export class RoleRepository extends Repository<Role> {
return acc;
}, {});
}
async getIdsInScopeWorkflowByNames(roleNames: RoleNames[]) {
return this.find({
select: ['id'],
where: { name: In(roleNames), scope: 'workflow' },
}).then((role) => role.map(({ id }) => id));
}
}

View File

@@ -1,5 +1,5 @@
import { Service } from 'typedi';
import type { FindOptionsWhere } from 'typeorm';
import type { EntityManager, FindOptionsWhere } from 'typeorm';
import { DataSource, In, Not, Repository } from 'typeorm';
import { SharedCredentials } from '../entities/SharedCredentials';
import type { User } from '../entities/User';
@@ -60,4 +60,11 @@ export class SharedCredentialsRepository extends Repository<SharedCredentials> {
return this.find({ where });
}
async deleteByIds(transaction: EntityManager, sharedCredentialsIds: string[], user?: User) {
return transaction.delete(SharedCredentials, {
user,
credentialsId: In(sharedCredentialsIds),
});
}
}

View File

@@ -1,6 +1,6 @@
import { Service } from 'typedi';
import { DataSource, Repository, In, Not } from 'typeorm';
import type { EntityManager, FindOptionsWhere } from 'typeorm';
import type { EntityManager, FindOptionsSelect, FindOptionsWhere } from 'typeorm';
import { SharedWorkflow } from '../entities/SharedWorkflow';
import { type User } from '../entities/User';
import type { Scope } from '@n8n/permissions';
@@ -125,4 +125,20 @@ export class SharedWorkflowRepository extends Repository<SharedWorkflow> {
return transaction.save(newSharedWorkflows);
}
async findWithFields(workflowIds: string[], { fields }: { fields: string[] }) {
return this.find({
where: {
workflowId: In(workflowIds),
},
select: fields as FindOptionsSelect<SharedWorkflow>,
});
}
async deleteByIds(transaction: EntityManager, sharedWorkflowIds: string[], user?: User) {
return transaction.delete(SharedWorkflow, {
user,
workflowId: In(sharedWorkflowIds),
});
}
}

View File

@@ -54,8 +54,14 @@ export class WorkflowRepository extends Repository<WorkflowEntity> {
});
}
async findByIds(workflowIds: string[]) {
return this.find({ where: { id: In(workflowIds) } });
async findByIds(workflowIds: string[], { fields }: { fields?: string[] } = {}) {
const options: FindManyOptions<WorkflowEntity> = {
where: { id: In(workflowIds) },
};
if (fields?.length) options.select = fields as FindOptionsSelect<WorkflowEntity>;
return this.find(options);
}
async getActiveTriggerCount() {