import { Injectable } from '@nestjs/common'; import { InjectKysely } from 'nestjs-kysely'; import { KyselyDB, KyselyTransaction } from '../../types/kysely.types'; import { dbOrTx } from '../../utils'; import { Comment, InsertableComment, UpdatableComment, } from '@docmost/db/types/entity.types'; import { PaginationOptions } from '@docmost/db/pagination/pagination-options'; import { executeWithPagination } from '@docmost/db/pagination/pagination'; @Injectable() export class CommentRepo { constructor(@InjectKysely() private readonly db: KyselyDB) {} // todo, add workspaceId async findById(commentId: string): Promise { return await this.db .selectFrom('comments') .selectAll() .where('id', '=', commentId) .executeTakeFirst(); } async findPageComments(pageId: string, pagination: PaginationOptions) { const query = this.db .selectFrom('comments') .selectAll() .where('pageId', '=', pageId) .orderBy('createdAt', 'asc'); const result = executeWithPagination(query, { page: pagination.page, perPage: pagination.limit, }); return result; } async updateComment( updatableComment: UpdatableComment, commentId: string, trx?: KyselyTransaction, ) { const db = dbOrTx(this.db, trx); db.updateTable('comments') .set(updatableComment) .where('id', '=', commentId) .execute(); } async insertComment( insertableComment: InsertableComment, trx?: KyselyTransaction, ): Promise { const db = dbOrTx(this.db, trx); return db .insertInto('comments') .values(insertableComment) .returningAll() .executeTakeFirst(); } async deleteComment(commentId: string): Promise { await this.db.deleteFrom('comments').where('id', '=', commentId).execute(); } }