Files
docmost_custom/apps/server/src/kysely/repos/comment/comment.repo.ts
T
Philipinho 4913975e99 server: refactor pagination
* fix transaction usgae in repos
* other bug fixes
2024-04-01 01:23:52 +01:00

71 lines
1.8 KiB
TypeScript

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<Comment> {
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<Comment> {
const db = dbOrTx(this.db, trx);
return db
.insertInto('comments')
.values(insertableComment)
.returningAll()
.executeTakeFirst();
}
async deleteComment(commentId: string): Promise<void> {
await this.db.deleteFrom('comments').where('id', '=', commentId).execute();
}
}