mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-13 21:05:19 +00: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(
|
||||
updatedSpy.calledOnceWith({
|
||||
workspaceId: workspace.id,
|
||||
count: 1,
|
||||
count: 2,
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { Inject, Logger } from '@nestjs/common';
|
||||
import { TransactionHost } from '@nestjs-cls/transactional';
|
||||
import type { TransactionalAdapterPrisma } from '@nestjs-cls/transactional-adapter-prisma';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
import { Config } from '../base';
|
||||
import type { Models } from '.';
|
||||
@@ -16,13 +15,12 @@ export class BaseModel {
|
||||
@Inject(Config)
|
||||
protected readonly config!: Config;
|
||||
|
||||
@Inject(PrismaClient)
|
||||
protected readonly db!: PrismaClient;
|
||||
|
||||
@Inject(TransactionHost)
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ export class FeatureModel extends BaseModel {
|
||||
|
||||
let feature: Feature;
|
||||
if (!latest) {
|
||||
feature = await this.tx.feature.create({
|
||||
feature = await this.db.feature.create({
|
||||
data: {
|
||||
type: FeatureType.Feature,
|
||||
feature: name,
|
||||
@@ -47,7 +47,7 @@ export class FeatureModel extends BaseModel {
|
||||
},
|
||||
});
|
||||
} else {
|
||||
feature = await this.tx.feature.update({
|
||||
feature = await this.db.feature.update({
|
||||
where: { id: latest.id },
|
||||
data: {
|
||||
configs: parsedConfigs,
|
||||
@@ -66,7 +66,7 @@ export class FeatureModel extends BaseModel {
|
||||
* @internal
|
||||
*/
|
||||
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 },
|
||||
orderBy: { version: 'desc' },
|
||||
});
|
||||
|
||||
@@ -109,7 +109,7 @@ export class PageModel extends BaseModel {
|
||||
if (!data || data.type !== permission) {
|
||||
if (data) {
|
||||
// Update the permission
|
||||
data = await this.tx.workspacePageUserPermission.update({
|
||||
data = await this.db.workspacePageUserPermission.update({
|
||||
where: {
|
||||
workspaceId_pageId_userId: {
|
||||
workspaceId,
|
||||
@@ -121,7 +121,7 @@ export class PageModel extends BaseModel {
|
||||
});
|
||||
} else {
|
||||
// Create a new permission
|
||||
data = await this.tx.workspacePageUserPermission.create({
|
||||
data = await this.db.workspacePageUserPermission.create({
|
||||
data: {
|
||||
workspaceId,
|
||||
pageId,
|
||||
@@ -135,7 +135,7 @@ export class PageModel extends BaseModel {
|
||||
|
||||
// If the new permission is owner, we need to revoke old owner
|
||||
if (permission === Permission.Owner) {
|
||||
await this.tx.workspacePageUserPermission.updateMany({
|
||||
await this.db.workspacePageUserPermission.updateMany({
|
||||
where: {
|
||||
workspaceId,
|
||||
pageId,
|
||||
|
||||
@@ -68,7 +68,7 @@ export class UserFeatureModel extends BaseModel {
|
||||
async add(userId: string, featureName: UserFeatureName, reason: string) {
|
||||
const feature = await this.models.feature.get_unchecked(featureName);
|
||||
|
||||
const existing = await this.tx.userFeature.findFirst({
|
||||
const existing = await this.db.userFeature.findFirst({
|
||||
where: {
|
||||
userId,
|
||||
featureId: feature.id,
|
||||
@@ -80,7 +80,7 @@ export class UserFeatureModel extends BaseModel {
|
||||
return existing;
|
||||
}
|
||||
|
||||
const userFeature = await this.tx.userFeature.create({
|
||||
const userFeature = await this.db.userFeature.create({
|
||||
data: {
|
||||
userId,
|
||||
featureId: feature.id,
|
||||
@@ -97,7 +97,7 @@ export class UserFeatureModel extends BaseModel {
|
||||
async remove(userId: string, featureName: UserFeatureName) {
|
||||
const feature = await this.models.feature.get_unchecked(featureName);
|
||||
|
||||
await this.tx.userFeature.deleteMany({
|
||||
await this.db.userFeature.deleteMany({
|
||||
where: {
|
||||
userId,
|
||||
featureId: feature.id,
|
||||
|
||||
@@ -9,7 +9,7 @@ export class WorkspaceFeatureModel extends BaseModel {
|
||||
async get<T extends WorkspaceFeatureName>(workspaceId: string, name: T) {
|
||||
const feature = await this.models.feature.get_unchecked(name);
|
||||
|
||||
const workspaceFeature = await this.tx.workspaceFeature.findFirst({
|
||||
const workspaceFeature = await this.db.workspaceFeature.findFirst({
|
||||
where: {
|
||||
workspaceId,
|
||||
featureId: feature.id,
|
||||
@@ -69,7 +69,7 @@ export class WorkspaceFeatureModel extends BaseModel {
|
||||
) {
|
||||
const feature = await this.models.feature.get_unchecked(featureName);
|
||||
|
||||
const existing = await this.tx.workspaceFeature.findFirst({
|
||||
const existing = await this.db.workspaceFeature.findFirst({
|
||||
where: {
|
||||
workspaceId,
|
||||
featureId: feature.id,
|
||||
@@ -99,7 +99,7 @@ export class WorkspaceFeatureModel extends BaseModel {
|
||||
|
||||
let workspaceFeature;
|
||||
if (existing) {
|
||||
workspaceFeature = await this.tx.workspaceFeature.update({
|
||||
workspaceFeature = await this.db.workspaceFeature.update({
|
||||
where: {
|
||||
id: existing.id,
|
||||
},
|
||||
@@ -109,7 +109,7 @@ export class WorkspaceFeatureModel extends BaseModel {
|
||||
},
|
||||
});
|
||||
} else {
|
||||
workspaceFeature = await this.tx.workspaceFeature.create({
|
||||
workspaceFeature = await this.db.workspaceFeature.create({
|
||||
data: {
|
||||
workspaceId,
|
||||
featureId: feature.id,
|
||||
@@ -130,7 +130,7 @@ export class WorkspaceFeatureModel extends BaseModel {
|
||||
async remove(workspaceId: string, featureName: WorkspaceFeatureName) {
|
||||
const feature = await this.models.feature.get_unchecked(featureName);
|
||||
|
||||
await this.tx.workspaceFeature.deleteMany({
|
||||
await this.db.workspaceFeature.deleteMany({
|
||||
where: {
|
||||
workspaceId,
|
||||
featureId: feature.id,
|
||||
|
||||
@@ -38,7 +38,7 @@ export class WorkspaceModel extends BaseModel {
|
||||
* Create a new workspace for the user, default to private.
|
||||
*/
|
||||
async create(userId: string) {
|
||||
const workspace = await this.tx.workspace.create({
|
||||
const workspace = await this.db.workspace.create({
|
||||
data: {
|
||||
public: false,
|
||||
permissions: {
|
||||
@@ -59,7 +59,7 @@ export class WorkspaceModel extends BaseModel {
|
||||
* Update the workspace with the given data.
|
||||
*/
|
||||
async update(workspaceId: string, data: UpdateWorkspaceInput) {
|
||||
await this.tx.workspace.update({
|
||||
await this.db.workspace.update({
|
||||
where: {
|
||||
id: workspaceId,
|
||||
},
|
||||
@@ -79,7 +79,7 @@ export class WorkspaceModel extends BaseModel {
|
||||
}
|
||||
|
||||
async delete(workspaceId: string) {
|
||||
await this.tx.workspace.deleteMany({
|
||||
await this.db.workspace.deleteMany({
|
||||
where: {
|
||||
id: workspaceId,
|
||||
},
|
||||
@@ -133,7 +133,7 @@ export class WorkspaceModel extends BaseModel {
|
||||
permission: Permission = Permission.Read,
|
||||
status: WorkspaceMemberStatus = WorkspaceMemberStatus.Pending
|
||||
): Promise<WorkspaceUserPermission> {
|
||||
const data = await this.tx.workspaceUserPermission.findUnique({
|
||||
const data = await this.db.workspaceUserPermission.findUnique({
|
||||
where: {
|
||||
workspaceId_userId: {
|
||||
workspaceId,
|
||||
@@ -145,7 +145,7 @@ export class WorkspaceModel extends BaseModel {
|
||||
if (!data) {
|
||||
// Create a new permission
|
||||
// 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: {
|
||||
workspaceId,
|
||||
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 (data.status === WorkspaceMemberStatus.Accepted || data.accepted) {
|
||||
const updated = await this.tx.workspaceUserPermission.update({
|
||||
const updated = await this.db.workspaceUserPermission.update({
|
||||
where: {
|
||||
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 (permission === Permission.Owner) {
|
||||
await this.tx.workspaceUserPermission.updateMany({
|
||||
await this.db.workspaceUserPermission.updateMany({
|
||||
where: {
|
||||
workspaceId,
|
||||
type: Permission.Owner,
|
||||
@@ -188,7 +188,7 @@ export class WorkspaceModel extends BaseModel {
|
||||
// If the user is not accepted, we can update the status directly
|
||||
const allowedStatus = this.getAllowedStatusSource(data.status);
|
||||
if (allowedStatus.includes(status)) {
|
||||
const updated = await this.tx.workspaceUserPermission.update({
|
||||
const updated = await this.db.workspaceUserPermission.update({
|
||||
where: { workspaceId_userId: { workspaceId, userId } },
|
||||
data: {
|
||||
status,
|
||||
@@ -207,7 +207,7 @@ export class WorkspaceModel extends BaseModel {
|
||||
* Get the workspace member invitation.
|
||||
*/
|
||||
async getMemberInvitation(invitationId: string) {
|
||||
return await this.tx.workspaceUserPermission.findUnique({
|
||||
return await this.db.workspaceUserPermission.findUnique({
|
||||
where: {
|
||||
id: invitationId,
|
||||
},
|
||||
@@ -223,7 +223,7 @@ export class WorkspaceModel extends BaseModel {
|
||||
workspaceId: string,
|
||||
status: WorkspaceMemberStatus = WorkspaceMemberStatus.Accepted
|
||||
) {
|
||||
const { count } = await this.tx.workspaceUserPermission.updateMany({
|
||||
const { count } = await this.db.workspaceUserPermission.updateMany({
|
||||
where: {
|
||||
id: invitationId,
|
||||
workspaceId: workspaceId,
|
||||
@@ -351,7 +351,7 @@ export class WorkspaceModel extends BaseModel {
|
||||
return false;
|
||||
}
|
||||
|
||||
await this.tx.workspaceUserPermission.deleteMany({
|
||||
await this.db.workspaceUserPermission.deleteMany({
|
||||
where: {
|
||||
workspaceId,
|
||||
userId,
|
||||
@@ -418,7 +418,7 @@ export class WorkspaceModel extends BaseModel {
|
||||
return;
|
||||
}
|
||||
|
||||
const members = await this.tx.workspaceUserPermission.findMany({
|
||||
const members = await this.db.workspaceUserPermission.findMany({
|
||||
select: { id: true, status: true },
|
||||
where: {
|
||||
workspaceId,
|
||||
@@ -439,7 +439,7 @@ export class WorkspaceModel extends BaseModel {
|
||||
const toPendings = groups.NeedMoreSeat;
|
||||
if (toPendings) {
|
||||
// NeedMoreSeat => Pending
|
||||
await this.tx.workspaceUserPermission.updateMany({
|
||||
await this.db.workspaceUserPermission.updateMany({
|
||||
where: { id: { in: toPendings.map(m => m.id) } },
|
||||
data: { status: WorkspaceMemberStatus.Pending },
|
||||
});
|
||||
@@ -448,7 +448,7 @@ export class WorkspaceModel extends BaseModel {
|
||||
const toUnderReviews = groups.NeedMoreSeatAndReview;
|
||||
if (toUnderReviews) {
|
||||
// NeedMoreSeatAndReview => UnderReview
|
||||
await this.tx.workspaceUserPermission.updateMany({
|
||||
await this.db.workspaceUserPermission.updateMany({
|
||||
where: { id: { in: toUnderReviews.map(m => m.id) } },
|
||||
data: { status: WorkspaceMemberStatus.UnderReview },
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user