Remove non-null assertions for Db collections (#3111)

* 📘 Remove unions to `null`

*  Track `Db` initialization state

* 🔥 Remove non-null assertions

* 👕 Remove lint exceptions

* 🔥 Remove leftover assertion
This commit is contained in:
Iván Ovejero
2022-04-14 09:02:12 +02:00
committed by GitHub
parent e45ac7eb6a
commit 3e5d981f3f
31 changed files with 130 additions and 152 deletions

View File

@@ -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);

View File

@@ -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 });

View File

@@ -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();

View File

@@ -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.');

View File

@@ -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 */
@@ -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' },
});
@@ -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}`);
@@ -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}`);

View File

@@ -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' },
});
@@ -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}`);
@@ -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}`);

View File

@@ -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 {

View File

@@ -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));
});
@@ -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;');
}
}

View File

@@ -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.');

View File

@@ -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';
@@ -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' },
);
@@ -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 });
}
}

View File

@@ -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.', {
@@ -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,
);