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