* ⚡ Enable `esModuleInterop` for /core * ⚡ Adjust imports in /core * ⚡ Enable `esModuleInterop` for /cli * ⚡ Adjust imports in /cli * ⚡ Enable `esModuleInterop` for /nodes-base * ⚡ Adjust imports in /nodes-base * ⚡ Make imports consistent * ⬆️ Upgrade TypeScript to 4.6 (#3109) * ⬆️ Upgrade TypeScript to 4.6 * 📦 Update package-lock.json * 🔧 Avoid erroring on untyped errors * 📘 Fix type error Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
71 lines
1.8 KiB
TypeScript
71 lines
1.8 KiB
TypeScript
/* eslint-disable import/no-cycle */
|
|
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 { User } from './User';
|
|
import { Role } from './Role';
|
|
|
|
// 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()
|
|
export class SharedCredentials {
|
|
@ManyToOne(() => Role, (role) => role.sharedCredentials, { nullable: false })
|
|
role: Role;
|
|
|
|
@ManyToOne(() => User, (user) => user.sharedCredentials, { primary: true })
|
|
user: User;
|
|
|
|
@RelationId((sharedCredential: SharedCredentials) => sharedCredential.user)
|
|
userId: string;
|
|
|
|
@ManyToOne(() => CredentialsEntity, (credentials) => credentials.shared, {
|
|
primary: true,
|
|
onDelete: 'CASCADE',
|
|
})
|
|
credentials: CredentialsEntity;
|
|
|
|
@RelationId((sharedCredential: SharedCredentials) => sharedCredential.credentials)
|
|
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();
|
|
}
|
|
}
|