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
15 changed files with 86 additions and 495 deletions

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 {
import type {
IBinaryKeyData,
IConnections,
IDataObject,
@@ -12,58 +10,24 @@ import {
} from 'n8n-workflow';
import {
BeforeUpdate,
Column,
ColumnOptions,
CreateDateColumn,
Entity,
Index,
JoinTable,
ManyToMany,
OneToMany,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm';
import * as config from '../../../config';
import { DatabaseType, IWorkflowDb } from '../..';
import { TagEntity } from './TagEntity';
import { SharedWorkflow } from './SharedWorkflow';
import { objectRetriever, sqlite } from '../utils/transformers';
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];
}
import { AbstractEntity, jsonColumnType } from './AbstractEntity';
import type { IWorkflowDb } from '../../Interfaces';
@Entity()
export class WorkflowEntity implements IWorkflowDb {
export class WorkflowEntity extends AbstractEntity implements IWorkflowDb {
@PrimaryGeneratedColumn()
id: number;
@@ -78,30 +42,20 @@ export class WorkflowEntity implements IWorkflowDb {
@Column()
active: boolean;
@Column(resolveDataType('json'))
@Column(jsonColumnType)
nodes: INode[];
@Column(resolveDataType('json'))
@Column(jsonColumnType)
connections: IConnections;
@CreateDateColumn({ precision: 3, default: () => getTimestampSyntax() })
createdAt: Date;
@UpdateDateColumn({
precision: 3,
default: () => getTimestampSyntax(),
onUpdate: getTimestampSyntax(),
})
updatedAt: Date;
@Column({
type: resolveDataType('json') as ColumnOptions['type'],
type: jsonColumnType,
nullable: true,
})
settings?: IWorkflowSettings;
@Column({
type: resolveDataType('json') as ColumnOptions['type'],
type: jsonColumnType,
nullable: true,
transformer: objectRetriever,
})
@@ -130,11 +84,6 @@ export class WorkflowEntity implements IWorkflowDb {
transformer: sqlite.jsonColumn,
})
pinData: ISimplifiedPinData;
@BeforeUpdate()
setUpdateDate() {
this.updatedAt = new Date();
}
}
/**