mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
Some checks failed
Test Master / install-and-build (push) Waiting to run
Test Master / Unit tests (18.x) (push) Blocked by required conditions
Test Master / Unit tests (20.x) (push) Blocked by required conditions
Test Master / Unit tests (22.4) (push) Blocked by required conditions
Test Master / Lint (push) Blocked by required conditions
Test Master / Notify Slack on failure (push) Blocked by required conditions
Benchmark Docker Image CI / build (push) Has been cancelled
Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
69 lines
1.6 KiB
TypeScript
69 lines
1.6 KiB
TypeScript
import { GlobalConfig } from '@n8n/config';
|
|
import { Container } from '@n8n/di';
|
|
import type { ColumnOptions } from '@n8n/typeorm';
|
|
import {
|
|
BeforeInsert,
|
|
BeforeUpdate,
|
|
CreateDateColumn,
|
|
PrimaryColumn,
|
|
UpdateDateColumn,
|
|
} from '@n8n/typeorm';
|
|
import type { Class } from 'n8n-core';
|
|
|
|
import { generateNanoId } from '../utils/generators';
|
|
|
|
export const { type: dbType } = Container.get(GlobalConfig).database;
|
|
|
|
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';
|
|
|
|
const tsColumnOptions: ColumnOptions = {
|
|
precision: 3,
|
|
default: () => timestampSyntax,
|
|
type: datetimeColumnType,
|
|
};
|
|
|
|
function mixinStringId<T extends Class<{}, any[]>>(base: T) {
|
|
class Derived extends base {
|
|
@PrimaryColumn('varchar')
|
|
id: string;
|
|
|
|
@BeforeInsert()
|
|
generateId() {
|
|
if (!this.id) {
|
|
this.id = generateNanoId();
|
|
}
|
|
}
|
|
}
|
|
return Derived;
|
|
}
|
|
|
|
function mixinTimestamps<T extends Class<{}, any[]>>(base: T) {
|
|
class Derived extends base {
|
|
@CreateDateColumn(tsColumnOptions)
|
|
createdAt: Date;
|
|
|
|
@UpdateDateColumn(tsColumnOptions)
|
|
updatedAt: Date;
|
|
|
|
@BeforeUpdate()
|
|
setUpdateDate(): void {
|
|
this.updatedAt = new Date();
|
|
}
|
|
}
|
|
return Derived;
|
|
}
|
|
|
|
class BaseEntity {}
|
|
|
|
export const WithStringId = mixinStringId(BaseEntity);
|
|
export const WithTimestamps = mixinTimestamps(BaseEntity);
|
|
export const WithTimestampsAndStringId = mixinStringId(WithTimestamps);
|