mirror of
https://github.com/Shadowfita/docmost.git
synced 2025-11-13 00:02:30 +10:00
server: refactor pagination
* fix transaction usgae in repos * other bug fixes
This commit is contained in:
@ -67,24 +67,6 @@ export class SignupService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async createWorkspace(
|
|
||||||
user: User,
|
|
||||||
workspaceName: string,
|
|
||||||
trx?: KyselyTransaction,
|
|
||||||
) {
|
|
||||||
return await executeTx(
|
|
||||||
this.db,
|
|
||||||
async (trx) => {
|
|
||||||
const workspaceData: CreateWorkspaceDto = {
|
|
||||||
name: workspaceName,
|
|
||||||
};
|
|
||||||
|
|
||||||
return await this.workspaceService.create(user, workspaceData, trx);
|
|
||||||
},
|
|
||||||
trx,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
async initialSetup(
|
async initialSetup(
|
||||||
createAdminUserDto: CreateAdminUserDto,
|
createAdminUserDto: CreateAdminUserDto,
|
||||||
trx?: KyselyTransaction,
|
trx?: KyselyTransaction,
|
||||||
@ -94,9 +76,15 @@ export class SignupService {
|
|||||||
async (trx) => {
|
async (trx) => {
|
||||||
// create user
|
// create user
|
||||||
const user = await this.userRepo.insertUser(createAdminUserDto, trx);
|
const user = await this.userRepo.insertUser(createAdminUserDto, trx);
|
||||||
const workspace = await this.createWorkspace(
|
|
||||||
|
// create workspace with full setup
|
||||||
|
const workspaceData: CreateWorkspaceDto = {
|
||||||
|
name: createAdminUserDto.workspaceName,
|
||||||
|
};
|
||||||
|
|
||||||
|
const workspace = await this.workspaceService.create(
|
||||||
user,
|
user,
|
||||||
createAdminUserDto.workspaceName,
|
workspaceData,
|
||||||
trx,
|
trx,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@ -13,7 +13,7 @@ import { CommentsInput, SingleCommentInput } from './dto/comments.input';
|
|||||||
import { AuthUser } from '../../decorators/auth-user.decorator';
|
import { AuthUser } from '../../decorators/auth-user.decorator';
|
||||||
import { AuthWorkspace } from '../../decorators/auth-workspace.decorator';
|
import { AuthWorkspace } from '../../decorators/auth-workspace.decorator';
|
||||||
import { JwtAuthGuard } from '../../guards/jwt-auth.guard';
|
import { JwtAuthGuard } from '../../guards/jwt-auth.guard';
|
||||||
import { PaginationOptions } from 'src/helpers/pagination/pagination-options';
|
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
|
||||||
import { User, Workspace } from '@docmost/db/types/entity.types';
|
import { User, Workspace } from '@docmost/db/types/entity.types';
|
||||||
|
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
|
|||||||
@ -4,9 +4,8 @@ import { UpdateCommentDto } from './dto/update-comment.dto';
|
|||||||
import { PageService } from '../page/services/page.service';
|
import { PageService } from '../page/services/page.service';
|
||||||
import { CommentRepo } from '@docmost/db/repos/comment/comment.repo';
|
import { CommentRepo } from '@docmost/db/repos/comment/comment.repo';
|
||||||
import { Comment } from '@docmost/db/types/entity.types';
|
import { Comment } from '@docmost/db/types/entity.types';
|
||||||
import { PaginationOptions } from 'src/helpers/pagination/pagination-options';
|
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
|
||||||
import { PaginatedResult } from 'src/helpers/pagination/paginated-result';
|
import { PaginationResult } from '@docmost/db/pagination/pagination';
|
||||||
import { PaginationMetaDto } from 'src/helpers/pagination/pagination-meta-dto';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class CommentService {
|
export class CommentService {
|
||||||
@ -63,15 +62,14 @@ export class CommentService {
|
|||||||
|
|
||||||
async findByPageId(
|
async findByPageId(
|
||||||
pageId: string,
|
pageId: string,
|
||||||
paginationOptions: PaginationOptions,
|
pagination: PaginationOptions,
|
||||||
): Promise<PaginatedResult<Comment>> {
|
): Promise<PaginationResult<Comment>> {
|
||||||
const { comments, count } = await this.commentRepo.findPageComments(
|
const pageComments = await this.commentRepo.findPageComments(
|
||||||
pageId,
|
pageId,
|
||||||
paginationOptions,
|
pagination,
|
||||||
);
|
);
|
||||||
|
|
||||||
const paginationMeta = new PaginationMetaDto({ count, paginationOptions });
|
return pageComments;
|
||||||
return new PaginatedResult(comments, paginationMeta);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(
|
async update(
|
||||||
|
|||||||
@ -12,7 +12,7 @@ import { AuthUser } from '../../decorators/auth-user.decorator';
|
|||||||
import { AuthWorkspace } from '../../decorators/auth-workspace.decorator';
|
import { AuthWorkspace } from '../../decorators/auth-workspace.decorator';
|
||||||
import { GroupUserService } from './services/group-user.service';
|
import { GroupUserService } from './services/group-user.service';
|
||||||
import { GroupIdDto } from './dto/group-id.dto';
|
import { GroupIdDto } from './dto/group-id.dto';
|
||||||
import { PaginationOptions } from '../../helpers/pagination/pagination-options';
|
import { PaginationOptions } from '../../kysely/pagination/pagination-options';
|
||||||
import { AddGroupUserDto } from './dto/add-group-user.dto';
|
import { AddGroupUserDto } from './dto/add-group-user.dto';
|
||||||
import { RemoveGroupUserDto } from './dto/remove-group-user.dto';
|
import { RemoveGroupUserDto } from './dto/remove-group-user.dto';
|
||||||
import { UpdateGroupDto } from './dto/update-group.dto';
|
import { UpdateGroupDto } from './dto/update-group.dto';
|
||||||
|
|||||||
@ -1,14 +1,16 @@
|
|||||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
import {
|
||||||
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
|
BadRequestException,
|
||||||
import { PaginationMetaDto } from '../../../helpers/pagination/pagination-meta-dto';
|
Injectable,
|
||||||
import { PaginatedResult } from '../../../helpers/pagination/paginated-result';
|
NotFoundException,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { PaginationOptions } from '../../../kysely/pagination/pagination-options';
|
||||||
import { GroupService } from './group.service';
|
import { GroupService } from './group.service';
|
||||||
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
|
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
|
||||||
import { executeTx } from '@docmost/db/utils';
|
import { executeTx } from '@docmost/db/utils';
|
||||||
import { InjectKysely } from 'nestjs-kysely';
|
import { InjectKysely } from 'nestjs-kysely';
|
||||||
import { GroupRepo } from '@docmost/db/repos/group/group.repo';
|
import { GroupRepo } from '@docmost/db/repos/group/group.repo';
|
||||||
import { GroupUserRepo } from '@docmost/db/repos/group/group-user.repo';
|
import { GroupUserRepo } from '@docmost/db/repos/group/group-user.repo';
|
||||||
import { User } from '@docmost/db/types/entity.types';
|
import { UserRepo } from '@docmost/db/repos/user/user.repo';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class GroupUserService {
|
export class GroupUserService {
|
||||||
@ -16,24 +18,23 @@ export class GroupUserService {
|
|||||||
private groupRepo: GroupRepo,
|
private groupRepo: GroupRepo,
|
||||||
private groupUserRepo: GroupUserRepo,
|
private groupUserRepo: GroupUserRepo,
|
||||||
private groupService: GroupService,
|
private groupService: GroupService,
|
||||||
|
private userRepo: UserRepo,
|
||||||
@InjectKysely() private readonly db: KyselyDB,
|
@InjectKysely() private readonly db: KyselyDB,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
async getGroupUsers(
|
async getGroupUsers(
|
||||||
groupId: string,
|
groupId: string,
|
||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
paginationOptions: PaginationOptions,
|
pagination: PaginationOptions,
|
||||||
): Promise<PaginatedResult<User>> {
|
) {
|
||||||
await this.groupService.findAndValidateGroup(groupId, workspaceId);
|
await this.groupService.findAndValidateGroup(groupId, workspaceId);
|
||||||
|
|
||||||
const { users, count } = await this.groupUserRepo.getGroupUsersPaginated(
|
const groupUsers = await this.groupUserRepo.getGroupUsersPaginated(
|
||||||
groupId,
|
groupId,
|
||||||
paginationOptions,
|
pagination,
|
||||||
);
|
);
|
||||||
|
|
||||||
const paginationMeta = new PaginationMetaDto({ count, paginationOptions });
|
return groupUsers;
|
||||||
|
|
||||||
return new PaginatedResult(users, paginationMeta);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async addUserToDefaultGroup(
|
async addUserToDefaultGroup(
|
||||||
@ -63,7 +64,18 @@ export class GroupUserService {
|
|||||||
await executeTx(
|
await executeTx(
|
||||||
this.db,
|
this.db,
|
||||||
async (trx) => {
|
async (trx) => {
|
||||||
// await this.groupService.findAndValidateGroup(groupId, workspaceId);
|
await this.groupService.findAndValidateGroup(groupId, workspaceId);
|
||||||
|
const user = await this.userRepo.findById(
|
||||||
|
userId,
|
||||||
|
workspaceId,
|
||||||
|
false,
|
||||||
|
trx,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
throw new NotFoundException('User not found');
|
||||||
|
}
|
||||||
|
|
||||||
const groupUserExists = await this.groupUserRepo.getGroupUserById(
|
const groupUserExists = await this.groupUserRepo.getGroupUserById(
|
||||||
userId,
|
userId,
|
||||||
groupId,
|
groupId,
|
||||||
@ -98,6 +110,12 @@ export class GroupUserService {
|
|||||||
workspaceId,
|
workspaceId,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const user = await this.userRepo.findById(userId, workspaceId);
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
throw new NotFoundException('User not found');
|
||||||
|
}
|
||||||
|
|
||||||
if (group.isDefault) {
|
if (group.isDefault) {
|
||||||
throw new BadRequestException(
|
throw new BadRequestException(
|
||||||
'You cannot remove users from a default group',
|
'You cannot remove users from a default group',
|
||||||
|
|||||||
@ -4,13 +4,12 @@ import {
|
|||||||
NotFoundException,
|
NotFoundException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { CreateGroupDto, DefaultGroup } from '../dto/create-group.dto';
|
import { CreateGroupDto, DefaultGroup } from '../dto/create-group.dto';
|
||||||
import { PaginationMetaDto } from '../../../helpers/pagination/pagination-meta-dto';
|
import { PaginationOptions } from '../../../kysely/pagination/pagination-options';
|
||||||
import { PaginatedResult } from '../../../helpers/pagination/paginated-result';
|
|
||||||
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
|
|
||||||
import { UpdateGroupDto } from '../dto/update-group.dto';
|
import { UpdateGroupDto } from '../dto/update-group.dto';
|
||||||
import { KyselyTransaction } from '@docmost/db/types/kysely.types';
|
import { KyselyTransaction } from '@docmost/db/types/kysely.types';
|
||||||
import { GroupRepo } from '@docmost/db/repos/group/group.repo';
|
import { GroupRepo } from '@docmost/db/repos/group/group.repo';
|
||||||
import { Group, InsertableGroup, User } from '@docmost/db/types/entity.types';
|
import { Group, InsertableGroup, User } from '@docmost/db/types/entity.types';
|
||||||
|
import { PaginationResult } from '@docmost/db/pagination/pagination';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class GroupService {
|
export class GroupService {
|
||||||
@ -71,15 +70,16 @@ export class GroupService {
|
|||||||
throw new BadRequestException('You cannot update a default group');
|
throw new BadRequestException('You cannot update a default group');
|
||||||
}
|
}
|
||||||
|
|
||||||
const groupExists = await this.groupRepo.findByName(
|
|
||||||
updateGroupDto.name,
|
|
||||||
workspaceId,
|
|
||||||
);
|
|
||||||
if (groupExists) {
|
|
||||||
throw new BadRequestException('Group name already exists');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (updateGroupDto.name) {
|
if (updateGroupDto.name) {
|
||||||
|
const existingGroup = await this.groupRepo.findByName(
|
||||||
|
updateGroupDto.name,
|
||||||
|
workspaceId,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (existingGroup && group.name !== existingGroup.name) {
|
||||||
|
throw new BadRequestException('Group name already exists');
|
||||||
|
}
|
||||||
|
|
||||||
group.name = updateGroupDto.name;
|
group.name = updateGroupDto.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,7 +100,6 @@ export class GroupService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getGroupInfo(groupId: string, workspaceId: string): Promise<Group> {
|
async getGroupInfo(groupId: string, workspaceId: string): Promise<Group> {
|
||||||
// todo: add member count
|
|
||||||
const group = await this.groupRepo.findById(groupId, workspaceId);
|
const group = await this.groupRepo.findById(groupId, workspaceId);
|
||||||
|
|
||||||
if (!group) {
|
if (!group) {
|
||||||
@ -113,15 +112,12 @@ export class GroupService {
|
|||||||
async getWorkspaceGroups(
|
async getWorkspaceGroups(
|
||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
paginationOptions: PaginationOptions,
|
paginationOptions: PaginationOptions,
|
||||||
): Promise<PaginatedResult<Group>> {
|
): Promise<PaginationResult<Group>> {
|
||||||
const { groups, count } = await this.groupRepo.getGroupsPaginated(
|
const groups = await this.groupRepo.getGroupsPaginated(
|
||||||
workspaceId,
|
workspaceId,
|
||||||
paginationOptions,
|
paginationOptions,
|
||||||
);
|
);
|
||||||
|
return groups;
|
||||||
const paginationMeta = new PaginationMetaDto({ count, paginationOptions });
|
|
||||||
|
|
||||||
return new PaginatedResult(groups, paginationMeta);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteGroup(groupId: string, workspaceId: string): Promise<void> {
|
async deleteGroup(groupId: string, workspaceId: string): Promise<void> {
|
||||||
|
|||||||
@ -14,9 +14,9 @@ export class CreatePageDto {
|
|||||||
icon?: string;
|
icon?: string;
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsString()
|
@IsUUID()
|
||||||
parentPageId?: string;
|
parentPageId?: string;
|
||||||
|
|
||||||
@IsString()
|
@IsUUID()
|
||||||
spaceId: string;
|
spaceId: string;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +0,0 @@
|
|||||||
import { IsUUID } from 'class-validator';
|
|
||||||
|
|
||||||
export class DeletePageDto {
|
|
||||||
@IsUUID()
|
|
||||||
pageId: string;
|
|
||||||
}
|
|
||||||
@ -1,6 +0,0 @@
|
|||||||
import { IsUUID } from 'class-validator';
|
|
||||||
|
|
||||||
export class HistoryDetailsDto {
|
|
||||||
@IsUUID()
|
|
||||||
historyId: string;
|
|
||||||
}
|
|
||||||
@ -1,6 +0,0 @@
|
|||||||
import { IsUUID } from 'class-validator';
|
|
||||||
|
|
||||||
export class PageDetailsDto {
|
|
||||||
@IsUUID()
|
|
||||||
pageId: string;
|
|
||||||
}
|
|
||||||
@ -1,6 +0,0 @@
|
|||||||
import { IsUUID } from 'class-validator';
|
|
||||||
|
|
||||||
export class PageHistoryDto {
|
|
||||||
@IsUUID()
|
|
||||||
pageId: string;
|
|
||||||
}
|
|
||||||
16
apps/server/src/core/page/dto/page.dto.ts
Normal file
16
apps/server/src/core/page/dto/page.dto.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import { IsUUID } from 'class-validator';
|
||||||
|
|
||||||
|
export class PageIdDto {
|
||||||
|
@IsUUID()
|
||||||
|
pageId: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class SpaceIdDto {
|
||||||
|
@IsUUID()
|
||||||
|
spaceId: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class PageHistoryIdDto {
|
||||||
|
@IsUUID()
|
||||||
|
historyId: string;
|
||||||
|
}
|
||||||
@ -10,16 +10,13 @@ import { PageService } from './services/page.service';
|
|||||||
import { CreatePageDto } from './dto/create-page.dto';
|
import { CreatePageDto } from './dto/create-page.dto';
|
||||||
import { UpdatePageDto } from './dto/update-page.dto';
|
import { UpdatePageDto } from './dto/update-page.dto';
|
||||||
import { MovePageDto } from './dto/move-page.dto';
|
import { MovePageDto } from './dto/move-page.dto';
|
||||||
import { PageDetailsDto } from './dto/page-details.dto';
|
import { PageHistoryIdDto, PageIdDto, SpaceIdDto } from './dto/page.dto';
|
||||||
import { DeletePageDto } from './dto/delete-page.dto';
|
|
||||||
import { PageOrderingService } from './services/page-ordering.service';
|
import { PageOrderingService } from './services/page-ordering.service';
|
||||||
import { PageHistoryService } from './services/page-history.service';
|
import { PageHistoryService } from './services/page-history.service';
|
||||||
import { HistoryDetailsDto } from './dto/history-details.dto';
|
|
||||||
import { PageHistoryDto } from './dto/page-history.dto';
|
|
||||||
import { AuthUser } from '../../decorators/auth-user.decorator';
|
import { AuthUser } from '../../decorators/auth-user.decorator';
|
||||||
import { AuthWorkspace } from '../../decorators/auth-workspace.decorator';
|
import { AuthWorkspace } from '../../decorators/auth-workspace.decorator';
|
||||||
import { JwtAuthGuard } from '../../guards/jwt-auth.guard';
|
import { JwtAuthGuard } from '../../guards/jwt-auth.guard';
|
||||||
import { PaginationOptions } from 'src/helpers/pagination/pagination-options';
|
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
|
||||||
import { User, Workspace } from '@docmost/db/types/entity.types';
|
import { User, Workspace } from '@docmost/db/types/entity.types';
|
||||||
|
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard)
|
||||||
@ -33,8 +30,8 @@ export class PageController {
|
|||||||
|
|
||||||
@HttpCode(HttpStatus.OK)
|
@HttpCode(HttpStatus.OK)
|
||||||
@Post('/info')
|
@Post('/info')
|
||||||
async getPage(@Body() input: PageDetailsDto) {
|
async getPage(@Body() pageIdDto: PageIdDto) {
|
||||||
return this.pageService.findById(input.pageId);
|
return this.pageService.findById(pageIdDto.pageId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@HttpCode(HttpStatus.CREATED)
|
@HttpCode(HttpStatus.CREATED)
|
||||||
@ -59,13 +56,13 @@ export class PageController {
|
|||||||
|
|
||||||
@HttpCode(HttpStatus.OK)
|
@HttpCode(HttpStatus.OK)
|
||||||
@Post('delete')
|
@Post('delete')
|
||||||
async delete(@Body() deletePageDto: DeletePageDto) {
|
async delete(@Body() pageIdDto: PageIdDto) {
|
||||||
await this.pageService.forceDelete(deletePageDto.pageId);
|
await this.pageService.forceDelete(pageIdDto.pageId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@HttpCode(HttpStatus.OK)
|
@HttpCode(HttpStatus.OK)
|
||||||
@Post('restore')
|
@Post('restore')
|
||||||
async restore(@Body() deletePageDto: DeletePageDto) {
|
async restore(@Body() pageIdDto: PageIdDto) {
|
||||||
// await this.pageService.restore(deletePageDto.id);
|
// await this.pageService.restore(deletePageDto.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,10 +75,10 @@ export class PageController {
|
|||||||
@HttpCode(HttpStatus.OK)
|
@HttpCode(HttpStatus.OK)
|
||||||
@Post('recent')
|
@Post('recent')
|
||||||
async getRecentSpacePages(
|
async getRecentSpacePages(
|
||||||
@Body() { spaceId },
|
@Body() spaceIdDto: SpaceIdDto,
|
||||||
@Body() pagination: PaginationOptions,
|
@Body() pagination: PaginationOptions,
|
||||||
) {
|
) {
|
||||||
return this.pageService.getRecentSpacePages(spaceId, pagination);
|
return this.pageService.getRecentSpacePages(spaceIdDto.spaceId, pagination);
|
||||||
}
|
}
|
||||||
|
|
||||||
@HttpCode(HttpStatus.OK)
|
@HttpCode(HttpStatus.OK)
|
||||||
@ -106,7 +103,7 @@ export class PageController {
|
|||||||
@HttpCode(HttpStatus.OK)
|
@HttpCode(HttpStatus.OK)
|
||||||
@Post('/history')
|
@Post('/history')
|
||||||
async getPageHistory(
|
async getPageHistory(
|
||||||
@Body() dto: PageHistoryDto,
|
@Body() dto: PageIdDto,
|
||||||
@Body() pagination: PaginationOptions,
|
@Body() pagination: PaginationOptions,
|
||||||
) {
|
) {
|
||||||
return this.pageHistoryService.findHistoryByPageId(dto.pageId, pagination);
|
return this.pageHistoryService.findHistoryByPageId(dto.pageId, pagination);
|
||||||
@ -114,7 +111,7 @@ export class PageController {
|
|||||||
|
|
||||||
@HttpCode(HttpStatus.OK)
|
@HttpCode(HttpStatus.OK)
|
||||||
@Post('/history/details')
|
@Post('/history/details')
|
||||||
async get(@Body() dto: HistoryDetailsDto) {
|
async get(@Body() dto: PageHistoryIdDto) {
|
||||||
return this.pageHistoryService.findById(dto.historyId);
|
return this.pageHistoryService.findById(dto.historyId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,9 +1,8 @@
|
|||||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||||
import { PageHistoryRepo } from '@docmost/db/repos/page/page-history.repo';
|
import { PageHistoryRepo } from '@docmost/db/repos/page/page-history.repo';
|
||||||
import { Page, PageHistory } from '@docmost/db/types/entity.types';
|
import { Page, PageHistory } from '@docmost/db/types/entity.types';
|
||||||
import { PaginationOptions } from 'src/helpers/pagination/pagination-options';
|
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
|
||||||
import { PaginatedResult } from 'src/helpers/pagination/paginated-result';
|
import { PaginationResult } from '@docmost/db/pagination/pagination';
|
||||||
import { PaginationMetaDto } from 'src/helpers/pagination/pagination-meta-dto';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PageHistoryService {
|
export class PageHistoryService {
|
||||||
@ -35,14 +34,12 @@ export class PageHistoryService {
|
|||||||
async findHistoryByPageId(
|
async findHistoryByPageId(
|
||||||
pageId: string,
|
pageId: string,
|
||||||
paginationOptions: PaginationOptions,
|
paginationOptions: PaginationOptions,
|
||||||
) {
|
): Promise<PaginationResult<any>> {
|
||||||
const { pageHistory, count } =
|
const pageHistory = await this.pageHistoryRepo.findPageHistoryByPageId(
|
||||||
await this.pageHistoryRepo.findPageHistoryByPageId(
|
pageId,
|
||||||
pageId,
|
paginationOptions,
|
||||||
paginationOptions,
|
);
|
||||||
);
|
|
||||||
|
|
||||||
const paginationMeta = new PaginationMetaDto({ count, paginationOptions });
|
return pageHistory;
|
||||||
return new PaginatedResult(pageHistory, paginationMeta);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,9 +11,8 @@ import { PageWithOrderingDto } from '../dto/page-with-ordering.dto';
|
|||||||
import { transformPageResult } from '../page.util';
|
import { transformPageResult } from '../page.util';
|
||||||
import { PageRepo } from '@docmost/db/repos/page/page.repo';
|
import { PageRepo } from '@docmost/db/repos/page/page.repo';
|
||||||
import { Page } from '@docmost/db/types/entity.types';
|
import { Page } from '@docmost/db/types/entity.types';
|
||||||
import { PaginationOptions } from 'src/helpers/pagination/pagination-options';
|
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
|
||||||
import { PaginationMetaDto } from 'src/helpers/pagination/pagination-meta-dto';
|
import { PaginationResult } from '@docmost/db/pagination/pagination';
|
||||||
import { PaginatedResult } from 'src/helpers/pagination/paginated-result';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PageService {
|
export class PageService {
|
||||||
@ -96,6 +95,30 @@ export class PageService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getSidebarPagesBySpaceId(
|
||||||
|
spaceId: string,
|
||||||
|
limit = 200,
|
||||||
|
): Promise<PageWithOrderingDto[]> {
|
||||||
|
const pages = await this.pageRepo.getSpaceSidebarPages(spaceId, limit);
|
||||||
|
return transformPageResult(pages);
|
||||||
|
}
|
||||||
|
|
||||||
|
async getRecentSpacePages(
|
||||||
|
spaceId: string,
|
||||||
|
pagination: PaginationOptions,
|
||||||
|
): Promise<PaginationResult<Page>> {
|
||||||
|
const pages = await this.pageRepo.getRecentPagesInSpace(
|
||||||
|
spaceId,
|
||||||
|
pagination,
|
||||||
|
);
|
||||||
|
|
||||||
|
return pages;
|
||||||
|
}
|
||||||
|
|
||||||
|
async forceDelete(pageId: string): Promise<void> {
|
||||||
|
await this.pageRepo.deletePage(pageId);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
// TODO: page deletion and restoration
|
// TODO: page deletion and restoration
|
||||||
async delete(pageId: string): Promise<void> {
|
async delete(pageId: string): Promise<void> {
|
||||||
@ -186,29 +209,4 @@ export class PageService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
async forceDelete(pageId: string): Promise<void> {
|
|
||||||
await this.pageRepo.deletePage(pageId);
|
|
||||||
}
|
|
||||||
|
|
||||||
async getSidebarPagesBySpaceId(
|
|
||||||
spaceId: string,
|
|
||||||
limit = 200,
|
|
||||||
): Promise<PageWithOrderingDto[]> {
|
|
||||||
const pages = await this.pageRepo.getSpaceSidebarPages(spaceId, limit);
|
|
||||||
return transformPageResult(pages);
|
|
||||||
}
|
|
||||||
|
|
||||||
async getRecentSpacePages(
|
|
||||||
spaceId: string,
|
|
||||||
paginationOptions: PaginationOptions,
|
|
||||||
): Promise<PaginatedResult<Page>> {
|
|
||||||
const { pages, count } = await this.pageRepo.getRecentPagesInSpace(
|
|
||||||
spaceId,
|
|
||||||
paginationOptions,
|
|
||||||
);
|
|
||||||
|
|
||||||
const paginationMeta = new PaginationMetaDto({ count, paginationOptions });
|
|
||||||
|
|
||||||
return new PaginatedResult(pages, paginationMeta);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,5 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
|
import { PaginationOptions } from '../../../kysely/pagination/pagination-options';
|
||||||
import { PaginationMetaDto } from '../../../helpers/pagination/pagination-meta-dto';
|
|
||||||
import { PaginatedResult } from '../../../helpers/pagination/paginated-result';
|
|
||||||
import { KyselyTransaction } from '@docmost/db/types/kysely.types';
|
import { KyselyTransaction } from '@docmost/db/types/kysely.types';
|
||||||
import { SpaceMemberRepo } from '@docmost/db/repos/space/space-member.repo';
|
import { SpaceMemberRepo } from '@docmost/db/repos/space/space-member.repo';
|
||||||
import { SpaceMember } from '@docmost/db/types/entity.types';
|
import { SpaceMember } from '@docmost/db/types/entity.types';
|
||||||
@ -53,10 +51,29 @@ export class SpaceMemberService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* get spaces a user is a member of
|
* get members of a space.
|
||||||
* either by direct membership or via groups
|
* can be a group or user
|
||||||
*/
|
*/
|
||||||
/*
|
async getSpaceMembers(
|
||||||
|
spaceId: string,
|
||||||
|
workspaceId: string,
|
||||||
|
pagination: PaginationOptions,
|
||||||
|
) {
|
||||||
|
//todo: validate the space is inside the workspace
|
||||||
|
const members = await this.spaceMemberRepo.getSpaceMembersPaginated(
|
||||||
|
spaceId,
|
||||||
|
pagination,
|
||||||
|
);
|
||||||
|
|
||||||
|
return members;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* get spaces a user is a member of
|
||||||
|
* either by direct membership or via groups
|
||||||
|
*/
|
||||||
|
/*
|
||||||
async getUserSpaces(
|
async getUserSpaces(
|
||||||
userId: string,
|
userId: string,
|
||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
@ -82,25 +99,3 @@ export class SpaceMemberService {
|
|||||||
return new PaginatedResult(spaces, paginationMeta);
|
return new PaginatedResult(spaces, paginationMeta);
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
|
||||||
* get members of a space.
|
|
||||||
* can be a group or user
|
|
||||||
*/
|
|
||||||
async getSpaceMembers(
|
|
||||||
spaceId: string,
|
|
||||||
workspaceId: string,
|
|
||||||
paginationOptions: PaginationOptions,
|
|
||||||
) {
|
|
||||||
//todo: validate the space is inside the workspace
|
|
||||||
const { members, count } =
|
|
||||||
await this.spaceMemberRepo.getSpaceMembersPaginated(
|
|
||||||
spaceId,
|
|
||||||
paginationOptions,
|
|
||||||
);
|
|
||||||
|
|
||||||
const paginationMeta = new PaginationMetaDto({ count, paginationOptions });
|
|
||||||
return new PaginatedResult(members, paginationMeta);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 231 lines
|
|
||||||
|
|||||||
@ -4,13 +4,12 @@ import {
|
|||||||
NotFoundException,
|
NotFoundException,
|
||||||
} from '@nestjs/common';
|
} from '@nestjs/common';
|
||||||
import { CreateSpaceDto } from '../dto/create-space.dto';
|
import { CreateSpaceDto } from '../dto/create-space.dto';
|
||||||
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
|
import { PaginationOptions } from '../../../kysely/pagination/pagination-options';
|
||||||
import { PaginationMetaDto } from '../../../helpers/pagination/pagination-meta-dto';
|
|
||||||
import { PaginatedResult } from '../../../helpers/pagination/paginated-result';
|
|
||||||
import slugify from 'slugify';
|
import slugify from 'slugify';
|
||||||
import { SpaceRepo } from '@docmost/db/repos/space/space.repo';
|
import { SpaceRepo } from '@docmost/db/repos/space/space.repo';
|
||||||
import { KyselyTransaction } from '@docmost/db/types/kysely.types';
|
import { KyselyTransaction } from '@docmost/db/types/kysely.types';
|
||||||
import { Space } from '@docmost/db/types/entity.types';
|
import { Space } from '@docmost/db/types/entity.types';
|
||||||
|
import { PaginationResult } from '@docmost/db/pagination/pagination';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SpaceService {
|
export class SpaceService {
|
||||||
@ -46,7 +45,6 @@ export class SpaceService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getSpaceInfo(spaceId: string, workspaceId: string): Promise<Space> {
|
async getSpaceInfo(spaceId: string, workspaceId: string): Promise<Space> {
|
||||||
// TODO: add memberCount
|
|
||||||
const space = await this.spaceRepo.findById(spaceId, workspaceId);
|
const space = await this.spaceRepo.findById(spaceId, workspaceId);
|
||||||
if (!space) {
|
if (!space) {
|
||||||
throw new NotFoundException('Space not found');
|
throw new NotFoundException('Space not found');
|
||||||
@ -57,15 +55,13 @@ export class SpaceService {
|
|||||||
|
|
||||||
async getWorkspaceSpaces(
|
async getWorkspaceSpaces(
|
||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
paginationOptions: PaginationOptions,
|
pagination: PaginationOptions,
|
||||||
): Promise<PaginatedResult<Space>> {
|
): Promise<PaginationResult<Space>> {
|
||||||
const { spaces, count } = await this.spaceRepo.getSpacesInWorkspace(
|
const spaces = await this.spaceRepo.getSpacesInWorkspace(
|
||||||
workspaceId,
|
workspaceId,
|
||||||
paginationOptions,
|
pagination,
|
||||||
);
|
);
|
||||||
|
|
||||||
const paginationMeta = new PaginationMetaDto({ count, paginationOptions });
|
return spaces;
|
||||||
|
|
||||||
return new PaginatedResult(spaces, paginationMeta);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,7 +11,7 @@ import { AuthUser } from '../../decorators/auth-user.decorator';
|
|||||||
import { AuthWorkspace } from '../../decorators/auth-workspace.decorator';
|
import { AuthWorkspace } from '../../decorators/auth-workspace.decorator';
|
||||||
import { JwtAuthGuard } from '../../guards/jwt-auth.guard';
|
import { JwtAuthGuard } from '../../guards/jwt-auth.guard';
|
||||||
import { SpaceIdDto } from './dto/space-id.dto';
|
import { SpaceIdDto } from './dto/space-id.dto';
|
||||||
import { PaginationOptions } from '../../helpers/pagination/pagination-options';
|
import { PaginationOptions } from '../../kysely/pagination/pagination-options';
|
||||||
import { SpaceMemberService } from './services/space-member.service';
|
import { SpaceMemberService } from './services/space-member.service';
|
||||||
import { User, Workspace } from '@docmost/db/types/entity.types';
|
import { User, Workspace } from '@docmost/db/types/entity.types';
|
||||||
|
|
||||||
|
|||||||
@ -12,7 +12,7 @@ import { DeleteWorkspaceDto } from '../dto/delete-workspace.dto';
|
|||||||
import { UpdateWorkspaceUserRoleDto } from '../dto/update-workspace-user-role.dto';
|
import { UpdateWorkspaceUserRoleDto } from '../dto/update-workspace-user-role.dto';
|
||||||
import { AuthUser } from '../../../decorators/auth-user.decorator';
|
import { AuthUser } from '../../../decorators/auth-user.decorator';
|
||||||
import { AuthWorkspace } from '../../../decorators/auth-workspace.decorator';
|
import { AuthWorkspace } from '../../../decorators/auth-workspace.decorator';
|
||||||
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
|
import { PaginationOptions } from '../../../kysely/pagination/pagination-options';
|
||||||
import { WorkspaceInvitationService } from '../services/workspace-invitation.service';
|
import { WorkspaceInvitationService } from '../services/workspace-invitation.service';
|
||||||
import { Public } from '../../../decorators/public.decorator';
|
import { Public } from '../../../decorators/public.decorator';
|
||||||
import {
|
import {
|
||||||
|
|||||||
@ -1,11 +1,10 @@
|
|||||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||||
import { UpdateWorkspaceUserRoleDto } from '../dto/update-workspace-user-role.dto';
|
import { UpdateWorkspaceUserRoleDto } from '../dto/update-workspace-user-role.dto';
|
||||||
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
|
import { PaginationOptions } from '../../../kysely/pagination/pagination-options';
|
||||||
import { PaginationMetaDto } from '../../../helpers/pagination/pagination-meta-dto';
|
|
||||||
import { PaginatedResult } from '../../../helpers/pagination/paginated-result';
|
|
||||||
import { UserRole } from '../../../helpers/types/permission';
|
import { UserRole } from '../../../helpers/types/permission';
|
||||||
import { UserRepo } from '@docmost/db/repos/user/user.repo';
|
import { UserRepo } from '@docmost/db/repos/user/user.repo';
|
||||||
import { User } from '@docmost/db/types/entity.types';
|
import { User } from '@docmost/db/types/entity.types';
|
||||||
|
import { PaginationResult } from '@docmost/db/pagination/pagination';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class WorkspaceUserService {
|
export class WorkspaceUserService {
|
||||||
@ -13,15 +12,14 @@ export class WorkspaceUserService {
|
|||||||
|
|
||||||
async getWorkspaceUsers(
|
async getWorkspaceUsers(
|
||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
paginationOptions: PaginationOptions,
|
pagination: PaginationOptions,
|
||||||
): Promise<PaginatedResult<any>> {
|
): Promise<PaginationResult<User>> {
|
||||||
const { users, count } = await this.userRepo.getUsersPaginated(
|
const users = await this.userRepo.getUsersPaginated(
|
||||||
workspaceId,
|
workspaceId,
|
||||||
paginationOptions,
|
pagination,
|
||||||
);
|
);
|
||||||
|
|
||||||
const paginationMeta = new PaginationMetaDto({ count, paginationOptions });
|
return users;
|
||||||
return new PaginatedResult(users, paginationMeta);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateWorkspaceUserRole(
|
async updateWorkspaceUserRole(
|
||||||
|
|||||||
@ -1,14 +0,0 @@
|
|||||||
import { IsArray } from 'class-validator';
|
|
||||||
import { PaginationMetaDto } from './pagination-meta-dto';
|
|
||||||
|
|
||||||
export class PaginatedResult<T> {
|
|
||||||
@IsArray()
|
|
||||||
readonly items: T[];
|
|
||||||
|
|
||||||
readonly pagination: PaginationMetaDto;
|
|
||||||
|
|
||||||
constructor(items: T[], pagination: PaginationMetaDto) {
|
|
||||||
this.items = items;
|
|
||||||
this.pagination = pagination;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,29 +0,0 @@
|
|||||||
import { PaginationOptions } from './pagination-options';
|
|
||||||
|
|
||||||
export class PaginationMetaDto {
|
|
||||||
readonly page: number;
|
|
||||||
|
|
||||||
readonly limit: number;
|
|
||||||
|
|
||||||
readonly total: number;
|
|
||||||
|
|
||||||
readonly pageCount: number;
|
|
||||||
|
|
||||||
readonly hasPreviousPage: boolean;
|
|
||||||
|
|
||||||
readonly hasNextPage: boolean;
|
|
||||||
|
|
||||||
constructor({ count, paginationOptions }: PageMetaDtoParameters) {
|
|
||||||
this.page = paginationOptions.page;
|
|
||||||
this.limit = paginationOptions.limit;
|
|
||||||
this.total = count;
|
|
||||||
this.pageCount = Math.ceil(this.total / this.limit);
|
|
||||||
this.hasPreviousPage = this.page > 1;
|
|
||||||
this.hasNextPage = this.page < this.pageCount;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface PageMetaDtoParameters {
|
|
||||||
count: number;
|
|
||||||
paginationOptions: PaginationOptions;
|
|
||||||
}
|
|
||||||
@ -2,7 +2,7 @@ import { Global, Module } from '@nestjs/common';
|
|||||||
import { KyselyModule } from 'nestjs-kysely';
|
import { KyselyModule } from 'nestjs-kysely';
|
||||||
import { EnvironmentService } from '../integrations/environment/environment.service';
|
import { EnvironmentService } from '../integrations/environment/environment.service';
|
||||||
import { CamelCasePlugin, LogEvent, PostgresDialect } from 'kysely';
|
import { CamelCasePlugin, LogEvent, PostgresDialect } from 'kysely';
|
||||||
import { Pool } from 'pg';
|
import { Pool, types } from 'pg';
|
||||||
import { GroupRepo } from '@docmost/db/repos/group/group.repo';
|
import { GroupRepo } from '@docmost/db/repos/group/group.repo';
|
||||||
import { WorkspaceRepo } from '@docmost/db/repos/workspace/workspace.repo';
|
import { WorkspaceRepo } from '@docmost/db/repos/workspace/workspace.repo';
|
||||||
import { UserRepo } from '@docmost/db/repos/user/user.repo';
|
import { UserRepo } from '@docmost/db/repos/user/user.repo';
|
||||||
@ -15,6 +15,9 @@ import { PageHistoryRepo } from './repos/page/page-history.repo';
|
|||||||
import { PageOrderingRepo } from './repos/page/page-ordering.repo';
|
import { PageOrderingRepo } from './repos/page/page-ordering.repo';
|
||||||
import { AttachmentRepo } from './repos/attachment/attachment.repo';
|
import { AttachmentRepo } from './repos/attachment/attachment.repo';
|
||||||
|
|
||||||
|
// https://github.com/brianc/node-postgres/issues/811
|
||||||
|
types.setTypeParser(types.builtins.INT8, (val) => Number(val));
|
||||||
|
|
||||||
@Global()
|
@Global()
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
|
|||||||
@ -1,4 +1,11 @@
|
|||||||
import { IsNumber, IsOptional, IsPositive, Max, Min } from 'class-validator';
|
import {
|
||||||
|
IsNumber,
|
||||||
|
IsOptional,
|
||||||
|
IsPositive,
|
||||||
|
IsString,
|
||||||
|
Max,
|
||||||
|
Min,
|
||||||
|
} from 'class-validator';
|
||||||
|
|
||||||
export class PaginationOptions {
|
export class PaginationOptions {
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@ -14,15 +21,15 @@ export class PaginationOptions {
|
|||||||
limit = 20;
|
limit = 20;
|
||||||
|
|
||||||
@IsOptional()
|
@IsOptional()
|
||||||
@IsNumber()
|
@IsString()
|
||||||
offset = 0;
|
query: string;
|
||||||
|
|
||||||
get skip(): number {
|
get offset(): number {
|
||||||
return (this.page - 1) * this.limit;
|
return (this.page - 1) * this.limit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum Order {
|
export enum PaginationSort {
|
||||||
ASC = 'ASC',
|
ASC = 'asc',
|
||||||
DESC = 'DESC',
|
DESC = 'desc',
|
||||||
}
|
}
|
||||||
66
apps/server/src/kysely/pagination/pagination.ts
Normal file
66
apps/server/src/kysely/pagination/pagination.ts
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
// adapted from https://github.com/charlie-hadden/kysely-paginate/blob/main/src/offset.ts - MIT
|
||||||
|
import { SelectQueryBuilder, StringReference, sql } from 'kysely';
|
||||||
|
|
||||||
|
export type PaginationMeta = {
|
||||||
|
limit: number;
|
||||||
|
page: number;
|
||||||
|
hasNextPage: boolean;
|
||||||
|
hasPrevPage: boolean;
|
||||||
|
};
|
||||||
|
export type PaginationResult<T> = {
|
||||||
|
items: T[];
|
||||||
|
meta: PaginationMeta;
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function executeWithPagination<O, DB, TB extends keyof DB>(
|
||||||
|
qb: SelectQueryBuilder<DB, TB, O>,
|
||||||
|
opts: {
|
||||||
|
perPage: number;
|
||||||
|
page: number;
|
||||||
|
experimental_deferredJoinPrimaryKey?: StringReference<DB, TB>;
|
||||||
|
},
|
||||||
|
): Promise<PaginationResult<O>> {
|
||||||
|
qb = qb.limit(opts.perPage + 1).offset((opts.page - 1) * opts.perPage);
|
||||||
|
|
||||||
|
const deferredJoinPrimaryKey = opts.experimental_deferredJoinPrimaryKey;
|
||||||
|
|
||||||
|
if (deferredJoinPrimaryKey) {
|
||||||
|
const primaryKeys = await qb
|
||||||
|
.clearSelect()
|
||||||
|
.select((eb) => eb.ref(deferredJoinPrimaryKey).as('primaryKey'))
|
||||||
|
.execute()
|
||||||
|
// @ts-expect-error TODO: Fix the type here later
|
||||||
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
||||||
|
.then((rows) => rows.map((row) => row.primaryKey));
|
||||||
|
|
||||||
|
qb = qb
|
||||||
|
.where((eb) =>
|
||||||
|
primaryKeys.length > 0
|
||||||
|
? // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
|
||||||
|
eb(deferredJoinPrimaryKey, 'in', primaryKeys as any)
|
||||||
|
: eb(sql`1`, '=', 0),
|
||||||
|
)
|
||||||
|
.clearOffset()
|
||||||
|
.clearLimit();
|
||||||
|
}
|
||||||
|
|
||||||
|
const rows = await qb.execute();
|
||||||
|
const hasNextPage = rows.length > 0 ? rows.length > opts.perPage : false;
|
||||||
|
const hasPrevPage = rows.length > 0 ? opts.page > 1 : false;
|
||||||
|
|
||||||
|
// If we fetched an extra row to determine if we have a next page, that
|
||||||
|
// shouldn't be in the returned results
|
||||||
|
if (rows.length > opts.perPage) {
|
||||||
|
rows.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
items: rows,
|
||||||
|
meta: {
|
||||||
|
limit: opts.perPage,
|
||||||
|
page: opts.page,
|
||||||
|
hasNextPage,
|
||||||
|
hasPrevPage,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
@ -1,7 +1,7 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { InjectKysely } from 'nestjs-kysely';
|
import { InjectKysely } from 'nestjs-kysely';
|
||||||
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
|
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
|
||||||
import { executeTx } from '@docmost/db/utils';
|
import { dbOrTx } from '@docmost/db/utils';
|
||||||
import {
|
import {
|
||||||
Attachment,
|
Attachment,
|
||||||
InsertableAttachment,
|
InsertableAttachment,
|
||||||
@ -28,17 +28,13 @@ export class AttachmentRepo {
|
|||||||
insertableAttachment: InsertableAttachment,
|
insertableAttachment: InsertableAttachment,
|
||||||
trx?: KyselyTransaction,
|
trx?: KyselyTransaction,
|
||||||
): Promise<Attachment> {
|
): Promise<Attachment> {
|
||||||
return await executeTx(
|
const db = dbOrTx(this.db, trx);
|
||||||
this.db,
|
|
||||||
async (trx) => {
|
return db
|
||||||
return await trx
|
.insertInto('attachments')
|
||||||
.insertInto('attachments')
|
.values(insertableAttachment)
|
||||||
.values(insertableAttachment)
|
.returningAll()
|
||||||
.returningAll()
|
.executeTakeFirst();
|
||||||
.executeTakeFirst();
|
|
||||||
},
|
|
||||||
trx,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateAttachment(
|
async updateAttachment(
|
||||||
|
|||||||
@ -1,13 +1,14 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { InjectKysely } from 'nestjs-kysely';
|
import { InjectKysely } from 'nestjs-kysely';
|
||||||
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
|
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
|
||||||
import { executeTx } from '../../utils';
|
import { dbOrTx } from '../../utils';
|
||||||
import {
|
import {
|
||||||
Comment,
|
Comment,
|
||||||
InsertableComment,
|
InsertableComment,
|
||||||
UpdatableComment,
|
UpdatableComment,
|
||||||
} from '@docmost/db/types/entity.types';
|
} from '@docmost/db/types/entity.types';
|
||||||
import { PaginationOptions } from 'src/helpers/pagination/pagination-options';
|
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
|
||||||
|
import { executeWithPagination } from '@docmost/db/pagination/pagination';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class CommentRepo {
|
export class CommentRepo {
|
||||||
@ -22,26 +23,19 @@ export class CommentRepo {
|
|||||||
.executeTakeFirst();
|
.executeTakeFirst();
|
||||||
}
|
}
|
||||||
|
|
||||||
async findPageComments(pageId: string, paginationOptions: PaginationOptions) {
|
async findPageComments(pageId: string, pagination: PaginationOptions) {
|
||||||
return executeTx(this.db, async (trx) => {
|
const query = this.db
|
||||||
const comments = await trx
|
.selectFrom('comments')
|
||||||
.selectFrom('comments')
|
.selectAll()
|
||||||
.selectAll()
|
.where('pageId', '=', pageId)
|
||||||
.where('pageId', '=', pageId)
|
.orderBy('createdAt', 'asc');
|
||||||
.orderBy('createdAt', 'asc')
|
|
||||||
.limit(paginationOptions.limit)
|
|
||||||
.offset(paginationOptions.offset)
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
let { count } = await trx
|
const result = executeWithPagination(query, {
|
||||||
.selectFrom('comments')
|
page: pagination.page,
|
||||||
.select((eb) => eb.fn.count('id').as('count'))
|
perPage: pagination.limit,
|
||||||
.where('pageId', '=', pageId)
|
|
||||||
.executeTakeFirst();
|
|
||||||
|
|
||||||
count = count as number;
|
|
||||||
return { comments, count };
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateComment(
|
async updateComment(
|
||||||
@ -49,34 +43,25 @@ export class CommentRepo {
|
|||||||
commentId: string,
|
commentId: string,
|
||||||
trx?: KyselyTransaction,
|
trx?: KyselyTransaction,
|
||||||
) {
|
) {
|
||||||
return await executeTx(
|
const db = dbOrTx(this.db, trx);
|
||||||
this.db,
|
|
||||||
async (trx) => {
|
db.updateTable('comments')
|
||||||
return await trx
|
.set(updatableComment)
|
||||||
.updateTable('comments')
|
.where('id', '=', commentId)
|
||||||
.set(updatableComment)
|
.execute();
|
||||||
.where('id', '=', commentId)
|
|
||||||
.execute();
|
|
||||||
},
|
|
||||||
trx,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async insertComment(
|
async insertComment(
|
||||||
insertableComment: InsertableComment,
|
insertableComment: InsertableComment,
|
||||||
trx?: KyselyTransaction,
|
trx?: KyselyTransaction,
|
||||||
): Promise<Comment> {
|
): Promise<Comment> {
|
||||||
return await executeTx(
|
const db = dbOrTx(this.db, trx);
|
||||||
this.db,
|
|
||||||
async (trx) => {
|
return db
|
||||||
return await trx
|
.insertInto('comments')
|
||||||
.insertInto('comments')
|
.values(insertableComment)
|
||||||
.values(insertableComment)
|
.returningAll()
|
||||||
.returningAll()
|
.executeTakeFirst();
|
||||||
.executeTakeFirst();
|
|
||||||
},
|
|
||||||
trx,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteComment(commentId: string): Promise<void> {
|
async deleteComment(commentId: string): Promise<void> {
|
||||||
|
|||||||
@ -1,14 +1,15 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { InjectKysely } from 'nestjs-kysely';
|
import { InjectKysely } from 'nestjs-kysely';
|
||||||
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
|
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
|
||||||
import { executeTx } from '@docmost/db/utils';
|
import { dbOrTx } from '@docmost/db/utils';
|
||||||
import {
|
import {
|
||||||
GroupUser,
|
GroupUser,
|
||||||
InsertableGroupUser,
|
InsertableGroupUser,
|
||||||
User,
|
User,
|
||||||
} from '@docmost/db/types/entity.types';
|
} from '@docmost/db/types/entity.types';
|
||||||
import { sql } from 'kysely';
|
import { sql } from 'kysely';
|
||||||
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
|
import { PaginationOptions } from '../../pagination/pagination-options';
|
||||||
|
import { executeWithPagination } from '@docmost/db/pagination/pagination';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class GroupUserRepo {
|
export class GroupUserRepo {
|
||||||
@ -19,67 +20,47 @@ export class GroupUserRepo {
|
|||||||
groupId: string,
|
groupId: string,
|
||||||
trx?: KyselyTransaction,
|
trx?: KyselyTransaction,
|
||||||
) {
|
) {
|
||||||
return await executeTx(
|
const db = dbOrTx(this.db, trx);
|
||||||
this.db,
|
return db
|
||||||
async (trx) => {
|
.selectFrom('groupUsers')
|
||||||
return await trx
|
.selectAll()
|
||||||
.selectFrom('groupUsers')
|
.where('userId', '=', userId)
|
||||||
.selectAll()
|
.where('groupId', '=', groupId)
|
||||||
.where('userId', '=', userId)
|
.executeTakeFirst();
|
||||||
.where('groupId', '=', groupId)
|
|
||||||
.executeTakeFirst();
|
|
||||||
},
|
|
||||||
trx,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async insertGroupUser(
|
async insertGroupUser(
|
||||||
insertableGroupUser: InsertableGroupUser,
|
insertableGroupUser: InsertableGroupUser,
|
||||||
trx?: KyselyTransaction,
|
trx?: KyselyTransaction,
|
||||||
): Promise<GroupUser> {
|
): Promise<GroupUser> {
|
||||||
return await executeTx(
|
const db = dbOrTx(this.db, trx);
|
||||||
this.db,
|
return db
|
||||||
async (trx) => {
|
.insertInto('groupUsers')
|
||||||
return await trx
|
.values(insertableGroupUser)
|
||||||
.insertInto('groupUsers')
|
.returningAll()
|
||||||
.values(insertableGroupUser)
|
.executeTakeFirst();
|
||||||
.returningAll()
|
|
||||||
.executeTakeFirst();
|
|
||||||
},
|
|
||||||
trx,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getGroupUsersPaginated(
|
async getGroupUsersPaginated(groupId: string, pagination: PaginationOptions) {
|
||||||
groupId: string,
|
let query = this.db
|
||||||
paginationOptions: PaginationOptions,
|
.selectFrom('groupUsers')
|
||||||
): Promise<{ users: User[]; count: number }> {
|
.innerJoin('users', 'users.id', 'groupUsers.userId')
|
||||||
// todo add group member count
|
.select(sql<User>`users.*` as any)
|
||||||
return executeTx(this.db, async (trx) => {
|
.where('groupId', '=', groupId)
|
||||||
const groupUsers = (await trx
|
.orderBy('createdAt', 'asc');
|
||||||
.selectFrom('groupUsers')
|
|
||||||
.innerJoin('users', 'users.id', 'groupUsers.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) => {
|
if (pagination.query) {
|
||||||
delete user.password;
|
query = query.where((eb) =>
|
||||||
return user;
|
eb('users.name', 'ilike', `%${pagination.query}%`),
|
||||||
});
|
);
|
||||||
|
}
|
||||||
|
|
||||||
let { count } = await trx
|
const result = executeWithPagination(query, {
|
||||||
.selectFrom('groupUsers')
|
page: pagination.page,
|
||||||
.select((eb) => eb.fn.count('id').as('count'))
|
perPage: pagination.limit,
|
||||||
.where('groupId', '=', groupId)
|
|
||||||
.executeTakeFirst();
|
|
||||||
|
|
||||||
count = count as number;
|
|
||||||
|
|
||||||
return { users, count };
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(userId: string, groupId: string): Promise<void> {
|
async delete(userId: string, groupId: string): Promise<void> {
|
||||||
|
|||||||
@ -1,23 +1,44 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { InjectKysely } from 'nestjs-kysely';
|
import { InjectKysely } from 'nestjs-kysely';
|
||||||
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
|
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
|
||||||
import { executeTx } from '@docmost/db/utils';
|
import { dbOrTx } from '@docmost/db/utils';
|
||||||
import {
|
import {
|
||||||
Group,
|
Group,
|
||||||
InsertableGroup,
|
InsertableGroup,
|
||||||
UpdatableGroup,
|
UpdatableGroup,
|
||||||
} from '@docmost/db/types/entity.types';
|
} from '@docmost/db/types/entity.types';
|
||||||
import { sql } from 'kysely';
|
import { ExpressionBuilder, sql } from 'kysely';
|
||||||
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
|
import { PaginationOptions } from '../../pagination/pagination-options';
|
||||||
|
import { DB } from '@docmost/db/types/db';
|
||||||
|
import { executeWithPagination } from '@docmost/db/pagination/pagination';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class GroupRepo {
|
export class GroupRepo {
|
||||||
constructor(@InjectKysely() private readonly db: KyselyDB) {}
|
constructor(@InjectKysely() private readonly db: KyselyDB) {}
|
||||||
|
|
||||||
|
private baseFields: Array<keyof Group> = [
|
||||||
|
'id',
|
||||||
|
'name',
|
||||||
|
'description',
|
||||||
|
'isDefault',
|
||||||
|
'workspaceId',
|
||||||
|
'creatorId',
|
||||||
|
'createdAt',
|
||||||
|
'updatedAt',
|
||||||
|
];
|
||||||
|
|
||||||
|
countGroupMembers(eb: ExpressionBuilder<DB, 'groups'>) {
|
||||||
|
return eb
|
||||||
|
.selectFrom('groupUsers')
|
||||||
|
.select((eb) => eb.fn.countAll().as('count'))
|
||||||
|
.whereRef('groupUsers.groupId', '=', 'groups.id')
|
||||||
|
.as('memberCount');
|
||||||
|
}
|
||||||
|
|
||||||
async findById(groupId: string, workspaceId: string): Promise<Group> {
|
async findById(groupId: string, workspaceId: string): Promise<Group> {
|
||||||
return await this.db
|
return await this.db
|
||||||
.selectFrom('groups')
|
.selectFrom('groups')
|
||||||
.selectAll()
|
.select((eb) => [...this.baseFields, this.countGroupMembers(eb)])
|
||||||
.where('id', '=', groupId)
|
.where('id', '=', groupId)
|
||||||
.where('workspaceId', '=', workspaceId)
|
.where('workspaceId', '=', workspaceId)
|
||||||
.executeTakeFirst();
|
.executeTakeFirst();
|
||||||
@ -26,7 +47,7 @@ export class GroupRepo {
|
|||||||
async findByName(groupName: string, workspaceId: string): Promise<Group> {
|
async findByName(groupName: string, workspaceId: string): Promise<Group> {
|
||||||
return await this.db
|
return await this.db
|
||||||
.selectFrom('groups')
|
.selectFrom('groups')
|
||||||
.selectAll()
|
.select((eb) => [...this.baseFields, this.countGroupMembers(eb)])
|
||||||
.where(sql`LOWER(name)`, '=', sql`LOWER(${groupName})`)
|
.where(sql`LOWER(name)`, '=', sql`LOWER(${groupName})`)
|
||||||
.where('workspaceId', '=', workspaceId)
|
.where('workspaceId', '=', workspaceId)
|
||||||
.executeTakeFirst();
|
.executeTakeFirst();
|
||||||
@ -49,60 +70,50 @@ export class GroupRepo {
|
|||||||
insertableGroup: InsertableGroup,
|
insertableGroup: InsertableGroup,
|
||||||
trx?: KyselyTransaction,
|
trx?: KyselyTransaction,
|
||||||
): Promise<Group> {
|
): Promise<Group> {
|
||||||
return await executeTx(
|
const db = dbOrTx(this.db, trx);
|
||||||
this.db,
|
return db
|
||||||
async (trx) => {
|
.insertInto('groups')
|
||||||
return await trx
|
.values(insertableGroup)
|
||||||
.insertInto('groups')
|
.returningAll()
|
||||||
.values(insertableGroup)
|
.executeTakeFirst();
|
||||||
.returningAll()
|
|
||||||
.executeTakeFirst();
|
|
||||||
},
|
|
||||||
trx,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getDefaultGroup(
|
async getDefaultGroup(
|
||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
trx: KyselyTransaction,
|
trx: KyselyTransaction,
|
||||||
): Promise<Group> {
|
): Promise<Group> {
|
||||||
return executeTx(
|
const db = dbOrTx(this.db, trx);
|
||||||
this.db,
|
return db
|
||||||
async (trx) => {
|
.selectFrom('groups')
|
||||||
return await trx
|
.select((eb) => [...this.baseFields, this.countGroupMembers(eb)])
|
||||||
.selectFrom('groups')
|
.where('isDefault', '=', true)
|
||||||
.selectAll()
|
.where('workspaceId', '=', workspaceId)
|
||||||
.where('isDefault', '=', true)
|
.executeTakeFirst();
|
||||||
.where('workspaceId', '=', workspaceId)
|
|
||||||
.executeTakeFirst();
|
|
||||||
},
|
|
||||||
trx,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getGroupsPaginated(
|
async getGroupsPaginated(workspaceId: string, pagination: PaginationOptions) {
|
||||||
workspaceId: string,
|
let query = this.db
|
||||||
paginationOptions: PaginationOptions,
|
.selectFrom('groups')
|
||||||
) {
|
.select((eb) => [...this.baseFields, this.countGroupMembers(eb)])
|
||||||
// todo add group member count
|
.where('workspaceId', '=', workspaceId)
|
||||||
return executeTx(this.db, async (trx) => {
|
.orderBy('createdAt', 'asc');
|
||||||
const groups = await trx
|
|
||||||
.selectFrom('groups')
|
|
||||||
.selectAll()
|
|
||||||
.where('workspaceId', '=', workspaceId)
|
|
||||||
.limit(paginationOptions.limit)
|
|
||||||
.offset(paginationOptions.offset)
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
let { count } = await trx
|
if (pagination.query) {
|
||||||
.selectFrom('groups')
|
query = query.where((eb) =>
|
||||||
.select((eb) => eb.fn.count('id').as('count'))
|
eb('name', 'ilike', `%${pagination.query}%`).or(
|
||||||
.where('workspaceId', '=', workspaceId)
|
'description',
|
||||||
.executeTakeFirst();
|
'ilike',
|
||||||
|
`%${pagination.query}%`,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
count = count as number;
|
const result = executeWithPagination(query, {
|
||||||
return { groups, count };
|
page: pagination.page,
|
||||||
|
perPage: pagination.limit,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
async delete(groupId: string, workspaceId: string): Promise<void> {
|
async delete(groupId: string, workspaceId: string): Promise<void> {
|
||||||
|
|||||||
@ -1,13 +1,14 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { InjectKysely } from 'nestjs-kysely';
|
import { InjectKysely } from 'nestjs-kysely';
|
||||||
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
|
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
|
||||||
import { executeTx } from '../../utils';
|
import { dbOrTx } from '../../utils';
|
||||||
import {
|
import {
|
||||||
InsertablePageHistory,
|
InsertablePageHistory,
|
||||||
PageHistory,
|
PageHistory,
|
||||||
UpdatablePageHistory,
|
UpdatablePageHistory,
|
||||||
} from '@docmost/db/types/entity.types';
|
} from '@docmost/db/types/entity.types';
|
||||||
import { PaginationOptions } from 'src/helpers/pagination/pagination-options';
|
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
|
||||||
|
import { executeWithPagination } from '@docmost/db/pagination/pagination';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PageHistoryRepo {
|
export class PageHistoryRepo {
|
||||||
@ -26,74 +27,55 @@ export class PageHistoryRepo {
|
|||||||
pageHistoryId: string,
|
pageHistoryId: string,
|
||||||
trx?: KyselyTransaction,
|
trx?: KyselyTransaction,
|
||||||
) {
|
) {
|
||||||
return await executeTx(
|
const db = dbOrTx(this.db, trx);
|
||||||
this.db,
|
return db
|
||||||
async (trx) => {
|
.updateTable('pageHistory')
|
||||||
return await trx
|
.set(updatablePageHistory)
|
||||||
.updateTable('pageHistory')
|
.where('id', '=', pageHistoryId)
|
||||||
.set(updatablePageHistory)
|
.execute();
|
||||||
.where('id', '=', pageHistoryId)
|
|
||||||
.execute();
|
|
||||||
},
|
|
||||||
trx,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async insertPageHistory(
|
async insertPageHistory(
|
||||||
insertablePageHistory: InsertablePageHistory,
|
insertablePageHistory: InsertablePageHistory,
|
||||||
trx?: KyselyTransaction,
|
trx?: KyselyTransaction,
|
||||||
): Promise<PageHistory> {
|
): Promise<PageHistory> {
|
||||||
return await executeTx(
|
const db = dbOrTx(this.db, trx);
|
||||||
this.db,
|
return db
|
||||||
async (trx) => {
|
.insertInto('pageHistory')
|
||||||
return await trx
|
.values(insertablePageHistory)
|
||||||
.insertInto('pageHistory')
|
.returningAll()
|
||||||
.values(insertablePageHistory)
|
.executeTakeFirst();
|
||||||
.returningAll()
|
|
||||||
.executeTakeFirst();
|
|
||||||
},
|
|
||||||
trx,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async findPageHistoryByPageId(
|
async findPageHistoryByPageId(pageId: string, pagination: PaginationOptions) {
|
||||||
pageId: string,
|
// todo: fix user relationship
|
||||||
paginationOptions: PaginationOptions,
|
const query = this.db
|
||||||
) {
|
.selectFrom('pageHistory as history')
|
||||||
return executeTx(this.db, async (trx) => {
|
.innerJoin('users as user', 'user.id', 'history.lastUpdatedById')
|
||||||
const pageHistory = await trx
|
.select([
|
||||||
.selectFrom('pageHistory as history')
|
'history.id',
|
||||||
.innerJoin('users as user', 'user.id', 'history.lastUpdatedById')
|
'history.pageId',
|
||||||
.select([
|
'history.title',
|
||||||
'history.id',
|
'history.slug',
|
||||||
'history.pageId',
|
'history.icon',
|
||||||
'history.title',
|
'history.coverPhoto',
|
||||||
'history.slug',
|
'history.version',
|
||||||
'history.icon',
|
'history.lastUpdatedById',
|
||||||
'history.coverPhoto',
|
'history.workspaceId',
|
||||||
'history.version',
|
'history.createdAt',
|
||||||
'history.lastUpdatedById',
|
'history.updatedAt',
|
||||||
'history.workspaceId',
|
'user.id',
|
||||||
'history.createdAt',
|
'user.name',
|
||||||
'history.updatedAt',
|
'user.avatarUrl',
|
||||||
'user.id',
|
])
|
||||||
'user.name',
|
.where('pageId', '=', pageId)
|
||||||
'user.avatarUrl',
|
.orderBy('createdAt', 'desc');
|
||||||
])
|
|
||||||
.where('pageId', '=', pageId)
|
|
||||||
.orderBy('createdAt', 'desc')
|
|
||||||
.limit(paginationOptions.limit)
|
|
||||||
.offset(paginationOptions.offset)
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
let { count } = await trx
|
const result = executeWithPagination(query, {
|
||||||
.selectFrom('pageHistory')
|
page: pagination.offset,
|
||||||
.select((eb) => eb.fn.count('id').as('count'))
|
perPage: pagination.limit,
|
||||||
.where('pageId', '=', pageId)
|
|
||||||
.executeTakeFirst();
|
|
||||||
|
|
||||||
count = count as number;
|
|
||||||
return { pageHistory, count };
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,66 +1,8 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { InjectKysely } from 'nestjs-kysely';
|
import { InjectKysely } from 'nestjs-kysely';
|
||||||
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
|
import { KyselyDB } from '../../types/kysely.types';
|
||||||
import { executeTx } from '../../utils';
|
|
||||||
import {
|
|
||||||
InsertablePage,
|
|
||||||
Page,
|
|
||||||
UpdatablePage,
|
|
||||||
} from '@docmost/db/types/entity.types';
|
|
||||||
import { sql } from 'kysely';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PageOrderingRepo {
|
export class PageOrderingRepo {
|
||||||
constructor(@InjectKysely() private readonly db: KyselyDB) {}
|
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,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,15 +1,16 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { InjectKysely } from 'nestjs-kysely';
|
import { InjectKysely } from 'nestjs-kysely';
|
||||||
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
|
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
|
||||||
import { executeTx } from '../../utils';
|
import { dbOrTx } from '../../utils';
|
||||||
import {
|
import {
|
||||||
InsertablePage,
|
InsertablePage,
|
||||||
Page,
|
Page,
|
||||||
UpdatablePage,
|
UpdatablePage,
|
||||||
} from '@docmost/db/types/entity.types';
|
} from '@docmost/db/types/entity.types';
|
||||||
import { sql } from 'kysely';
|
import { sql } from 'kysely';
|
||||||
import { PaginationOptions } from 'src/helpers/pagination/pagination-options';
|
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
|
||||||
import { OrderingEntity } from 'src/core/page/page.util';
|
import { OrderingEntity } from 'src/core/page/page.util';
|
||||||
|
import { executeWithPagination } from '@docmost/db/pagination/pagination';
|
||||||
|
|
||||||
// TODO: scope to space/workspace
|
// TODO: scope to space/workspace
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@ -63,63 +64,43 @@ export class PageRepo {
|
|||||||
pageId: string,
|
pageId: string,
|
||||||
trx?: KyselyTransaction,
|
trx?: KyselyTransaction,
|
||||||
) {
|
) {
|
||||||
return await executeTx(
|
const db = dbOrTx(this.db, trx);
|
||||||
this.db,
|
return db
|
||||||
async (trx) => {
|
.updateTable('pages')
|
||||||
return await trx
|
.set(updatablePage)
|
||||||
.updateTable('pages')
|
.where('id', '=', pageId)
|
||||||
.set(updatablePage)
|
.execute();
|
||||||
.where('id', '=', pageId)
|
|
||||||
.execute();
|
|
||||||
},
|
|
||||||
trx,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async insertPage(
|
async insertPage(
|
||||||
insertablePage: InsertablePage,
|
insertablePage: InsertablePage,
|
||||||
trx?: KyselyTransaction,
|
trx?: KyselyTransaction,
|
||||||
): Promise<Page> {
|
): Promise<Page> {
|
||||||
return await executeTx(
|
const db = dbOrTx(this.db, trx);
|
||||||
this.db,
|
return db
|
||||||
async (trx) => {
|
.insertInto('pages')
|
||||||
return await trx
|
.values(insertablePage)
|
||||||
.insertInto('pages')
|
.returningAll()
|
||||||
.values(insertablePage)
|
.executeTakeFirst();
|
||||||
.returningAll()
|
|
||||||
.executeTakeFirst();
|
|
||||||
},
|
|
||||||
trx,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async deletePage(pageId: string): Promise<void> {
|
async deletePage(pageId: string): Promise<void> {
|
||||||
await this.db.deleteFrom('pages').where('id', '=', pageId).execute();
|
await this.db.deleteFrom('pages').where('id', '=', pageId).execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
async getRecentPagesInSpace(
|
async getRecentPagesInSpace(spaceId: string, pagination: PaginationOptions) {
|
||||||
spaceId: string,
|
const query = this.db
|
||||||
paginationOptions: PaginationOptions,
|
.selectFrom('pages')
|
||||||
) {
|
.select(this.baseFields)
|
||||||
return executeTx(this.db, async (trx) => {
|
.where('spaceId', '=', spaceId)
|
||||||
const pages = await trx
|
.orderBy('updatedAt', 'desc');
|
||||||
.selectFrom('pages')
|
|
||||||
.select(this.baseFields)
|
|
||||||
.where('spaceId', '=', spaceId)
|
|
||||||
.orderBy('updatedAt', 'desc')
|
|
||||||
.limit(paginationOptions.limit)
|
|
||||||
.offset(paginationOptions.offset)
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
let { count } = await trx
|
const result = executeWithPagination(query, {
|
||||||
.selectFrom('pages')
|
page: pagination.page,
|
||||||
.select((eb) => eb.fn.count('id').as('count'))
|
perPage: pagination.limit,
|
||||||
.where('spaceId', '=', spaceId)
|
|
||||||
.executeTakeFirst();
|
|
||||||
|
|
||||||
count = count as number;
|
|
||||||
return { pages, count };
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getSpaceSidebarPages(spaceId: string, limit: number) {
|
async getSpaceSidebarPages(spaceId: string, limit: number) {
|
||||||
@ -138,8 +119,7 @@ export class PageRepo {
|
|||||||
'page.creatorId',
|
'page.creatorId',
|
||||||
'page.createdAt',
|
'page.createdAt',
|
||||||
])
|
])
|
||||||
.orderBy('page.createdAt', 'desc')
|
.orderBy('page.updatedAt', 'desc')
|
||||||
.orderBy('updatedAt', 'desc')
|
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
.execute();
|
.execute();
|
||||||
|
|
||||||
|
|||||||
@ -1,14 +1,15 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { InjectKysely } from 'nestjs-kysely';
|
import { InjectKysely } from 'nestjs-kysely';
|
||||||
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
|
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
|
||||||
import { executeTx } from '@docmost/db/utils';
|
import { dbOrTx } from '@docmost/db/utils';
|
||||||
import {
|
import {
|
||||||
InsertableSpaceMember,
|
InsertableSpaceMember,
|
||||||
SpaceMember,
|
SpaceMember,
|
||||||
} from '@docmost/db/types/entity.types';
|
} from '@docmost/db/types/entity.types';
|
||||||
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
|
import { PaginationOptions } from '../../pagination/pagination-options';
|
||||||
import { MemberInfo } from './types';
|
import { MemberInfo } from './types';
|
||||||
import { sql } from 'kysely';
|
import { sql } from 'kysely';
|
||||||
|
import { executeWithPagination } from '@docmost/db/pagination/pagination';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SpaceMemberRepo {
|
export class SpaceMemberRepo {
|
||||||
@ -18,83 +19,70 @@ export class SpaceMemberRepo {
|
|||||||
insertableSpaceMember: InsertableSpaceMember,
|
insertableSpaceMember: InsertableSpaceMember,
|
||||||
trx?: KyselyTransaction,
|
trx?: KyselyTransaction,
|
||||||
): Promise<SpaceMember> {
|
): Promise<SpaceMember> {
|
||||||
return await executeTx(
|
const db = dbOrTx(this.db, trx);
|
||||||
this.db,
|
return db
|
||||||
async (trx) => {
|
.insertInto('spaceMembers')
|
||||||
return await trx
|
.values(insertableSpaceMember)
|
||||||
.insertInto('spaceMembers')
|
.returningAll()
|
||||||
.values(insertableSpaceMember)
|
.executeTakeFirst();
|
||||||
.returningAll()
|
|
||||||
.executeTakeFirst();
|
|
||||||
},
|
|
||||||
trx,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getSpaceMembersPaginated(
|
async getSpaceMembersPaginated(
|
||||||
spaceId: string,
|
spaceId: string,
|
||||||
paginationOptions: PaginationOptions,
|
pagination: PaginationOptions,
|
||||||
) {
|
) {
|
||||||
return executeTx(this.db, async (trx) => {
|
const query = this.db
|
||||||
const spaceMembers = await trx
|
.selectFrom('spaceMembers')
|
||||||
.selectFrom('spaceMembers')
|
.leftJoin('users', 'users.id', 'spaceMembers.userId')
|
||||||
.leftJoin('users', 'users.id', 'spaceMembers.userId')
|
.leftJoin('groups', 'groups.id', 'spaceMembers.groupId')
|
||||||
.leftJoin('groups', 'groups.id', 'spaceMembers.groupId')
|
.select([
|
||||||
.select([
|
'groups.id as group_id',
|
||||||
'groups.id as group_id',
|
'groups.name as group_name',
|
||||||
'groups.name as group_name',
|
'groups.isDefault as group_isDefault',
|
||||||
'groups.isDefault as group_isDefault',
|
'groups.id as groups_id',
|
||||||
'groups.id as groups_id',
|
'groups.id as groups_id',
|
||||||
'groups.id as groups_id',
|
'groups.id as groups_id',
|
||||||
'groups.id as groups_id',
|
'users.id as user_id',
|
||||||
'users.id as user_id',
|
'users.name as user_name',
|
||||||
'users.name as user_name',
|
'users.avatarUrl as user_avatarUrl',
|
||||||
'users.avatarUrl as user_avatarUrl',
|
'users.email as user_email',
|
||||||
'users.email as user_email',
|
'spaceMembers.role',
|
||||||
'spaceMembers.role',
|
])
|
||||||
])
|
.where('spaceId', '=', spaceId)
|
||||||
.where('spaceId', '=', spaceId)
|
.orderBy('spaceMembers.createdAt', 'asc');
|
||||||
.orderBy('spaceMembers.createdAt', 'asc')
|
|
||||||
.limit(paginationOptions.limit)
|
|
||||||
.offset(paginationOptions.offset)
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
let memberInfo: MemberInfo;
|
const result = await executeWithPagination(query, {
|
||||||
|
page: pagination.page,
|
||||||
const members = spaceMembers.map((member) => {
|
perPage: pagination.limit,
|
||||||
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('spaceMembers')
|
|
||||||
.select((eb) => eb.fn.count('id').as('count'))
|
|
||||||
.where('spaceId', '=', spaceId)
|
|
||||||
.executeTakeFirst();
|
|
||||||
count = count as number;
|
|
||||||
|
|
||||||
return { members, count };
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let memberInfo: MemberInfo;
|
||||||
|
|
||||||
|
const members = result.items.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',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
...memberInfo,
|
||||||
|
role: member.role,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
return members;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -263,18 +251,13 @@ export class SpaceMemberRepo {
|
|||||||
groupId: string,
|
groupId: string,
|
||||||
trx?: KyselyTransaction,
|
trx?: KyselyTransaction,
|
||||||
) {
|
) {
|
||||||
return await executeTx(
|
const db = dbOrTx(this.db, trx);
|
||||||
this.db,
|
return db
|
||||||
async (trx) => {
|
.selectFrom('spaceMembers')
|
||||||
return await trx
|
.selectAll()
|
||||||
.selectFrom('spaceMembers')
|
.where('userId', '=', userId)
|
||||||
.selectAll()
|
.where('groupId', '=', groupId)
|
||||||
.where('userId', '=', userId)
|
.executeTakeFirst();
|
||||||
.where('groupId', '=', groupId)
|
|
||||||
.executeTakeFirst();
|
|
||||||
},
|
|
||||||
trx,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async removeUser(userId: string, spaceId: string): Promise<void> {
|
async removeUser(userId: string, spaceId: string): Promise<void> {
|
||||||
|
|||||||
@ -1,23 +1,40 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { InjectKysely } from 'nestjs-kysely';
|
import { InjectKysely } from 'nestjs-kysely';
|
||||||
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
|
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
|
||||||
import { executeTx } from '@docmost/db/utils';
|
import { dbOrTx } from '@docmost/db/utils';
|
||||||
import {
|
import {
|
||||||
InsertableSpace,
|
InsertableSpace,
|
||||||
Space,
|
Space,
|
||||||
UpdatableSpace,
|
UpdatableSpace,
|
||||||
} from '@docmost/db/types/entity.types';
|
} from '@docmost/db/types/entity.types';
|
||||||
import { sql } from 'kysely';
|
import { ExpressionBuilder, sql } from 'kysely';
|
||||||
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
|
import { PaginationOptions } from '../../pagination/pagination-options';
|
||||||
|
import { executeWithPagination } from '@docmost/db/pagination/pagination';
|
||||||
|
import { DB } from '@docmost/db/types/db';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class SpaceRepo {
|
export class SpaceRepo {
|
||||||
constructor(@InjectKysely() private readonly db: KyselyDB) {}
|
constructor(@InjectKysely() private readonly db: KyselyDB) {}
|
||||||
|
|
||||||
|
private baseFields: Array<keyof Space> = [
|
||||||
|
'id',
|
||||||
|
'name',
|
||||||
|
'description',
|
||||||
|
'slug',
|
||||||
|
'icon',
|
||||||
|
'visibility',
|
||||||
|
'defaultRole',
|
||||||
|
'workspaceId',
|
||||||
|
'creatorId',
|
||||||
|
'createdAt',
|
||||||
|
'updatedAt',
|
||||||
|
'deletedAt',
|
||||||
|
];
|
||||||
|
|
||||||
async findById(spaceId: string, workspaceId: string): Promise<Space> {
|
async findById(spaceId: string, workspaceId: string): Promise<Space> {
|
||||||
return await this.db
|
return await this.db
|
||||||
.selectFrom('spaces')
|
.selectFrom('spaces')
|
||||||
.selectAll()
|
.select((eb) => [...this.baseFields, this.countSpaceMembers(eb)])
|
||||||
.where('id', '=', spaceId)
|
.where('id', '=', spaceId)
|
||||||
.where('workspaceId', '=', workspaceId)
|
.where('workspaceId', '=', workspaceId)
|
||||||
.executeTakeFirst();
|
.executeTakeFirst();
|
||||||
@ -26,7 +43,7 @@ export class SpaceRepo {
|
|||||||
async findBySlug(slug: string, workspaceId: string): Promise<Space> {
|
async findBySlug(slug: string, workspaceId: string): Promise<Space> {
|
||||||
return await this.db
|
return await this.db
|
||||||
.selectFrom('spaces')
|
.selectFrom('spaces')
|
||||||
.selectAll()
|
.select((eb) => [...this.baseFields, this.countSpaceMembers(eb)])
|
||||||
.where(sql`LOWER(slug)`, '=', sql`LOWER(${slug})`)
|
.where(sql`LOWER(slug)`, '=', sql`LOWER(${slug})`)
|
||||||
.where('workspaceId', '=', workspaceId)
|
.where('workspaceId', '=', workspaceId)
|
||||||
.executeTakeFirst();
|
.executeTakeFirst();
|
||||||
@ -37,28 +54,25 @@ export class SpaceRepo {
|
|||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
trx?: KyselyTransaction,
|
trx?: KyselyTransaction,
|
||||||
): Promise<boolean> {
|
): Promise<boolean> {
|
||||||
return executeTx(
|
const db = dbOrTx(this.db, trx);
|
||||||
this.db,
|
let { count } = await db
|
||||||
async (trx) => {
|
.selectFrom('spaces')
|
||||||
let { count } = await trx
|
.select((eb) => eb.fn.count('id').as('count'))
|
||||||
.selectFrom('spaces')
|
.where(sql`LOWER(slug)`, '=', sql`LOWER(${slug})`)
|
||||||
.select((eb) => eb.fn.count('id').as('count'))
|
.where('workspaceId', '=', workspaceId)
|
||||||
.where(sql`LOWER(slug)`, '=', sql`LOWER(${slug})`)
|
.executeTakeFirst();
|
||||||
.where('workspaceId', '=', workspaceId)
|
count = count as number;
|
||||||
.executeTakeFirst();
|
return count == 0 ? false : true;
|
||||||
count = count as number;
|
|
||||||
return count == 0 ? false : true;
|
|
||||||
},
|
|
||||||
trx,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateSpace(
|
async updateSpace(
|
||||||
updatableSpace: UpdatableSpace,
|
updatableSpace: UpdatableSpace,
|
||||||
spaceId: string,
|
spaceId: string,
|
||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
|
trx?: KyselyTransaction,
|
||||||
) {
|
) {
|
||||||
return await this.db
|
const db = dbOrTx(this.db, trx);
|
||||||
|
return db
|
||||||
.updateTable('spaces')
|
.updateTable('spaces')
|
||||||
.set(updatableSpace)
|
.set(updatableSpace)
|
||||||
.where('id', '=', spaceId)
|
.where('id', '=', spaceId)
|
||||||
@ -70,44 +84,50 @@ export class SpaceRepo {
|
|||||||
insertableSpace: InsertableSpace,
|
insertableSpace: InsertableSpace,
|
||||||
trx?: KyselyTransaction,
|
trx?: KyselyTransaction,
|
||||||
): Promise<Space> {
|
): Promise<Space> {
|
||||||
return await executeTx(
|
const db = dbOrTx(this.db, trx);
|
||||||
this.db,
|
return db
|
||||||
async (trx) => {
|
.insertInto('spaces')
|
||||||
return await trx
|
.values(insertableSpace)
|
||||||
.insertInto('spaces')
|
.returningAll()
|
||||||
.values(insertableSpace)
|
.executeTakeFirst();
|
||||||
.returningAll()
|
|
||||||
.executeTakeFirst();
|
|
||||||
},
|
|
||||||
trx,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async getSpacesInWorkspace(
|
async getSpacesInWorkspace(
|
||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
paginationOptions: PaginationOptions,
|
pagination: PaginationOptions,
|
||||||
) {
|
) {
|
||||||
//todo: add member count
|
// todo: show spaces user have access based on visibility and memberships
|
||||||
// to: show spaces user have access based on visibility and membership
|
let query = this.db
|
||||||
|
.selectFrom('spaces')
|
||||||
|
.select((eb) => [...this.baseFields, this.countSpaceMembers(eb)])
|
||||||
|
.where('workspaceId', '=', workspaceId)
|
||||||
|
.orderBy('createdAt', 'asc');
|
||||||
|
|
||||||
return executeTx(this.db, async (trx) => {
|
if (pagination.query) {
|
||||||
const spaces = await trx
|
query = query.where((eb) =>
|
||||||
.selectFrom('spaces')
|
eb('name', 'ilike', `%${pagination.query}%`).or(
|
||||||
.selectAll()
|
'description',
|
||||||
.where('workspaceId', '=', workspaceId)
|
'ilike',
|
||||||
.limit(paginationOptions.limit)
|
`%${pagination.query}%`,
|
||||||
.offset(paginationOptions.offset)
|
),
|
||||||
.execute();
|
);
|
||||||
|
}
|
||||||
|
|
||||||
let { count } = await trx
|
const result = executeWithPagination(query, {
|
||||||
.selectFrom('spaces')
|
page: pagination.page,
|
||||||
.select((eb) => eb.fn.count('id').as('count'))
|
perPage: pagination.limit,
|
||||||
.where('workspaceId', '=', workspaceId)
|
|
||||||
.executeTakeFirst();
|
|
||||||
|
|
||||||
count = count as number;
|
|
||||||
return { spaces, count };
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
countSpaceMembers(eb: ExpressionBuilder<DB, 'spaces'>) {
|
||||||
|
// should get unique members via groups?
|
||||||
|
return eb
|
||||||
|
.selectFrom('spaceMembers')
|
||||||
|
.select((eb) => eb.fn.countAll().as('count'))
|
||||||
|
.whereRef('spaceMembers.spaceId', '=', 'spaces.id')
|
||||||
|
.as('memberCount');
|
||||||
}
|
}
|
||||||
|
|
||||||
async deleteSpace(spaceId: string, workspaceId: string): Promise<void> {
|
async deleteSpace(spaceId: string, workspaceId: string): Promise<void> {
|
||||||
|
|||||||
@ -3,13 +3,14 @@ import { InjectKysely } from 'nestjs-kysely';
|
|||||||
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
|
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
|
||||||
import { Users } from '@docmost/db/types/db';
|
import { Users } from '@docmost/db/types/db';
|
||||||
import { hashPassword } from '../../../helpers/utils';
|
import { hashPassword } from '../../../helpers/utils';
|
||||||
import { executeTx } from '@docmost/db/utils';
|
import { dbOrTx } from '@docmost/db/utils';
|
||||||
import {
|
import {
|
||||||
InsertableUser,
|
InsertableUser,
|
||||||
UpdatableUser,
|
UpdatableUser,
|
||||||
User,
|
User,
|
||||||
} from '@docmost/db/types/entity.types';
|
} from '@docmost/db/types/entity.types';
|
||||||
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
|
import { PaginationOptions } from '../../pagination/pagination-options';
|
||||||
|
import { executeWithPagination } from '@docmost/db/pagination/pagination';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class UserRepo {
|
export class UserRepo {
|
||||||
@ -35,8 +36,10 @@ export class UserRepo {
|
|||||||
userId: string,
|
userId: string,
|
||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
includePassword?: boolean,
|
includePassword?: boolean,
|
||||||
|
trx?: KyselyTransaction,
|
||||||
): Promise<User> {
|
): Promise<User> {
|
||||||
return this.db
|
const db = dbOrTx(this.db, trx);
|
||||||
|
return db
|
||||||
.selectFrom('users')
|
.selectFrom('users')
|
||||||
.select(this.baseFields)
|
.select(this.baseFields)
|
||||||
.$if(includePassword, (qb) => qb.select('password'))
|
.$if(includePassword, (qb) => qb.select('password'))
|
||||||
@ -95,17 +98,12 @@ export class UserRepo {
|
|||||||
lastLoginAt: new Date(),
|
lastLoginAt: new Date(),
|
||||||
};
|
};
|
||||||
|
|
||||||
return await executeTx(
|
const db = dbOrTx(this.db, trx);
|
||||||
this.db,
|
return db
|
||||||
async (trx) => {
|
.insertInto('users')
|
||||||
return await trx
|
.values(user)
|
||||||
.insertInto('users')
|
.returningAll()
|
||||||
.values(user)
|
.executeTakeFirst();
|
||||||
.returningAll()
|
|
||||||
.executeTakeFirst();
|
|
||||||
},
|
|
||||||
trx,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async roleCountByWorkspaceId(
|
async roleCountByWorkspaceId(
|
||||||
@ -122,28 +120,24 @@ export class UserRepo {
|
|||||||
return count as number;
|
return count as number;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getUsersPaginated(
|
async getUsersPaginated(workspaceId: string, pagination: PaginationOptions) {
|
||||||
workspaceId: string,
|
let query = this.db
|
||||||
paginationOptions: PaginationOptions,
|
.selectFrom('users')
|
||||||
) {
|
.select(this.baseFields)
|
||||||
return executeTx(this.db, async (trx) => {
|
.where('workspaceId', '=', workspaceId)
|
||||||
const users = await trx
|
.orderBy('createdAt', 'asc');
|
||||||
.selectFrom('users')
|
|
||||||
.select(this.baseFields)
|
|
||||||
.where('workspaceId', '=', workspaceId)
|
|
||||||
.orderBy('createdAt asc')
|
|
||||||
.limit(paginationOptions.limit)
|
|
||||||
.offset(paginationOptions.offset)
|
|
||||||
.execute();
|
|
||||||
|
|
||||||
let { count } = await trx
|
if (pagination.query) {
|
||||||
.selectFrom('users')
|
query = query.where((eb) =>
|
||||||
.select((eb) => eb.fn.countAll().as('count'))
|
eb('users.name', 'ilike', `%${pagination.query}%`),
|
||||||
.where('workspaceId', '=', workspaceId)
|
);
|
||||||
.executeTakeFirst();
|
}
|
||||||
|
|
||||||
count = count as number;
|
const result = executeWithPagination(query, {
|
||||||
return { users, count };
|
page: pagination.page,
|
||||||
|
perPage: pagination.limit,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { InjectKysely } from 'nestjs-kysely';
|
import { InjectKysely } from 'nestjs-kysely';
|
||||||
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
|
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
|
||||||
import { executeTx } from '../../utils';
|
import { dbOrTx } from '../../utils';
|
||||||
import {
|
import {
|
||||||
InsertableWorkspace,
|
InsertableWorkspace,
|
||||||
UpdatableWorkspace,
|
UpdatableWorkspace,
|
||||||
@ -43,34 +43,24 @@ export class WorkspaceRepo {
|
|||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
trx?: KyselyTransaction,
|
trx?: KyselyTransaction,
|
||||||
) {
|
) {
|
||||||
return await executeTx(
|
const db = dbOrTx(this.db, trx);
|
||||||
this.db,
|
return db
|
||||||
async (trx) => {
|
.updateTable('workspaces')
|
||||||
return await trx
|
.set(updatableWorkspace)
|
||||||
.updateTable('workspaces')
|
.where('id', '=', workspaceId)
|
||||||
.set(updatableWorkspace)
|
.execute();
|
||||||
.where('id', '=', workspaceId)
|
|
||||||
.execute();
|
|
||||||
},
|
|
||||||
trx,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async insertWorkspace(
|
async insertWorkspace(
|
||||||
insertableWorkspace: InsertableWorkspace,
|
insertableWorkspace: InsertableWorkspace,
|
||||||
trx?: KyselyTransaction,
|
trx?: KyselyTransaction,
|
||||||
): Promise<Workspace> {
|
): Promise<Workspace> {
|
||||||
return await executeTx(
|
const db = dbOrTx(this.db, trx);
|
||||||
this.db,
|
return db
|
||||||
async (trx) => {
|
.insertInto('workspaces')
|
||||||
return await trx
|
.values(insertableWorkspace)
|
||||||
.insertInto('workspaces')
|
.returningAll()
|
||||||
.values(insertableWorkspace)
|
.executeTakeFirst();
|
||||||
.returningAll()
|
|
||||||
.executeTakeFirst();
|
|
||||||
},
|
|
||||||
trx,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async count(): Promise<number> {
|
async count(): Promise<number> {
|
||||||
|
|||||||
@ -42,6 +42,8 @@ export type InsertableSpaceMember = Insertable<SpaceMembers>;
|
|||||||
export type UpdatableSpaceMember = Updateable<Omit<SpaceMembers, 'id'>>;
|
export type UpdatableSpaceMember = Updateable<Omit<SpaceMembers, 'id'>>;
|
||||||
|
|
||||||
// Group
|
// Group
|
||||||
|
export type ExtendedGroup = Groups & { memberCount: number };
|
||||||
|
|
||||||
export type Group = Selectable<Groups>;
|
export type Group = Selectable<Groups>;
|
||||||
export type InsertableGroup = Insertable<Groups>;
|
export type InsertableGroup = Insertable<Groups>;
|
||||||
export type UpdatableGroup = Updateable<Omit<Groups, 'id'>>;
|
export type UpdatableGroup = Updateable<Omit<Groups, 'id'>>;
|
||||||
|
|||||||
@ -1,13 +1,33 @@
|
|||||||
import { KyselyDB, KyselyTransaction } from './types/kysely.types';
|
import { KyselyDB, KyselyTransaction } from './types/kysely.types';
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Executes a transaction or a callback using the provided database instance.
|
||||||
|
* If an existing transaction is provided, it directly executes the callback with it.
|
||||||
|
* Otherwise, it starts a new transaction using the provided database instance and executes the callback within that transaction.
|
||||||
|
*/
|
||||||
export async function executeTx<T>(
|
export async function executeTx<T>(
|
||||||
db: KyselyDB,
|
db: KyselyDB,
|
||||||
callback: (trx: KyselyTransaction) => Promise<T>,
|
callback: (trx: KyselyTransaction) => Promise<T>,
|
||||||
existingTrx?: KyselyTransaction,
|
existingTrx?: KyselyTransaction,
|
||||||
): Promise<T> {
|
): Promise<T> {
|
||||||
if (existingTrx) {
|
if (existingTrx) {
|
||||||
return await callback(existingTrx);
|
return await callback(existingTrx); // Execute callback with existing transaction
|
||||||
} else {
|
} else {
|
||||||
return await db.transaction().execute((trx) => callback(trx));
|
return await db.transaction().execute((trx) => callback(trx)); // Start new transaction and execute callback
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This function returns either an existing transaction if provided,
|
||||||
|
* or the normal database instance.
|
||||||
|
*/
|
||||||
|
export function dbOrTx(
|
||||||
|
db: KyselyDB,
|
||||||
|
existingTrx?: KyselyTransaction,
|
||||||
|
): KyselyDB | KyselyTransaction {
|
||||||
|
if (existingTrx) {
|
||||||
|
return existingTrx; // Use existing transaction
|
||||||
|
} else {
|
||||||
|
return db; // Use normal database instance
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user