mirror of
https://github.com/docmost/docmost.git
synced 2026-08-24 12:22:17 +10:00
refactor(page): move next page position query into page repo
This commit is contained in:
@@ -175,44 +175,7 @@ export class PageService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async nextPagePosition(spaceId: string, parentPageId?: string) {
|
async nextPagePosition(spaceId: string, parentPageId?: string) {
|
||||||
let pagePosition: string;
|
return this.pageRepo.nextPagePosition(spaceId, parentPageId);
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async update(
|
async update(
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import { jsonArrayFrom, jsonObjectFrom } from 'kysely/helpers/postgres';
|
|||||||
import { SpaceMemberRepo } from '@docmost/db/repos/space/space-member.repo';
|
import { SpaceMemberRepo } from '@docmost/db/repos/space/space-member.repo';
|
||||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||||
import { EventName } from '../../../common/events/event.contants';
|
import { EventName } from '../../../common/events/event.contants';
|
||||||
|
import { generateJitteredKeyBetween } from 'fractional-indexing-jittered';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class PageRepo {
|
export class PageRepo {
|
||||||
@@ -605,4 +606,27 @@ export class PageRepo {
|
|||||||
.execute()
|
.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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user