diff --git a/packages/daf-core/src/entities/credential.ts b/packages/daf-core/src/entities/credential.ts index e9e47929c..0c3cbd810 100644 --- a/packages/daf-core/src/entities/credential.ts +++ b/packages/daf-core/src/entities/credential.ts @@ -9,7 +9,6 @@ import { OneToMany, ManyToMany, BeforeInsert, - AfterInsert, } from 'typeorm' import { Identity } from './identity' import { Message } from './message' @@ -48,23 +47,29 @@ export class Credential extends BaseEntity { @ManyToOne( type => Identity, identity => identity.issuedCredentials, + { + cascade: ['insert'], + }, ) issuer: Identity @ManyToOne( type => Identity, identity => identity.receivedCredentials, + { + cascade: ['insert'], + }, ) subject: Identity - @Column() - issuedAt: number + @Column({ nullable: true }) + issuedAt?: Date - @Column() - notBefore: number + @Column({ nullable: true }) + notBefore?: Date - @Column() - expiresAt: number + @Column({ nullable: true }) + expiresAt?: Date @Column() raw: string diff --git a/packages/daf-core/src/entities/identity.ts b/packages/daf-core/src/entities/identity.ts index 601c3e27f..a2bc445e0 100644 --- a/packages/daf-core/src/entities/identity.ts +++ b/packages/daf-core/src/entities/identity.ts @@ -1,4 +1,4 @@ -import { Entity, Column, PrimaryColumn, BaseEntity, OneToMany } from 'typeorm' +import { Entity, Column, PrimaryColumn, BaseEntity, OneToMany, ManyToMany } from 'typeorm' import { Key } from './key' import { Action } from './action' import { Message } from './message' @@ -11,7 +11,7 @@ export class Identity extends BaseEntity { @PrimaryColumn() did: string - @Column() + @Column({ nullable: true }) controllerKeyId: string @OneToMany( @@ -28,13 +28,13 @@ export class Identity extends BaseEntity { @OneToMany( type => Message, - message => message.sender, + message => message.from, ) sentMessages: Message[] - @OneToMany( + @ManyToMany( type => Message, - message => message.receiver, + message => message.to, ) receivedMessages: Message[] diff --git a/packages/daf-core/src/entities/message-meta-data.ts b/packages/daf-core/src/entities/message-meta-data.ts index 3174ef88b..f0491e12d 100644 --- a/packages/daf-core/src/entities/message-meta-data.ts +++ b/packages/daf-core/src/entities/message-meta-data.ts @@ -17,7 +17,4 @@ export class MessageMetaData extends BaseEntity { message => message.metaData, ) message: Message - - @Column() - timestamp: number } diff --git a/packages/daf-core/src/entities/message.ts b/packages/daf-core/src/entities/message.ts index 2d0da27d5..b97d65787 100644 --- a/packages/daf-core/src/entities/message.ts +++ b/packages/daf-core/src/entities/message.ts @@ -7,6 +7,8 @@ import { PrimaryGeneratedColumn, OneToMany, JoinTable, + CreateDateColumn, + UpdateDateColumn, } from 'typeorm' import { Identity } from './identity' import { MessageMetaData } from './message-meta-data' @@ -18,42 +20,74 @@ export class Message extends BaseEntity { @PrimaryGeneratedColumn() id: string - @Column() - threadId: string + @CreateDateColumn() + saveDate: Date + + @UpdateDateColumn() + updateDate: Date + + @Column({ nullable: true }) + createdAt?: Date + + @Column({ nullable: true }) + expiresAt?: Date + + @Column({ nullable: true }) + threadId?: string @Column() type: string + @Column() + raw: string + + @Column('simple-json', { nullable: true }) + data?: object + + // https://github.com/decentralized-identity/didcomm-messaging/blob/41f35f992275dd71d459504d14eb8d70b4185533/jwm.md#jwm-profile + + @Column('simple-array', { nullable: true }) + replyTo?: string[] + + @Column({ nullable: true }) + replyUrl?: string + @ManyToOne( type => Identity, identity => identity.sentMessages, + { + nullable: true, + cascade: ['insert'], + }, ) - sender: Identity + from?: Identity - @ManyToOne( + @ManyToMany( type => Identity, identity => identity.receivedMessages, + { + nullable: true, + cascade: ['insert'], + }, ) - receiver: Identity - - @Column() - timestamp: number - - @Column() - raw: string - - @Column() - data: string + @JoinTable() + to?: Identity[] @OneToMany( type => MessageMetaData, messageMetaData => messageMetaData.message, + { + cascade: true, + }, ) metaData: MessageMetaData[] @ManyToMany( type => Presentation, presentation => presentation.messages, + { + cascade: true, + }, ) @JoinTable() presentations: Presentation[] @@ -61,6 +95,9 @@ export class Message extends BaseEntity { @ManyToMany( type => Credential, credential => credential.messages, + { + cascade: true, + }, ) @JoinTable() credentials: Credential[] diff --git a/packages/daf-core/src/entities/presentation.ts b/packages/daf-core/src/entities/presentation.ts index ff2e9a783..a237c6892 100644 --- a/packages/daf-core/src/entities/presentation.ts +++ b/packages/daf-core/src/entities/presentation.ts @@ -1,3 +1,4 @@ +import { blake2bHex } from 'blakejs' import { Entity, Column, @@ -5,7 +6,7 @@ import { ManyToOne, JoinTable, PrimaryColumn, - OneToMany, + BeforeInsert, ManyToMany, } from 'typeorm' import { Identity } from './identity' @@ -17,26 +18,36 @@ export class Presentation extends BaseEntity { @PrimaryColumn() hash: string + @BeforeInsert() + async updateHash() { + this.hash = blake2bHex(this.raw) + } @ManyToOne( type => Identity, identity => identity.issuedPresentations, + { + cascade: ['insert'], + }, ) issuer: Identity @ManyToOne( type => Identity, identity => identity.receivedPresentations, + { + cascade: ['insert'], + }, ) audience: Identity - @Column() - issuedAt: number + @Column({ nullable: true }) + issuedAt?: Date - @Column() - notBefore: number + @Column({ nullable: true }) + notBefore?: Date - @Column() - expiresAt: number + @Column({ nullable: true }) + expiresAt?: Date @Column() raw: string @@ -48,6 +59,9 @@ export class Presentation extends BaseEntity { @ManyToMany( type => Credential, credential => credential.presentations, + { + cascade: true, + }, ) @JoinTable() credentials: Credential[]