Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type Db collections to remove non-null assertions #3111

Merged
merged 6 commits into from
Apr 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions packages/cli/commands/execute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ export class Execute extends Command {
if (flags.id) {
// Id of workflow is given
workflowId = flags.id;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
workflowData = await Db.collections.Workflow!.findOne(workflowId);
workflowData = await Db.collections.Workflow.findOne(workflowId);
if (workflowData === undefined) {
console.info(`The workflow with the id "${workflowId}" does not exist.`);
process.exit(1);
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/commands/executeBatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ export class ExecuteBatch extends Command {

let allWorkflows;

const query = Db.collections.Workflow!.createQueryBuilder('workflows');
const query = Db.collections.Workflow.createQueryBuilder('workflows');

if (ids.length > 0) {
query.andWhere(`workflows.id in (:...ids)`, { ids });
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/commands/export/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class ExportCredentialsCommand extends Command {
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const credentials = await Db.collections.Credentials!.find(findQuery);
const credentials = await Db.collections.Credentials.find(findQuery);

if (flags.decrypted) {
const encryptionKey = await UserSettings.getEncryptionKey();
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/commands/export/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export class ExportWorkflowsCommand extends Command {
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const workflows = await Db.collections.Workflow!.find(findQuery);
const workflows = await Db.collections.Workflow.find(findQuery);

if (workflows.length === 0) {
throw new Error('No workflows found with specified filters.');
Expand Down
9 changes: 4 additions & 5 deletions packages/cli/commands/import/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
/* eslint-disable @typescript-eslint/no-shadow */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable no-await-in-loop */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable no-console */
Expand Down Expand Up @@ -150,7 +149,7 @@ export class ImportCredentialsCommand extends Command {
}

private async initOwnerCredentialRole() {
const ownerCredentialRole = await Db.collections.Role!.findOne({
const ownerCredentialRole = await Db.collections.Role.findOne({
where: { name: 'owner', scope: 'credential' },
});

Expand Down Expand Up @@ -180,11 +179,11 @@ export class ImportCredentialsCommand extends Command {
}

private async getOwner() {
const ownerGlobalRole = await Db.collections.Role!.findOne({
const ownerGlobalRole = await Db.collections.Role.findOne({
where: { name: 'owner', scope: 'global' },
});

const owner = await Db.collections.User!.findOne({ globalRole: ownerGlobalRole });
const owner = await Db.collections.User.findOne({ globalRole: ownerGlobalRole });

if (!owner) {
throw new Error(`Failed to find owner. ${FIX_INSTRUCTION}`);
Expand All @@ -194,7 +193,7 @@ export class ImportCredentialsCommand extends Command {
}

private async getAssignee(userId: string) {
const user = await Db.collections.User!.findOne(userId);
const user = await Db.collections.User.findOne(userId);

if (!user) {
throw new Error(`Failed to find user with ID ${userId}`);
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/commands/import/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class ImportWorkflowsCommand extends Command {
}

private async initOwnerWorkflowRole() {
const ownerWorkflowRole = await Db.collections.Role!.findOne({
const ownerWorkflowRole = await Db.collections.Role.findOne({
where: { name: 'owner', scope: 'workflow' },
});

Expand Down Expand Up @@ -187,11 +187,11 @@ export class ImportWorkflowsCommand extends Command {
}

private async getOwner() {
const ownerGlobalRole = await Db.collections.Role!.findOne({
const ownerGlobalRole = await Db.collections.Role.findOne({
where: { name: 'owner', scope: 'global' },
});

const owner = await Db.collections.User!.findOne({ globalRole: ownerGlobalRole });
const owner = await Db.collections.User.findOne({ globalRole: ownerGlobalRole });

if (!owner) {
throw new Error(`Failed to find owner. ${FIX_INSTRUCTION}`);
Expand All @@ -201,7 +201,7 @@ export class ImportWorkflowsCommand extends Command {
}

private async getAssignee(userId: string) {
const user = await Db.collections.User!.findOne(userId);
const user = await Db.collections.User.findOne(userId);

if (!user) {
throw new Error(`Failed to find user with ID ${userId}`);
Expand Down
3 changes: 1 addition & 2 deletions packages/cli/commands/list/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ export class ListWorkflowCommand extends Command {
findQuery.active = flags.active === 'true';
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const workflows = await Db.collections.Workflow!.find(findQuery);
const workflows = await Db.collections.Workflow.find(findQuery);
if (flags.onlyId) {
workflows.forEach((workflow) => console.log(workflow.id));
} else {
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export class Start extends Command {
}

// Load settings from database and set them to config.
const databaseSettings = await Db.collections.Settings!.find({ loadOnStartup: true });
const databaseSettings = await Db.collections.Settings.find({ loadOnStartup: true });
databaseSettings.forEach((setting) => {
config.set(setting.key, JSON.parse(setting.value));
});
Expand Down Expand Up @@ -287,8 +287,8 @@ export class Start extends Command {
if (dbType === 'sqlite') {
const shouldRunVacuum = config.getEnv('database.sqlite.executeVacuumOnStartup');
if (shouldRunVacuum) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises, @typescript-eslint/no-non-null-assertion
await Db.collections.Execution!.query('VACUUM;');
// eslint-disable-next-line @typescript-eslint/no-floating-promises
await Db.collections.Execution.query('VACUUM;');
}
}

Expand Down
3 changes: 1 addition & 2 deletions packages/cli/commands/update/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ export class UpdateWorkflowCommand extends Command {
findQuery.active = true;
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await Db.collections.Workflow!.update(findQuery, updateQuery);
await Db.collections.Workflow.update(findQuery, updateQuery);
console.info('Done');
} catch (e) {
console.error('Error updating database. See log messages for details.');
Expand Down
25 changes: 12 additions & 13 deletions packages/cli/commands/user-management/reset.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-non-null-assertion */

import Command from '@oclif/command';
import { Not } from 'typeorm';
Expand Down Expand Up @@ -27,33 +26,33 @@ export class Reset extends Command {
try {
const owner = await this.getInstanceOwner();

const ownerWorkflowRole = await Db.collections.Role!.findOneOrFail({
const ownerWorkflowRole = await Db.collections.Role.findOneOrFail({
name: 'owner',
scope: 'workflow',
});

const ownerCredentialRole = await Db.collections.Role!.findOneOrFail({
const ownerCredentialRole = await Db.collections.Role.findOneOrFail({
name: 'owner',
scope: 'credential',
});

await Db.collections.SharedWorkflow!.update(
await Db.collections.SharedWorkflow.update(
{ user: { id: Not(owner.id) }, role: ownerWorkflowRole },
{ user: owner },
);

await Db.collections.SharedCredentials!.update(
await Db.collections.SharedCredentials.update(
{ user: { id: Not(owner.id) }, role: ownerCredentialRole },
{ user: owner },
);
await Db.collections.User!.delete({ id: Not(owner.id) });
await Db.collections.User!.save(Object.assign(owner, this.defaultUserProps));
await Db.collections.User.delete({ id: Not(owner.id) });
await Db.collections.User.save(Object.assign(owner, this.defaultUserProps));

await Db.collections.Settings!.update(
await Db.collections.Settings.update(
{ key: 'userManagement.isInstanceOwnerSetUp' },
{ value: 'false' },
);
await Db.collections.Settings!.update(
await Db.collections.Settings.update(
{ key: 'userManagement.skipInstanceOwnerSetup' },
{ value: 'false' },
);
Expand All @@ -68,19 +67,19 @@ export class Reset extends Command {
}

private async getInstanceOwner(): Promise<User> {
const globalRole = await Db.collections.Role!.findOneOrFail({
const globalRole = await Db.collections.Role.findOneOrFail({
name: 'owner',
scope: 'global',
});

const owner = await Db.collections.User!.findOne({ globalRole });
const owner = await Db.collections.User.findOne({ globalRole });

if (owner) return owner;

const user = new User();

await Db.collections.User!.save(Object.assign(user, { ...this.defaultUserProps, globalRole }));
await Db.collections.User.save(Object.assign(user, { ...this.defaultUserProps, globalRole }));

return Db.collections.User!.findOneOrFail({ globalRole });
return Db.collections.User.findOneOrFail({ globalRole });
}
}
4 changes: 2 additions & 2 deletions packages/cli/commands/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class Worker extends Command {

async runJob(job: Bull.Job, nodeTypes: INodeTypes): Promise<IBullJobResponse> {
const jobData = job.data as IBullJobData;
const executionDb = await Db.collections.Execution!.findOne(jobData.executionId);
const executionDb = await Db.collections.Execution.findOne(jobData.executionId);

if (!executionDb) {
LoggerProxy.error('Worker failed to find execution data in database. Cannot continue.', {
Expand All @@ -139,7 +139,7 @@ export class Worker extends Command {
const findOptions = {
select: ['id', 'staticData'],
} as FindOneOptions;
const workflowData = await Db.collections.Workflow!.findOne(
const workflowData = await Db.collections.Workflow.findOne(
currentExecutionDb.workflowData.id,
findOptions,
);
Expand Down
16 changes: 8 additions & 8 deletions packages/cli/src/ActiveWorkflowRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class ActiveWorkflowRunner {
// NOTE
// Here I guess we can have a flag on the workflow table like hasTrigger
// so intead of pulling all the active wehhooks just pull the actives that have a trigger
const workflowsData: IWorkflowDb[] = (await Db.collections.Workflow!.find({
const workflowsData: IWorkflowDb[] = (await Db.collections.Workflow.find({
where: { active: true },
relations: ['shared', 'shared.user', 'shared.user.globalRole'],
})) as IWorkflowDb[];
Expand Down Expand Up @@ -256,7 +256,7 @@ export class ActiveWorkflowRunner {
});
}

const workflowData = await Db.collections.Workflow!.findOne(webhook.workflowId, {
const workflowData = await Db.collections.Workflow.findOne(webhook.workflowId, {
relations: ['shared', 'shared.user', 'shared.user.globalRole'],
});
if (workflowData === undefined) {
Expand Down Expand Up @@ -332,7 +332,7 @@ export class ActiveWorkflowRunner {
* @memberof ActiveWorkflowRunner
*/
async getWebhookMethods(path: string): Promise<string[]> {
const webhooks = (await Db.collections.Webhook?.find({ webhookPath: path })) as IWebhookDb[];
const webhooks = await Db.collections.Webhook?.find({ webhookPath: path });

// Gather all request methods in string array
const webhookMethods: string[] = webhooks.map((webhook) => webhook.method);
Expand All @@ -349,12 +349,12 @@ export class ActiveWorkflowRunner {
let activeWorkflows: WorkflowEntity[] = [];

if (!user || user.globalRole.name === 'owner') {
activeWorkflows = await Db.collections.Workflow!.find({
activeWorkflows = await Db.collections.Workflow.find({
select: ['id'],
where: { active: true },
});
} else {
const shared = await Db.collections.SharedWorkflow!.find({
const shared = await Db.collections.SharedWorkflow.find({
relations: ['workflow'],
where: whereClause({
user,
Expand All @@ -379,7 +379,7 @@ export class ActiveWorkflowRunner {
* @memberof ActiveWorkflowRunner
*/
async isActive(id: string): Promise<boolean> {
const workflow = await Db.collections.Workflow!.findOne(id);
const workflow = await Db.collections.Workflow.findOne(id);
return !!workflow?.active;
}

Expand Down Expand Up @@ -512,7 +512,7 @@ export class ActiveWorkflowRunner {
* @memberof ActiveWorkflowRunner
*/
async removeWorkflowWebhooks(workflowId: string): Promise<void> {
const workflowData = await Db.collections.Workflow!.findOne(workflowId, {
const workflowData = await Db.collections.Workflow.findOne(workflowId, {
relations: ['shared', 'shared.user', 'shared.user.globalRole'],
});
if (workflowData === undefined) {
Expand Down Expand Up @@ -715,7 +715,7 @@ export class ActiveWorkflowRunner {
let workflowInstance: Workflow;
try {
if (workflowData === undefined) {
workflowData = (await Db.collections.Workflow!.findOne(workflowId, {
workflowData = (await Db.collections.Workflow.findOne(workflowId, {
relations: ['shared', 'shared.user', 'shared.user.globalRole'],
})) as IWorkflowDb;
}
Expand Down
17 changes: 7 additions & 10 deletions packages/cli/src/CredentialsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,11 @@ export class CredentialsHelper extends ICredentialsHelper {
}

const credential = userId
? await Db.collections
.SharedCredentials!.findOneOrFail({
relations: ['credentials'],
where: { credentials: { id: nodeCredential.id, type }, user: { id: userId } },
})
.then((shared) => shared.credentials)
: await Db.collections.Credentials!.findOneOrFail({ id: nodeCredential.id, type });
? await Db.collections.SharedCredentials.findOneOrFail({
relations: ['credentials'],
where: { credentials: { id: nodeCredential.id, type }, user: { id: userId } },
}).then((shared) => shared.credentials)
: await Db.collections.Credentials.findOneOrFail({ id: nodeCredential.id, type });

if (!credential) {
throw new Error(
Expand Down Expand Up @@ -445,7 +443,7 @@ export class CredentialsHelper extends ICredentialsHelper {
type,
};

await Db.collections.Credentials!.update(findQuery, newCredentialsData);
await Db.collections.Credentials.update(findQuery, newCredentialsData);
}

getCredentialTestFunction(
Expand Down Expand Up @@ -721,8 +719,7 @@ export async function getCredentialForUser(
credentialId: string,
user: User,
): Promise<ICredentialsDb | null> {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const sharedCredential = await Db.collections.SharedCredentials!.findOne({
const sharedCredential = await Db.collections.SharedCredentials.findOne({
relations: ['credentials'],
where: whereClause({
user,
Expand Down
17 changes: 5 additions & 12 deletions packages/cli/src/Db.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable import/no-mutable-exports */
/* eslint-disable import/no-cycle */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/restrict-template-expressions */
Expand Down Expand Up @@ -28,18 +29,8 @@ import { postgresMigrations } from './databases/postgresdb/migrations';
import { mysqlMigrations } from './databases/mysqldb/migrations';
import { sqliteMigrations } from './databases/sqlite/migrations';

export const collections: IDatabaseCollections = {
Credentials: null,
Execution: null,
Workflow: null,
Webhook: null,
Tag: null,
Role: null,
User: null,
SharedCredentials: null,
SharedWorkflow: null,
Settings: null,
};
export let isInitialized = false;
export const collections = {} as IDatabaseCollections;
BHesseldieck marked this conversation as resolved.
Show resolved Hide resolved

let connection: Connection;

Expand Down Expand Up @@ -202,5 +193,7 @@ export async function init(
collections.SharedWorkflow = linkRepository(entities.SharedWorkflow);
collections.Settings = linkRepository(entities.Settings);

isInitialized = true;

return collections;
}
4 changes: 2 additions & 2 deletions packages/cli/src/GenericHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ export async function generateUniqueName(

const found: Array<WorkflowEntity | ICredentialsDb> =
entityType === 'workflow'
? await Db.collections.Workflow!.find(findConditions)
: await Db.collections.Credentials!.find(findConditions);
? await Db.collections.Workflow.find(findConditions)
: await Db.collections.Credentials.find(findConditions);

// name is unique
if (found.length === 0) {
Expand Down
Loading