refactor(page): move next page position query into page repo

This commit is contained in:
Philipinho
2026-08-12 20:47:58 +01:00
parent 1dd7d79453
commit dacaaf5629
2 changed files with 25 additions and 38 deletions
@@ -175,44 +175,7 @@ export class PageService {
}
async nextPagePosition(spaceId: string, parentPageId?: string) {
let pagePosition: string;
const lastPageQuery = this.db
.selectFrom('pages')
.select(['position'])
.where('spaceId', '=', spaceId)
.where('deletedAt', 'is', null)
.orderBy('position', (ob) => ob.collate('C').desc())
.limit(1);
if (parentPageId) {
// check for children of this page
const lastPage = await lastPageQuery
.where('parentPageId', '=', parentPageId)
.executeTakeFirst();
if (!lastPage) {
pagePosition = generateJitteredKeyBetween(null, null);
} else {
// if there is an existing page, we should get a position below it
pagePosition = generateJitteredKeyBetween(lastPage.position, null);
}
} else {
// for root page
const lastPage = await lastPageQuery
.where('parentPageId', 'is', null)
.executeTakeFirst();
// if no existing page, make this the first
if (!lastPage) {
pagePosition = generateJitteredKeyBetween(null, null); // we expect "a0"
} else {
// if there is an existing page, we should get a position below it
pagePosition = generateJitteredKeyBetween(lastPage.position, null);
}
}
return pagePosition;
return this.pageRepo.nextPagePosition(spaceId, parentPageId);
}
async update(
@@ -16,6 +16,7 @@ import { jsonArrayFrom, jsonObjectFrom } from 'kysely/helpers/postgres';
import { SpaceMemberRepo } from '@docmost/db/repos/space/space-member.repo';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { EventName } from '../../../common/events/event.contants';
import { generateJitteredKeyBetween } from 'fractional-indexing-jittered';
@Injectable()
export class PageRepo {
@@ -605,4 +606,27 @@ export class PageRepo {
.execute()
);
}
async nextPagePosition(
spaceId: string,
parentPageId?: string,
): Promise<string> {
const lastPageQuery = this.db
.selectFrom('pages')
.select(['position'])
.where('spaceId', '=', spaceId)
.where('deletedAt', 'is', null)
.orderBy('position', (ob) => ob.collate('C').desc())
.limit(1);
const lastPage = parentPageId
? await lastPageQuery
.where('parentPageId', '=', parentPageId)
.executeTakeFirst()
: await lastPageQuery
.where('parentPageId', 'is', null)
.executeTakeFirst();
return generateJitteredKeyBetween(lastPage?.position ?? null, null);
}
}