Files
n8n/packages/@n8n/db/src/entities/abstract-entity.ts
T
alighasami 3d5eaf9445
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
first commit
2026-03-17 16:22:57 +03:30

103 lines
2.5 KiB
TypeScript

import { GlobalConfig } from '@n8n/config';
import { Container } from '@n8n/di';
import type { ColumnOptions } from '@n8n/typeorm';
import {
BeforeInsert,
BeforeUpdate,
Column,
CreateDateColumn,
PrimaryColumn,
UpdateDateColumn,
} from '@n8n/typeorm';
import { generateNanoId } from '@n8n/utils';
import type { Class } from 'n8n-core';
export const { type: dbType } = Container.get(GlobalConfig).database;
const timestampSyntax = {
sqlite: "STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW')",
postgresdb: 'CURRENT_TIMESTAMP(3)',
}[dbType];
export const jsonColumnType = dbType === 'sqlite' ? 'simple-json' : 'json';
export const datetimeColumnType = dbType === 'postgresdb' ? 'timestamptz' : 'datetime';
const binaryColumnTypeMap = {
sqlite: 'blob',
postgresdb: 'bytea',
} as const;
const binaryColumnType = binaryColumnTypeMap[dbType];
export function JsonColumn(options?: Omit<ColumnOptions, 'type'>) {
return Column({
...options,
type: jsonColumnType,
});
}
export function DateTimeColumn(options?: Omit<ColumnOptions, 'type'>) {
return Column({
...options,
type: datetimeColumnType,
});
}
export function BinaryColumn(options?: Omit<ColumnOptions, 'type'>) {
return Column({
...options,
type: binaryColumnType,
});
}
const tsColumnOptions: ColumnOptions = {
precision: 3,
default: () => timestampSyntax,
type: datetimeColumnType,
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
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;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function mixinUpdatedAt<T extends Class<{}, any[]>>(base: T) {
class Derived extends base {
@UpdateDateColumn(tsColumnOptions)
updatedAt: Date;
@BeforeUpdate()
setUpdateDate(): void {
this.updatedAt = new Date();
}
}
return Derived;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function mixinCreatedAt<T extends Class<{}, any[]>>(base: T) {
class Derived extends base {
@CreateDateColumn(tsColumnOptions)
createdAt: Date;
}
return Derived;
}
class BaseEntity {}
export const WithStringId = mixinStringId(BaseEntity);
export const WithCreatedAt = mixinCreatedAt(BaseEntity);
export const WithUpdatedAt = mixinUpdatedAt(BaseEntity);
export const WithTimestamps = mixinCreatedAt(mixinUpdatedAt(BaseEntity));
export const WithTimestampsAndStringId = mixinStringId(WithTimestamps);