mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-10 22:56:24 +08:00
062537c2cf
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced comprehensive management for workspace blobs, including creating, updating, soft and permanent deletion, and retrieval of blobs. - Added the ability to list all blobs, list deleted blobs, and calculate the total size of blobs within a workspace. - **Tests** - Added extensive automated tests to validate blob creation, updating, deletion, retrieval, and aggregation functionalities. <!-- end of auto-generated comment: release notes by coderabbit.ai --> #### PR Dependency Tree * **PR #12894** 👈 * **PR #12897** This tree was auto-generated by [Charcoal](https://github.com/danerwilliams/charcoal)
102 lines
1.9 KiB
TypeScript
102 lines
1.9 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { Prisma } from '@prisma/client';
|
|
|
|
import { BaseModel } from './base';
|
|
|
|
export type CreateBlobInput = Prisma.BlobUncheckedCreateInput;
|
|
|
|
/**
|
|
* Blob Model
|
|
*/
|
|
@Injectable()
|
|
export class BlobModel extends BaseModel {
|
|
async upsert(blob: CreateBlobInput) {
|
|
return await this.db.blob.upsert({
|
|
where: {
|
|
workspaceId_key: {
|
|
workspaceId: blob.workspaceId,
|
|
key: blob.key,
|
|
},
|
|
},
|
|
update: {
|
|
mime: blob.mime,
|
|
size: blob.size,
|
|
},
|
|
create: {
|
|
workspaceId: blob.workspaceId,
|
|
key: blob.key,
|
|
mime: blob.mime,
|
|
size: blob.size,
|
|
},
|
|
});
|
|
}
|
|
|
|
async delete(workspaceId: string, key: string, permanently = false) {
|
|
if (permanently) {
|
|
await this.db.blob.deleteMany({
|
|
where: {
|
|
workspaceId,
|
|
key,
|
|
},
|
|
});
|
|
this.logger.log(`deleted blob ${workspaceId}/${key} permanently`);
|
|
return;
|
|
}
|
|
|
|
await this.db.blob.update({
|
|
where: {
|
|
workspaceId_key: {
|
|
workspaceId,
|
|
key,
|
|
},
|
|
},
|
|
data: {
|
|
deletedAt: new Date(),
|
|
},
|
|
});
|
|
}
|
|
|
|
async get(workspaceId: string, key: string) {
|
|
return await this.db.blob.findUnique({
|
|
where: {
|
|
workspaceId_key: {
|
|
workspaceId,
|
|
key,
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
async list(workspaceId: string) {
|
|
return await this.db.blob.findMany({
|
|
where: {
|
|
workspaceId,
|
|
deletedAt: null,
|
|
},
|
|
});
|
|
}
|
|
|
|
async listDeleted(workspaceId: string) {
|
|
return await this.db.blob.findMany({
|
|
where: {
|
|
workspaceId,
|
|
deletedAt: { not: null },
|
|
},
|
|
});
|
|
}
|
|
|
|
async totalSize(workspaceId: string) {
|
|
const sum = await this.db.blob.aggregate({
|
|
where: {
|
|
workspaceId,
|
|
deletedAt: null,
|
|
},
|
|
_sum: {
|
|
size: true,
|
|
},
|
|
});
|
|
|
|
return sum._sum.size ?? 0;
|
|
}
|
|
}
|