Refactoring

* replace TypeORM with Kysely query builder
* refactor migrations
* other changes and fixes
This commit is contained in:
Philipinho
2024-03-29 01:46:11 +00:00
parent cacb5606b1
commit c18c9ae02b
122 changed files with 2619 additions and 3541 deletions

View File

@ -1,13 +1,15 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { PageHistory } from '../entities/page-history.entity';
import { Page } from '../entities/page.entity';
import { PageHistoryRepository } from '../repositories/page-history.repository';
import { PageHistoryRepo } from '@docmost/db/repos/page/page-history.repo';
import { Page, PageHistory } from '@docmost/db/types/entity.types';
import { PaginationOptions } from 'src/helpers/pagination/pagination-options';
import { PaginatedResult } from 'src/helpers/pagination/paginated-result';
import { PaginationMetaDto } from 'src/helpers/pagination/pagination-meta-dto';
@Injectable()
export class PageHistoryService {
constructor(private pageHistoryRepo: PageHistoryRepository) {}
constructor(private pageHistoryRepo: PageHistoryRepo) {}
async findOne(historyId: string): Promise<PageHistory> {
async findById(historyId: string): Promise<PageHistory> {
const history = await this.pageHistoryRepo.findById(historyId);
if (!history) {
throw new BadRequestException('History not found');
@ -16,45 +18,31 @@ export class PageHistoryService {
}
async saveHistory(page: Page): Promise<void> {
const pageHistory = new PageHistory();
pageHistory.pageId = page.id;
pageHistory.title = page.title;
pageHistory.content = page.content;
pageHistory.slug = page.slug;
pageHistory.icon = page.icon;
pageHistory.version = 1; // TODO: make incremental
pageHistory.coverPhoto = page.coverPhoto;
pageHistory.lastUpdatedById = page.lastUpdatedById ?? page.creatorId;
pageHistory.workspaceId = page.workspaceId;
await this.pageHistoryRepo.save(pageHistory);
await this.pageHistoryRepo.insertPageHistory({
pageId: page.id,
title: page.title,
content: page.content,
slug: page.slug,
icon: page.icon,
version: 1, // TODO: make incremental
coverPhoto: page.coverPhoto,
lastUpdatedById: page.lastUpdatedById ?? page.creatorId,
spaceId: page.spaceId,
workspaceId: page.workspaceId,
});
}
async findHistoryByPageId(pageId: string, limit = 50, offset = 0) {
const history = await this.pageHistoryRepo
.createQueryBuilder('history')
.where('history.pageId = :pageId', { pageId })
.leftJoinAndSelect('history.lastUpdatedBy', 'user')
.select([
'history.id',
'history.pageId',
'history.title',
'history.slug',
'history.icon',
'history.coverPhoto',
'history.version',
'history.lastUpdatedById',
'history.workspaceId',
'history.createdAt',
'history.updatedAt',
'user.id',
'user.name',
'user.avatarUrl',
])
.orderBy('history.updatedAt', 'DESC')
.offset(offset)
.take(limit)
.getMany();
return history;
async findHistoryByPageId(
pageId: string,
paginationOptions: PaginationOptions,
) {
const { pageHistory, count } =
await this.pageHistoryRepo.findPageHistoryByPageId(
pageId,
paginationOptions,
);
const paginationMeta = new PaginationMetaDto({ count, paginationOptions });
return new PaginatedResult(pageHistory, paginationMeta);
}
}

View File

@ -1,11 +1,9 @@
import {
BadRequestException,
forwardRef,
Inject,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { PageRepository } from '../repositories/page.repository';
import { Page } from '../entities/page.entity';
import { MovePageDto } from '../dto/move-page.dto';
import {
OrderingEntity,
@ -13,141 +11,185 @@ import {
removeFromArrayAndSave,
TreeNode,
} from '../page.util';
import { DataSource, EntityManager } from 'typeorm';
import { PageService } from './page.service';
import { PageOrdering } from '../entities/page-ordering.entity';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
import { executeTx } from '@docmost/db/utils';
import { Page, PageOrdering } from '@docmost/db/types/entity.types';
import { PageWithOrderingDto } from '../dto/page-with-ordering.dto';
@Injectable()
export class PageOrderingService {
constructor(
private pageRepository: PageRepository,
private dataSource: DataSource,
@Inject(forwardRef(() => PageService))
private pageService: PageService,
@InjectKysely() private readonly db: KyselyDB,
) {}
async movePage(dto: MovePageDto): Promise<void> {
await this.dataSource.transaction(async (manager: EntityManager) => {
const movedPageId = dto.id;
// TODO: scope to workspace and space
const movedPage = await manager
.createQueryBuilder(Page, 'page')
.where('page.id = :movedPageId', { movedPageId })
.select(['page.id', 'page.spaceId', 'page.parentPageId'])
.getOne();
async movePage(dto: MovePageDto, trx?: KyselyTransaction): Promise<void> {
await executeTx(
this.db,
async (trx) => {
const movedPageId = dto.pageId;
if (!movedPage) throw new BadRequestException('Moved page not found');
const movedPage = await trx
.selectFrom('pages as page')
.select(['page.id', 'page.spaceId', 'page.parentPageId'])
.where('page.id', '=', movedPageId)
.executeTakeFirst();
if (!dto.parentId) {
if (movedPage.parentPageId) {
await this.removeFromParent(movedPage.parentPageId, dto.id, manager);
}
const spaceOrdering = await this.getEntityOrdering(
movedPage.spaceId,
OrderingEntity.space,
manager,
);
if (!movedPage) throw new NotFoundException('Moved page not found');
orderPageList(spaceOrdering.childrenIds, dto);
// if no parentId, it means the page is a root page or now a root page
if (!dto.parentId) {
// if it had a parent before being moved, we detach it from the previous parent
if (movedPage.parentPageId) {
await this.removeFromParent(
movedPage.parentPageId,
dto.pageId,
trx,
);
}
const spaceOrdering = await this.getEntityOrdering(
movedPage.spaceId,
OrderingEntity.SPACE,
trx,
);
await manager.save(spaceOrdering);
} else {
const parentPageId = dto.parentId;
orderPageList(spaceOrdering.childrenIds, dto);
// it should save or update right?
// await manager.save(spaceOrdering); //TODO: to update or create new record? pretty confusing
await trx
.updateTable('page_ordering')
.set(spaceOrdering)
.where('id', '=', spaceOrdering.id)
.execute();
} else {
const parentPageId = dto.parentId;
let parentPageOrdering = await this.getEntityOrdering(
parentPageId,
OrderingEntity.page,
manager,
);
if (!parentPageOrdering) {
parentPageOrdering = await this.createPageOrdering(
let parentPageOrdering = await this.getEntityOrdering(
parentPageId,
OrderingEntity.page,
movedPage.spaceId,
manager,
OrderingEntity.PAGE,
trx,
);
if (!parentPageOrdering) {
parentPageOrdering = await this.createPageOrdering(
parentPageId,
OrderingEntity.PAGE,
movedPage.spaceId,
trx,
);
}
// Check if the parent was changed
if (
movedPage.parentPageId &&
movedPage.parentPageId !== parentPageId
) {
//if yes, remove moved page from old parent's children
await this.removeFromParent(
movedPage.parentPageId,
dto.pageId,
trx,
);
}
// If movedPage didn't have a parent initially (was at root level), update the root level
if (!movedPage.parentPageId) {
await this.removeFromSpacePageOrder(
movedPage.spaceId,
dto.pageId,
trx,
);
}
// Modify the children list of the new parentPage and save
orderPageList(parentPageOrdering.childrenIds, dto);
await trx
.updateTable('page_ordering')
.set(parentPageOrdering)
.where('id', '=', parentPageOrdering.id)
.execute();
}
// Check if the parent was changed
if (movedPage.parentPageId && movedPage.parentPageId !== parentPageId) {
//if yes, remove moved page from old parent's children
await this.removeFromParent(movedPage.parentPageId, dto.id, manager);
}
// If movedPage didn't have a parent initially (was at root level), update the root level
if (!movedPage.parentPageId) {
await this.removeFromSpacePageOrder(
movedPage.spaceId,
dto.id,
manager,
);
}
// Modify the children list of the new parentPage and save
orderPageList(parentPageOrdering.childrenIds, dto);
await manager.save(parentPageOrdering);
}
movedPage.parentPageId = dto.parentId || null;
await manager.save(movedPage);
});
// update the parent Id of the moved page
await trx
.updateTable('pages')
.set({
parentPageId: movedPage.parentPageId || null,
})
.where('id', '=', movedPage.id)
.execute();
},
trx,
);
}
async addPageToOrder(spaceId: string, pageId: string, parentPageId?: string) {
await this.dataSource.transaction(async (manager: EntityManager) => {
if (parentPageId) {
await this.upsertOrdering(
parentPageId,
OrderingEntity.page,
pageId,
spaceId,
manager,
);
} else {
await this.addToSpacePageOrder(spaceId, pageId, manager);
}
});
async addPageToOrder(
spaceId: string,
pageId: string,
parentPageId?: string,
trx?: KyselyTransaction,
) {
await executeTx(
this.db,
async (trx: KyselyTransaction) => {
if (parentPageId) {
await this.upsertOrdering(
parentPageId,
OrderingEntity.PAGE,
pageId,
spaceId,
trx,
);
} else {
await this.addToSpacePageOrder(spaceId, pageId, trx);
}
},
trx,
);
}
async addToSpacePageOrder(
spaceId: string,
pageId: string,
manager: EntityManager,
trx: KyselyTransaction,
) {
await this.upsertOrdering(
spaceId,
OrderingEntity.space,
OrderingEntity.SPACE,
pageId,
spaceId,
manager,
trx,
);
}
async removeFromParent(
parentId: string,
childId: string,
manager: EntityManager,
trx: KyselyTransaction,
): Promise<void> {
await this.removeChildFromOrdering(
parentId,
OrderingEntity.page,
OrderingEntity.PAGE,
childId,
manager,
trx,
);
}
async removeFromSpacePageOrder(
spaceId: string,
pageId: string,
manager: EntityManager,
trx: KyselyTransaction,
) {
await this.removeChildFromOrdering(
spaceId,
OrderingEntity.space,
OrderingEntity.SPACE,
pageId,
manager,
trx,
);
}
@ -155,27 +197,23 @@ export class PageOrderingService {
entityId: string,
entityType: string,
childId: string,
manager: EntityManager,
trx: KyselyTransaction,
): Promise<void> {
const ordering = await this.getEntityOrdering(
entityId,
entityType,
manager,
);
const ordering = await this.getEntityOrdering(entityId, entityType, trx);
if (ordering && ordering.childrenIds.includes(childId)) {
await removeFromArrayAndSave(ordering, 'childrenIds', childId, manager);
await removeFromArrayAndSave(ordering, 'childrenIds', childId, trx);
}
}
async removePageFromHierarchy(
page: Page,
manager: EntityManager,
trx: KyselyTransaction,
): Promise<void> {
if (page.parentPageId) {
await this.removeFromParent(page.parentPageId, page.id, manager);
await this.removeFromParent(page.parentPageId, page.id, trx);
} else {
await this.removeFromSpacePageOrder(page.spaceId, page.id, manager);
await this.removeFromSpacePageOrder(page.spaceId, page.id, trx);
}
}
@ -184,65 +222,74 @@ export class PageOrderingService {
entityType: string,
childId: string,
spaceId: string,
manager: EntityManager,
trx: KyselyTransaction,
) {
let ordering = await this.getEntityOrdering(entityId, entityType, manager);
let ordering = await this.getEntityOrdering(entityId, entityType, trx);
if (!ordering) {
ordering = await this.createPageOrdering(
entityId,
entityType,
spaceId,
manager,
trx,
);
}
if (!ordering.childrenIds.includes(childId)) {
ordering.childrenIds.unshift(childId);
await manager.save(PageOrdering, ordering);
await trx
.updateTable('page_ordering')
.set(ordering)
.where('id', '=', ordering.id)
.execute();
//await manager.save(PageOrdering, ordering);
}
}
async getEntityOrdering(
entityId: string,
entityType: string,
manager,
trx: KyselyTransaction,
): Promise<PageOrdering> {
return manager
.createQueryBuilder(PageOrdering, 'ordering')
.setLock('pessimistic_write')
.where('ordering.entityId = :entityId', { entityId })
.andWhere('ordering.entityType = :entityType', {
entityType,
})
.getOne();
return trx
.selectFrom('page_ordering')
.selectAll()
.where('entityId', '=', entityId)
.where('entityType', '=', entityType)
.forUpdate()
.executeTakeFirst();
}
async createPageOrdering(
entityId: string,
entityType: string,
spaceId: string,
manager: EntityManager,
trx: KyselyTransaction,
): Promise<PageOrdering> {
await manager.query(
`INSERT INTO page_ordering ("entityId", "entityType", "spaceId", "childrenIds")
VALUES ($1, $2, $3, '{}')
ON CONFLICT ("entityId", "entityType") DO NOTHING`,
[entityId, entityType, spaceId],
);
await trx
.insertInto('page_ordering')
.values({
entityId,
entityType,
spaceId,
childrenIds: [],
})
.onConflict((oc) => oc.columns(['entityId', 'entityType']).doNothing())
.execute();
return await this.getEntityOrdering(entityId, entityType, manager);
// Todo: maybe use returning above
return await this.getEntityOrdering(entityId, entityType, trx);
}
async getSpacePageOrder(spaceId: string): Promise<PageOrdering> {
return await this.dataSource
.createQueryBuilder(PageOrdering, 'ordering')
.select(['ordering.id', 'ordering.childrenIds', 'ordering.spaceId'])
.where('ordering.entityId = :spaceId', { spaceId })
.andWhere('ordering.entityType = :entityType', {
entityType: OrderingEntity.space,
})
.getOne();
async getSpacePageOrder(
spaceId: string,
): Promise<{ id: string; childrenIds: string[]; spaceId: string }> {
return await this.db
.selectFrom('page_ordering')
.select(['id', 'childrenIds', 'spaceId'])
.where('entityId', '=', spaceId)
.where('entityType', '=', OrderingEntity.SPACE)
.executeTakeFirst();
}
async convertToTree(spaceId: string): Promise<TreeNode[]> {

View File

@ -1,59 +1,34 @@
import {
BadRequestException,
forwardRef,
Inject,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { PageRepository } from '../repositories/page.repository';
import { CreatePageDto } from '../dto/create-page.dto';
import { Page } from '../entities/page.entity';
import { UpdatePageDto } from '../dto/update-page.dto';
import { plainToInstance } from 'class-transformer';
import { DataSource, EntityManager } from 'typeorm';
import { PageOrderingService } from './page-ordering.service';
import { PageWithOrderingDto } from '../dto/page-with-ordering.dto';
import { OrderingEntity, transformPageResult } from '../page.util';
import { transformPageResult } from '../page.util';
import { PageRepo } from '@docmost/db/repos/page/page.repo';
import { Page } from '@docmost/db/types/entity.types';
import { PaginationOptions } from 'src/helpers/pagination/pagination-options';
import { PaginationMetaDto } from 'src/helpers/pagination/pagination-meta-dto';
import { PaginatedResult } from 'src/helpers/pagination/paginated-result';
@Injectable()
export class PageService {
constructor(
private pageRepository: PageRepository,
private dataSource: DataSource,
private pageRepo: PageRepo,
@Inject(forwardRef(() => PageOrderingService))
private pageOrderingService: PageOrderingService,
) {}
async findWithBasic(pageId: string) {
return this.pageRepository.findOne({
where: { id: pageId },
select: ['id', 'title'],
});
}
async findById(pageId: string) {
return this.pageRepository.findById(pageId);
}
async findWithContent(pageId: string) {
return this.pageRepository.findWithContent(pageId);
}
async findWithYdoc(pageId: string) {
return this.pageRepository.findWithYDoc(pageId);
}
async findWithAllFields(pageId: string) {
return this.pageRepository.findWithAllFields(pageId);
}
async findOne(pageId: string): Promise<Page> {
const page = await this.findById(pageId);
if (!page) {
throw new BadRequestException('Page not found');
}
return page;
async findById(
pageId: string,
includeContent?: boolean,
includeYdoc?: boolean,
): Promise<Page> {
return this.pageRepo.findById(pageId, includeContent, includeYdoc);
}
async create(
@ -61,26 +36,26 @@ export class PageService {
workspaceId: string,
createPageDto: CreatePageDto,
): Promise<Page> {
const page = plainToInstance(Page, createPageDto);
page.creatorId = userId;
page.workspaceId = workspaceId;
page.lastUpdatedById = userId;
// check if parent page exists
if (createPageDto.parentPageId) {
// TODO: make sure parent page belongs to same space and user has permissions
const parentPage = await this.pageRepository.findOne({
where: { id: createPageDto.parentPageId },
select: ['id'],
});
if (!parentPage) throw new BadRequestException('Parent page not found');
const parentPage = await this.pageRepo.findById(
createPageDto.parentPageId,
);
if (!parentPage) throw new NotFoundException('Parent page not found');
}
const createdPage = await this.pageRepository.save(page);
//TODO: should be in a transaction
const createdPage = await this.pageRepo.insertPage({
...createPageDto,
creatorId: userId,
workspaceId: workspaceId,
lastUpdatedById: userId,
});
await this.pageOrderingService.addPageToOrder(
createPageDto.spaceId,
createPageDto.id,
createPageDto.pageId,
createPageDto.parentPageId,
);
@ -91,18 +66,16 @@ export class PageService {
pageId: string,
updatePageDto: UpdatePageDto,
userId: string,
): Promise<Page> {
const updateData = {
...updatePageDto,
lastUpdatedById: userId,
};
): Promise<void> {
await this.pageRepo.updatePage(
{
...updatePageDto,
lastUpdatedById: userId,
},
pageId,
);
const result = await this.pageRepository.update(pageId, updateData);
if (result.affected === 0) {
throw new BadRequestException(`Page not found`);
}
return await this.pageRepository.findById(pageId);
//return await this.pageRepo.findById(pageId);
}
async updateState(
@ -112,14 +85,19 @@ export class PageService {
ydoc: any,
userId?: string, // TODO: fix this
): Promise<void> {
await this.pageRepository.update(pageId, {
content: content,
textContent: textContent,
ydoc: ydoc,
...(userId && { lastUpdatedById: userId }),
});
await this.pageRepo.updatePage(
{
content: content,
textContent: textContent,
ydoc: ydoc,
...(userId && { lastUpdatedById: userId }),
},
pageId,
);
}
/*
// TODO: page deletion and restoration
async delete(pageId: string): Promise<void> {
await this.dataSource.transaction(async (manager: EntityManager) => {
const page = await manager
@ -207,59 +185,30 @@ export class PageService {
await manager.recover(Page, { id: child.id });
}
}
*/
async forceDelete(pageId: string): Promise<void> {
await this.pageRepository.delete(pageId);
}
async lockOrUnlockPage(pageId: string, lock: boolean): Promise<Page> {
await this.pageRepository.update(pageId, { isLocked: lock });
return await this.pageRepository.findById(pageId);
await this.pageRepo.deletePage(pageId);
}
async getSidebarPagesBySpaceId(
spaceId: string,
limit = 200,
): Promise<PageWithOrderingDto[]> {
const pages = await this.pageRepository
.createQueryBuilder('page')
.leftJoin(
'page_ordering',
'ordering',
'ordering.entityId = page.id AND ordering.entityType = :entityType',
{ entityType: OrderingEntity.page },
)
.where('page.spaceId = :spaceId', { spaceId })
.select([
'page.id',
'page.title',
'page.icon',
'page.parentPageId',
'page.spaceId',
'ordering.childrenIds',
'page.creatorId',
'page.createdAt',
])
.orderBy('page.createdAt', 'DESC')
.take(limit)
.getRawMany<PageWithOrderingDto[]>();
const pages = await this.pageRepo.getSpaceSidebarPages(spaceId, limit);
return transformPageResult(pages);
}
async getRecentSpacePages(
spaceId: string,
limit = 20,
offset = 0,
): Promise<Page[]> {
const pages = await this.pageRepository
.createQueryBuilder('page')
.where('page.spaceId = :spaceId', { spaceId })
.select(this.pageRepository.baseFields)
.orderBy('page.updatedAt', 'DESC')
.offset(offset)
.take(limit)
.getMany();
return pages;
paginationOptions: PaginationOptions,
): Promise<PaginatedResult<Page>> {
const { pages, count } = await this.pageRepo.getRecentPagesInSpace(
spaceId,
paginationOptions,
);
const paginationMeta = new PaginationMetaDto({ count, paginationOptions });
return new PaginatedResult(pages, paginationMeta);
}
}