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,70 @@
import { Global, Module } from '@nestjs/common';
import { KyselyModule } from 'nestjs-kysely';
import { EnvironmentService } from '../integrations/environment/environment.service';
import { LogEvent, PostgresDialect } from 'kysely';
import { Pool } from 'pg';
import { GroupRepo } from '@docmost/db/repos/group/group.repo';
import { WorkspaceRepo } from '@docmost/db/repos/workspace/workspace.repo';
import { UserRepo } from '@docmost/db/repos/user/user.repo';
import { GroupUserRepo } from '@docmost/db/repos/group/group-user.repo';
import { SpaceRepo } from '@docmost/db/repos/space/space.repo';
import { SpaceMemberRepo } from '@docmost/db/repos/space/space-member.repo';
import { PageRepo } from './repos/page/page.repo';
import { CommentRepo } from './repos/comment/comment.repo';
import { PageHistoryRepo } from './repos/page/page-history.repo';
import { PageOrderingRepo } from './repos/page/page-ordering.repo';
import { AttachmentRepo } from './repos/attachment/attachment.repo';
@Global()
@Module({
imports: [
KyselyModule.forRootAsync({
imports: [],
inject: [EnvironmentService],
useFactory: (environmentService: EnvironmentService) => ({
dialect: new PostgresDialect({
pool: new Pool({
connectionString: environmentService.getDatabaseURL(),
}) as any,
}),
log: (event: LogEvent) => {
if (environmentService.getEnv() !== 'development') return;
if (event.level === 'query') {
console.log(event.query.sql);
if (event.query.parameters.length > 0) {
console.log('parameters: ' + event.query.parameters);
}
console.log('time: ' + event.queryDurationMillis);
}
},
}),
}),
],
providers: [
WorkspaceRepo,
UserRepo,
GroupRepo,
GroupUserRepo,
SpaceRepo,
SpaceMemberRepo,
PageRepo,
PageHistoryRepo,
PageOrderingRepo,
CommentRepo,
AttachmentRepo,
],
exports: [
WorkspaceRepo,
UserRepo,
GroupRepo,
GroupUserRepo,
SpaceRepo,
SpaceMemberRepo,
PageRepo,
PageHistoryRepo,
PageOrderingRepo,
CommentRepo,
AttachmentRepo,
],
})
export class KyselyDbModule {}

View File

@ -12,7 +12,9 @@ export async function up(db: Kysely<any>): Promise<void> {
.addColumn('logo', 'varchar', (col) => col)
.addColumn('hostname', 'varchar', (col) => col)
.addColumn('customDomain', 'varchar', (col) => col)
.addColumn('enableInvite', 'boolean', (col) => col.notNull())
.addColumn('enableInvite', 'boolean', (col) =>
col.defaultTo(true).notNull(),
)
.addColumn('inviteCode', 'varchar', (col) => col)
.addColumn('settings', 'jsonb', (col) => col)
.addColumn('defaultRole', 'varchar', (col) =>
@ -27,13 +29,9 @@ export async function up(db: Kysely<any>): Promise<void> {
col.notNull().defaultTo(sql`now()`),
)
.addColumn('deletedAt', 'timestamp', (col) => col)
.addUniqueConstraint('UQ_workspaces_hostname', ['hostname'])
.addUniqueConstraint('UQ_workspaces_inviteCode', ['inviteCode'])
.addUniqueConstraint('UQ_workspaces_inviteCode', ['inviteCode'])
.addUniqueConstraint('workspaces_hostname_unique', ['hostname'])
.addUniqueConstraint('workspaces_inviteCode_unique', ['inviteCode'])
.execute();
// CONSTRAINT "REL_workspaces_creatorId" UNIQUE ("creatorId"),
// CONSTRAINT "REL_workspaces_defaultSpaceId" UNIQUE ("defaultSpaceId"),
}
export async function down(db: Kysely<any>): Promise<void> {

View File

@ -12,10 +12,14 @@ export async function up(db: Kysely<any>): Promise<void> {
.addColumn('password', 'varchar', (col) => col.notNull())
.addColumn('avatarUrl', 'varchar', (col) => col)
.addColumn('role', 'varchar', (col) => col)
.addColumn('workspaceId', 'uuid', (col) => col)
.addColumn('status', 'varchar', (col) => col)
.addColumn('workspaceId', 'uuid', (col) =>
col.references('workspaces.id').onDelete('cascade'),
)
.addColumn('locale', 'varchar', (col) => col)
.addColumn('timezone', 'varchar', (col) => col)
.addColumn('settings', 'jsonb', (col) => col)
.addColumn('lastActiveAt', 'timestamp', (col) => col)
.addColumn('lastLoginAt', 'timestamp', (col) => col)
.addColumn('lastLoginIp', 'varchar', (col) => col)
.addColumn('createdAt', 'timestamp', (col) =>
@ -24,26 +28,17 @@ export async function up(db: Kysely<any>): Promise<void> {
.addColumn('updatedAt', 'timestamp', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addUniqueConstraint('UQ_users_email_workspaceId', ['email', 'workspaceId'])
.execute();
// foreign key relations
await db.schema
.alterTable('users')
.addForeignKeyConstraint(
'FK_users_workspaces_workspaceId',
['workspaceId'],
'workspaces',
['id'],
)
.onDelete('cascade')
.addUniqueConstraint('users_email_workspaceId_unique', [
'email',
'workspaceId',
])
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('users')
.dropConstraint('FK_users_workspaces_workspaceId')
.dropConstraint('users_workspaceId_fkey')
.execute();
await db.schema.dropTable('users').execute();

View File

@ -9,50 +9,33 @@ export async function up(db: Kysely<any>): Promise<void> {
.addColumn('name', 'varchar', (col) => col.notNull())
.addColumn('description', 'text', (col) => col)
.addColumn('isDefault', 'boolean', (col) => col.notNull())
.addColumn('workspaceId', 'uuid', (col) => col.notNull())
.addColumn('creatorId', 'uuid', (col) => col)
.addColumn('workspaceId', 'uuid', (col) =>
col.references('workspaces.id').onDelete('cascade').notNull(),
)
.addColumn('creatorId', 'uuid', (col) => col.references('users.id'))
.addColumn('createdAt', 'timestamp', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn('updatedAt', 'timestamp', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addUniqueConstraint('UQ_groups_name_workspaceId', ['name', 'workspaceId'])
.addUniqueConstraint('groups_name_workspaceId_unique', [
'name',
'workspaceId',
])
.execute();
// foreign key relations
await db.schema
.alterTable('groups')
.addForeignKeyConstraint(
'FK_groups_workspaces_workspaceId',
['workspaceId'],
'workspaces',
['id'],
)
.onDelete('cascade')
.execute();
await db.schema
.alterTable('groups')
.addForeignKeyConstraint(
'FK_groups_users_creatorId',
['creatorId'],
'users',
['id'],
)
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('groups')
.dropConstraint('FK_groups_workspaces_workspaceId')
.dropConstraint('groups_workspaceId_fkey')
.execute();
await db.schema
.alterTable('groups')
.dropConstraint('FK_groups_users_creatorId')
.dropConstraint('groups_creatorId_fkey')
.execute();
await db.schema.dropTable('groups').execute();

View File

@ -6,50 +6,34 @@ export async function up(db: Kysely<any>): Promise<void> {
.addColumn('id', 'uuid', (col) =>
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
)
.addColumn('userId', 'uuid', (col) => col.notNull())
.addColumn('groupId', 'uuid', (col) => col.notNull())
.addColumn('userId', 'uuid', (col) =>
col.references('users.id').onDelete('cascade').notNull(),
)
.addColumn('groupId', 'uuid', (col) =>
col.references('groups.id').onDelete('cascade').notNull(),
)
.addColumn('createdAt', 'timestamp', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn('updatedAt', 'timestamp', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addUniqueConstraint('UQ_group_users_groupId_userId', ['groupId', 'userId'])
.execute();
// foreign key relations
await db.schema
.alterTable('group_users')
.addForeignKeyConstraint(
'FK_group_users_users_userId',
['userId'],
'users',
['id'],
)
.onDelete('cascade')
.execute();
await db.schema
.alterTable('group_users')
.addForeignKeyConstraint(
'FK_group_users_groups_groupId',
['groupId'],
'groups',
['id'],
)
.onDelete('cascade')
.addUniqueConstraint('group_users_groupId_userId_unique', [
'groupId',
'userId',
])
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('group_users')
.dropConstraint('FK_group_users_users_userId')
.dropConstraint('group_users_userId_fkey')
.execute();
await db.schema
.alterTable('group_users')
.dropConstraint('FK_group_users_groups_groupId')
.dropConstraint('group_users_groupId_fkey')
.execute();
await db.schema.dropTable('group_users').execute();

View File

@ -17,49 +17,32 @@ export async function up(db: Kysely<any>): Promise<void> {
.addColumn('defaultRole', 'varchar', (col) =>
col.defaultTo(SpaceRole.WRITER).notNull(),
)
.addColumn('creatorId', 'uuid', (col) => col)
.addColumn('workspaceId', 'uuid', (col) => col.notNull())
.addColumn('creatorId', 'uuid', (col) => col.references('users.id'))
.addColumn('workspaceId', 'uuid', (col) =>
col.references('workspaces.id').onDelete('cascade').notNull(),
)
.addColumn('createdAt', 'timestamp', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn('updatedAt', 'timestamp', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addUniqueConstraint('UQ_spaces_slug_workspaceId', ['slug', 'workspaceId'])
.execute();
// foreign key relations
await db.schema
.alterTable('spaces')
.addForeignKeyConstraint(
'FK_spaces_users_creatorId',
['creatorId'],
'users',
['id'],
)
.execute();
await db.schema
.alterTable('spaces')
.addForeignKeyConstraint(
'FK_spaces_workspaces_workspaceId',
['workspaceId'],
'workspaces',
['id'],
)
.onDelete('cascade')
.addUniqueConstraint('spaces_slug_workspaceId_unique', [
'slug',
'workspaceId',
])
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('spaces')
.dropConstraint('FK_spaces_users_creatorId')
.dropConstraint('spaces_creatorId_fkey')
.execute();
await db.schema
.alterTable('spaces')
.dropConstraint('FK_spaces_workspaces_workspaceId')
.dropConstraint('spaces_workspaceId_fkey')
.execute();
await db.schema.dropTable('spaces').execute();

View File

@ -6,95 +6,57 @@ export async function up(db: Kysely<any>): Promise<void> {
.addColumn('id', 'uuid', (col) =>
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
)
.addColumn('userId', 'uuid', (col) => col)
.addColumn('groupId', 'uuid', (col) => col)
.addColumn('spaceId', 'uuid', (col) => col.notNull())
.addColumn('userId', 'uuid', (col) =>
col.references('users.id').onDelete('cascade'),
)
.addColumn('groupId', 'uuid', (col) =>
col.references('groups.id').onDelete('cascade'),
)
.addColumn('spaceId', 'uuid', (col) =>
col.references('spaces.id').onDelete('cascade').notNull(),
)
.addColumn('role', 'varchar', (col) => col.notNull())
.addColumn('creatorId', 'uuid', (col) => col)
.addColumn('creatorId', 'uuid', (col) => col.references('users.id'))
.addColumn('createdAt', 'timestamp', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn('updatedAt', 'timestamp', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addUniqueConstraint('UQ_space_members_spaceId_userId', [
.addUniqueConstraint('space_members_spaceId_userId_unique', [
'spaceId',
'userId',
])
.addUniqueConstraint('UQ_space_members_spaceId_groupId', [
.addUniqueConstraint('space_members_spaceId_groupId_unique', [
'spaceId',
'groupId',
])
.addCheckConstraint(
'CHK_allow_userId_or_groupId',
'allow_either_userId_or_groupId_check',
sql`(("userId" IS NOT NULL AND "groupId" IS NULL) OR ("userId" IS NULL AND "groupId" IS NOT NULL))`,
)
.execute();
// foreign key relations
await db.schema
.alterTable('space_members')
.addForeignKeyConstraint(
'FK_space_members_users_userId',
['userId'],
'users',
['id'],
)
.onDelete('cascade')
.execute();
await db.schema
.alterTable('space_members')
.addForeignKeyConstraint(
'FK_space_members_groups_groupId',
['groupId'],
'groups',
['id'],
)
.onDelete('cascade')
.execute();
await db.schema
.alterTable('space_members')
.addForeignKeyConstraint(
'FK_space_members_spaces_spaceId',
['spaceId'],
'spaces',
['id'],
)
.onDelete('cascade')
.execute();
await db.schema
.alterTable('space_members')
.addForeignKeyConstraint(
'FK_space_members_users_creatorId',
['creatorId'],
'users',
['id'],
)
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('space_members')
.dropConstraint('FK_space_members_users_userId')
.dropConstraint('space_members_userId_fkey')
.execute();
await db.schema
.alterTable('space_members')
.dropConstraint('FK_space_members_groups_groupId')
.dropConstraint('space_members_groupId_fkey')
.execute();
await db.schema
.alterTable('space_members')
.dropConstraint('FK_space_members_spaces_spaceId')
.dropConstraint('space_members_spaceId_fkey')
.execute();
await db.schema
.alterTable('space_members')
.dropConstraint('FK_space_members_users_creatorId')
.dropConstraint('space_members_creatorId_fkey')
.execute();
await db.schema.dropTable('space_members').execute();
}

View File

@ -4,7 +4,7 @@ export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('workspaces')
.addForeignKeyConstraint(
'FK_workspaces_users_creatorId',
'workspaces_creatorId_fkey',
['creatorId'],
'users',
['id'],
@ -14,7 +14,7 @@ export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('workspaces')
.addForeignKeyConstraint(
'FK_workspaces_spaces_defaultSpaceId',
'workspaces_defaultSpaceId_fkey',
['defaultSpaceId'],
'spaces',
['id'],
@ -26,11 +26,11 @@ export async function up(db: Kysely<any>): Promise<void> {
export async function down(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('workspaces')
.dropConstraint('FK_workspaces_users_creatorId')
.dropConstraint('workspaces_creatorId_fkey')
.execute();
await db.schema
.alterTable('workspaces')
.dropConstraint('FK_workspaces_spaces_defaultSpaceId')
.dropConstraint('workspaces_defaultSpaceId_fkey')
.execute();
}

View File

@ -6,8 +6,10 @@ export async function up(db: Kysely<any>): Promise<void> {
.addColumn('id', 'uuid', (col) =>
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
)
.addColumn('workspaceId', 'uuid', (col) => col.notNull())
.addColumn('invitedById', 'uuid', (col) => col.notNull())
.addColumn('workspaceId', 'uuid', (col) =>
col.references('workspaces.id').onDelete('cascade').notNull(),
)
.addColumn('invitedById', 'uuid', (col) => col.references('users.id'))
.addColumn('email', 'varchar', (col) => col.notNull())
.addColumn('role', 'varchar', (col) => col.notNull())
.addColumn('status', 'varchar', (col) => col)
@ -18,39 +20,17 @@ export async function up(db: Kysely<any>): Promise<void> {
col.notNull().defaultTo(sql`now()`),
)
.execute();
// foreign key relations
await db.schema
.alterTable('workspace_invitations')
.addForeignKeyConstraint(
'FK_workspace_invitations_workspaces_workspaceId',
['workspaceId'],
'workspaces',
['id'],
)
.onDelete('cascade')
.execute();
await db.schema
.alterTable('workspace_invitations')
.addForeignKeyConstraint(
'FK_workspace_invitations_users_invitedById',
['invitedById'],
'users',
['id'],
)
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('workspace_invitations')
.dropConstraint('FK_workspace_invitations_workspaces_workspaceId')
.dropConstraint('workspace_invitations_workspaceId_fkey')
.execute();
await db.schema
.alterTable('workspace_invitations')
.dropConstraint('FK_workspace_invitations_users_invitedById')
.dropConstraint('workspace_invitations_invitedById_fkey')
.execute();
await db.schema.dropTable('workspace_invitations').execute();
}

View File

@ -17,12 +17,18 @@ export async function up(db: Kysely<any>): Promise<void> {
.addColumn('coverPhoto', 'varchar', (col) => col)
.addColumn('editor', 'varchar', (col) => col)
.addColumn('shareId', 'varchar', (col) => col)
.addColumn('parentPageId', 'uuid', (col) => col)
.addColumn('creatorId', 'uuid', (col) => col.notNull())
.addColumn('lastUpdatedById', 'uuid', (col) => col)
.addColumn('deletedById', 'uuid', (col) => col)
.addColumn('spaceId', 'uuid', (col) => col.notNull())
.addColumn('workspaceId', 'uuid', (col) => col.notNull())
.addColumn('parentPageId', 'uuid', (col) =>
col.references('pages.id').onDelete('cascade'),
)
.addColumn('creatorId', 'uuid', (col) => col.references('users.id'))
.addColumn('lastUpdatedById', 'uuid', (col) => col.references('users.id'))
.addColumn('deletedById', 'uuid', (col) => col.references('users.id'))
.addColumn('spaceId', 'uuid', (col) =>
col.references('spaces.id').onDelete('cascade').notNull(),
)
.addColumn('workspaceId', 'uuid', (col) =>
col.references('workspaces.id').onDelete('cascade').notNull(),
)
.addColumn('isLocked', 'boolean', (col) => col.defaultTo(false).notNull())
.addColumn('status', 'varchar', (col) => col)
.addColumn('publishedAt', 'date', (col) => col)
@ -36,7 +42,7 @@ export async function up(db: Kysely<any>): Promise<void> {
.execute();
await db.schema
.createIndex('IDX_pages_tsv')
.createIndex('pages_tsv_idx')
.on('pages')
.using('GIN')
.column('tsv')
@ -44,5 +50,35 @@ export async function up(db: Kysely<any>): Promise<void> {
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('pages')
.dropConstraint('pages_creatorId_fkey')
.execute();
await db.schema
.alterTable('pages')
.dropConstraint('pages_lastUpdatedById_fkey')
.execute();
await db.schema
.alterTable('pages')
.dropConstraint('pages_deletedById_fkey')
.execute();
await db.schema
.alterTable('pages')
.dropConstraint('pages_spaceId_fkey')
.execute();
await db.schema
.alterTable('pages')
.dropConstraint('pages_workspaceId_fkey')
.execute();
await db.schema
.alterTable('pages')
.dropConstraint('pages_parentPageId_fkey')
.execute();
await db.schema.dropTable('pages').execute();
}

View File

@ -1,94 +0,0 @@
import { Kysely } from 'kysely';
export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('pages')
.addForeignKeyConstraint(
'FK_pages_users_creatorId',
['creatorId'],
'users',
['id'],
)
.execute();
await db.schema
.alterTable('pages')
.addForeignKeyConstraint(
'FK_pages_users_lastUpdatedById',
['lastUpdatedById'],
'users',
['id'],
)
.execute();
await db.schema
.alterTable('pages')
.addForeignKeyConstraint(
'FK_pages_users_deletedById',
['deletedById'],
'users',
['id'],
)
.execute();
await db.schema
.alterTable('pages')
.addForeignKeyConstraint('FK_pages_spaces_spaceId', ['spaceId'], 'spaces', [
'id',
])
.onDelete('cascade')
.execute();
await db.schema
.alterTable('pages')
.addForeignKeyConstraint(
'FK_pages_workspaces_workspaceId',
['workspaceId'],
'workspaces',
['id'],
)
.onDelete('cascade')
.execute();
await db.schema
.alterTable('pages')
.addForeignKeyConstraint(
'FK_pages_pages_parentPageId',
['parentPageId'],
'pages',
['id'],
)
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('pages')
.dropConstraint('FK_pages_users_creatorId')
.execute();
await db.schema
.alterTable('pages')
.dropConstraint('FK_pages_users_lastUpdatedById')
.execute();
await db.schema
.alterTable('pages')
.dropConstraint('FK_pages_users_deletedById')
.execute();
await db.schema
.alterTable('pages')
.dropConstraint('FK_pages_spaces_spaceId')
.execute();
await db.schema
.alterTable('pages')
.dropConstraint('FK_pages_workspaces_workspaceId')
.execute();
await db.schema
.alterTable('pages')
.dropConstraint('FK_pages_pages_parentPageId')
.execute();
}

View File

@ -6,16 +6,22 @@ export async function up(db: Kysely<any>): Promise<void> {
.addColumn('id', 'uuid', (col) =>
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
)
.addColumn('pageId', 'uuid', (col) => col.notNull())
.addColumn('pageId', 'uuid', (col) =>
col.references('pages.id').onDelete('cascade').notNull(),
)
.addColumn('title', 'varchar', (col) => col)
.addColumn('content', 'jsonb', (col) => col)
.addColumn('slug', 'varchar', (col) => col)
.addColumn('icon', 'varchar', (col) => col)
.addColumn('coverPhoto', 'varchar', (col) => col)
.addColumn('version', 'int4', (col) => col.notNull())
.addColumn('lastUpdatedById', 'uuid', (col) => col.notNull())
.addColumn('spaceId', 'uuid', (col) => col.notNull())
.addColumn('workspaceId', 'uuid', (col) => col.notNull())
.addColumn('lastUpdatedById', 'uuid', (col) => col.references('users.id'))
.addColumn('spaceId', 'uuid', (col) =>
col.references('spaces.id').onDelete('cascade').notNull(),
)
.addColumn('workspaceId', 'uuid', (col) =>
col.references('workspaces.id').onDelete('cascade').notNull(),
)
.addColumn('createdAt', 'timestamp', (col) =>
col.notNull().defaultTo(sql`now()`),
)
@ -23,72 +29,27 @@ export async function up(db: Kysely<any>): Promise<void> {
col.notNull().defaultTo(sql`now()`),
)
.execute();
// foreign key relations
await db.schema
.alterTable('page_history')
.addForeignKeyConstraint(
'FK_page_history_pages_pageId',
['pageId'],
'pages',
['id'],
)
.onDelete('cascade')
.execute();
await db.schema
.alterTable('page_history')
.addForeignKeyConstraint(
'FK_page_history_users_lastUpdatedById',
['lastUpdatedById'],
'users',
['id'],
)
.execute();
await db.schema
.alterTable('page_history')
.addForeignKeyConstraint(
'FK_page_history_spaces_spaceId',
['spaceId'],
'spaces',
['id'],
)
.onDelete('cascade')
.execute();
await db.schema
.alterTable('page_history')
.addForeignKeyConstraint(
'FK_page_history_workspaces_workspaceId',
['workspaceId'],
'workspaces',
['id'],
)
.onDelete('cascade')
.onUpdate('no action')
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('page_history')
.dropConstraint('FK_page_history_pages_pageId')
.dropConstraint('page_history_pageId_fkey')
.execute();
await db.schema
.alterTable('page_history')
.dropConstraint('FK_page_history_users_lastUpdatedById')
.dropConstraint('page_history_lastUpdatedById_fkey')
.execute();
await db.schema
.alterTable('page_history')
.dropConstraint('FK_page_history_spaces_spaceId')
.dropConstraint('page_history_spaceId_fkey')
.execute();
await db.schema
.alterTable('page_history')
.dropConstraint('FK_page_history_workspaces_workspaceId')
.dropConstraint('page_history_workspaceId_fkey')
.execute();
await db.schema.dropTable('page_history').execute();

View File

@ -9,8 +9,12 @@ export async function up(db: Kysely<any>): Promise<void> {
.addColumn('entityId', 'uuid', (col) => col.notNull())
.addColumn('entityType', 'varchar', (col) => col.notNull())
.addColumn('childrenIds', sql`uuid[]`, (col) => col.notNull())
.addColumn('spaceId', 'uuid', (col) => col.notNull())
.addColumn('workspaceId', 'uuid', (col) => col.notNull())
.addColumn('spaceId', 'uuid', (col) =>
col.references('spaces.id').onDelete('cascade').notNull(),
)
.addColumn('workspaceId', 'uuid', (col) =>
col.references('workspaces.id').onDelete('cascade').notNull(),
)
.addColumn('createdAt', 'timestamp', (col) =>
col.notNull().defaultTo(sql`now()`),
)
@ -18,45 +22,22 @@ export async function up(db: Kysely<any>): Promise<void> {
col.notNull().defaultTo(sql`now()`),
)
.addColumn('deletedAt', 'timestamp', (col) => col)
.addUniqueConstraint('UQ_page_ordering_entityId_entityType', [
.addUniqueConstraint('page_ordering_entityId_entityType_unique', [
'entityId',
'entityType',
])
.execute();
// foreign key relations
await db.schema
.alterTable('page_ordering')
.addForeignKeyConstraint(
'FK_page_ordering_spaces_spaceId',
['spaceId'],
'spaces',
['id'],
)
.onDelete('cascade')
.execute();
await db.schema
.alterTable('page_ordering')
.addForeignKeyConstraint(
'FK_page_ordering_workspaces_workspaceId',
['workspaceId'],
'workspaces',
['id'],
)
.onDelete('cascade')
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('page_ordering')
.dropConstraint('FK_page_ordering_spaces_spaceId')
.dropConstraint('page_ordering_spaceId_fkey')
.execute();
await db.schema
.alterTable('page_ordering')
.dropConstraint('FK_page_ordering_workspaces_workspaceId')
.dropConstraint('page_ordering_workspaceId_fkey')
.execute();
await db.schema.dropTable('page_ordering').execute();

View File

@ -9,112 +9,43 @@ export async function up(db: Kysely<any>): Promise<void> {
.addColumn('content', 'jsonb', (col) => col)
.addColumn('selection', 'varchar', (col) => col)
.addColumn('type', 'varchar', (col) => col)
.addColumn('creatorId', 'uuid', (col) => col.notNull())
.addColumn('pageId', 'uuid', (col) => col.notNull())
.addColumn('parentCommentId', 'uuid', (col) => col)
.addColumn('resolvedById', 'uuid', (col) => col)
.addColumn('resolvedAt', 'timestamp', (col) => col)
.addColumn('spaceId', 'uuid', (col) => col.notNull())
.addColumn('workspaceId', 'uuid', (col) => col.notNull())
.addColumn('creatorId', 'uuid', (col) => col.references('users.id'))
.addColumn('pageId', 'uuid', (col) =>
col.references('pages.id').onDelete('cascade').notNull(),
)
.addColumn('parentCommentId', 'uuid', (col) =>
col.references('comments.id').onDelete('cascade'),
)
.addColumn('workspaceId', 'uuid', (col) =>
col.references('workspaces.id').notNull(),
)
.addColumn('createdAt', 'timestamp', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn('editedAt', 'timestamp', (col) => col)
.addColumn('deletedAt', 'timestamp', (col) => col)
.execute();
// foreign key relations
await db.schema
.alterTable('comments')
.addForeignKeyConstraint(
'FK_comments_users_creatorId',
['creatorId'],
'users',
['id'],
)
.execute();
await db.schema
.alterTable('comments')
.addForeignKeyConstraint('FK_comments_pages_pageId', ['pageId'], 'pages', [
'id',
])
.onDelete('cascade')
.execute();
await db.schema
.alterTable('comments')
.addForeignKeyConstraint(
'FK_comments_comments_parentCommentId',
['parentCommentId'],
'comments',
['id'],
)
.onDelete('cascade')
.execute();
await db.schema
.alterTable('comments')
.addForeignKeyConstraint(
'FK_comments_users_resolvedById',
['resolvedById'],
'users',
['id'],
)
.execute();
await db.schema
.alterTable('comments')
.addForeignKeyConstraint(
'FK_comments_spaces_spaceId',
['spaceId'],
'spaces',
['id'],
)
.onDelete('cascade')
.execute();
await db.schema
.alterTable('comments')
.addForeignKeyConstraint(
'FK_comments_workspaces_workspaceId',
['workspaceId'],
'workspaces',
['id'],
)
.onDelete('cascade')
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('comments')
.dropConstraint('FK_comments_users_creatorId')
.dropConstraint('comments_creatorId_fkey')
.execute();
await db.schema
.alterTable('comments')
.dropConstraint('FK_comments_pages_pageId')
.dropConstraint('comments_pageId_fkey')
.execute();
await db.schema
.alterTable('comments')
.dropConstraint('FK_comments_comments_parentCommentId')
.dropConstraint('comments_parentCommentId_fkey')
.execute();
await db.schema
.alterTable('comments')
.dropConstraint('FK_comments_users_resolvedById')
.execute();
await db.schema
.alterTable('comments')
.dropConstraint('FK_comments_spaces_spaceId')
.execute();
await db.schema
.alterTable('comments')
.dropConstraint('FK_comments_workspaces_workspaceId')
.dropConstraint('comments_workspaceId_fkey')
.execute();
await db.schema.dropTable('comments').execute();

View File

@ -12,10 +12,14 @@ export async function up(db: Kysely<any>): Promise<void> {
.addColumn('fileExt', 'varchar', (col) => col.notNull())
.addColumn('mimeType', 'varchar', (col) => col)
.addColumn('type', 'varchar', (col) => col)
.addColumn('creatorId', 'uuid', (col) => col.notNull())
.addColumn('pageId', 'uuid', (col) => col)
.addColumn('spaceId', 'uuid', (col) => col)
.addColumn('workspaceId', 'uuid', (col) => col.notNull())
.addColumn('creatorId', 'uuid', (col) =>
col.references('users.id').notNull(),
)
.addColumn('pageId', 'uuid', (col) => col.references('pages.id'))
.addColumn('spaceId', 'uuid', (col) => col.references('spaces.id'))
.addColumn('workspaceId', 'uuid', (col) =>
col.references('workspaces.id').onDelete('cascade').notNull(),
)
.addColumn('createdAt', 'timestamp', (col) =>
col.notNull().defaultTo(sql`now()`),
)
@ -24,70 +28,27 @@ export async function up(db: Kysely<any>): Promise<void> {
)
.addColumn('deletedAt', 'timestamp', (col) => col)
.execute();
// foreign key relations
await db.schema
.alterTable('attachments')
.addForeignKeyConstraint(
'FK_attachments_users_creatorId',
['creatorId'],
'users',
['id'],
)
.execute();
await db.schema
.alterTable('attachments')
.addForeignKeyConstraint(
'FK_attachments_pages_pageId',
['pageId'],
'pages',
['id'],
)
.execute();
await db.schema
.alterTable('attachments')
.addForeignKeyConstraint(
'FK_attachments_spaces_spaceId',
['spaceId'],
'spaces',
['id'],
)
.onDelete('cascade')
.execute();
await db.schema
.alterTable('attachments')
.addForeignKeyConstraint(
'FK_attachments_workspaces_workspaceId',
['workspaceId'],
'workspaces',
['id'],
)
.onDelete('cascade')
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('attachments')
.dropConstraint('FK_attachments_users_creatorId')
.dropConstraint('attachments_creatorId_fkey')
.execute();
await db.schema
.alterTable('attachments')
.dropConstraint('FK_attachments_pages_pageId')
.dropConstraint('attachments_pageId_fkey')
.execute();
await db.schema
.alterTable('attachments')
.dropConstraint('FK_attachments_spaces_spaceId')
.dropConstraint('attachments_spaceId_fkey')
.execute();
await db.schema
.alterTable('attachments')
.dropConstraint('FK_attachments_workspaces_workspaceId')
.dropConstraint('attachments_workspaceId_fkey')
.execute();
await db.schema.dropTable('attachments').execute();

View File

@ -1,7 +1,7 @@
import { type Kysely, sql } from 'kysely';
export async function up(db: Kysely<any>): Promise<void> {
await sql`CREATE FUNCTION pages_tsvector_trigger() RETURNS trigger AS $$
await sql`CREATE OR REPLACE FUNCTION pages_tsvector_trigger() RETURNS trigger AS $$
begin
new.tsv :=
setweight(to_tsvector('english', coalesce(new.title, '')), 'A') ||
@ -10,7 +10,7 @@ export async function up(db: Kysely<any>): Promise<void> {
end;
$$ LANGUAGE plpgsql;`.execute(db);
await sql`CREATE TRIGGER pages_tsvector_update BEFORE INSERT OR UPDATE
await sql`CREATE OR REPLACE TRIGGER pages_tsvector_update BEFORE INSERT OR UPDATE
ON pages FOR EACH ROW EXECUTE FUNCTION pages_tsvector_trigger();`.execute(
db,
);

View File

@ -0,0 +1,55 @@
import { Injectable } from '@nestjs/common';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
import { executeTx } from '@docmost/db/utils';
import {
Attachment,
InsertableAttachment,
UpdatableAttachment,
} from '@docmost/db/types/entity.types';
@Injectable()
export class AttachmentRepo {
constructor(@InjectKysely() private readonly db: KyselyDB) {}
async findById(
attachmentId: string,
workspaceId: string,
): Promise<Attachment> {
return this.db
.selectFrom('attachments')
.selectAll()
.where('id', '=', attachmentId)
.where('workspaceId', '=', workspaceId)
.executeTakeFirst();
}
async insertAttachment(
insertableAttachment: InsertableAttachment,
trx?: KyselyTransaction,
): Promise<Attachment> {
return await executeTx(
this.db,
async (trx) => {
return await trx
.insertInto('attachments')
.values(insertableAttachment)
.returningAll()
.executeTakeFirst();
},
trx,
);
}
async updateAttachment(
updatableAttachment: UpdatableAttachment,
attachmentId: string,
): Promise<void> {
await this.db
.updateTable('attachments')
.set(updatableAttachment)
.where('id', '=', attachmentId)
.returningAll()
.executeTakeFirst();
}
}

View File

@ -0,0 +1,85 @@
import { Injectable } from '@nestjs/common';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
import { executeTx } from '../../utils';
import {
Comment,
InsertableComment,
UpdatableComment,
} from '@docmost/db/types/entity.types';
import { PaginationOptions } from 'src/helpers/pagination/pagination-options';
@Injectable()
export class CommentRepo {
constructor(@InjectKysely() private readonly db: KyselyDB) {}
// todo, add workspaceId
async findById(commentId: string): Promise<Comment> {
return await this.db
.selectFrom('comments')
.selectAll()
.where('id', '=', commentId)
.executeTakeFirst();
}
async findPageComments(pageId: string, paginationOptions: PaginationOptions) {
return executeTx(this.db, async (trx) => {
const comments = await trx
.selectFrom('comments')
.selectAll()
.where('pageId', '=', pageId)
.orderBy('createdAt', 'asc')
.limit(paginationOptions.limit)
.offset(paginationOptions.offset)
.execute();
let { count } = await trx
.selectFrom('comments')
.select((eb) => eb.fn.count('id').as('count'))
.where('pageId', '=', pageId)
.executeTakeFirst();
count = count as number;
return { comments, count };
});
}
async updateComment(
updatableComment: UpdatableComment,
commentId: string,
trx?: KyselyTransaction,
) {
return await executeTx(
this.db,
async (trx) => {
return await trx
.updateTable('comments')
.set(updatableComment)
.where('id', '=', commentId)
.execute();
},
trx,
);
}
async insertComment(
insertableComment: InsertableComment,
trx?: KyselyTransaction,
): Promise<Comment> {
return await executeTx(
this.db,
async (trx) => {
return await trx
.insertInto('comments')
.values(insertableComment)
.returningAll()
.executeTakeFirst();
},
trx,
);
}
async deleteComment(commentId: string): Promise<void> {
await this.db.deleteFrom('comments').where('id', '=', commentId).execute();
}
}

View File

@ -0,0 +1,92 @@
import { Injectable } from '@nestjs/common';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
import { executeTx } from '@docmost/db/utils';
import {
GroupUser,
InsertableGroupUser,
User,
} from '@docmost/db/types/entity.types';
import { sql } from 'kysely';
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
@Injectable()
export class GroupUserRepo {
constructor(@InjectKysely() private readonly db: KyselyDB) {}
async getGroupUserById(
userId: string,
groupId: string,
trx?: KyselyTransaction,
) {
return await executeTx(
this.db,
async (trx) => {
return await trx
.selectFrom('group_users')
.selectAll()
.where('userId', '=', userId)
.where('groupId', '=', groupId)
.executeTakeFirst();
},
trx,
);
}
async insertGroupUser(
insertableGroupUser: InsertableGroupUser,
trx?: KyselyTransaction,
): Promise<GroupUser> {
return await executeTx(
this.db,
async (trx) => {
return await trx
.insertInto('group_users')
.values(insertableGroupUser)
.returningAll()
.executeTakeFirst();
},
trx,
);
}
async getGroupUsersPaginated(
groupId: string,
paginationOptions: PaginationOptions,
): Promise<{ users: User[]; count: number }> {
// todo add group member count
return executeTx(this.db, async (trx) => {
const groupUsers = (await trx
.selectFrom('group_users')
.innerJoin('users', 'users.id', 'group_users.userId')
.select(sql<User>`users.*` as any)
.where('groupId', '=', groupId)
.limit(paginationOptions.limit)
.offset(paginationOptions.offset)
.execute()) as User[];
const users: User[] = groupUsers.map((user: User) => {
delete user.password;
return user;
});
let { count } = await trx
.selectFrom('group_users')
.select((eb) => eb.fn.count('id').as('count'))
.where('groupId', '=', groupId)
.executeTakeFirst();
count = count as number;
return { users, count };
});
}
async delete(userId: string, groupId: string): Promise<void> {
await this.db
.deleteFrom('group_users')
.where('userId', '=', userId)
.where('groupId', '=', groupId)
.execute();
}
}

View File

@ -0,0 +1,116 @@
import { Injectable } from '@nestjs/common';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
import { executeTx } from '@docmost/db/utils';
import {
Group,
InsertableGroup,
UpdatableGroup,
} from '@docmost/db/types/entity.types';
import { sql } from 'kysely';
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
@Injectable()
export class GroupRepo {
constructor(@InjectKysely() private readonly db: KyselyDB) {}
async findById(groupId: string, workspaceId: string): Promise<Group> {
return await this.db
.selectFrom('groups')
.selectAll()
.where('id', '=', groupId)
.where('workspaceId', '=', workspaceId)
.executeTakeFirst();
}
async findByName(groupName: string, workspaceId: string): Promise<Group> {
return await this.db
.selectFrom('groups')
.selectAll()
.where(sql`LOWER(name)`, '=', sql`LOWER(${groupName})`)
.where('workspaceId', '=', workspaceId)
.executeTakeFirst();
}
async update(
updatableGroup: UpdatableGroup,
groupId: string,
workspaceId: string,
): Promise<void> {
await this.db
.updateTable('groups')
.set(updatableGroup)
.where('id', '=', groupId)
.where('workspaceId', '=', workspaceId)
.execute();
}
async insertGroup(
insertableGroup: InsertableGroup,
trx?: KyselyTransaction,
): Promise<Group> {
return await executeTx(
this.db,
async (trx) => {
return await trx
.insertInto('groups')
.values(insertableGroup)
.returningAll()
.executeTakeFirst();
},
trx,
);
}
async getDefaultGroup(
workspaceId: string,
trx: KyselyTransaction,
): Promise<Group> {
return executeTx(
this.db,
async (trx) => {
return await trx
.selectFrom('groups')
.selectAll()
.where('isDefault', '=', true)
.where('workspaceId', '=', workspaceId)
.executeTakeFirst();
},
trx,
);
}
async getGroupsPaginated(
workspaceId: string,
paginationOptions: PaginationOptions,
) {
// todo add group member count
return executeTx(this.db, async (trx) => {
const groups = await trx
.selectFrom('groups')
.selectAll()
.where('workspaceId', '=', workspaceId)
.limit(paginationOptions.limit)
.offset(paginationOptions.offset)
.execute();
let { count } = await trx
.selectFrom('groups')
.select((eb) => eb.fn.count('id').as('count'))
.where('workspaceId', '=', workspaceId)
.executeTakeFirst();
count = count as number;
return { groups, count };
});
}
async delete(groupId: string, workspaceId: string): Promise<void> {
await this.db
.deleteFrom('groups')
.where('id', '=', groupId)
.where('workspaceId', '=', workspaceId)
.execute();
}
}

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 };
});
}
}

View File

@ -0,0 +1,66 @@
import { Injectable } from '@nestjs/common';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
import { executeTx } from '../../utils';
import {
InsertablePage,
Page,
UpdatablePage,
} from '@docmost/db/types/entity.types';
import { sql } from 'kysely';
@Injectable()
export class PageOrderingRepo {
constructor(@InjectKysely() private readonly db: KyselyDB) {}
async findById(pageId: string): Promise<Page> {
return await this.db
.selectFrom('pages')
.selectAll()
.where('id', '=', pageId)
.executeTakeFirst();
}
async slug(slug: string): Promise<Page> {
return await this.db
.selectFrom('pages')
.selectAll()
.where(sql`LOWER(slug)`, '=', sql`LOWER(${slug})`)
.executeTakeFirst();
}
async updatePage(
updatablePage: UpdatablePage,
pageId: string,
trx?: KyselyTransaction,
) {
return await executeTx(
this.db,
async (trx) => {
return await trx
.updateTable('pages')
.set(updatablePage)
.where('id', '=', pageId)
.execute();
},
trx,
);
}
async insertPage(
insertablePage: InsertablePage,
trx?: KyselyTransaction,
): Promise<Page> {
return await executeTx(
this.db,
async (trx) => {
return await trx
.insertInto('pages')
.values(insertablePage)
.returningAll()
.executeTakeFirst();
},
trx,
);
}
}

View File

@ -0,0 +1,149 @@
import { Injectable } from '@nestjs/common';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
import { executeTx } from '../../utils';
import {
InsertablePage,
Page,
UpdatablePage,
} from '@docmost/db/types/entity.types';
import { sql } from 'kysely';
import { PaginationOptions } from 'src/helpers/pagination/pagination-options';
import { OrderingEntity } from 'src/core/page/page.util';
import { PageWithOrderingDto } from 'src/core/page/dto/page-with-ordering.dto';
// TODO: scope to space/workspace
@Injectable()
export class PageRepo {
constructor(@InjectKysely() private readonly db: KyselyDB) {}
private baseFields: Array<keyof Page> = [
'id',
'title',
'slug',
'icon',
'coverPhoto',
'shareId',
'parentPageId',
'creatorId',
'lastUpdatedById',
'spaceId',
'workspaceId',
'isLocked',
'status',
'publishedAt',
'createdAt',
'updatedAt',
'deletedAt',
];
async findById(
pageId: string,
withJsonContent?: boolean,
withYdoc?: boolean,
): Promise<Page> {
return await this.db
.selectFrom('pages')
.select(this.baseFields)
.where('id', '=', pageId)
.$if(withJsonContent, (qb) => qb.select('content'))
.$if(withYdoc, (qb) => qb.select('ydoc'))
.executeTakeFirst();
}
async slug(slug: string): Promise<Page> {
return await this.db
.selectFrom('pages')
.selectAll()
.where(sql`LOWER(slug)`, '=', sql`LOWER(${slug})`)
.executeTakeFirst();
}
async updatePage(
updatablePage: UpdatablePage,
pageId: string,
trx?: KyselyTransaction,
) {
return await executeTx(
this.db,
async (trx) => {
return await trx
.updateTable('pages')
.set(updatablePage)
.where('id', '=', pageId)
.execute();
},
trx,
);
}
async insertPage(
insertablePage: InsertablePage,
trx?: KyselyTransaction,
): Promise<Page> {
return await executeTx(
this.db,
async (trx) => {
return await trx
.insertInto('pages')
.values(insertablePage)
.returningAll()
.executeTakeFirst();
},
trx,
);
}
async deletePage(pageId: string): Promise<void> {
await this.db.deleteFrom('pages').where('id', '=', pageId).execute();
}
async getRecentPagesInSpace(
spaceId: string,
paginationOptions: PaginationOptions,
) {
return executeTx(this.db, async (trx) => {
const pages = await trx
.selectFrom('pages')
.select(this.baseFields)
.where('spaceId', '=', spaceId)
.orderBy('updatedAt', 'desc')
.limit(paginationOptions.limit)
.offset(paginationOptions.offset)
.execute();
let { count } = await trx
.selectFrom('pages')
.select((eb) => eb.fn.count('id').as('count'))
.where('spaceId', '=', spaceId)
.executeTakeFirst();
count = count as number;
return { pages, count };
});
}
async getSpaceSidebarPages(spaceId: string, limit: number) {
const pages = await this.db
.selectFrom('pages as page')
.innerJoin('page_ordering as ordering', 'ordering.entityId', 'page.id')
.where('ordering.entityType', '=', OrderingEntity.PAGE)
.where('page.spaceId', '=', spaceId)
.select([
'page.id',
'page.title',
'page.icon',
'page.parentPageId',
'page.spaceId',
'ordering.childrenIds',
'page.creatorId',
'page.createdAt',
])
.orderBy('page.createdAt', 'desc')
.orderBy('updatedAt', 'desc')
.limit(limit)
.execute();
return pages;
}
}

View File

@ -0,0 +1,303 @@
import { Injectable } from '@nestjs/common';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
import { executeTx } from '@docmost/db/utils';
import {
InsertableSpaceMember,
SpaceMember,
} from '@docmost/db/types/entity.types';
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
import { MemberInfo } from './types';
import { sql } from 'kysely';
@Injectable()
export class SpaceMemberRepo {
constructor(@InjectKysely() private readonly db: KyselyDB) {}
async insertSpaceMember(
insertableSpaceMember: InsertableSpaceMember,
trx?: KyselyTransaction,
): Promise<SpaceMember> {
return await executeTx(
this.db,
async (trx) => {
return await trx
.insertInto('space_members')
.values(insertableSpaceMember)
.returningAll()
.executeTakeFirst();
},
trx,
);
}
async getSpaceMembersPaginated(
spaceId: string,
paginationOptions: PaginationOptions,
) {
return executeTx(this.db, async (trx) => {
const spaceMembers = await trx
.selectFrom('space_members')
.leftJoin('users', 'users.id', 'space_members.userId')
.leftJoin('groups', 'groups.id', 'space_members.groupId')
.select([
'groups.id as group_id',
'groups.name as group_name',
'groups.isDefault as group_isDefault',
'groups.id as groups_id',
'groups.id as groups_id',
'groups.id as groups_id',
'users.id as user_id',
'users.name as user_name',
'users.avatarUrl as user_avatarUrl',
'users.email as user_email',
'space_members.role',
])
.where('spaceId', '=', spaceId)
.orderBy('space_members.createdAt', 'asc')
.limit(paginationOptions.limit)
.offset(paginationOptions.offset)
.execute();
let memberInfo: MemberInfo;
const members = spaceMembers.map((member) => {
if (member.user_id) {
memberInfo = {
id: member.user_id,
name: member.user_name,
email: member.user_email,
avatarUrl: member.user_avatarUrl,
type: 'user',
};
} else if (member.group_id) {
memberInfo = {
id: member.group_id,
name: member.group_name,
isDefault: member.group_isDefault,
type: 'group',
};
// todo: member count
}
return {
...memberInfo,
role: member.role,
};
});
let { count } = await trx
.selectFrom('space_members')
.select((eb) => eb.fn.count('id').as('count'))
.where('spaceId', '=', spaceId)
.executeTakeFirst();
count = count as number;
return { members, count };
});
}
/*
* we want to get all the spaces a user belongs either directly or via a group
* we will pass the user id and workspace id as parameters
* if the user is a member of the space via multiple groups
* we will return the one with the highest role permission
* it should return an array
* Todo: needs more work. this is a draft
*/
async getUserSpaces(userId: string, workspaceId: string) {
const rolePriority = sql`CASE "space_members"."role"
WHEN 'owner' THEN 3
WHEN 'writer' THEN 2
WHEN 'reader' THEN 1
END`.as('role_priority');
const subquery = this.db
.selectFrom('spaces')
.innerJoin('space_members', 'spaces.id', 'space_members.spaceId')
.select([
'spaces.id',
'spaces.name',
'spaces.slug',
'spaces.icon',
'space_members.role',
rolePriority,
])
.where('space_members.userId', '=', userId)
.where('spaces.workspaceId', '=', workspaceId)
.unionAll(
this.db
.selectFrom('spaces')
.innerJoin('space_members', 'spaces.id', 'space_members.spaceId')
.innerJoin(
'group_users',
'space_members.groupId',
'group_users.groupId',
)
.select([
'spaces.id',
'spaces.name',
'spaces.slug',
'spaces.icon',
'space_members.role',
rolePriority,
])
.where('group_users.userId', '=', userId),
)
.as('membership');
const results = await this.db
.selectFrom(subquery)
.select([
'membership.id as space_id',
'membership.name as space_name',
'membership.slug as space_slug',
sql`MAX('role_priority')`.as('max_role_priority'),
sql`CASE MAX("role_priority")
WHEN 3 THEN 'owner'
WHEN 2 THEN 'writer'
WHEN 1 THEN 'reader'
END`.as('highest_role'),
])
.groupBy('membership.id')
.groupBy('membership.name')
.groupBy('membership.slug')
.execute();
let membership = {};
const spaces = results.map((result) => {
membership = {
id: result.space_id,
name: result.space_name,
role: result.highest_role,
};
return membership;
});
return spaces;
}
/*
* we want to get a user's role in a space.
* they user can be a member either directly or via a group
* we will pass the user id and space id and workspaceId to return the user's role
* if the user is a member of the space via multiple groups
* we will return the one with the highest role permission
* It returns the space id, space name, user role
* and how the role was derived 'via'
* if the user has no space permission (not a member) it returns undefined
*/
async getUserRoleInSpace(
userId: string,
spaceId: string,
workspaceId: string,
) {
const rolePriority = sql`CASE "space_members"."role"
WHEN 'owner' THEN 3
WHEN 'writer' THEN 2
WHEN 'reader' THEN 1
END`.as('role_priority');
const subquery = this.db
.selectFrom('spaces')
.innerJoin('space_members', 'spaces.id', 'space_members.spaceId')
.select([
'spaces.id',
'spaces.name',
'space_members.role',
'space_members.userId',
rolePriority,
])
.where('space_members.userId', '=', userId)
.where('spaces.id', '=', spaceId)
.where('spaces.workspaceId', '=', workspaceId)
.unionAll(
this.db
.selectFrom('spaces')
.innerJoin('space_members', 'spaces.id', 'space_members.spaceId')
.innerJoin(
'group_users',
'space_members.groupId',
'group_users.groupId',
)
.select([
'spaces.id',
'spaces.name',
'space_members.role',
'space_members.userId',
rolePriority,
])
.where('spaces.id', '=', spaceId)
.where('spaces.workspaceId', '=', workspaceId)
.where('group_users.userId', '=', userId),
)
.as('membership');
const result = await this.db
.selectFrom(subquery)
.select([
'membership.id as space_id',
'membership.name as space_name',
'membership.userId as user_id',
sql`MAX('role_priority')`.as('max_role_priority'),
sql`CASE MAX("role_priority")
WHEN 3 THEN 'owner'
WHEN 2 THEN 'writer'
WHEN 1 THEN 'reader'
END`.as('highest_role'),
])
.groupBy('membership.id')
.groupBy('membership.name')
.groupBy('membership.userId')
.executeTakeFirst();
let membership = {};
if (result) {
membership = {
id: result.space_id,
name: result.space_name,
role: result.highest_role,
via: result.user_id ? 'user' : 'group', // user_id is empty then role was derived via a group
};
return membership;
}
return undefined;
}
async getSpaceMemberById(
userId: string,
groupId: string,
trx?: KyselyTransaction,
) {
return await executeTx(
this.db,
async (trx) => {
return await trx
.selectFrom('space_members')
.selectAll()
.where('userId', '=', userId)
.where('groupId', '=', groupId)
.executeTakeFirst();
},
trx,
);
}
async removeUser(userId: string, spaceId: string): Promise<void> {
await this.db
.deleteFrom('space_members')
.where('userId', '=', userId)
.where('spaceId', '=', spaceId)
.execute();
}
async removeGroup(groupId: string, spaceId: string): Promise<void> {
await this.db
.deleteFrom('space_members')
.where('userId', '=', groupId)
.where('spaceId', '=', spaceId)
.execute();
}
}

View File

@ -0,0 +1,111 @@
import { Injectable } from '@nestjs/common';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
import { executeTx } from '@docmost/db/utils';
import {
InsertableSpace,
Space,
UpdatableSpace,
} from '@docmost/db/types/entity.types';
import { sql } from 'kysely';
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
@Injectable()
export class SpaceRepo {
constructor(@InjectKysely() private readonly db: KyselyDB) {}
async findById(spaceId: string, workspaceId: string): Promise<Space> {
return await this.db
.selectFrom('spaces')
.selectAll()
.where('id', '=', spaceId)
.where('workspaceId', '=', workspaceId)
.executeTakeFirst();
}
async findBySlug(slug: string, workspaceId: string): Promise<Space> {
return await this.db
.selectFrom('spaces')
.selectAll()
.where(sql`LOWER(slug)`, '=', sql`LOWER(${slug})`)
.where('workspaceId', '=', workspaceId)
.executeTakeFirst();
}
async slugExists(slug: string, workspaceId: string): Promise<boolean> {
let { count } = await this.db
.selectFrom('spaces')
.select((eb) => eb.fn.count('id').as('count'))
.where(sql`LOWER(slug)`, '=', sql`LOWER(${slug})`)
.where('workspaceId', '=', workspaceId)
.executeTakeFirst();
count = count as number;
return !!count;
}
async updateSpace(
updatableSpace: UpdatableSpace,
spaceId: string,
workspaceId: string,
) {
return await this.db
.updateTable('spaces')
.set(updatableSpace)
.where('id', '=', spaceId)
.where('workspaceId', '=', workspaceId)
.execute();
}
async insertSpace(
insertableSpace: InsertableSpace,
trx?: KyselyTransaction,
): Promise<Space> {
return await executeTx(
this.db,
async (trx) => {
return await trx
.insertInto('spaces')
.values(insertableSpace)
.returningAll()
.executeTakeFirst();
},
trx,
);
}
async getSpacesInWorkspace(
workspaceId: string,
paginationOptions: PaginationOptions,
) {
//todo: add member count
// to: show spaces user have access based on visibility and membership
return executeTx(this.db, async (trx) => {
const spaces = await trx
.selectFrom('spaces')
.selectAll()
.where('workspaceId', '=', workspaceId)
.limit(paginationOptions.limit)
.offset(paginationOptions.offset)
.execute();
let { count } = await trx
.selectFrom('spaces')
.select((eb) => eb.fn.count('id').as('count'))
.where('workspaceId', '=', workspaceId)
.executeTakeFirst();
count = count as number;
return { spaces, count };
});
}
async deleteSpace(spaceId: string, workspaceId: string): Promise<void> {
await this.db
.deleteFrom('spaces')
.where('id', '=', spaceId)
.where('workspaceId', '=', workspaceId)
.execute();
}
}

View File

@ -0,0 +1,17 @@
interface SpaceUserInfo {
id: string;
name: string;
email: string;
avatarUrl: string;
type: 'user';
}
interface SpaceGroupInfo {
id: string;
name: string;
isDefault: boolean;
memberCount?: number;
type: 'group';
}
export type MemberInfo = SpaceUserInfo | SpaceGroupInfo;

View File

@ -0,0 +1,149 @@
import { Injectable } from '@nestjs/common';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
import { Users } from '@docmost/db/types/db';
import { hashPassword } from '../../../helpers/utils';
import { executeTx } from '@docmost/db/utils';
import {
InsertableUser,
UpdatableUser,
User,
} from '@docmost/db/types/entity.types';
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
@Injectable()
export class UserRepo {
constructor(@InjectKysely() private readonly db: KyselyDB) {}
private baseFields: Array<keyof Users> = [
'id',
'email',
'name',
'emailVerifiedAt',
'avatarUrl',
'role',
'workspaceId',
'locale',
'timezone',
'settings',
'lastLoginAt',
'createdAt',
'updatedAt',
];
async findById(
userId: string,
workspaceId: string,
includePassword?: boolean,
): Promise<User> {
return this.db
.selectFrom('users')
.select(this.baseFields)
.$if(includePassword, (qb) => qb.select('password'))
.where('id', '=', userId)
.where('workspaceId', '=', workspaceId)
.executeTakeFirst();
}
async findByEmail(
email: string,
workspaceId: string,
includePassword?: boolean,
): Promise<User> {
return this.db
.selectFrom('users')
.select(this.baseFields)
.$if(includePassword, (qb) => qb.select('password'))
.where('email', '=', email)
.where('workspaceId', '=', workspaceId)
.executeTakeFirst();
}
async updateUser(
updatableUser: UpdatableUser,
userId: string,
workspaceId: string,
) {
return await this.db
.updateTable('users')
.set(updatableUser)
.where('id', '=', userId)
.where('workspaceId', '=', workspaceId)
.execute();
}
async updateLastLogin(userId: string, workspaceId: string) {
return await this.db
.updateTable('users')
.set({
lastLoginAt: new Date(),
})
.where('id', '=', userId)
.where('workspaceId', '=', workspaceId)
.execute();
}
async insertUser(
insertableUser: InsertableUser,
trx?: KyselyTransaction,
): Promise<User> {
const user: InsertableUser = {
name: insertableUser.name || insertableUser.email.split('@')[0],
email: insertableUser.email.toLowerCase(),
password: await hashPassword(insertableUser.password),
locale: 'en',
lastLoginAt: new Date(),
};
return await executeTx(
this.db,
async (trx) => {
return await trx
.insertInto('users')
.values(user)
.returningAll()
.executeTakeFirst();
},
trx,
);
}
async roleCountByWorkspaceId(
role: string,
workspaceId: string,
): Promise<number> {
const { count } = await this.db
.selectFrom('users')
.select((eb) => eb.fn.count('role').as('count'))
.where('role', '=', role)
.where('workspaceId', '=', workspaceId)
.executeTakeFirst();
return count as number;
}
async getUsersPaginated(
workspaceId: string,
paginationOptions: PaginationOptions,
) {
return executeTx(this.db, async (trx) => {
const users = await trx
.selectFrom('users')
.select(this.baseFields)
.where('workspaceId', '=', workspaceId)
.orderBy('createdAt asc')
.limit(paginationOptions.limit)
.offset(paginationOptions.offset)
.execute();
let { count } = await trx
.selectFrom('users')
.select((eb) => eb.fn.countAll().as('count'))
.where('workspaceId', '=', workspaceId)
.executeTakeFirst();
count = count as number;
return { users, count };
});
}
}

View File

@ -0,0 +1,83 @@
import { Injectable } from '@nestjs/common';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
import { executeTx } from '../../utils';
import {
InsertableWorkspace,
UpdatableWorkspace,
Workspace,
} from '@docmost/db/types/entity.types';
import { sql } from 'kysely';
@Injectable()
export class WorkspaceRepo {
constructor(@InjectKysely() private readonly db: KyselyDB) {}
async findById(workspaceId: string): Promise<Workspace> {
return await this.db
.selectFrom('workspaces')
.selectAll()
.where('id', '=', workspaceId)
.executeTakeFirst();
}
async findFirst(): Promise<Workspace> {
return await this.db
.selectFrom('workspaces')
.selectAll()
.orderBy('createdAt asc')
.limit(1)
.executeTakeFirst();
}
async findByHostname(hostname: string): Promise<Workspace> {
return await this.db
.selectFrom('workspaces')
.selectAll()
.where(sql`LOWER(hostname)`, '=', sql`LOWER(${hostname})`)
.executeTakeFirst();
}
async updateWorkspace(
updatableWorkspace: UpdatableWorkspace,
workspaceId: string,
trx?: KyselyTransaction,
) {
return await executeTx(
this.db,
async (trx) => {
return await trx
.updateTable('workspaces')
.set(updatableWorkspace)
.where('id', '=', workspaceId)
.execute();
},
trx,
);
}
async insertWorkspace(
insertableWorkspace: InsertableWorkspace,
trx?: KyselyTransaction,
): Promise<Workspace> {
return await executeTx(
this.db,
async (trx) => {
return await trx
.insertInto('workspaces')
.values(insertableWorkspace)
.returningAll()
.executeTakeFirst();
},
trx,
);
}
async count(): Promise<number> {
const { count } = await this.db
.selectFrom('workspaces')
.select((eb) => eb.fn.count('id').as('count'))
.executeTakeFirst();
return count as number;
}
}

View File

@ -45,16 +45,13 @@ export interface Attachments {
export interface Comments {
content: Json | null;
createdAt: Generated<Timestamp>;
creatorId: string;
creatorId: string | null;
deletedAt: Timestamp | null;
editedAt: Timestamp | null;
id: Generated<string>;
pageId: string;
parentCommentId: string | null;
resolvedAt: Timestamp | null;
resolvedById: string | null;
selection: string | null;
spaceId: string;
type: string | null;
workspaceId: string;
}
@ -84,7 +81,7 @@ export interface PageHistory {
createdAt: Generated<Timestamp>;
icon: string | null;
id: Generated<string>;
lastUpdatedById: string;
lastUpdatedById: string | null;
pageId: string;
slug: string | null;
spaceId: string;
@ -110,14 +107,14 @@ export interface Pages {
content: Json | null;
coverPhoto: string | null;
createdAt: Generated<Timestamp>;
creatorId: string;
creatorId: string | null;
deletedAt: Timestamp | null;
deletedById: string | null;
editor: string | null;
html: string | null;
icon: string | null;
id: Generated<string>;
isLocked: boolean;
isLocked: Generated<boolean>;
lastUpdatedById: string | null;
parentPageId: string | null;
publishedAt: Timestamp | null;
@ -164,6 +161,7 @@ export interface Users {
email: string;
emailVerifiedAt: Timestamp | null;
id: Generated<string>;
lastActiveAt: Timestamp | null;
lastLoginAt: Timestamp | null;
lastLoginIp: string | null;
locale: string | null;
@ -171,6 +169,7 @@ export interface Users {
password: string;
role: string | null;
settings: Json | null;
status: string | null;
timezone: string | null;
updatedAt: Generated<Timestamp>;
workspaceId: string | null;
@ -180,7 +179,7 @@ export interface WorkspaceInvitations {
createdAt: Generated<Timestamp>;
email: string;
id: Generated<string>;
invitedById: string;
invitedById: string | null;
role: string;
status: string | null;
updatedAt: Generated<Timestamp>;
@ -195,7 +194,7 @@ export interface Workspaces {
defaultSpaceId: string | null;
deletedAt: Timestamp | null;
description: string | null;
enableInvite: boolean;
enableInvite: Generated<boolean>;
hostname: string | null;
id: Generated<string>;
inviteCode: string | null;

View File

@ -0,0 +1,77 @@
import { Insertable, Selectable, Updateable } from 'kysely';
import {
Attachments,
Comments,
Groups,
Pages,
Spaces,
Users,
Workspaces,
PageHistory as History,
PageOrdering as Ordering,
GroupUsers,
SpaceMembers,
WorkspaceInvitations,
} from './db';
// Workspace
export type Workspace = Selectable<Workspaces>;
export type InsertableWorkspace = Insertable<Workspaces>;
export type UpdatableWorkspace = Updateable<Omit<Workspaces, 'id'>>;
// WorkspaceInvitation
export type WorkspaceInvitation = Selectable<WorkspaceInvitations>;
export type InsertableWorkspaceInvitation = Insertable<WorkspaceInvitations>;
export type UpdatableWorkspaceInvitation = Updateable<
Omit<WorkspaceInvitations, 'id'>
>;
// User
export type User = Selectable<Users>;
export type InsertableUser = Insertable<Users>;
export type UpdatableUser = Updateable<Omit<Users, 'id'>>;
// Space
export type Space = Selectable<Spaces>;
export type InsertableSpace = Insertable<Spaces>;
export type UpdatableSpace = Updateable<Omit<Spaces, 'id'>>;
// SpaceMember
export type SpaceMember = Selectable<SpaceMembers>;
export type InsertableSpaceMember = Insertable<SpaceMembers>;
export type UpdatableSpaceMember = Updateable<Omit<SpaceMembers, 'id'>>;
// Group
export type Group = Selectable<Groups>;
export type InsertableGroup = Insertable<Groups>;
export type UpdatableGroup = Updateable<Omit<Groups, 'id'>>;
// GroupUser
export type GroupUser = Selectable<GroupUsers>;
export type InsertableGroupUser = Insertable<GroupUsers>;
export type UpdatableGroupUser = Updateable<Omit<GroupUsers, 'id'>>;
// Page
export type Page = Selectable<Pages>;
export type InsertablePage = Insertable<Pages>;
export type UpdatablePage = Updateable<Omit<Pages, 'id'>>;
// PageHistory
export type PageHistory = Selectable<History>;
export type InsertablePageHistory = Insertable<History>;
export type UpdatablePageHistory = Updateable<Omit<History, 'id'>>;
// PageOrdering
export type PageOrdering = Selectable<Ordering>;
export type InsertablePageOrdering = Insertable<Ordering>;
export type UpdatablePageOrdering = Updateable<Omit<Ordering, 'id'>>;
// Comment
export type Comment = Selectable<Comments>;
export type InsertableComment = Insertable<Comments>;
export type UpdatableComment = Updateable<Omit<Comments, 'id'>>;
// Attachment
export type Attachment = Selectable<Attachments>;
export type InsertableAttachment = Insertable<Attachments>;
export type UpdatableAttachment = Updateable<Omit<Attachments, 'id'>>;

View File

@ -0,0 +1,5 @@
import { DB } from './db';
import { Kysely, Transaction } from 'kysely';
export type KyselyDB = Kysely<DB>;
export type KyselyTransaction = Transaction<DB>;

View File

@ -0,0 +1,13 @@
import { KyselyDB, KyselyTransaction } from './types/kysely.types';
export async function executeTx<T>(
db: KyselyDB,
callback: (trx: KyselyTransaction) => Promise<T>,
existingTrx?: KyselyTransaction,
): Promise<T> {
if (existingTrx) {
return await callback(existingTrx);
} else {
return await db.transaction().execute((trx) => callback(trx));
}
}