fix(API): reduce code duplication between DB entities (#4351)

This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™ 2022-10-21 12:29:25 +02:00 committed by GitHub
parent 5eb1eb88e4
commit 5eebd91ba7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 86 additions and 495 deletions

View file

@ -0,0 +1,39 @@
import { BeforeUpdate, CreateDateColumn, UpdateDateColumn } from 'typeorm';
import { IsDate, IsOptional } from 'class-validator';
import config from '../../../config';
const dbType = config.getEnv('database.type');
const timestampSyntax = {
sqlite: `STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')`,
postgresdb: 'CURRENT_TIMESTAMP(3)',
mysqldb: 'CURRENT_TIMESTAMP(3)',
mariadb: 'CURRENT_TIMESTAMP(3)',
}[dbType];
export const jsonColumnType = dbType === 'sqlite' ? 'simple-json' : 'json';
export const datetimeColumnType = dbType === 'postgresdb' ? 'timestamptz' : 'datetime';
export abstract class AbstractEntity {
@CreateDateColumn({
precision: 3,
default: () => timestampSyntax,
})
@IsOptional() // ignored by validation because set at DB level
@IsDate()
createdAt: Date;
@UpdateDateColumn({
precision: 3,
default: () => timestampSyntax,
onUpdate: timestampSyntax,
})
@IsOptional() // ignored by validation because set at DB level
@IsDate()
updatedAt: Date;
@BeforeUpdate()
setUpdateDate(): void {
this.updatedAt = new Date();
}
}

View file

@ -1,35 +1,10 @@
/* eslint-disable import/no-cycle */ import { Entity, ManyToOne, PrimaryColumn, RelationId } from 'typeorm';
import {
BeforeUpdate,
CreateDateColumn,
Entity,
ManyToOne,
PrimaryColumn,
RelationId,
UpdateDateColumn,
} from 'typeorm';
import { IsDate, IsOptional } from 'class-validator';
import config = require('../../../config');
import { DatabaseType } from '../../index';
import { WorkflowEntity } from './WorkflowEntity'; import { WorkflowEntity } from './WorkflowEntity';
import { CredentialsEntity } from './CredentialsEntity'; import { CredentialsEntity } from './CredentialsEntity';
import { AbstractEntity } from './AbstractEntity';
function getTimestampSyntax() {
const dbType = config.get('database.type') as DatabaseType;
const map: { [key in DatabaseType]: string } = {
sqlite: "STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')",
postgresdb: 'CURRENT_TIMESTAMP(3)',
mysqldb: 'CURRENT_TIMESTAMP(3)',
mariadb: 'CURRENT_TIMESTAMP(3)',
};
return map[dbType];
}
@Entity() @Entity()
export class CredentialUsage { export class CredentialUsage extends AbstractEntity {
@ManyToOne(() => WorkflowEntity, { @ManyToOne(() => WorkflowEntity, {
onDelete: 'CASCADE', onDelete: 'CASCADE',
}) })
@ -50,23 +25,4 @@ export class CredentialUsage {
@RelationId((credentialUsage: CredentialUsage) => credentialUsage.credential) @RelationId((credentialUsage: CredentialUsage) => credentialUsage.credential)
@PrimaryColumn() @PrimaryColumn()
credentialId: string; credentialId: string;
@CreateDateColumn({ precision: 3, default: () => getTimestampSyntax() })
@IsOptional() // ignored by validation because set at DB level
@IsDate()
createdAt: Date;
@UpdateDateColumn({
precision: 3,
default: () => getTimestampSyntax(),
onUpdate: getTimestampSyntax(),
})
@IsOptional() // ignored by validation because set at DB level
@IsDate()
updatedAt: Date;
@BeforeUpdate()
setUpdateDate(): void {
this.updatedAt = new Date();
}
} }

View file

@ -1,56 +1,12 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import type { ICredentialNodeAccess } from 'n8n-workflow';
/* eslint-disable import/no-cycle */ import { Column, Entity, Index, OneToMany, PrimaryGeneratedColumn } from 'typeorm';
import { ICredentialNodeAccess } from 'n8n-workflow';
import {
BeforeUpdate,
Column,
CreateDateColumn,
Entity,
Index,
OneToMany,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm';
import { IsArray, IsObject, IsString, Length } from 'class-validator'; import { IsArray, IsObject, IsString, Length } from 'class-validator';
import * as config from '../../../config';
import { DatabaseType, ICredentialsDb } from '../..';
import { SharedCredentials } from './SharedCredentials'; import { SharedCredentials } from './SharedCredentials';
import { AbstractEntity, jsonColumnType } from './AbstractEntity';
function resolveDataType(dataType: string) { import type { ICredentialsDb } from '../../Interfaces';
const dbType = config.getEnv('database.type');
const typeMap: { [key in DatabaseType]: { [key: string]: string } } = {
sqlite: {
json: 'simple-json',
},
postgresdb: {
datetime: 'timestamptz',
},
mysqldb: {},
mariadb: {},
};
return typeMap[dbType][dataType] ?? dataType;
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
function getTimestampSyntax() {
const dbType = config.getEnv('database.type');
const map: { [key in DatabaseType]: string } = {
sqlite: `STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')`,
postgresdb: 'CURRENT_TIMESTAMP(3)',
mysqldb: 'CURRENT_TIMESTAMP(3)',
mariadb: 'CURRENT_TIMESTAMP(3)',
};
return map[dbType];
}
@Entity() @Entity()
export class CredentialsEntity implements ICredentialsDb { export class CredentialsEntity extends AbstractEntity implements ICredentialsDb {
@PrimaryGeneratedColumn() @PrimaryGeneratedColumn()
id: number; id: number;
@ -75,22 +31,7 @@ export class CredentialsEntity implements ICredentialsDb {
@OneToMany(() => SharedCredentials, (sharedCredentials) => sharedCredentials.credentials) @OneToMany(() => SharedCredentials, (sharedCredentials) => sharedCredentials.credentials)
shared: SharedCredentials[]; shared: SharedCredentials[];
@Column(resolveDataType('json')) @Column(jsonColumnType)
@IsArray() @IsArray()
nodesAccess: ICredentialNodeAccess[]; nodesAccess: ICredentialNodeAccess[];
@CreateDateColumn({ precision: 3, default: () => getTimestampSyntax() })
createdAt: Date;
@UpdateDateColumn({
precision: 3,
default: () => getTimestampSyntax(),
onUpdate: getTimestampSyntax(),
})
updatedAt: Date;
@BeforeUpdate()
setUpdateDate() {
this.updatedAt = new Date();
}
} }

View file

@ -1,26 +1,7 @@
/* eslint-disable import/no-cycle */ import type { WorkflowExecuteMode } from 'n8n-workflow';
import { WorkflowExecuteMode } from 'n8n-workflow'; import { Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm';
import { datetimeColumnType, jsonColumnType } from './AbstractEntity';
import { Column, ColumnOptions, Entity, Index, PrimaryGeneratedColumn } from 'typeorm'; import type { IExecutionFlattedDb, IWorkflowDb } from '../../Interfaces';
import * as config from '../../../config';
import { DatabaseType, IExecutionFlattedDb, IWorkflowDb } from '../..';
function resolveDataType(dataType: string) {
const dbType = config.getEnv('database.type');
const typeMap: { [key in DatabaseType]: { [key: string]: string } } = {
sqlite: {
json: 'simple-json',
},
postgresdb: {
datetime: 'timestamptz',
},
mysqldb: {},
mariadb: {},
};
return typeMap[dbType][dataType] ?? dataType;
}
@Entity() @Entity()
@Index(['workflowId', 'id']) @Index(['workflowId', 'id'])
@ -47,19 +28,19 @@ export class ExecutionEntity implements IExecutionFlattedDb {
@Column({ nullable: true }) @Column({ nullable: true })
retrySuccessId: string; retrySuccessId: string;
@Column(resolveDataType('datetime')) @Column(datetimeColumnType)
startedAt: Date; startedAt: Date;
@Index() @Index()
@Column({ type: resolveDataType('datetime') as ColumnOptions['type'], nullable: true }) @Column({ type: datetimeColumnType, nullable: true })
stoppedAt: Date; stoppedAt: Date;
@Column(resolveDataType('json')) @Column(jsonColumnType)
workflowData: IWorkflowDb; workflowData: IWorkflowDb;
@Column({ nullable: true }) @Column({ nullable: true })
workflowId: string; workflowId: string;
@Column({ type: resolveDataType('datetime') as ColumnOptions['type'], nullable: true }) @Column({ type: datetimeColumnType, nullable: true })
waitTill: Date; waitTill: Date;
} }

View file

@ -1,4 +1,3 @@
/* eslint-disable import/no-cycle */
import { Column, Entity, JoinColumn, ManyToOne, PrimaryColumn } from 'typeorm'; import { Column, Entity, JoinColumn, ManyToOne, PrimaryColumn } from 'typeorm';
import { InstalledPackages } from './InstalledPackages'; import { InstalledPackages } from './InstalledPackages';

View file

@ -1,35 +1,9 @@
/* eslint-disable import/no-cycle */ import { Column, Entity, JoinColumn, OneToMany, PrimaryColumn } from 'typeorm';
import {
BeforeUpdate,
Column,
CreateDateColumn,
Entity,
JoinColumn,
OneToMany,
PrimaryColumn,
UpdateDateColumn,
} from 'typeorm';
import { IsDate, IsOptional } from 'class-validator';
import config = require('../../../config');
import { DatabaseType } from '../../index';
import { InstalledNodes } from './InstalledNodes'; import { InstalledNodes } from './InstalledNodes';
import { AbstractEntity } from './AbstractEntity';
function getTimestampSyntax() {
const dbType = config.get('database.type') as DatabaseType;
const map: { [key in DatabaseType]: string } = {
sqlite: "STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')",
postgresdb: 'CURRENT_TIMESTAMP(3)',
mysqldb: 'CURRENT_TIMESTAMP(3)',
mariadb: 'CURRENT_TIMESTAMP(3)',
};
return map[dbType];
}
@Entity() @Entity()
export class InstalledPackages { export class InstalledPackages extends AbstractEntity {
@PrimaryColumn() @PrimaryColumn()
packageName: string; packageName: string;
@ -45,23 +19,4 @@ export class InstalledPackages {
@OneToMany(() => InstalledNodes, (installedNode) => installedNode.package) @OneToMany(() => InstalledNodes, (installedNode) => installedNode.package)
@JoinColumn({ referencedColumnName: 'package' }) @JoinColumn({ referencedColumnName: 'package' })
installedNodes: InstalledNodes[]; installedNodes: InstalledNodes[];
@CreateDateColumn({ precision: 3, default: () => getTimestampSyntax() })
@IsOptional() // ignored by validation because set at DB level
@IsDate()
createdAt: Date;
@UpdateDateColumn({
precision: 3,
default: () => getTimestampSyntax(),
onUpdate: getTimestampSyntax(),
})
@IsOptional() // ignored by validation because set at DB level
@IsDate()
updatedAt: Date;
@BeforeUpdate()
setUpdateDate(): void {
this.updatedAt = new Date();
}
} }

View file

@ -1,42 +1,17 @@
/* eslint-disable import/no-cycle */ import { Column, Entity, OneToMany, PrimaryGeneratedColumn, Unique } from 'typeorm';
import { import { IsString, Length } from 'class-validator';
BeforeUpdate,
Column,
CreateDateColumn,
Entity,
OneToMany,
PrimaryGeneratedColumn,
Unique,
UpdateDateColumn,
} from 'typeorm';
import { IsDate, IsOptional, IsString, Length } from 'class-validator';
import * as config from '../../../config';
import { DatabaseType } from '../../index';
import { User } from './User'; import { User } from './User';
import { SharedWorkflow } from './SharedWorkflow'; import { SharedWorkflow } from './SharedWorkflow';
import { SharedCredentials } from './SharedCredentials'; import { SharedCredentials } from './SharedCredentials';
import { AbstractEntity } from './AbstractEntity';
type RoleNames = 'owner' | 'member' | 'user' | 'editor'; type RoleNames = 'owner' | 'member' | 'user' | 'editor';
type RoleScopes = 'global' | 'workflow' | 'credential'; type RoleScopes = 'global' | 'workflow' | 'credential';
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
function getTimestampSyntax() {
const dbType = config.getEnv('database.type');
const map: { [key in DatabaseType]: string } = {
sqlite: "STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')",
postgresdb: 'CURRENT_TIMESTAMP(3)',
mysqldb: 'CURRENT_TIMESTAMP(3)',
mariadb: 'CURRENT_TIMESTAMP(3)',
};
return map[dbType];
}
@Entity() @Entity()
@Unique(['scope', 'name']) @Unique(['scope', 'name'])
export class Role { export class Role extends AbstractEntity {
@PrimaryGeneratedColumn() @PrimaryGeneratedColumn()
id: number; id: number;
@ -51,28 +26,9 @@ export class Role {
@OneToMany(() => User, (user) => user.globalRole) @OneToMany(() => User, (user) => user.globalRole)
globalForUsers: User[]; globalForUsers: User[];
@CreateDateColumn({ precision: 3, default: () => getTimestampSyntax() })
@IsOptional() // ignored by validation because set at DB level
@IsDate()
createdAt: Date;
@UpdateDateColumn({
precision: 3,
default: () => getTimestampSyntax(),
onUpdate: getTimestampSyntax(),
})
@IsOptional() // ignored by validation because set at DB level
@IsDate()
updatedAt: Date;
@OneToMany(() => SharedWorkflow, (sharedWorkflow) => sharedWorkflow.role) @OneToMany(() => SharedWorkflow, (sharedWorkflow) => sharedWorkflow.role)
sharedWorkflows: SharedWorkflow[]; sharedWorkflows: SharedWorkflow[];
@OneToMany(() => SharedCredentials, (sharedCredentials) => sharedCredentials.role) @OneToMany(() => SharedCredentials, (sharedCredentials) => sharedCredentials.role)
sharedCredentials: SharedCredentials[]; sharedCredentials: SharedCredentials[];
@BeforeUpdate()
setUpdateDate(): void {
this.updatedAt = new Date();
}
} }

View file

@ -1,6 +1,3 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable import/no-cycle */
import { Column, Entity, PrimaryColumn } from 'typeorm'; import { Column, Entity, PrimaryColumn } from 'typeorm';
import { ISettingsDb } from '../..'; import { ISettingsDb } from '../..';

View file

@ -1,36 +1,11 @@
/* eslint-disable import/no-cycle */ import { Entity, ManyToOne, RelationId } from 'typeorm';
import {
BeforeUpdate,
CreateDateColumn,
Entity,
ManyToOne,
RelationId,
UpdateDateColumn,
} from 'typeorm';
import { IsDate, IsOptional } from 'class-validator';
import * as config from '../../../config';
import { DatabaseType } from '../../index';
import { CredentialsEntity } from './CredentialsEntity'; import { CredentialsEntity } from './CredentialsEntity';
import { User } from './User'; import { User } from './User';
import { Role } from './Role'; import { Role } from './Role';
import { AbstractEntity } from './AbstractEntity';
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
function getTimestampSyntax() {
const dbType = config.getEnv('database.type');
const map: { [key in DatabaseType]: string } = {
sqlite: "STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')",
postgresdb: 'CURRENT_TIMESTAMP(3)',
mysqldb: 'CURRENT_TIMESTAMP(3)',
mariadb: 'CURRENT_TIMESTAMP(3)',
};
return map[dbType];
}
@Entity() @Entity()
export class SharedCredentials { export class SharedCredentials extends AbstractEntity {
@ManyToOne(() => Role, (role) => role.sharedCredentials, { nullable: false }) @ManyToOne(() => Role, (role) => role.sharedCredentials, { nullable: false })
role: Role; role: Role;
@ -48,23 +23,4 @@ export class SharedCredentials {
@RelationId((sharedCredential: SharedCredentials) => sharedCredential.credentials) @RelationId((sharedCredential: SharedCredentials) => sharedCredential.credentials)
credentialId: number; credentialId: number;
@CreateDateColumn({ precision: 3, default: () => getTimestampSyntax() })
@IsOptional() // ignored by validation because set at DB level
@IsDate()
createdAt: Date;
@UpdateDateColumn({
precision: 3,
default: () => getTimestampSyntax(),
onUpdate: getTimestampSyntax(),
})
@IsOptional() // ignored by validation because set at DB level
@IsDate()
updatedAt: Date;
@BeforeUpdate()
setUpdateDate(): void {
this.updatedAt = new Date();
}
} }

View file

@ -1,36 +1,11 @@
/* eslint-disable import/no-cycle */ import { Entity, ManyToOne, RelationId } from 'typeorm';
import {
BeforeUpdate,
CreateDateColumn,
Entity,
ManyToOne,
RelationId,
UpdateDateColumn,
} from 'typeorm';
import { IsDate, IsOptional } from 'class-validator';
import * as config from '../../../config';
import { DatabaseType } from '../../index';
import { WorkflowEntity } from './WorkflowEntity'; import { WorkflowEntity } from './WorkflowEntity';
import { User } from './User'; import { User } from './User';
import { Role } from './Role'; import { Role } from './Role';
import { AbstractEntity } from './AbstractEntity';
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
function getTimestampSyntax() {
const dbType = config.getEnv('database.type');
const map: { [key in DatabaseType]: string } = {
sqlite: "STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')",
postgresdb: 'CURRENT_TIMESTAMP(3)',
mysqldb: 'CURRENT_TIMESTAMP(3)',
mariadb: 'CURRENT_TIMESTAMP(3)',
};
return map[dbType];
}
@Entity() @Entity()
export class SharedWorkflow { export class SharedWorkflow extends AbstractEntity {
@ManyToOne(() => Role, (role) => role.sharedWorkflows, { nullable: false }) @ManyToOne(() => Role, (role) => role.sharedWorkflows, { nullable: false })
role: Role; role: Role;
@ -48,23 +23,4 @@ export class SharedWorkflow {
@RelationId((sharedWorkflow: SharedWorkflow) => sharedWorkflow.workflow) @RelationId((sharedWorkflow: SharedWorkflow) => sharedWorkflow.workflow)
workflowId: number; workflowId: number;
@CreateDateColumn({ precision: 3, default: () => getTimestampSyntax() })
@IsOptional() // ignored by validation because set at DB level
@IsDate()
createdAt: Date;
@UpdateDateColumn({
precision: 3,
default: () => getTimestampSyntax(),
onUpdate: getTimestampSyntax(),
})
@IsOptional() // ignored by validation because set at DB level
@IsDate()
updatedAt: Date;
@BeforeUpdate()
setUpdateDate(): void {
this.updatedAt = new Date();
}
} }

View file

@ -1,40 +1,13 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import { Column, Entity, Generated, Index, ManyToMany, PrimaryColumn } from 'typeorm';
/* eslint-disable import/no-cycle */ import { IsString, Length } from 'class-validator';
import {
BeforeUpdate,
Column,
CreateDateColumn,
Entity,
Generated,
Index,
ManyToMany,
PrimaryColumn,
UpdateDateColumn,
} from 'typeorm';
import { IsDate, IsOptional, IsString, Length } from 'class-validator';
import * as config from '../../../config';
import { DatabaseType } from '../../index';
import { ITagDb } from '../../Interfaces'; import { ITagDb } from '../../Interfaces';
import { idStringifier } from '../utils/transformers'; import { idStringifier } from '../utils/transformers';
import { WorkflowEntity } from './WorkflowEntity'; import { WorkflowEntity } from './WorkflowEntity';
import { AbstractEntity } from './AbstractEntity';
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
function getTimestampSyntax() {
const dbType = config.getEnv('database.type');
const map: { [key in DatabaseType]: string } = {
sqlite: "STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')",
postgresdb: 'CURRENT_TIMESTAMP(3)',
mysqldb: 'CURRENT_TIMESTAMP(3)',
mariadb: 'CURRENT_TIMESTAMP(3)',
};
return map[dbType];
}
@Entity() @Entity()
export class TagEntity implements ITagDb { export class TagEntity extends AbstractEntity implements ITagDb {
@Generated() @Generated()
@PrimaryColumn({ @PrimaryColumn({
transformer: idStringifier, transformer: idStringifier,
@ -47,25 +20,6 @@ export class TagEntity implements ITagDb {
@Length(1, 24, { message: 'Tag name must be $constraint1 to $constraint2 characters long.' }) @Length(1, 24, { message: 'Tag name must be $constraint1 to $constraint2 characters long.' })
name: string; name: string;
@CreateDateColumn({ precision: 3, default: () => getTimestampSyntax() })
@IsOptional() // ignored by validation because set at DB level
@IsDate()
createdAt: Date;
@UpdateDateColumn({
precision: 3,
default: () => getTimestampSyntax(),
onUpdate: getTimestampSyntax(),
})
@IsOptional() // ignored by validation because set at DB level
@IsDate()
updatedAt: Date;
@ManyToMany(() => WorkflowEntity, (workflow) => workflow.tags) @ManyToMany(() => WorkflowEntity, (workflow) => workflow.tags)
workflows: WorkflowEntity[]; workflows: WorkflowEntity[];
@BeforeUpdate()
setUpdateDate() {
this.updatedAt = new Date();
}
} }

View file

@ -1,66 +1,31 @@
/* eslint-disable import/no-cycle */
import { import {
AfterLoad, AfterLoad,
AfterUpdate, AfterUpdate,
BeforeUpdate, BeforeUpdate,
Column, Column,
ColumnOptions,
CreateDateColumn,
Entity, Entity,
Index, Index,
OneToMany, OneToMany,
ManyToOne, ManyToOne,
PrimaryGeneratedColumn, PrimaryGeneratedColumn,
UpdateDateColumn,
BeforeInsert, BeforeInsert,
} from 'typeorm'; } from 'typeorm';
import { IsEmail, IsString, Length } from 'class-validator'; import { IsEmail, IsString, Length } from 'class-validator';
import type { IUser } from 'n8n-workflow'; import type { IUser } from 'n8n-workflow';
import * as config from '../../../config';
import { DatabaseType, IPersonalizationSurveyAnswers, IUserSettings } from '../..';
import { Role } from './Role'; import { Role } from './Role';
import { SharedWorkflow } from './SharedWorkflow'; import { SharedWorkflow } from './SharedWorkflow';
import { SharedCredentials } from './SharedCredentials'; import { SharedCredentials } from './SharedCredentials';
import { NoXss } from '../utils/customValidators'; import { NoXss } from '../utils/customValidators';
import { objectRetriever, lowerCaser } from '../utils/transformers'; import { objectRetriever, lowerCaser } from '../utils/transformers';
import { AbstractEntity, jsonColumnType } from './AbstractEntity';
import type { IPersonalizationSurveyAnswers, IUserSettings } from '../../Interfaces';
export const MIN_PASSWORD_LENGTH = 8; export const MIN_PASSWORD_LENGTH = 8;
export const MAX_PASSWORD_LENGTH = 64; export const MAX_PASSWORD_LENGTH = 64;
function resolveDataType(dataType: string) {
const dbType = config.getEnv('database.type');
const typeMap: { [key in DatabaseType]: { [key: string]: string } } = {
sqlite: {
json: 'simple-json',
},
postgresdb: {
datetime: 'timestamptz',
},
mysqldb: {},
mariadb: {},
};
return typeMap[dbType][dataType] ?? dataType;
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
function getTimestampSyntax() {
const dbType = config.getEnv('database.type');
const map: { [key in DatabaseType]: string } = {
sqlite: "STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')",
postgresdb: 'CURRENT_TIMESTAMP(3)',
mysqldb: 'CURRENT_TIMESTAMP(3)',
mariadb: 'CURRENT_TIMESTAMP(3)',
};
return map[dbType];
}
@Entity() @Entity()
export class User implements IUser { export class User extends AbstractEntity implements IUser {
@PrimaryGeneratedColumn('uuid') @PrimaryGeneratedColumn('uuid')
id: string; id: string;
@ -97,14 +62,14 @@ export class User implements IUser {
resetPasswordTokenExpiration?: number | null; resetPasswordTokenExpiration?: number | null;
@Column({ @Column({
type: resolveDataType('json') as ColumnOptions['type'], type: jsonColumnType,
nullable: true, nullable: true,
transformer: objectRetriever, transformer: objectRetriever,
}) })
personalizationAnswers: IPersonalizationSurveyAnswers | null; personalizationAnswers: IPersonalizationSurveyAnswers | null;
@Column({ @Column({
type: resolveDataType('json') as ColumnOptions['type'], type: jsonColumnType,
nullable: true, nullable: true,
}) })
settings: IUserSettings | null; settings: IUserSettings | null;
@ -121,21 +86,10 @@ export class User implements IUser {
@OneToMany(() => SharedCredentials, (sharedCredentials) => sharedCredentials.user) @OneToMany(() => SharedCredentials, (sharedCredentials) => sharedCredentials.user)
sharedCredentials: SharedCredentials[]; sharedCredentials: SharedCredentials[];
@CreateDateColumn({ precision: 3, default: () => getTimestampSyntax() })
createdAt: Date;
@UpdateDateColumn({
precision: 3,
default: () => getTimestampSyntax(),
onUpdate: getTimestampSyntax(),
})
updatedAt: Date;
@BeforeInsert() @BeforeInsert()
@BeforeUpdate() @BeforeUpdate()
preUpsertHook(): void { preUpsertHook(): void {
this.email = this.email?.toLowerCase() ?? null; this.email = this.email?.toLowerCase() ?? null;
this.updatedAt = new Date();
} }
@Column({ type: String, nullable: true }) @Column({ type: String, nullable: true })

View file

@ -1,6 +1,5 @@
import { Column, Entity, Index, PrimaryColumn } from 'typeorm'; import { Column, Entity, Index, PrimaryColumn } from 'typeorm';
// eslint-disable-next-line import/no-cycle
import { IWebhookDb } from '../../Interfaces'; import { IWebhookDb } from '../../Interfaces';
@Entity() @Entity()

View file

@ -1,8 +1,6 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable import/no-cycle */
import { Length } from 'class-validator'; import { Length } from 'class-validator';
import { import type {
IBinaryKeyData, IBinaryKeyData,
IConnections, IConnections,
IDataObject, IDataObject,
@ -12,58 +10,24 @@ import {
} from 'n8n-workflow'; } from 'n8n-workflow';
import { import {
BeforeUpdate,
Column, Column,
ColumnOptions,
CreateDateColumn,
Entity, Entity,
Index, Index,
JoinTable, JoinTable,
ManyToMany, ManyToMany,
OneToMany, OneToMany,
PrimaryGeneratedColumn, PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm'; } from 'typeorm';
import * as config from '../../../config'; import * as config from '../../../config';
import { DatabaseType, IWorkflowDb } from '../..';
import { TagEntity } from './TagEntity'; import { TagEntity } from './TagEntity';
import { SharedWorkflow } from './SharedWorkflow'; import { SharedWorkflow } from './SharedWorkflow';
import { objectRetriever, sqlite } from '../utils/transformers'; import { objectRetriever, sqlite } from '../utils/transformers';
import { AbstractEntity, jsonColumnType } from './AbstractEntity';
function resolveDataType(dataType: string) { import type { IWorkflowDb } from '../../Interfaces';
const dbType = config.getEnv('database.type');
const typeMap: { [key in DatabaseType]: { [key: string]: string } } = {
sqlite: {
json: 'simple-json',
},
postgresdb: {
datetime: 'timestamptz',
},
mysqldb: {},
mariadb: {},
};
return typeMap[dbType][dataType] ?? dataType;
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
function getTimestampSyntax() {
const dbType = config.getEnv('database.type');
const map: { [key in DatabaseType]: string } = {
sqlite: "STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')",
postgresdb: 'CURRENT_TIMESTAMP(3)',
mysqldb: 'CURRENT_TIMESTAMP(3)',
mariadb: 'CURRENT_TIMESTAMP(3)',
};
return map[dbType];
}
@Entity() @Entity()
export class WorkflowEntity implements IWorkflowDb { export class WorkflowEntity extends AbstractEntity implements IWorkflowDb {
@PrimaryGeneratedColumn() @PrimaryGeneratedColumn()
id: number; id: number;
@ -78,30 +42,20 @@ export class WorkflowEntity implements IWorkflowDb {
@Column() @Column()
active: boolean; active: boolean;
@Column(resolveDataType('json')) @Column(jsonColumnType)
nodes: INode[]; nodes: INode[];
@Column(resolveDataType('json')) @Column(jsonColumnType)
connections: IConnections; connections: IConnections;
@CreateDateColumn({ precision: 3, default: () => getTimestampSyntax() })
createdAt: Date;
@UpdateDateColumn({
precision: 3,
default: () => getTimestampSyntax(),
onUpdate: getTimestampSyntax(),
})
updatedAt: Date;
@Column({ @Column({
type: resolveDataType('json') as ColumnOptions['type'], type: jsonColumnType,
nullable: true, nullable: true,
}) })
settings?: IWorkflowSettings; settings?: IWorkflowSettings;
@Column({ @Column({
type: resolveDataType('json') as ColumnOptions['type'], type: jsonColumnType,
nullable: true, nullable: true,
transformer: objectRetriever, transformer: objectRetriever,
}) })
@ -130,11 +84,6 @@ export class WorkflowEntity implements IWorkflowDb {
transformer: sqlite.jsonColumn, transformer: sqlite.jsonColumn,
}) })
pinData: ISimplifiedPinData; pinData: ISimplifiedPinData;
@BeforeUpdate()
setUpdateDate() {
this.updatedAt = new Date();
}
} }
/** /**

View file

@ -1,5 +1,4 @@
/* eslint-disable @typescript-eslint/naming-convention */ /* eslint-disable @typescript-eslint/naming-convention */
/* eslint-disable import/no-cycle */
import { CredentialsEntity } from './CredentialsEntity'; import { CredentialsEntity } from './CredentialsEntity';
import { ExecutionEntity } from './ExecutionEntity'; import { ExecutionEntity } from './ExecutionEntity';
import { WorkflowEntity } from './WorkflowEntity'; import { WorkflowEntity } from './WorkflowEntity';