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

@ -0,0 +1,99 @@
import { Injectable } from '@nestjs/common';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
import { executeTx } from '../../utils';
import {
InsertablePageHistory,
PageHistory,
UpdatablePageHistory,
} from '@docmost/db/types/entity.types';
import { PaginationOptions } from 'src/helpers/pagination/pagination-options';
@Injectable()
export class PageHistoryRepo {
constructor(@InjectKysely() private readonly db: KyselyDB) {}
async findById(pageHistoryId: string): Promise<PageHistory> {
return await this.db
.selectFrom('page_history')
.selectAll()
.where('id', '=', pageHistoryId)
.executeTakeFirst();
}
async updatePageHistory(
updatablePageHistory: UpdatablePageHistory,
pageHistoryId: string,
trx?: KyselyTransaction,
) {
return await executeTx(
this.db,
async (trx) => {
return await trx
.updateTable('page_history')
.set(updatablePageHistory)
.where('id', '=', pageHistoryId)
.execute();
},
trx,
);
}
async insertPageHistory(
insertablePageHistory: InsertablePageHistory,
trx?: KyselyTransaction,
): Promise<PageHistory> {
return await executeTx(
this.db,
async (trx) => {
return await trx
.insertInto('page_history')
.values(insertablePageHistory)
.returningAll()
.executeTakeFirst();
},
trx,
);
}
async findPageHistoryByPageId(
pageId: string,
paginationOptions: PaginationOptions,
) {
return executeTx(this.db, async (trx) => {
const pageHistory = await trx
.selectFrom('page_history as history')
.innerJoin('users as user', 'user.id', 'history.lastUpdatedById')
.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',
])
.where('pageId', '=', pageId)
.orderBy('createdAt', 'desc')
.limit(paginationOptions.limit)
.offset(paginationOptions.offset)
.execute();
let { count } = await trx
.selectFrom('page_history')
.select((eb) => eb.fn.count('id').as('count'))
.where('pageId', '=', pageId)
.executeTakeFirst();
count = count as number;
return { pageHistory, count };
});
}
}