hedgedoc/backend/src/revisions/revision.entity.ts
Erik Michelson 68780f54e1
wip: range authorships backend storage
Co-authored-by: Philip Molares <philip.molares@udo.edu>
Signed-off-by: Philip Molares <philip.molares@udo.edu>
Signed-off-by: Erik Michelson <github@erik.michelson.eu>
2024-04-28 16:48:54 +02:00

120 lines
2.5 KiB
TypeScript

/*
* SPDX-FileCopyrightText: 2024 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import {
Column,
CreateDateColumn,
Entity,
JoinTable,
ManyToMany,
ManyToOne,
OneToMany,
PrimaryGeneratedColumn,
} from 'typeorm';
import { Note } from '../notes/note.entity';
import { Tag } from '../notes/tag.entity';
import { RangeAuthorship } from './range-authorship.entity';
/**
* The state of a note at a particular point in time,
* with the content at that time and the diff to the previous revision.
*
*/
@Entity()
export class Revision {
@PrimaryGeneratedColumn()
id: number;
/**
* The patch from the previous revision to this one.
*/
@Column({
type: 'text',
})
patch: string;
@Column({
type: 'text',
})
title: string;
@Column({
type: 'text',
})
description: string;
@ManyToMany((_) => Tag, (tag) => tag.revisions, {
eager: true,
cascade: true,
})
@JoinTable()
tags: Promise<Tag[]>;
/**
* The note content at this revision.
*/
@Column({
type: 'text',
})
content: string;
/**
* The length of the note content.
*/
@Column()
length: number;
@Column('simple-array', { nullable: true })
yjsStateVector: null | number[];
/**
* Date at which the revision was created.
*/
@CreateDateColumn()
createdAt: Date;
/**
* Note this revision belongs to.
*/
@ManyToOne((_) => Note, (note) => note.revisions, { onDelete: 'CASCADE' })
note: Promise<Note>;
/**
* All edit objects which are used in the revision.
*/
@OneToMany(
(_) => RangeAuthorship,
(rangeAuthorship) => rangeAuthorship.revision,
{ onDelete: 'CASCADE' },
)
rangeAuthorships: Promise<RangeAuthorship[]>;
// eslint-disable-next-line @typescript-eslint/no-empty-function
private constructor() {}
static create(
content: string,
patch: string,
note: Note,
yjsStateVector: number[] | null,
title: string,
description: string,
tags: Tag[],
rangeAuthorships: RangeAuthorship[],
): Omit<Revision, 'id' | 'createdAt'> {
const newRevision = new Revision();
newRevision.patch = patch;
newRevision.content = content;
newRevision.length = content.length;
newRevision.title = title;
newRevision.description = description;
newRevision.tags = Promise.resolve(tags);
newRevision.note = Promise.resolve(note);
newRevision.rangeAuthorships = Promise.resolve(rangeAuthorships);
newRevision.yjsStateVector = yjsStateVector ?? null;
return newRevision;
}
}