@sphereon/ssi-sdk.data-store
Version:
69 lines (57 loc) • 1.8 kB
text/typescript
import { typeOrmDateTime } from '@sphereon/ssi-sdk.agent-config'
import {
BeforeInsert,
BeforeUpdate,
Column,
CreateDateColumn,
Entity,
Index,
JoinColumn,
ManyToOne,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm'
import { PartyEntity } from './PartyEntity'
('PartyRelationship')
('IDX_PartyRelationship_left_right', ['left', 'right'], { unique: true })
export class PartyRelationshipEntity {
('uuid')
id!: string
(() => PartyEntity, {
nullable: false,
onDelete: 'CASCADE',
})
({ name: 'left_id' })
left!: PartyEntity
('text', { name: 'left_id', nullable: false })
leftId!: string
(() => PartyEntity, {
nullable: false,
onDelete: 'CASCADE',
})
({ name: 'right_id' })
right!: PartyEntity
('text', { name: 'right_id', nullable: false })
rightId!: string
('text', { name: 'owner_id', nullable: true })
ownerId?: string
('text', { name: 'tenant_id', nullable: true })
tenantId?: string
({ name: 'created_at', nullable: false, type: typeOrmDateTime() })
createdAt!: Date
({ name: 'last_updated_at', nullable: false, type: typeOrmDateTime() })
lastUpdatedAt!: Date
// By default, @UpdateDateColumn in TypeORM updates the timestamp only when the entity's top-level properties change.
()
()
updateUpdatedDate(): void {
this.lastUpdatedAt = new Date()
}
()
()
async checkRelationshipSides(): Promise<void> {
if ((this.left?.id ?? this.leftId) === (this.right?.id ?? this.rightId)) {
return Promise.reject(Error('Cannot use the same id for both sides of the relationship'))
}
}
}