updates and fixes

* seo friendly urls
* custom client serve-static module
* database fixes
* fix recent pages
* other fixes
This commit is contained in:
Philipinho
2024-05-18 03:19:42 +01:00
parent eefe63d1cd
commit 9c7c2f1163
102 changed files with 921 additions and 536 deletions
@@ -0,0 +1,86 @@
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';
import { ExpressionBuilder } from 'kysely';
import { DB } from '@docmost/db/types/db';
import { jsonObjectFrom } from 'kysely/helpers/postgres';
@Injectable()
export class CommentRepo {
constructor(@InjectKysely() private readonly db: KyselyDB) {}
// todo, add workspaceId
async findById(
commentId: string,
opts?: { includeCreator: boolean },
): Promise<Comment> {
return await this.db
.selectFrom('comments')
.selectAll('comments')
.$if(opts?.includeCreator, (qb) => qb.select(this.withCreator))
.where('id', '=', commentId)
.executeTakeFirst();
}
async findPageComments(pageId: string, pagination: PaginationOptions) {
const query = this.db
.selectFrom('comments')
.selectAll('comments')
.select((eb) => this.withCreator(eb))
.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);
await 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();
}
withCreator(eb: ExpressionBuilder<DB, 'comments'>) {
return jsonObjectFrom(
eb
.selectFrom('users')
.select(['users.id', 'users.name', 'users.avatarUrl'])
.whereRef('users.id', '=', 'comments.creatorId'),
).as('creator');
}
async deleteComment(commentId: string): Promise<void> {
await this.db.deleteFrom('comments').where('id', '=', commentId).execute();
}
}