mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 21:27:20 +00:00
feat(server): docs pagination (#12086)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added paginated document listing for workspaces, allowing users to browse documents with pagination controls. - Enhanced document details to display creation and update timestamps, as well as information about the creator and last updater. - **Bug Fixes** - Updated deprecation notice for workspace document metadata fields to guide users to the latest recommended field. - **Tests** - Added new tests to verify document info retrieval and pagination functionality. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -3,6 +3,7 @@ import { Transactional } from '@nestjs-cls/transactional';
|
||||
import type { Update } from '@prisma/client';
|
||||
import { Prisma } from '@prisma/client';
|
||||
|
||||
import { PaginationInput } from '../base';
|
||||
import { DocIsNotPublic } from '../base/error';
|
||||
import { BaseModel } from './base';
|
||||
import { Doc, DocRole, PublicDocMode, publicUserSelect } from './common';
|
||||
@@ -480,5 +481,92 @@ export class DocModel extends BaseModel {
|
||||
});
|
||||
return docMeta?.public ?? false;
|
||||
}
|
||||
|
||||
async getDocInfo(workspaceId: string, docId: string) {
|
||||
const rows = await this.db.$queryRaw<
|
||||
{
|
||||
workspaceId: string;
|
||||
docId: string;
|
||||
mode: PublicDocMode;
|
||||
public: boolean;
|
||||
defaultRole: DocRole;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
creatorId?: string;
|
||||
lastUpdaterId?: string;
|
||||
}[]
|
||||
>`
|
||||
SELECT
|
||||
"workspace_pages"."workspace_id" as "workspaceId",
|
||||
"workspace_pages"."page_id" as "docId",
|
||||
"workspace_pages"."mode" as "mode",
|
||||
"workspace_pages"."public" as "public",
|
||||
"workspace_pages"."defaultRole" as "defaultRole",
|
||||
"snapshots"."created_at" as "createdAt",
|
||||
"snapshots"."updated_at" as "updatedAt",
|
||||
"snapshots"."created_by" as "creatorId",
|
||||
"snapshots"."updated_by" as "lastUpdaterId"
|
||||
FROM "workspace_pages"
|
||||
INNER JOIN "snapshots"
|
||||
ON "workspace_pages"."workspace_id" = "snapshots"."workspace_id"
|
||||
AND "workspace_pages"."page_id" = "snapshots"."guid"
|
||||
WHERE
|
||||
"workspace_pages"."workspace_id" = ${workspaceId}
|
||||
AND "workspace_pages"."page_id" = ${docId}
|
||||
LIMIT 1;
|
||||
`;
|
||||
|
||||
return rows.at(0) ?? null;
|
||||
}
|
||||
|
||||
async paginateDocInfo(workspaceId: string, pagination: PaginationInput) {
|
||||
const count = await this.db.workspaceDoc.count({
|
||||
where: {
|
||||
workspaceId,
|
||||
},
|
||||
});
|
||||
|
||||
const after = pagination.after
|
||||
? Prisma.sql`AND "snapshots"."created_at" > ${new Date(pagination.after)}`
|
||||
: Prisma.sql``;
|
||||
|
||||
const rows = await this.db.$queryRaw<
|
||||
{
|
||||
workspaceId: string;
|
||||
docId: string;
|
||||
mode: PublicDocMode;
|
||||
public: boolean;
|
||||
defaultRole: DocRole;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
creatorId?: string;
|
||||
lastUpdaterId?: string;
|
||||
}[]
|
||||
>`
|
||||
SELECT
|
||||
"workspace_pages"."workspace_id" as "workspaceId",
|
||||
"workspace_pages"."page_id" as "docId",
|
||||
"workspace_pages"."mode" as "mode",
|
||||
"workspace_pages"."public" as "public",
|
||||
"workspace_pages"."defaultRole" as "defaultRole",
|
||||
"snapshots"."created_at" as "createdAt",
|
||||
"snapshots"."updated_at" as "updatedAt",
|
||||
"snapshots"."created_by" as "creatorId",
|
||||
"snapshots"."updated_by" as "lastUpdaterId"
|
||||
FROM "workspace_pages"
|
||||
INNER JOIN "snapshots"
|
||||
ON "workspace_pages"."workspace_id" = "snapshots"."workspace_id"
|
||||
AND "workspace_pages"."page_id" = "snapshots"."guid"
|
||||
WHERE
|
||||
"workspace_pages"."workspace_id" = ${workspaceId}
|
||||
${after}
|
||||
ORDER BY
|
||||
"snapshots"."created_at" ASC
|
||||
LIMIT ${pagination.first}
|
||||
OFFSET ${pagination.offset}
|
||||
`;
|
||||
|
||||
return [count, rows] as const;
|
||||
}
|
||||
// #endregion
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user