refactor(base): rewrite BaseRepo over pages (is_base + base_schema_version)

This commit is contained in:
Philipinho
2026-04-27 01:06:19 +01:00
parent 731fa45672
commit 0257f03003
@@ -1,61 +1,50 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { InjectKysely } from 'nestjs-kysely'; import { InjectKysely } from 'nestjs-kysely';
import { sql, ExpressionBuilder } from 'kysely';
import { jsonArrayFrom } from 'kysely/helpers/postgres';
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types'; import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
import { dbOrTx } from '../../utils'; import { dbOrTx } from '../../utils';
import { import { DB } from '@docmost/db/types/db';
Base, import { Page } from '@docmost/db/types/entity.types';
InsertableBase,
UpdatableBase,
} from '@docmost/db/types/entity.types';
import { PaginationOptions } from '@docmost/db/pagination/pagination-options'; import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
import { executeWithCursorPagination } from '@docmost/db/pagination/cursor-pagination'; import { executeWithCursorPagination } from '@docmost/db/pagination/cursor-pagination';
import { ExpressionBuilder, sql } from 'kysely';
import { DB } from '@docmost/db/types/db'; export type BasePage = Page & {
import { jsonArrayFrom } from 'kysely/helpers/postgres'; properties?: unknown[];
views?: unknown[];
};
@Injectable() @Injectable()
export class BaseRepo { export class BaseRepo {
constructor(@InjectKysely() private readonly db: KyselyDB) {} constructor(@InjectKysely() private readonly db: KyselyDB) {}
private baseFields: Array<keyof Base> = [ // The "base id" is the page id of an is_base=true page.
'id',
'name',
'description',
'icon',
'pageId',
'spaceId',
'workspaceId',
'creatorId',
'createdAt',
'updatedAt',
'deletedAt',
];
async findById( async findById(
baseId: string, pageId: string,
opts?: { opts?: {
includeProperties?: boolean; includeProperties?: boolean;
includeViews?: boolean; includeViews?: boolean;
trx?: KyselyTransaction; trx?: KyselyTransaction;
}, },
): Promise<Base | undefined> { ): Promise<BasePage | undefined> {
const db = dbOrTx(this.db, opts?.trx); const db = dbOrTx(this.db, opts?.trx);
let query = db let query = db
.selectFrom('bases') .selectFrom('pages')
.select(this.baseFields) .selectAll('pages')
.where('id', '=', baseId) .where('id', '=', pageId)
.where('isBase', '=', true)
.where('deletedAt', 'is', null); .where('deletedAt', 'is', null);
if (opts?.includeProperties) { if (opts?.includeProperties) {
query = query.select((eb) => this.withProperties(eb)); query = query.select((eb) => this.withProperties(eb));
} }
if (opts?.includeViews) { if (opts?.includeViews) {
query = query.select((eb) => this.withViews(eb)); query = query.select((eb) => this.withViews(eb));
} }
return query.executeTakeFirst() as Promise<Base | undefined>; return query.executeTakeFirst() as Promise<BasePage | undefined>;
} }
async findBySpaceId( async findBySpaceId(
@@ -66,9 +55,10 @@ export class BaseRepo {
const db = dbOrTx(this.db, opts?.trx); const db = dbOrTx(this.db, opts?.trx);
const query = db const query = db
.selectFrom('bases') .selectFrom('pages')
.select(this.baseFields) .selectAll('pages')
.where('spaceId', '=', spaceId) .where('spaceId', '=', spaceId)
.where('isBase', '=', true)
.where('deletedAt', 'is', null); .where('deletedAt', 'is', null);
return executeWithCursorPagination(query, { return executeWithCursorPagination(query, {
@@ -86,74 +76,51 @@ export class BaseRepo {
}); });
} }
async insertBase( async softDelete(pageId: string, trx?: KyselyTransaction): Promise<void> {
base: InsertableBase,
trx?: KyselyTransaction,
): Promise<Base> {
const db = dbOrTx(this.db, trx);
return db
.insertInto('bases')
.values(base)
.returningAll()
.executeTakeFirstOrThrow() as Promise<Base>;
}
async updateBase(
baseId: string,
data: UpdatableBase,
trx?: KyselyTransaction,
): Promise<void> {
const db = dbOrTx(this.db, trx); const db = dbOrTx(this.db, trx);
await db await db
.updateTable('bases') .updateTable('pages')
.set({ ...data, updatedAt: new Date() })
.where('id', '=', baseId)
.execute();
}
async softDelete(baseId: string, trx?: KyselyTransaction): Promise<void> {
const db = dbOrTx(this.db, trx);
await db
.updateTable('bases')
.set({ deletedAt: new Date() }) .set({ deletedAt: new Date() })
.where('id', '=', baseId) .where('id', '=', pageId)
.where('isBase', '=', true)
.execute(); .execute();
} }
async bumpSchemaVersion( async bumpSchemaVersion(
baseId: string, pageId: string,
trx?: KyselyTransaction, trx?: KyselyTransaction,
): Promise<number> { ): Promise<number> {
const db = dbOrTx(this.db, trx); const db = dbOrTx(this.db, trx);
const result = await db const result = await db
.updateTable('bases') .updateTable('pages')
.set({ .set({
schemaVersion: sql`schema_version + 1`, baseSchemaVersion: sql`base_schema_version + 1`,
updatedAt: new Date(), updatedAt: new Date(),
}) })
.where('id', '=', baseId) .where('id', '=', pageId)
.returning('schemaVersion') .where('isBase', '=', true)
.returning('baseSchemaVersion')
.executeTakeFirst(); .executeTakeFirst();
return result?.schemaVersion ?? 0; return result?.baseSchemaVersion ?? 0;
} }
private withProperties(eb: ExpressionBuilder<DB, 'bases'>) { private withProperties(eb: ExpressionBuilder<DB, 'pages'>) {
return jsonArrayFrom( return jsonArrayFrom(
eb eb
.selectFrom('baseProperties') .selectFrom('baseProperties')
.selectAll('baseProperties') .selectAll('baseProperties')
.whereRef('baseProperties.baseId', '=', 'bases.id') .whereRef('baseProperties.pageId', '=', 'pages.id')
.where('baseProperties.deletedAt', 'is', null) .where('baseProperties.deletedAt', 'is', null)
.orderBy('baseProperties.position', 'asc'), .orderBy('baseProperties.position', 'asc'),
).as('properties'); ).as('properties');
} }
private withViews(eb: ExpressionBuilder<DB, 'bases'>) { private withViews(eb: ExpressionBuilder<DB, 'pages'>) {
return jsonArrayFrom( return jsonArrayFrom(
eb eb
.selectFrom('baseViews') .selectFrom('baseViews')
.selectAll('baseViews') .selectAll('baseViews')
.whereRef('baseViews.baseId', '=', 'bases.id') .whereRef('baseViews.pageId', '=', 'pages.id')
.orderBy('baseViews.position', 'asc'), .orderBy('baseViews.position', 'asc'),
).as('views'); ).as('views');
} }