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
@@ -27,6 +27,10 @@ import { ShareRepo } from '@docmost/db/repos/share/share.repo';
import { NotificationRepo } from '@docmost/db/repos/notification/notification.repo';
import { WatcherRepo } from '@docmost/db/repos/watcher/watcher.repo';
import { PageListener } from '@docmost/db/listeners/page.listener';
import { BaseRepo } from '@docmost/db/repos/base/base.repo';
import { BasePropertyRepo } from '@docmost/db/repos/base/base-property.repo';
import { BaseRowRepo } from '@docmost/db/repos/base/base-row.repo';
import { BaseViewRepo } from '@docmost/db/repos/base/base-view.repo';
import { PostgresJSDialect } from 'kysely-postgres-js';
import * as postgres from 'postgres';
import { normalizePostgresUrl } from '../common/helpers';
@@ -85,6 +89,10 @@ import { normalizePostgresUrl } from '../common/helpers';
NotificationRepo,
WatcherRepo,
PageListener,
BaseRepo,
BasePropertyRepo,
BaseRowRepo,
BaseViewRepo,
],
exports: [
WorkspaceRepo,
@@ -102,6 +110,10 @@ import { normalizePostgresUrl } from '../common/helpers';
ShareRepo,
NotificationRepo,
WatcherRepo,
BaseRepo,
BasePropertyRepo,
BaseRowRepo,
BaseViewRepo,
],
})
export class DatabaseModule
@@ -0,0 +1,154 @@
import { type Kysely, sql } from 'kysely';
export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.createTable('bases')
.addColumn('id', 'uuid', (col) =>
col.primaryKey().defaultTo(sql`gen_uuid_v7()`),
)
.addColumn('name', 'varchar', (col) => col.notNull())
.addColumn('description', 'varchar')
.addColumn('icon', 'varchar')
.addColumn('page_id', 'uuid', (col) =>
col.references('pages.id').onDelete('cascade'),
)
.addColumn('space_id', 'uuid', (col) =>
col.references('spaces.id').onDelete('cascade').notNull(),
)
.addColumn('workspace_id', 'uuid', (col) =>
col.references('workspaces.id').onDelete('cascade').notNull(),
)
.addColumn('creator_id', 'uuid', (col) =>
col.references('users.id').onDelete('set null'),
)
.addColumn('created_at', 'timestamptz', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn('updated_at', 'timestamptz', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn('deleted_at', 'timestamptz')
.execute();
await db.schema
.createIndex('idx_bases_space_id')
.on('bases')
.column('space_id')
.execute();
await db.schema
.createTable('base_properties')
.addColumn('id', 'uuid', (col) =>
col.primaryKey().defaultTo(sql`gen_uuid_v7()`),
)
.addColumn('base_id', 'uuid', (col) =>
col.references('bases.id').onDelete('cascade').notNull(),
)
.addColumn('name', 'varchar', (col) => col.notNull())
.addColumn('type', 'varchar', (col) => col.notNull())
.addColumn('position', 'varchar', (col) => col.notNull())
.addColumn('type_options', 'jsonb')
.addColumn('is_primary', 'boolean', (col) =>
col.notNull().defaultTo(false),
)
.addColumn('workspace_id', 'uuid', (col) =>
col.references('workspaces.id').onDelete('cascade').notNull(),
)
.addColumn('created_at', 'timestamptz', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn('updated_at', 'timestamptz', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.execute();
await db.schema
.createIndex('idx_base_properties_base_id')
.on('base_properties')
.column('base_id')
.execute();
await db.schema
.createTable('base_rows')
.addColumn('id', 'uuid', (col) =>
col.primaryKey().defaultTo(sql`gen_uuid_v7()`),
)
.addColumn('base_id', 'uuid', (col) =>
col.references('bases.id').onDelete('cascade').notNull(),
)
.addColumn('cells', 'jsonb', (col) =>
col.notNull().defaultTo(sql`'{}'::jsonb`),
)
.addColumn('position', 'varchar', (col) => col.notNull())
.addColumn('creator_id', 'uuid', (col) =>
col.references('users.id').onDelete('set null'),
)
.addColumn('last_updated_by_id', 'uuid', (col) =>
col.references('users.id').onDelete('set null'),
)
.addColumn('workspace_id', 'uuid', (col) =>
col.references('workspaces.id').onDelete('cascade').notNull(),
)
.addColumn('created_at', 'timestamptz', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn('updated_at', 'timestamptz', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn('deleted_at', 'timestamptz')
.execute();
await db.schema
.createIndex('idx_base_rows_base_id')
.on('base_rows')
.column('base_id')
.execute();
await db.schema
.createIndex('idx_base_rows_cells_gin')
.on('base_rows')
.using('gin')
.column('cells')
.execute();
await db.schema
.createTable('base_views')
.addColumn('id', 'uuid', (col) =>
col.primaryKey().defaultTo(sql`gen_uuid_v7()`),
)
.addColumn('base_id', 'uuid', (col) =>
col.references('bases.id').onDelete('cascade').notNull(),
)
.addColumn('name', 'varchar', (col) => col.notNull())
.addColumn('type', 'varchar', (col) => col.notNull().defaultTo('table'))
.addColumn('position', 'varchar', (col) => col.notNull())
.addColumn('config', 'jsonb', (col) =>
col.notNull().defaultTo(sql`'{}'::jsonb`),
)
.addColumn('workspace_id', 'uuid', (col) =>
col.references('workspaces.id').onDelete('cascade').notNull(),
)
.addColumn('creator_id', 'uuid', (col) =>
col.references('users.id').onDelete('set null'),
)
.addColumn('created_at', 'timestamptz', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn('updated_at', 'timestamptz', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.execute();
await db.schema
.createIndex('idx_base_views_base_id')
.on('base_views')
.column('base_id')
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema.dropTable('base_views').execute();
await db.schema.dropTable('base_rows').execute();
await db.schema.dropTable('base_properties').execute();
await db.schema.dropTable('bases').execute();
}
@@ -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');
}
}
+57
View File
@@ -390,9 +390,66 @@ export interface Watchers {
createdAt: Generated<Timestamp>;
}
export interface Bases {
id: Generated<string>;
name: string;
description: string | null;
icon: string | null;
pageId: string | null;
spaceId: string;
workspaceId: string;
creatorId: string | null;
createdAt: Generated<Timestamp>;
updatedAt: Generated<Timestamp>;
deletedAt: Timestamp | null;
}
export interface BaseProperties {
id: Generated<string>;
baseId: string;
name: string;
type: string;
position: string;
typeOptions: Json | null;
isPrimary: Generated<boolean>;
workspaceId: string;
createdAt: Generated<Timestamp>;
updatedAt: Generated<Timestamp>;
}
export interface BaseRows {
id: Generated<string>;
baseId: string;
cells: Generated<Json>;
position: string;
creatorId: string | null;
lastUpdatedById: string | null;
workspaceId: string;
createdAt: Generated<Timestamp>;
updatedAt: Generated<Timestamp>;
deletedAt: Timestamp | null;
}
export interface BaseViews {
id: Generated<string>;
baseId: string;
name: string;
type: Generated<string>;
position: string;
config: Generated<Json>;
workspaceId: string;
creatorId: string | null;
createdAt: Generated<Timestamp>;
updatedAt: Generated<Timestamp>;
}
export interface DB {
apiKeys: ApiKeys;
attachments: Attachments;
baseProperties: BaseProperties;
baseRows: BaseRows;
baseViews: BaseViews;
bases: Bases;
authAccounts: AuthAccounts;
authProviders: AuthProviders;
backlinks: Backlinks;
@@ -4,6 +4,10 @@ import {
AuthAccounts,
AuthProviders,
Backlinks,
BaseProperties,
BaseRows,
BaseViews,
Bases,
Billing,
Comments,
FileTasks,
@@ -27,6 +31,10 @@ import { PageEmbeddings } from '@docmost/db/types/embeddings.types';
export interface DbInterface {
attachments: Attachments;
authAccounts: AuthAccounts;
baseProperties: BaseProperties;
baseRows: BaseRows;
baseViews: BaseViews;
bases: Bases;
authProviders: AuthProviders;
backlinks: Backlinks;
billing: Billing;
@@ -1,6 +1,10 @@
import { Insertable, Selectable, Updateable } from 'kysely';
import {
Attachments,
BaseProperties,
BaseRows,
BaseViews,
Bases,
Comments,
Groups,
Notifications,
@@ -143,3 +147,23 @@ export type UpdatableNotification = Updateable<Omit<Notifications, 'id'>>;
export type Watcher = Selectable<Watchers>;
export type InsertableWatcher = Insertable<Watchers>;
export type UpdatableWatcher = Updateable<Omit<Watchers, 'id'>>;
// Base
export type Base = Selectable<Bases>;
export type InsertableBase = Insertable<Bases>;
export type UpdatableBase = Updateable<Omit<Bases, 'id'>>;
// Base Property
export type BaseProperty = Selectable<BaseProperties>;
export type InsertableBaseProperty = Insertable<BaseProperties>;
export type UpdatableBaseProperty = Updateable<Omit<BaseProperties, 'id'>>;
// Base Row
export type BaseRow = Selectable<BaseRows>;
export type InsertableBaseRow = Insertable<BaseRows>;
export type UpdatableBaseRow = Updateable<Omit<BaseRows, 'id'>>;
// Base View
export type BaseView = Selectable<BaseViews>;
export type InsertableBaseView = Insertable<BaseViews>;
export type UpdatableBaseView = Updateable<Omit<BaseViews, 'id'>>;