mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-12 23:56:36 +08:00
refactor(server): merge PageModel into DocModel (#10592)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Transactional } from '@nestjs-cls/transactional';
|
||||
import type { Update } from '@prisma/client';
|
||||
import { Prisma } from '@prisma/client';
|
||||
|
||||
import { BaseModel } from './base';
|
||||
import type { Doc, DocEditor } from './common';
|
||||
@@ -31,6 +32,11 @@ export interface DocHistoryFilter {
|
||||
take?: number;
|
||||
}
|
||||
|
||||
export type DocMetaUpsertInput = Omit<
|
||||
Prisma.WorkspaceDocUncheckedCreateInput,
|
||||
'workspaceId' | 'docId'
|
||||
>;
|
||||
|
||||
/**
|
||||
* Workspace Doc Model
|
||||
*
|
||||
@@ -38,6 +44,7 @@ export interface DocHistoryFilter {
|
||||
* - Updates: the changes made to the doc.
|
||||
* - History: the doc history of the doc.
|
||||
* - Doc: the doc itself.
|
||||
* - DocMeta: the doc meta.
|
||||
*/
|
||||
@Injectable()
|
||||
export class DocModel extends BaseModel {
|
||||
@@ -338,7 +345,7 @@ export class DocModel extends BaseModel {
|
||||
};
|
||||
}
|
||||
|
||||
async getMeta(workspaceId: string, docId: string) {
|
||||
async getAuthors(workspaceId: string, docId: string) {
|
||||
return await this.db.snapshot.findUnique({
|
||||
where: {
|
||||
workspaceId_id: {
|
||||
@@ -461,4 +468,78 @@ export class DocModel extends BaseModel {
|
||||
}
|
||||
|
||||
// #endregion
|
||||
|
||||
// #region DocMeta
|
||||
|
||||
/**
|
||||
* Create or update the doc meta.
|
||||
*/
|
||||
async upsertMeta(
|
||||
workspaceId: string,
|
||||
docId: string,
|
||||
data?: DocMetaUpsertInput
|
||||
) {
|
||||
return await this.db.workspaceDoc.upsert({
|
||||
where: {
|
||||
workspaceId_docId: {
|
||||
workspaceId,
|
||||
docId,
|
||||
},
|
||||
},
|
||||
update: {
|
||||
...data,
|
||||
},
|
||||
create: {
|
||||
...data,
|
||||
workspaceId,
|
||||
docId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the doc meta.
|
||||
*/
|
||||
async getMeta<Select extends Prisma.WorkspaceDocSelect>(
|
||||
workspaceId: string,
|
||||
docId: string,
|
||||
options?: {
|
||||
select?: Select;
|
||||
}
|
||||
) {
|
||||
return (await this.db.workspaceDoc.findUnique({
|
||||
where: {
|
||||
workspaceId_docId: {
|
||||
workspaceId,
|
||||
docId,
|
||||
},
|
||||
},
|
||||
select: options?.select,
|
||||
})) as Prisma.WorkspaceDocGetPayload<{ select: Select }> | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the workspace public doc metas.
|
||||
*/
|
||||
async findPublics(workspaceId: string) {
|
||||
return await this.db.workspaceDoc.findMany({
|
||||
where: {
|
||||
workspaceId,
|
||||
public: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the workspace public docs count.
|
||||
*/
|
||||
async getPublicsCount(workspaceId: string) {
|
||||
return await this.db.workspaceDoc.count({
|
||||
where: {
|
||||
workspaceId,
|
||||
public: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
// #endregion
|
||||
}
|
||||
|
||||
@@ -10,7 +10,6 @@ import { ApplyType } from '../base';
|
||||
import { DocModel } from './doc';
|
||||
import { DocUserModel } from './doc-user';
|
||||
import { FeatureModel } from './feature';
|
||||
import { PageModel } from './page';
|
||||
import { MODELS_SYMBOL } from './provider';
|
||||
import { SessionModel } from './session';
|
||||
import { UserModel } from './user';
|
||||
@@ -27,7 +26,6 @@ const MODELS = {
|
||||
verificationToken: VerificationTokenModel,
|
||||
feature: FeatureModel,
|
||||
workspace: WorkspaceModel,
|
||||
page: PageModel,
|
||||
userFeature: UserFeatureModel,
|
||||
workspaceFeature: WorkspaceFeatureModel,
|
||||
doc: DocModel,
|
||||
@@ -89,7 +87,6 @@ export * from './common';
|
||||
export * from './doc';
|
||||
export * from './doc-user';
|
||||
export * from './feature';
|
||||
export * from './page';
|
||||
export * from './session';
|
||||
export * from './user';
|
||||
export * from './user-doc';
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { type WorkspaceDoc as Page } from '@prisma/client';
|
||||
|
||||
import { BaseModel } from './base';
|
||||
import { PublicDocMode } from './common';
|
||||
export type { Page };
|
||||
export type UpdatePageInput = {
|
||||
mode?: PublicDocMode;
|
||||
public?: boolean;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class PageModel extends BaseModel {
|
||||
// #region page
|
||||
|
||||
/**
|
||||
* Create or update the page.
|
||||
*/
|
||||
async upsert(workspaceId: string, docId: string, data?: UpdatePageInput) {
|
||||
return await this.db.workspaceDoc.upsert({
|
||||
where: {
|
||||
workspaceId_docId: {
|
||||
workspaceId,
|
||||
docId,
|
||||
},
|
||||
},
|
||||
update: {
|
||||
...data,
|
||||
},
|
||||
create: {
|
||||
...data,
|
||||
workspaceId,
|
||||
docId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the page.
|
||||
* @param isPublic: if true, only return the public page. If false, only return the private page.
|
||||
* If not set, return public or private both.
|
||||
*/
|
||||
async get(workspaceId: string, docId: string, isPublic?: boolean) {
|
||||
return await this.db.workspaceDoc.findUnique({
|
||||
where: {
|
||||
workspaceId_docId: {
|
||||
workspaceId,
|
||||
docId,
|
||||
},
|
||||
public: isPublic,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the workspace public pages.
|
||||
*/
|
||||
async findPublics(workspaceId: string) {
|
||||
return await this.db.workspaceDoc.findMany({
|
||||
where: {
|
||||
workspaceId,
|
||||
public: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the workspace public pages count.
|
||||
*/
|
||||
async getPublicsCount(workspaceId: string) {
|
||||
return await this.db.workspaceDoc.count({
|
||||
where: {
|
||||
workspaceId,
|
||||
public: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// #endregion
|
||||
}
|
||||
Reference in New Issue
Block a user