server: refactor pagination

* fix transaction usgae in repos
* other bug fixes
This commit is contained in:
Philipinho
2024-04-01 01:23:52 +01:00
parent ade3a5b589
commit 4913975e99
38 changed files with 648 additions and 756 deletions

View File

@ -2,7 +2,7 @@ import { Global, Module } from '@nestjs/common';
import { KyselyModule } from 'nestjs-kysely';
import { EnvironmentService } from '../integrations/environment/environment.service';
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 { WorkspaceRepo } from '@docmost/db/repos/workspace/workspace.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 { AttachmentRepo } from './repos/attachment/attachment.repo';
// https://github.com/brianc/node-postgres/issues/811
types.setTypeParser(types.builtins.INT8, (val) => Number(val));
@Global()
@Module({
imports: [

View File

@ -0,0 +1,35 @@
import {
IsNumber,
IsOptional,
IsPositive,
IsString,
Max,
Min,
} from 'class-validator';
export class PaginationOptions {
@IsOptional()
@IsNumber()
@Min(1)
page = 1;
@IsOptional()
@IsNumber()
@IsPositive()
@Min(1)
@Max(100)
limit = 20;
@IsOptional()
@IsString()
query: string;
get offset(): number {
return (this.page - 1) * this.limit;
}
}
export enum PaginationSort {
ASC = 'asc',
DESC = 'desc',
}

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

View File

@ -1,7 +1,7 @@
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 { dbOrTx } from '@docmost/db/utils';
import {
Attachment,
InsertableAttachment,
@ -28,17 +28,13 @@ export class AttachmentRepo {
insertableAttachment: InsertableAttachment,
trx?: KyselyTransaction,
): Promise<Attachment> {
return await executeTx(
this.db,
async (trx) => {
return await trx
.insertInto('attachments')
.values(insertableAttachment)
.returningAll()
.executeTakeFirst();
},
trx,
);
const db = dbOrTx(this.db, trx);
return db
.insertInto('attachments')
.values(insertableAttachment)
.returningAll()
.executeTakeFirst();
}
async updateAttachment(

View File

@ -1,13 +1,14 @@
import { Injectable } from '@nestjs/common';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
import { executeTx } from '../../utils';
import { dbOrTx } from '../../utils';
import {
Comment,
InsertableComment,
UpdatableComment,
} 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()
export class CommentRepo {
@ -22,26 +23,19 @@ export class CommentRepo {
.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();
async findPageComments(pageId: string, pagination: PaginationOptions) {
const query = this.db
.selectFrom('comments')
.selectAll()
.where('pageId', '=', pageId)
.orderBy('createdAt', 'asc');
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 };
const result = executeWithPagination(query, {
page: pagination.page,
perPage: pagination.limit,
});
return result;
}
async updateComment(
@ -49,34 +43,25 @@ export class CommentRepo {
commentId: string,
trx?: KyselyTransaction,
) {
return await executeTx(
this.db,
async (trx) => {
return await trx
.updateTable('comments')
.set(updatableComment)
.where('id', '=', commentId)
.execute();
},
trx,
);
const db = dbOrTx(this.db, trx);
db.updateTable('comments')
.set(updatableComment)
.where('id', '=', commentId)
.execute();
}
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,
);
const db = dbOrTx(this.db, trx);
return db
.insertInto('comments')
.values(insertableComment)
.returningAll()
.executeTakeFirst();
}
async deleteComment(commentId: string): Promise<void> {

View File

@ -1,14 +1,15 @@
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 { dbOrTx } 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';
import { PaginationOptions } from '../../pagination/pagination-options';
import { executeWithPagination } from '@docmost/db/pagination/pagination';
@Injectable()
export class GroupUserRepo {
@ -19,67 +20,47 @@ export class GroupUserRepo {
groupId: string,
trx?: KyselyTransaction,
) {
return await executeTx(
this.db,
async (trx) => {
return await trx
.selectFrom('groupUsers')
.selectAll()
.where('userId', '=', userId)
.where('groupId', '=', groupId)
.executeTakeFirst();
},
trx,
);
const db = dbOrTx(this.db, trx);
return db
.selectFrom('groupUsers')
.selectAll()
.where('userId', '=', userId)
.where('groupId', '=', groupId)
.executeTakeFirst();
}
async insertGroupUser(
insertableGroupUser: InsertableGroupUser,
trx?: KyselyTransaction,
): Promise<GroupUser> {
return await executeTx(
this.db,
async (trx) => {
return await trx
.insertInto('groupUsers')
.values(insertableGroupUser)
.returningAll()
.executeTakeFirst();
},
trx,
);
const db = dbOrTx(this.db, trx);
return db
.insertInto('groupUsers')
.values(insertableGroupUser)
.returningAll()
.executeTakeFirst();
}
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('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[];
async getGroupUsersPaginated(groupId: string, pagination: PaginationOptions) {
let query = this.db
.selectFrom('groupUsers')
.innerJoin('users', 'users.id', 'groupUsers.userId')
.select(sql<User>`users.*` as any)
.where('groupId', '=', groupId)
.orderBy('createdAt', 'asc');
const users: User[] = groupUsers.map((user: User) => {
delete user.password;
return user;
});
if (pagination.query) {
query = query.where((eb) =>
eb('users.name', 'ilike', `%${pagination.query}%`),
);
}
let { count } = await trx
.selectFrom('groupUsers')
.select((eb) => eb.fn.count('id').as('count'))
.where('groupId', '=', groupId)
.executeTakeFirst();
count = count as number;
return { users, count };
const result = executeWithPagination(query, {
page: pagination.page,
perPage: pagination.limit,
});
return result;
}
async delete(userId: string, groupId: string): Promise<void> {

View File

@ -1,23 +1,44 @@
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 { dbOrTx } 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';
import { ExpressionBuilder, sql } from 'kysely';
import { PaginationOptions } from '../../pagination/pagination-options';
import { DB } from '@docmost/db/types/db';
import { executeWithPagination } from '@docmost/db/pagination/pagination';
@Injectable()
export class GroupRepo {
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> {
return await this.db
.selectFrom('groups')
.selectAll()
.select((eb) => [...this.baseFields, this.countGroupMembers(eb)])
.where('id', '=', groupId)
.where('workspaceId', '=', workspaceId)
.executeTakeFirst();
@ -26,7 +47,7 @@ export class GroupRepo {
async findByName(groupName: string, workspaceId: string): Promise<Group> {
return await this.db
.selectFrom('groups')
.selectAll()
.select((eb) => [...this.baseFields, this.countGroupMembers(eb)])
.where(sql`LOWER(name)`, '=', sql`LOWER(${groupName})`)
.where('workspaceId', '=', workspaceId)
.executeTakeFirst();
@ -49,60 +70,50 @@ export class GroupRepo {
insertableGroup: InsertableGroup,
trx?: KyselyTransaction,
): Promise<Group> {
return await executeTx(
this.db,
async (trx) => {
return await trx
.insertInto('groups')
.values(insertableGroup)
.returningAll()
.executeTakeFirst();
},
trx,
);
const db = dbOrTx(this.db, trx);
return db
.insertInto('groups')
.values(insertableGroup)
.returningAll()
.executeTakeFirst();
}
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,
);
const db = dbOrTx(this.db, trx);
return db
.selectFrom('groups')
.select((eb) => [...this.baseFields, this.countGroupMembers(eb)])
.where('isDefault', '=', true)
.where('workspaceId', '=', workspaceId)
.executeTakeFirst();
}
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();
async getGroupsPaginated(workspaceId: string, pagination: PaginationOptions) {
let query = this.db
.selectFrom('groups')
.select((eb) => [...this.baseFields, this.countGroupMembers(eb)])
.where('workspaceId', '=', workspaceId)
.orderBy('createdAt', 'asc');
let { count } = await trx
.selectFrom('groups')
.select((eb) => eb.fn.count('id').as('count'))
.where('workspaceId', '=', workspaceId)
.executeTakeFirst();
if (pagination.query) {
query = query.where((eb) =>
eb('name', 'ilike', `%${pagination.query}%`).or(
'description',
'ilike',
`%${pagination.query}%`,
),
);
}
count = count as number;
return { groups, count };
const result = executeWithPagination(query, {
page: pagination.page,
perPage: pagination.limit,
});
return result;
}
async delete(groupId: string, workspaceId: string): Promise<void> {

View File

@ -1,13 +1,14 @@
import { Injectable } from '@nestjs/common';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
import { executeTx } from '../../utils';
import { dbOrTx } from '../../utils';
import {
InsertablePageHistory,
PageHistory,
UpdatablePageHistory,
} 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()
export class PageHistoryRepo {
@ -26,74 +27,55 @@ export class PageHistoryRepo {
pageHistoryId: string,
trx?: KyselyTransaction,
) {
return await executeTx(
this.db,
async (trx) => {
return await trx
.updateTable('pageHistory')
.set(updatablePageHistory)
.where('id', '=', pageHistoryId)
.execute();
},
trx,
);
const db = dbOrTx(this.db, trx);
return db
.updateTable('pageHistory')
.set(updatablePageHistory)
.where('id', '=', pageHistoryId)
.execute();
}
async insertPageHistory(
insertablePageHistory: InsertablePageHistory,
trx?: KyselyTransaction,
): Promise<PageHistory> {
return await executeTx(
this.db,
async (trx) => {
return await trx
.insertInto('pageHistory')
.values(insertablePageHistory)
.returningAll()
.executeTakeFirst();
},
trx,
);
const db = dbOrTx(this.db, trx);
return db
.insertInto('pageHistory')
.values(insertablePageHistory)
.returningAll()
.executeTakeFirst();
}
async findPageHistoryByPageId(
pageId: string,
paginationOptions: PaginationOptions,
) {
return executeTx(this.db, async (trx) => {
const pageHistory = await trx
.selectFrom('pageHistory 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();
async findPageHistoryByPageId(pageId: string, pagination: PaginationOptions) {
// todo: fix user relationship
const query = this.db
.selectFrom('pageHistory 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');
let { count } = await trx
.selectFrom('pageHistory')
.select((eb) => eb.fn.count('id').as('count'))
.where('pageId', '=', pageId)
.executeTakeFirst();
count = count as number;
return { pageHistory, count };
const result = executeWithPagination(query, {
page: pagination.offset,
perPage: pagination.limit,
});
return result;
}
}

View File

@ -1,66 +1,8 @@
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 { KyselyDB } from '../../types/kysely.types';
@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

@ -1,15 +1,16 @@
import { Injectable } from '@nestjs/common';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
import { executeTx } from '../../utils';
import { dbOrTx } 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 { PaginationOptions } from '@docmost/db/pagination/pagination-options';
import { OrderingEntity } from 'src/core/page/page.util';
import { executeWithPagination } from '@docmost/db/pagination/pagination';
// TODO: scope to space/workspace
@Injectable()
@ -63,63 +64,43 @@ export class PageRepo {
pageId: string,
trx?: KyselyTransaction,
) {
return await executeTx(
this.db,
async (trx) => {
return await trx
.updateTable('pages')
.set(updatablePage)
.where('id', '=', pageId)
.execute();
},
trx,
);
const db = dbOrTx(this.db, trx);
return db
.updateTable('pages')
.set(updatablePage)
.where('id', '=', pageId)
.execute();
}
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,
);
const db = dbOrTx(this.db, trx);
return db
.insertInto('pages')
.values(insertablePage)
.returningAll()
.executeTakeFirst();
}
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();
async getRecentPagesInSpace(spaceId: string, pagination: PaginationOptions) {
const query = this.db
.selectFrom('pages')
.select(this.baseFields)
.where('spaceId', '=', spaceId)
.orderBy('updatedAt', 'desc');
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 };
const result = executeWithPagination(query, {
page: pagination.page,
perPage: pagination.limit,
});
return result;
}
async getSpaceSidebarPages(spaceId: string, limit: number) {
@ -138,8 +119,7 @@ export class PageRepo {
'page.creatorId',
'page.createdAt',
])
.orderBy('page.createdAt', 'desc')
.orderBy('updatedAt', 'desc')
.orderBy('page.updatedAt', 'desc')
.limit(limit)
.execute();

View File

@ -1,14 +1,15 @@
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 { dbOrTx } from '@docmost/db/utils';
import {
InsertableSpaceMember,
SpaceMember,
} from '@docmost/db/types/entity.types';
import { PaginationOptions } from '../../../helpers/pagination/pagination-options';
import { PaginationOptions } from '../../pagination/pagination-options';
import { MemberInfo } from './types';
import { sql } from 'kysely';
import { executeWithPagination } from '@docmost/db/pagination/pagination';
@Injectable()
export class SpaceMemberRepo {
@ -18,83 +19,70 @@ export class SpaceMemberRepo {
insertableSpaceMember: InsertableSpaceMember,
trx?: KyselyTransaction,
): Promise<SpaceMember> {
return await executeTx(
this.db,
async (trx) => {
return await trx
.insertInto('spaceMembers')
.values(insertableSpaceMember)
.returningAll()
.executeTakeFirst();
},
trx,
);
const db = dbOrTx(this.db, trx);
return db
.insertInto('spaceMembers')
.values(insertableSpaceMember)
.returningAll()
.executeTakeFirst();
}
async getSpaceMembersPaginated(
spaceId: string,
paginationOptions: PaginationOptions,
pagination: PaginationOptions,
) {
return executeTx(this.db, async (trx) => {
const spaceMembers = await trx
.selectFrom('spaceMembers')
.leftJoin('users', 'users.id', 'spaceMembers.userId')
.leftJoin('groups', 'groups.id', 'spaceMembers.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',
'spaceMembers.role',
])
.where('spaceId', '=', spaceId)
.orderBy('spaceMembers.createdAt', 'asc')
.limit(paginationOptions.limit)
.offset(paginationOptions.offset)
.execute();
const query = this.db
.selectFrom('spaceMembers')
.leftJoin('users', 'users.id', 'spaceMembers.userId')
.leftJoin('groups', 'groups.id', 'spaceMembers.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',
'spaceMembers.role',
])
.where('spaceId', '=', spaceId)
.orderBy('spaceMembers.createdAt', 'asc');
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('spaceMembers')
.select((eb) => eb.fn.count('id').as('count'))
.where('spaceId', '=', spaceId)
.executeTakeFirst();
count = count as number;
return { members, count };
const result = await executeWithPagination(query, {
page: pagination.page,
perPage: pagination.limit,
});
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,
trx?: KyselyTransaction,
) {
return await executeTx(
this.db,
async (trx) => {
return await trx
.selectFrom('spaceMembers')
.selectAll()
.where('userId', '=', userId)
.where('groupId', '=', groupId)
.executeTakeFirst();
},
trx,
);
const db = dbOrTx(this.db, trx);
return db
.selectFrom('spaceMembers')
.selectAll()
.where('userId', '=', userId)
.where('groupId', '=', groupId)
.executeTakeFirst();
}
async removeUser(userId: string, spaceId: string): Promise<void> {

View File

@ -1,23 +1,40 @@
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 { dbOrTx } 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';
import { ExpressionBuilder, sql } from 'kysely';
import { PaginationOptions } from '../../pagination/pagination-options';
import { executeWithPagination } from '@docmost/db/pagination/pagination';
import { DB } from '@docmost/db/types/db';
@Injectable()
export class SpaceRepo {
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> {
return await this.db
.selectFrom('spaces')
.selectAll()
.select((eb) => [...this.baseFields, this.countSpaceMembers(eb)])
.where('id', '=', spaceId)
.where('workspaceId', '=', workspaceId)
.executeTakeFirst();
@ -26,7 +43,7 @@ export class SpaceRepo {
async findBySlug(slug: string, workspaceId: string): Promise<Space> {
return await this.db
.selectFrom('spaces')
.selectAll()
.select((eb) => [...this.baseFields, this.countSpaceMembers(eb)])
.where(sql`LOWER(slug)`, '=', sql`LOWER(${slug})`)
.where('workspaceId', '=', workspaceId)
.executeTakeFirst();
@ -37,28 +54,25 @@ export class SpaceRepo {
workspaceId: string,
trx?: KyselyTransaction,
): Promise<boolean> {
return executeTx(
this.db,
async (trx) => {
let { count } = await trx
.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 == 0 ? false : true;
},
trx,
);
const db = dbOrTx(this.db, trx);
let { count } = await 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 == 0 ? false : true;
}
async updateSpace(
updatableSpace: UpdatableSpace,
spaceId: string,
workspaceId: string,
trx?: KyselyTransaction,
) {
return await this.db
const db = dbOrTx(this.db, trx);
return db
.updateTable('spaces')
.set(updatableSpace)
.where('id', '=', spaceId)
@ -70,44 +84,50 @@ export class SpaceRepo {
insertableSpace: InsertableSpace,
trx?: KyselyTransaction,
): Promise<Space> {
return await executeTx(
this.db,
async (trx) => {
return await trx
.insertInto('spaces')
.values(insertableSpace)
.returningAll()
.executeTakeFirst();
},
trx,
);
const db = dbOrTx(this.db, trx);
return db
.insertInto('spaces')
.values(insertableSpace)
.returningAll()
.executeTakeFirst();
}
async getSpacesInWorkspace(
workspaceId: string,
paginationOptions: PaginationOptions,
pagination: PaginationOptions,
) {
//todo: add member count
// to: show spaces user have access based on visibility and membership
// todo: show spaces user have access based on visibility and memberships
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) => {
const spaces = await trx
.selectFrom('spaces')
.selectAll()
.where('workspaceId', '=', workspaceId)
.limit(paginationOptions.limit)
.offset(paginationOptions.offset)
.execute();
if (pagination.query) {
query = query.where((eb) =>
eb('name', 'ilike', `%${pagination.query}%`).or(
'description',
'ilike',
`%${pagination.query}%`,
),
);
}
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 };
const result = executeWithPagination(query, {
page: pagination.page,
perPage: pagination.limit,
});
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> {

View File

@ -3,13 +3,14 @@ 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 { dbOrTx } from '@docmost/db/utils';
import {
InsertableUser,
UpdatableUser,
User,
} 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()
export class UserRepo {
@ -35,8 +36,10 @@ export class UserRepo {
userId: string,
workspaceId: string,
includePassword?: boolean,
trx?: KyselyTransaction,
): Promise<User> {
return this.db
const db = dbOrTx(this.db, trx);
return db
.selectFrom('users')
.select(this.baseFields)
.$if(includePassword, (qb) => qb.select('password'))
@ -95,17 +98,12 @@ export class UserRepo {
lastLoginAt: new Date(),
};
return await executeTx(
this.db,
async (trx) => {
return await trx
.insertInto('users')
.values(user)
.returningAll()
.executeTakeFirst();
},
trx,
);
const db = dbOrTx(this.db, trx);
return db
.insertInto('users')
.values(user)
.returningAll()
.executeTakeFirst();
}
async roleCountByWorkspaceId(
@ -122,28 +120,24 @@ export class UserRepo {
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();
async getUsersPaginated(workspaceId: string, pagination: PaginationOptions) {
let query = this.db
.selectFrom('users')
.select(this.baseFields)
.where('workspaceId', '=', workspaceId)
.orderBy('createdAt', 'asc');
let { count } = await trx
.selectFrom('users')
.select((eb) => eb.fn.countAll().as('count'))
.where('workspaceId', '=', workspaceId)
.executeTakeFirst();
if (pagination.query) {
query = query.where((eb) =>
eb('users.name', 'ilike', `%${pagination.query}%`),
);
}
count = count as number;
return { users, count };
const result = executeWithPagination(query, {
page: pagination.page,
perPage: pagination.limit,
});
return result;
}
}

View File

@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { InjectKysely } from 'nestjs-kysely';
import { KyselyDB, KyselyTransaction } from '../../types/kysely.types';
import { executeTx } from '../../utils';
import { dbOrTx } from '../../utils';
import {
InsertableWorkspace,
UpdatableWorkspace,
@ -43,34 +43,24 @@ export class WorkspaceRepo {
workspaceId: string,
trx?: KyselyTransaction,
) {
return await executeTx(
this.db,
async (trx) => {
return await trx
.updateTable('workspaces')
.set(updatableWorkspace)
.where('id', '=', workspaceId)
.execute();
},
trx,
);
const db = dbOrTx(this.db, trx);
return db
.updateTable('workspaces')
.set(updatableWorkspace)
.where('id', '=', workspaceId)
.execute();
}
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,
);
const db = dbOrTx(this.db, trx);
return db
.insertInto('workspaces')
.values(insertableWorkspace)
.returningAll()
.executeTakeFirst();
}
async count(): Promise<number> {

View File

@ -42,6 +42,8 @@ export type InsertableSpaceMember = Insertable<SpaceMembers>;
export type UpdatableSpaceMember = Updateable<Omit<SpaceMembers, 'id'>>;
// Group
export type ExtendedGroup = Groups & { memberCount: number };
export type Group = Selectable<Groups>;
export type InsertableGroup = Insertable<Groups>;
export type UpdatableGroup = Updateable<Omit<Groups, 'id'>>;

View File

@ -1,13 +1,33 @@
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>(
db: KyselyDB,
callback: (trx: KyselyTransaction) => Promise<T>,
existingTrx?: KyselyTransaction,
): Promise<T> {
if (existingTrx) {
return await callback(existingTrx);
return await callback(existingTrx); // Execute callback with existing transaction
} 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
}
}