mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-09-22 19:53:48 +08:00
refactor(server): rename tx to db (#9867)
This commit is contained in:
@@ -326,7 +326,7 @@ test('should grant member with read permission and Pending status by default', a
|
|||||||
t.true(
|
t.true(
|
||||||
updatedSpy.calledOnceWith({
|
updatedSpy.calledOnceWith({
|
||||||
workspaceId: workspace.id,
|
workspaceId: workspace.id,
|
||||||
count: 1,
|
count: 2,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { Inject, Logger } from '@nestjs/common';
|
import { Inject, Logger } from '@nestjs/common';
|
||||||
import { TransactionHost } from '@nestjs-cls/transactional';
|
import { TransactionHost } from '@nestjs-cls/transactional';
|
||||||
import type { TransactionalAdapterPrisma } from '@nestjs-cls/transactional-adapter-prisma';
|
import type { TransactionalAdapterPrisma } from '@nestjs-cls/transactional-adapter-prisma';
|
||||||
import { PrismaClient } from '@prisma/client';
|
|
||||||
|
|
||||||
import { Config } from '../base';
|
import { Config } from '../base';
|
||||||
import type { Models } from '.';
|
import type { Models } from '.';
|
||||||
@@ -16,13 +15,12 @@ export class BaseModel {
|
|||||||
@Inject(Config)
|
@Inject(Config)
|
||||||
protected readonly config!: Config;
|
protected readonly config!: Config;
|
||||||
|
|
||||||
@Inject(PrismaClient)
|
|
||||||
protected readonly db!: PrismaClient;
|
|
||||||
|
|
||||||
@Inject(TransactionHost)
|
@Inject(TransactionHost)
|
||||||
private readonly txHost!: TransactionHost<TransactionalAdapterPrisma>;
|
private readonly txHost!: TransactionHost<TransactionalAdapterPrisma>;
|
||||||
|
|
||||||
protected get tx() {
|
protected get db() {
|
||||||
|
// When a transaction is not active, the Transaction instance refers to the default non-transactional instance.
|
||||||
|
// See https://papooch.github.io/nestjs-cls/plugins/available-plugins/transactional#using-the-injecttransaction-decorator
|
||||||
return this.txHost.tx;
|
return this.txHost.tx;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ export class FeatureModel extends BaseModel {
|
|||||||
|
|
||||||
let feature: Feature;
|
let feature: Feature;
|
||||||
if (!latest) {
|
if (!latest) {
|
||||||
feature = await this.tx.feature.create({
|
feature = await this.db.feature.create({
|
||||||
data: {
|
data: {
|
||||||
type: FeatureType.Feature,
|
type: FeatureType.Feature,
|
||||||
feature: name,
|
feature: name,
|
||||||
@@ -47,7 +47,7 @@ export class FeatureModel extends BaseModel {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
feature = await this.tx.feature.update({
|
feature = await this.db.feature.update({
|
||||||
where: { id: latest.id },
|
where: { id: latest.id },
|
||||||
data: {
|
data: {
|
||||||
configs: parsedConfigs,
|
configs: parsedConfigs,
|
||||||
@@ -66,7 +66,7 @@ export class FeatureModel extends BaseModel {
|
|||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
async try_get_unchecked<T extends FeatureName>(name: T) {
|
async try_get_unchecked<T extends FeatureName>(name: T) {
|
||||||
const feature = await this.tx.feature.findFirst({
|
const feature = await this.db.feature.findFirst({
|
||||||
where: { feature: name },
|
where: { feature: name },
|
||||||
orderBy: { version: 'desc' },
|
orderBy: { version: 'desc' },
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -109,7 +109,7 @@ export class PageModel extends BaseModel {
|
|||||||
if (!data || data.type !== permission) {
|
if (!data || data.type !== permission) {
|
||||||
if (data) {
|
if (data) {
|
||||||
// Update the permission
|
// Update the permission
|
||||||
data = await this.tx.workspacePageUserPermission.update({
|
data = await this.db.workspacePageUserPermission.update({
|
||||||
where: {
|
where: {
|
||||||
workspaceId_pageId_userId: {
|
workspaceId_pageId_userId: {
|
||||||
workspaceId,
|
workspaceId,
|
||||||
@@ -121,7 +121,7 @@ export class PageModel extends BaseModel {
|
|||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// Create a new permission
|
// Create a new permission
|
||||||
data = await this.tx.workspacePageUserPermission.create({
|
data = await this.db.workspacePageUserPermission.create({
|
||||||
data: {
|
data: {
|
||||||
workspaceId,
|
workspaceId,
|
||||||
pageId,
|
pageId,
|
||||||
@@ -135,7 +135,7 @@ export class PageModel extends BaseModel {
|
|||||||
|
|
||||||
// If the new permission is owner, we need to revoke old owner
|
// If the new permission is owner, we need to revoke old owner
|
||||||
if (permission === Permission.Owner) {
|
if (permission === Permission.Owner) {
|
||||||
await this.tx.workspacePageUserPermission.updateMany({
|
await this.db.workspacePageUserPermission.updateMany({
|
||||||
where: {
|
where: {
|
||||||
workspaceId,
|
workspaceId,
|
||||||
pageId,
|
pageId,
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ export class UserFeatureModel extends BaseModel {
|
|||||||
async add(userId: string, featureName: UserFeatureName, reason: string) {
|
async add(userId: string, featureName: UserFeatureName, reason: string) {
|
||||||
const feature = await this.models.feature.get_unchecked(featureName);
|
const feature = await this.models.feature.get_unchecked(featureName);
|
||||||
|
|
||||||
const existing = await this.tx.userFeature.findFirst({
|
const existing = await this.db.userFeature.findFirst({
|
||||||
where: {
|
where: {
|
||||||
userId,
|
userId,
|
||||||
featureId: feature.id,
|
featureId: feature.id,
|
||||||
@@ -80,7 +80,7 @@ export class UserFeatureModel extends BaseModel {
|
|||||||
return existing;
|
return existing;
|
||||||
}
|
}
|
||||||
|
|
||||||
const userFeature = await this.tx.userFeature.create({
|
const userFeature = await this.db.userFeature.create({
|
||||||
data: {
|
data: {
|
||||||
userId,
|
userId,
|
||||||
featureId: feature.id,
|
featureId: feature.id,
|
||||||
@@ -97,7 +97,7 @@ export class UserFeatureModel extends BaseModel {
|
|||||||
async remove(userId: string, featureName: UserFeatureName) {
|
async remove(userId: string, featureName: UserFeatureName) {
|
||||||
const feature = await this.models.feature.get_unchecked(featureName);
|
const feature = await this.models.feature.get_unchecked(featureName);
|
||||||
|
|
||||||
await this.tx.userFeature.deleteMany({
|
await this.db.userFeature.deleteMany({
|
||||||
where: {
|
where: {
|
||||||
userId,
|
userId,
|
||||||
featureId: feature.id,
|
featureId: feature.id,
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ export class WorkspaceFeatureModel extends BaseModel {
|
|||||||
async get<T extends WorkspaceFeatureName>(workspaceId: string, name: T) {
|
async get<T extends WorkspaceFeatureName>(workspaceId: string, name: T) {
|
||||||
const feature = await this.models.feature.get_unchecked(name);
|
const feature = await this.models.feature.get_unchecked(name);
|
||||||
|
|
||||||
const workspaceFeature = await this.tx.workspaceFeature.findFirst({
|
const workspaceFeature = await this.db.workspaceFeature.findFirst({
|
||||||
where: {
|
where: {
|
||||||
workspaceId,
|
workspaceId,
|
||||||
featureId: feature.id,
|
featureId: feature.id,
|
||||||
@@ -69,7 +69,7 @@ export class WorkspaceFeatureModel extends BaseModel {
|
|||||||
) {
|
) {
|
||||||
const feature = await this.models.feature.get_unchecked(featureName);
|
const feature = await this.models.feature.get_unchecked(featureName);
|
||||||
|
|
||||||
const existing = await this.tx.workspaceFeature.findFirst({
|
const existing = await this.db.workspaceFeature.findFirst({
|
||||||
where: {
|
where: {
|
||||||
workspaceId,
|
workspaceId,
|
||||||
featureId: feature.id,
|
featureId: feature.id,
|
||||||
@@ -99,7 +99,7 @@ export class WorkspaceFeatureModel extends BaseModel {
|
|||||||
|
|
||||||
let workspaceFeature;
|
let workspaceFeature;
|
||||||
if (existing) {
|
if (existing) {
|
||||||
workspaceFeature = await this.tx.workspaceFeature.update({
|
workspaceFeature = await this.db.workspaceFeature.update({
|
||||||
where: {
|
where: {
|
||||||
id: existing.id,
|
id: existing.id,
|
||||||
},
|
},
|
||||||
@@ -109,7 +109,7 @@ export class WorkspaceFeatureModel extends BaseModel {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
workspaceFeature = await this.tx.workspaceFeature.create({
|
workspaceFeature = await this.db.workspaceFeature.create({
|
||||||
data: {
|
data: {
|
||||||
workspaceId,
|
workspaceId,
|
||||||
featureId: feature.id,
|
featureId: feature.id,
|
||||||
@@ -130,7 +130,7 @@ export class WorkspaceFeatureModel extends BaseModel {
|
|||||||
async remove(workspaceId: string, featureName: WorkspaceFeatureName) {
|
async remove(workspaceId: string, featureName: WorkspaceFeatureName) {
|
||||||
const feature = await this.models.feature.get_unchecked(featureName);
|
const feature = await this.models.feature.get_unchecked(featureName);
|
||||||
|
|
||||||
await this.tx.workspaceFeature.deleteMany({
|
await this.db.workspaceFeature.deleteMany({
|
||||||
where: {
|
where: {
|
||||||
workspaceId,
|
workspaceId,
|
||||||
featureId: feature.id,
|
featureId: feature.id,
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ export class WorkspaceModel extends BaseModel {
|
|||||||
* Create a new workspace for the user, default to private.
|
* Create a new workspace for the user, default to private.
|
||||||
*/
|
*/
|
||||||
async create(userId: string) {
|
async create(userId: string) {
|
||||||
const workspace = await this.tx.workspace.create({
|
const workspace = await this.db.workspace.create({
|
||||||
data: {
|
data: {
|
||||||
public: false,
|
public: false,
|
||||||
permissions: {
|
permissions: {
|
||||||
@@ -59,7 +59,7 @@ export class WorkspaceModel extends BaseModel {
|
|||||||
* Update the workspace with the given data.
|
* Update the workspace with the given data.
|
||||||
*/
|
*/
|
||||||
async update(workspaceId: string, data: UpdateWorkspaceInput) {
|
async update(workspaceId: string, data: UpdateWorkspaceInput) {
|
||||||
await this.tx.workspace.update({
|
await this.db.workspace.update({
|
||||||
where: {
|
where: {
|
||||||
id: workspaceId,
|
id: workspaceId,
|
||||||
},
|
},
|
||||||
@@ -79,7 +79,7 @@ export class WorkspaceModel extends BaseModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async delete(workspaceId: string) {
|
async delete(workspaceId: string) {
|
||||||
await this.tx.workspace.deleteMany({
|
await this.db.workspace.deleteMany({
|
||||||
where: {
|
where: {
|
||||||
id: workspaceId,
|
id: workspaceId,
|
||||||
},
|
},
|
||||||
@@ -133,7 +133,7 @@ export class WorkspaceModel extends BaseModel {
|
|||||||
permission: Permission = Permission.Read,
|
permission: Permission = Permission.Read,
|
||||||
status: WorkspaceMemberStatus = WorkspaceMemberStatus.Pending
|
status: WorkspaceMemberStatus = WorkspaceMemberStatus.Pending
|
||||||
): Promise<WorkspaceUserPermission> {
|
): Promise<WorkspaceUserPermission> {
|
||||||
const data = await this.tx.workspaceUserPermission.findUnique({
|
const data = await this.db.workspaceUserPermission.findUnique({
|
||||||
where: {
|
where: {
|
||||||
workspaceId_userId: {
|
workspaceId_userId: {
|
||||||
workspaceId,
|
workspaceId,
|
||||||
@@ -145,7 +145,7 @@ export class WorkspaceModel extends BaseModel {
|
|||||||
if (!data) {
|
if (!data) {
|
||||||
// Create a new permission
|
// Create a new permission
|
||||||
// TODO(fengmk2): should we check the permission here? Like owner can't be pending?
|
// TODO(fengmk2): should we check the permission here? Like owner can't be pending?
|
||||||
const created = await this.tx.workspaceUserPermission.create({
|
const created = await this.db.workspaceUserPermission.create({
|
||||||
data: {
|
data: {
|
||||||
workspaceId,
|
workspaceId,
|
||||||
userId,
|
userId,
|
||||||
@@ -162,7 +162,7 @@ export class WorkspaceModel extends BaseModel {
|
|||||||
|
|
||||||
// If the user is already accepted and the new permission is owner, we need to revoke old owner
|
// If the user is already accepted and the new permission is owner, we need to revoke old owner
|
||||||
if (data.status === WorkspaceMemberStatus.Accepted || data.accepted) {
|
if (data.status === WorkspaceMemberStatus.Accepted || data.accepted) {
|
||||||
const updated = await this.tx.workspaceUserPermission.update({
|
const updated = await this.db.workspaceUserPermission.update({
|
||||||
where: {
|
where: {
|
||||||
workspaceId_userId: { workspaceId, userId },
|
workspaceId_userId: { workspaceId, userId },
|
||||||
},
|
},
|
||||||
@@ -170,7 +170,7 @@ export class WorkspaceModel extends BaseModel {
|
|||||||
});
|
});
|
||||||
// If the new permission is owner, we need to revoke old owner
|
// If the new permission is owner, we need to revoke old owner
|
||||||
if (permission === Permission.Owner) {
|
if (permission === Permission.Owner) {
|
||||||
await this.tx.workspaceUserPermission.updateMany({
|
await this.db.workspaceUserPermission.updateMany({
|
||||||
where: {
|
where: {
|
||||||
workspaceId,
|
workspaceId,
|
||||||
type: Permission.Owner,
|
type: Permission.Owner,
|
||||||
@@ -188,7 +188,7 @@ export class WorkspaceModel extends BaseModel {
|
|||||||
// If the user is not accepted, we can update the status directly
|
// If the user is not accepted, we can update the status directly
|
||||||
const allowedStatus = this.getAllowedStatusSource(data.status);
|
const allowedStatus = this.getAllowedStatusSource(data.status);
|
||||||
if (allowedStatus.includes(status)) {
|
if (allowedStatus.includes(status)) {
|
||||||
const updated = await this.tx.workspaceUserPermission.update({
|
const updated = await this.db.workspaceUserPermission.update({
|
||||||
where: { workspaceId_userId: { workspaceId, userId } },
|
where: { workspaceId_userId: { workspaceId, userId } },
|
||||||
data: {
|
data: {
|
||||||
status,
|
status,
|
||||||
@@ -207,7 +207,7 @@ export class WorkspaceModel extends BaseModel {
|
|||||||
* Get the workspace member invitation.
|
* Get the workspace member invitation.
|
||||||
*/
|
*/
|
||||||
async getMemberInvitation(invitationId: string) {
|
async getMemberInvitation(invitationId: string) {
|
||||||
return await this.tx.workspaceUserPermission.findUnique({
|
return await this.db.workspaceUserPermission.findUnique({
|
||||||
where: {
|
where: {
|
||||||
id: invitationId,
|
id: invitationId,
|
||||||
},
|
},
|
||||||
@@ -223,7 +223,7 @@ export class WorkspaceModel extends BaseModel {
|
|||||||
workspaceId: string,
|
workspaceId: string,
|
||||||
status: WorkspaceMemberStatus = WorkspaceMemberStatus.Accepted
|
status: WorkspaceMemberStatus = WorkspaceMemberStatus.Accepted
|
||||||
) {
|
) {
|
||||||
const { count } = await this.tx.workspaceUserPermission.updateMany({
|
const { count } = await this.db.workspaceUserPermission.updateMany({
|
||||||
where: {
|
where: {
|
||||||
id: invitationId,
|
id: invitationId,
|
||||||
workspaceId: workspaceId,
|
workspaceId: workspaceId,
|
||||||
@@ -351,7 +351,7 @@ export class WorkspaceModel extends BaseModel {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.tx.workspaceUserPermission.deleteMany({
|
await this.db.workspaceUserPermission.deleteMany({
|
||||||
where: {
|
where: {
|
||||||
workspaceId,
|
workspaceId,
|
||||||
userId,
|
userId,
|
||||||
@@ -418,7 +418,7 @@ export class WorkspaceModel extends BaseModel {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const members = await this.tx.workspaceUserPermission.findMany({
|
const members = await this.db.workspaceUserPermission.findMany({
|
||||||
select: { id: true, status: true },
|
select: { id: true, status: true },
|
||||||
where: {
|
where: {
|
||||||
workspaceId,
|
workspaceId,
|
||||||
@@ -439,7 +439,7 @@ export class WorkspaceModel extends BaseModel {
|
|||||||
const toPendings = groups.NeedMoreSeat;
|
const toPendings = groups.NeedMoreSeat;
|
||||||
if (toPendings) {
|
if (toPendings) {
|
||||||
// NeedMoreSeat => Pending
|
// NeedMoreSeat => Pending
|
||||||
await this.tx.workspaceUserPermission.updateMany({
|
await this.db.workspaceUserPermission.updateMany({
|
||||||
where: { id: { in: toPendings.map(m => m.id) } },
|
where: { id: { in: toPendings.map(m => m.id) } },
|
||||||
data: { status: WorkspaceMemberStatus.Pending },
|
data: { status: WorkspaceMemberStatus.Pending },
|
||||||
});
|
});
|
||||||
@@ -448,7 +448,7 @@ export class WorkspaceModel extends BaseModel {
|
|||||||
const toUnderReviews = groups.NeedMoreSeatAndReview;
|
const toUnderReviews = groups.NeedMoreSeatAndReview;
|
||||||
if (toUnderReviews) {
|
if (toUnderReviews) {
|
||||||
// NeedMoreSeatAndReview => UnderReview
|
// NeedMoreSeatAndReview => UnderReview
|
||||||
await this.tx.workspaceUserPermission.updateMany({
|
await this.db.workspaceUserPermission.updateMany({
|
||||||
where: { id: { in: toUnderReviews.map(m => m.id) } },
|
where: { id: { in: toUnderReviews.map(m => m.id) } },
|
||||||
data: { status: WorkspaceMemberStatus.UnderReview },
|
data: { status: WorkspaceMemberStatus.UnderReview },
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user