From 0257f03003d7272da09ff38d631c3291043870b6 Mon Sep 17 00:00:00 2001 From: Philipinho <16838612+Philipinho@users.noreply.github.com> Date: Mon, 27 Apr 2026 01:06:19 +0100 Subject: [PATCH] refactor(base): rewrite BaseRepo over pages (is_base + base_schema_version) --- .../src/database/repos/base/base.repo.ts | 105 ++++++------------ 1 file changed, 36 insertions(+), 69 deletions(-) diff --git a/apps/server/src/database/repos/base/base.repo.ts b/apps/server/src/database/repos/base/base.repo.ts index 8a987521b..77529ed16 100644 --- a/apps/server/src/database/repos/base/base.repo.ts +++ b/apps/server/src/database/repos/base/base.repo.ts @@ -1,61 +1,50 @@ import { Injectable } from '@nestjs/common'; import { InjectKysely } from 'nestjs-kysely'; +import { sql, ExpressionBuilder } from 'kysely'; +import { jsonArrayFrom } from 'kysely/helpers/postgres'; + import { KyselyDB, KyselyTransaction } from '../../types/kysely.types'; import { dbOrTx } from '../../utils'; -import { - Base, - InsertableBase, - UpdatableBase, -} from '@docmost/db/types/entity.types'; +import { DB } from '@docmost/db/types/db'; +import { Page } from '@docmost/db/types/entity.types'; import { PaginationOptions } from '@docmost/db/pagination/pagination-options'; import { executeWithCursorPagination } from '@docmost/db/pagination/cursor-pagination'; -import { ExpressionBuilder, sql } from 'kysely'; -import { DB } from '@docmost/db/types/db'; -import { jsonArrayFrom } from 'kysely/helpers/postgres'; + +export type BasePage = Page & { + properties?: unknown[]; + views?: unknown[]; +}; @Injectable() export class BaseRepo { constructor(@InjectKysely() private readonly db: KyselyDB) {} - private baseFields: Array = [ - 'id', - 'name', - 'description', - 'icon', - 'pageId', - 'spaceId', - 'workspaceId', - 'creatorId', - 'createdAt', - 'updatedAt', - 'deletedAt', - ]; - + // The "base id" is the page id of an is_base=true page. async findById( - baseId: string, + pageId: string, opts?: { includeProperties?: boolean; includeViews?: boolean; trx?: KyselyTransaction; }, - ): Promise { + ): Promise { const db = dbOrTx(this.db, opts?.trx); let query = db - .selectFrom('bases') - .select(this.baseFields) - .where('id', '=', baseId) + .selectFrom('pages') + .selectAll('pages') + .where('id', '=', pageId) + .where('isBase', '=', true) .where('deletedAt', 'is', null); if (opts?.includeProperties) { query = query.select((eb) => this.withProperties(eb)); } - if (opts?.includeViews) { query = query.select((eb) => this.withViews(eb)); } - return query.executeTakeFirst() as Promise; + return query.executeTakeFirst() as Promise; } async findBySpaceId( @@ -66,9 +55,10 @@ export class BaseRepo { const db = dbOrTx(this.db, opts?.trx); const query = db - .selectFrom('bases') - .select(this.baseFields) + .selectFrom('pages') + .selectAll('pages') .where('spaceId', '=', spaceId) + .where('isBase', '=', true) .where('deletedAt', 'is', null); return executeWithCursorPagination(query, { @@ -86,74 +76,51 @@ export class BaseRepo { }); } - async insertBase( - base: InsertableBase, - trx?: KyselyTransaction, - ): Promise { - const db = dbOrTx(this.db, trx); - return db - .insertInto('bases') - .values(base) - .returningAll() - .executeTakeFirstOrThrow() as Promise; - } - - async updateBase( - baseId: string, - data: UpdatableBase, - trx?: KyselyTransaction, - ): Promise { + async softDelete(pageId: string, trx?: KyselyTransaction): Promise { const db = dbOrTx(this.db, trx); await db - .updateTable('bases') - .set({ ...data, updatedAt: new Date() }) - .where('id', '=', baseId) - .execute(); - } - - async softDelete(baseId: string, trx?: KyselyTransaction): Promise { - const db = dbOrTx(this.db, trx); - await db - .updateTable('bases') + .updateTable('pages') .set({ deletedAt: new Date() }) - .where('id', '=', baseId) + .where('id', '=', pageId) + .where('isBase', '=', true) .execute(); } async bumpSchemaVersion( - baseId: string, + pageId: string, trx?: KyselyTransaction, ): Promise { const db = dbOrTx(this.db, trx); const result = await db - .updateTable('bases') + .updateTable('pages') .set({ - schemaVersion: sql`schema_version + 1`, + baseSchemaVersion: sql`base_schema_version + 1`, updatedAt: new Date(), }) - .where('id', '=', baseId) - .returning('schemaVersion') + .where('id', '=', pageId) + .where('isBase', '=', true) + .returning('baseSchemaVersion') .executeTakeFirst(); - return result?.schemaVersion ?? 0; + return result?.baseSchemaVersion ?? 0; } - private withProperties(eb: ExpressionBuilder) { + private withProperties(eb: ExpressionBuilder) { return jsonArrayFrom( eb .selectFrom('baseProperties') .selectAll('baseProperties') - .whereRef('baseProperties.baseId', '=', 'bases.id') + .whereRef('baseProperties.pageId', '=', 'pages.id') .where('baseProperties.deletedAt', 'is', null) .orderBy('baseProperties.position', 'asc'), ).as('properties'); } - private withViews(eb: ExpressionBuilder) { + private withViews(eb: ExpressionBuilder) { return jsonArrayFrom( eb .selectFrom('baseViews') .selectAll('baseViews') - .whereRef('baseViews.baseId', '=', 'bases.id') + .whereRef('baseViews.pageId', '=', 'pages.id') .orderBy('baseViews.position', 'asc'), ).as('views'); }