feat: bases - WIP

This commit is contained in:
Philipinho
2026-03-08 00:56:24 +00:00
parent 0aeaa43112
commit 94ee1e80fb
83 changed files with 9243 additions and 38 deletions
@@ -0,0 +1,91 @@
import { Injectable } from '@nestjs/common';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
import { dbOrTx } from '../../utils';
import {
BaseProperty,
InsertableBaseProperty,
UpdatableBaseProperty,
} from '@docmost/db/types/entity.types';
import { sql } from 'kysely';
@Injectable()
export class BasePropertyRepo {
constructor(@InjectKysely() private readonly db: KyselyDB) {}
async findById(
propertyId: string,
opts?: { trx?: KyselyTransaction },
): Promise<BaseProperty | undefined> {
const db = dbOrTx(this.db, opts?.trx);
return db
.selectFrom('baseProperties')
.selectAll()
.where('id', '=', propertyId)
.executeTakeFirst() as Promise<BaseProperty | undefined>;
}
async findByBaseId(
baseId: string,
opts?: { trx?: KyselyTransaction },
): Promise<BaseProperty[]> {
const db = dbOrTx(this.db, opts?.trx);
return db
.selectFrom('baseProperties')
.selectAll()
.where('baseId', '=', baseId)
.orderBy('position', 'asc')
.execute() as Promise<BaseProperty[]>;
}
async getLastPosition(
baseId: string,
trx?: KyselyTransaction,
): Promise<string | null> {
const db = dbOrTx(this.db, trx);
const result = await db
.selectFrom('baseProperties')
.select('position')
.where('baseId', '=', baseId)
.orderBy(sql`position COLLATE "C"`, sql`DESC`)
.limit(1)
.executeTakeFirst();
return result?.position ?? null;
}
async insertProperty(
property: InsertableBaseProperty,
trx?: KyselyTransaction,
): Promise<BaseProperty> {
const db = dbOrTx(this.db, trx);
return db
.insertInto('baseProperties')
.values(property)
.returningAll()
.executeTakeFirstOrThrow() as Promise<BaseProperty>;
}
async updateProperty(
propertyId: string,
data: UpdatableBaseProperty,
trx?: KyselyTransaction,
): Promise<void> {
const db = dbOrTx(this.db, trx);
await db
.updateTable('baseProperties')
.set({ ...data, updatedAt: new Date() })
.where('id', '=', propertyId)
.execute();
}
async deleteProperty(
propertyId: string,
trx?: KyselyTransaction,
): Promise<void> {
const db = dbOrTx(this.db, trx);
await db
.deleteFrom('baseProperties')
.where('id', '=', propertyId)
.execute();
}
}
@@ -0,0 +1,172 @@
import { Injectable } from '@nestjs/common';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
import { dbOrTx } from '../../utils';
import {
BaseRow,
InsertableBaseRow,
} from '@docmost/db/types/entity.types';
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
import { executeWithCursorPagination } from '@docmost/db/pagination/cursor-pagination';
import { sql } from 'kysely';
@Injectable()
export class BaseRowRepo {
constructor(@InjectKysely() private readonly db: KyselyDB) {}
async findById(
rowId: string,
opts?: { trx?: KyselyTransaction },
): Promise<BaseRow | undefined> {
const db = dbOrTx(this.db, opts?.trx);
return db
.selectFrom('baseRows')
.selectAll()
.where('id', '=', rowId)
.where('deletedAt', 'is', null)
.executeTakeFirst() as Promise<BaseRow | undefined>;
}
async findByBaseId(
baseId: string,
pagination: PaginationOptions,
opts?: { trx?: KyselyTransaction },
) {
const db = dbOrTx(this.db, opts?.trx);
const query = db
.selectFrom('baseRows')
.selectAll()
.where('baseId', '=', baseId)
.where('deletedAt', 'is', null);
return executeWithCursorPagination(query, {
perPage: pagination.limit,
cursor: pagination.cursor,
beforeCursor: pagination.beforeCursor,
fields: [
{ expression: 'position', direction: 'asc' },
{ expression: 'id', direction: 'asc' },
],
parseCursor: (cursor) => ({
position: cursor.position,
id: cursor.id,
}),
});
}
async getLastPosition(
baseId: string,
trx?: KyselyTransaction,
): Promise<string | null> {
const db = dbOrTx(this.db, trx);
const result = await db
.selectFrom('baseRows')
.select('position')
.where('baseId', '=', baseId)
.where('deletedAt', 'is', null)
.orderBy(sql`position COLLATE "C"`, sql`DESC`)
.limit(1)
.executeTakeFirst();
return result?.position ?? null;
}
async insertRow(
row: InsertableBaseRow,
trx?: KyselyTransaction,
): Promise<BaseRow> {
const db = dbOrTx(this.db, trx);
return db
.insertInto('baseRows')
.values(row)
.returningAll()
.executeTakeFirstOrThrow() as Promise<BaseRow>;
}
async updateCells(
rowId: string,
cells: Record<string, unknown>,
userId?: string,
trx?: KyselyTransaction,
): Promise<void> {
const db = dbOrTx(this.db, trx);
await db
.updateTable('baseRows')
.set({
cells: sql`cells || ${cells}`,
updatedAt: new Date(),
lastUpdatedById: userId ?? null,
})
.where('id', '=', rowId)
.where('deletedAt', 'is', null)
.execute();
}
async updatePosition(
rowId: string,
position: string,
trx?: KyselyTransaction,
): Promise<void> {
const db = dbOrTx(this.db, trx);
await db
.updateTable('baseRows')
.set({ position, updatedAt: new Date() })
.where('id', '=', rowId)
.execute();
}
async softDelete(rowId: string, trx?: KyselyTransaction): Promise<void> {
const db = dbOrTx(this.db, trx);
await db
.updateTable('baseRows')
.set({ deletedAt: new Date() })
.where('id', '=', rowId)
.execute();
}
async removeCellKey(
baseId: string,
propertyId: string,
trx?: KyselyTransaction,
): Promise<void> {
const db = dbOrTx(this.db, trx);
await db
.updateTable('baseRows')
.set({
cells: sql`cells - ${propertyId}`,
updatedAt: new Date(),
})
.where('baseId', '=', baseId)
.execute();
}
async findAllByBaseId(
baseId: string,
trx?: KyselyTransaction,
): Promise<BaseRow[]> {
const db = dbOrTx(this.db, trx);
return db
.selectFrom('baseRows')
.selectAll()
.where('baseId', '=', baseId)
.where('deletedAt', 'is', null)
.execute() as Promise<BaseRow[]>;
}
async batchUpdateCells(
updates: Array<{ id: string; cells: Record<string, unknown> }>,
trx?: KyselyTransaction,
): Promise<void> {
const db = dbOrTx(this.db, trx);
for (const update of updates) {
await db
.updateTable('baseRows')
.set({
cells: sql`cells || ${update.cells}`,
updatedAt: new Date(),
})
.where('id', '=', update.id)
.execute();
}
}
}
@@ -0,0 +1,104 @@
import { Injectable } from '@nestjs/common';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
import { dbOrTx } from '../../utils';
import {
BaseView,
InsertableBaseView,
UpdatableBaseView,
} from '@docmost/db/types/entity.types';
import { sql } from 'kysely';
@Injectable()
export class BaseViewRepo {
constructor(@InjectKysely() private readonly db: KyselyDB) {}
async findById(
viewId: string,
opts?: { trx?: KyselyTransaction },
): Promise<BaseView | undefined> {
const db = dbOrTx(this.db, opts?.trx);
return db
.selectFrom('baseViews')
.selectAll()
.where('id', '=', viewId)
.executeTakeFirst() as Promise<BaseView | undefined>;
}
async findByBaseId(
baseId: string,
opts?: { trx?: KyselyTransaction },
): Promise<BaseView[]> {
const db = dbOrTx(this.db, opts?.trx);
return db
.selectFrom('baseViews')
.selectAll()
.where('baseId', '=', baseId)
.orderBy('position', 'asc')
.execute() as Promise<BaseView[]>;
}
async countByBaseId(
baseId: string,
trx?: KyselyTransaction,
): Promise<number> {
const db = dbOrTx(this.db, trx);
const result = await db
.selectFrom('baseViews')
.select((eb) => eb.fn.countAll<number>().as('count'))
.where('baseId', '=', baseId)
.executeTakeFirstOrThrow();
return Number(result.count);
}
async getLastPosition(
baseId: string,
trx?: KyselyTransaction,
): Promise<string | null> {
const db = dbOrTx(this.db, trx);
const result = await db
.selectFrom('baseViews')
.select('position')
.where('baseId', '=', baseId)
.orderBy(sql`position COLLATE "C"`, sql`DESC`)
.limit(1)
.executeTakeFirst();
return result?.position ?? null;
}
async insertView(
view: InsertableBaseView,
trx?: KyselyTransaction,
): Promise<BaseView> {
const db = dbOrTx(this.db, trx);
return db
.insertInto('baseViews')
.values(view)
.returningAll()
.executeTakeFirstOrThrow() as Promise<BaseView>;
}
async updateView(
viewId: string,
data: UpdatableBaseView,
trx?: KyselyTransaction,
): Promise<void> {
const db = dbOrTx(this.db, trx);
await db
.updateTable('baseViews')
.set({ ...data, updatedAt: new Date() })
.where('id', '=', viewId)
.execute();
}
async deleteView(
viewId: string,
trx?: KyselyTransaction,
): Promise<void> {
const db = dbOrTx(this.db, trx);
await db
.deleteFrom('baseViews')
.where('id', '=', viewId)
.execute();
}
}
@@ -0,0 +1,142 @@
import { Injectable } from '@nestjs/common';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
import { dbOrTx } from '../../utils';
import {
Base,
InsertableBase,
UpdatableBase,
} from '@docmost/db/types/entity.types';
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
import { executeWithCursorPagination } from '@docmost/db/pagination/cursor-pagination';
import { ExpressionBuilder } from 'kysely';
import { DB } from '@docmost/db/types/db';
import { jsonArrayFrom } from 'kysely/helpers/postgres';
@Injectable()
export class BaseRepo {
constructor(@InjectKysely() private readonly db: KyselyDB) {}
private baseFields: Array<keyof Base> = [
'id',
'name',
'description',
'icon',
'pageId',
'spaceId',
'workspaceId',
'creatorId',
'createdAt',
'updatedAt',
'deletedAt',
];
async findById(
baseId: string,
opts?: {
includeProperties?: boolean;
includeViews?: boolean;
trx?: KyselyTransaction;
},
): Promise<Base | undefined> {
const db = dbOrTx(this.db, opts?.trx);
let query = db
.selectFrom('bases')
.select(this.baseFields)
.where('id', '=', baseId)
.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<Base | undefined>;
}
async findBySpaceId(
spaceId: string,
pagination: PaginationOptions,
opts?: { trx?: KyselyTransaction },
) {
const db = dbOrTx(this.db, opts?.trx);
const query = db
.selectFrom('bases')
.select(this.baseFields)
.where('spaceId', '=', spaceId)
.where('deletedAt', 'is', null);
return executeWithCursorPagination(query, {
perPage: pagination.limit,
cursor: pagination.cursor,
beforeCursor: pagination.beforeCursor,
fields: [
{ expression: 'createdAt', direction: 'desc' },
{ expression: 'id', direction: 'desc' },
],
parseCursor: (cursor) => ({
createdAt: new Date(cursor.createdAt),
id: cursor.id,
}),
});
}
async insertBase(
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);
await db
.updateTable('bases')
.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() })
.where('id', '=', baseId)
.execute();
}
private withProperties(eb: ExpressionBuilder<DB, 'bases'>) {
return jsonArrayFrom(
eb
.selectFrom('baseProperties')
.selectAll('baseProperties')
.whereRef('baseProperties.baseId', '=', 'bases.id')
.orderBy('baseProperties.position', 'asc'),
).as('properties');
}
private withViews(eb: ExpressionBuilder<DB, 'bases'>) {
return jsonArrayFrom(
eb
.selectFrom('baseViews')
.selectAll('baseViews')
.whereRef('baseViews.baseId', '=', 'bases.id')
.orderBy('baseViews.position', 'asc'),
).as('views');
}
}