From f1f57eca54d57956d3bea050a59ed3205a3ef60b Mon Sep 17 00:00:00 2001 From: David Mehren Date: Sat, 3 Oct 2020 11:17:07 +0200 Subject: [PATCH] Add note metadata properties and Tag entity. These were planned to be parsed at runtime from the note-content in the database, but having to run a markdown parser in the backend was found to be a bad idea. Now the frontend (that already implements the parsing logic) has to set title, description and tags. Signed-off-by: David Mehren Co-authored-by: Yannick Bungers --- src/notes/note.entity.ts | 27 +++++++++++++++++++++++++-- src/notes/notes.module.ts | 3 ++- src/notes/tag.entity.ts | 19 +++++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 src/notes/tag.entity.ts diff --git a/src/notes/note.entity.ts b/src/notes/note.entity.ts index 815e7c643..1f412775b 100644 --- a/src/notes/note.entity.ts +++ b/src/notes/note.entity.ts @@ -2,6 +2,8 @@ import { generate as shortIdGenerate } from 'shortid'; import { Column, Entity, + JoinTable, + ManyToMany, ManyToOne, OneToMany, PrimaryGeneratedColumn, @@ -11,6 +13,7 @@ import { NoteUserPermission } from '../permissions/note-user-permission.entity'; import { Revision } from '../revisions/revision.entity'; import { User } from '../users/user.entity'; import { AuthorColor } from './author-color.entity'; +import { Tag } from './tag.entity'; @Entity('Notes') export class Note { @@ -25,7 +28,7 @@ export class Note { unique: true, nullable: true, }) - alias: string; + alias?: string; @OneToMany( _ => NoteGroupPermission, groupPermission => groupPermission.note, @@ -59,10 +62,26 @@ export class Note { ) authorColors: AuthorColor[]; + @Column({ + nullable: true, + }) + description?: string; + @Column({ + nullable: true, + }) + title?: string; + + @ManyToMany( + _ => Tag, + tag => tag.notes, + ) + @JoinTable() + tags: Tag[]; + // eslint-disable-next-line @typescript-eslint/no-empty-function private constructor() {} - public static create(owner?: User, alias?: string, shortid?: string) { + public static create(owner?: User, alias?: string, shortid?: string): Note { if (!shortid) { shortid = shortIdGenerate(); } @@ -74,6 +93,10 @@ export class Note { newNote.authorColors = []; newNote.userPermissions = []; newNote.groupPermissions = []; + newNote.revisions = Promise.resolve([]); + newNote.description = null; + newNote.title = null; + newNote.tags = []; return newNote; } } diff --git a/src/notes/notes.module.ts b/src/notes/notes.module.ts index f5a93a721..b35c300d3 100644 --- a/src/notes/notes.module.ts +++ b/src/notes/notes.module.ts @@ -6,10 +6,11 @@ import { UsersModule } from '../users/users.module'; import { AuthorColor } from './author-color.entity'; import { Note } from './note.entity'; import { NotesService } from './notes.service'; +import { Tag } from './tag.entity'; @Module({ imports: [ - TypeOrmModule.forFeature([Note, AuthorColor]), + TypeOrmModule.forFeature([Note, AuthorColor, Tag]), forwardRef(() => RevisionsModule), UsersModule, LoggerModule, diff --git a/src/notes/tag.entity.ts b/src/notes/tag.entity.ts new file mode 100644 index 000000000..fb3a1ad98 --- /dev/null +++ b/src/notes/tag.entity.ts @@ -0,0 +1,19 @@ +import { Column, Entity, ManyToMany, PrimaryGeneratedColumn } from 'typeorm'; +import { Note } from './note.entity'; + +@Entity() +export class Tag { + @PrimaryGeneratedColumn() + id: number; + + @Column({ + nullable: false, + }) + name: string; + + @ManyToMany( + _ => Note, + note => note.tags, + ) + notes: Note[]; +}