-
-
Notifications
You must be signed in to change notification settings - Fork 631
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: table trash * chore: db migration * chore: linting types * fix: e2e testing
- Loading branch information
Showing
44 changed files
with
1,448 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
apps/nestjs-backend/src/features/trash/listener/table-trash.listener.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { OnEvent } from '@nestjs/event-emitter'; | ||
import { generateRecordTrashId } from '@teable/core'; | ||
import { PrismaService } from '@teable/db-main-prisma'; | ||
import { ResourceType } from '@teable/openapi'; | ||
import { Knex } from 'knex'; | ||
import { InjectModel } from 'nest-knexjs'; | ||
import { IThresholdConfig, ThresholdConfig } from '../../../configs/threshold.config'; | ||
import { Events } from '../../../event-emitter/events'; | ||
import { IDeleteFieldsPayload } from '../../undo-redo/operations/delete-fields.operation'; | ||
import { IDeleteRecordsPayload } from '../../undo-redo/operations/delete-records.operation'; | ||
import { IDeleteViewPayload } from '../../undo-redo/operations/delete-view.operation'; | ||
|
||
@Injectable() | ||
export class TableTrashListener { | ||
constructor( | ||
private readonly prismaService: PrismaService, | ||
@InjectModel('CUSTOM_KNEX') private readonly knex: Knex, | ||
@ThresholdConfig() private readonly thresholdConfig: IThresholdConfig | ||
) {} | ||
|
||
@OnEvent(Events.OPERATION_RECORDS_DELETE, { async: true }) | ||
async recordDeleteListener(payload: IDeleteRecordsPayload) { | ||
const { operationId, userId, tableId, records } = payload; | ||
|
||
if (!operationId) return; | ||
|
||
const recordIds = records.map((record) => record.id); | ||
|
||
await this.prismaService.$tx( | ||
async (prisma) => { | ||
await prisma.tableTrash.create({ | ||
data: { | ||
id: operationId, | ||
tableId, | ||
createdBy: userId, | ||
resourceType: ResourceType.Record, | ||
snapshot: JSON.stringify(recordIds), | ||
}, | ||
}); | ||
|
||
const batchSize = 5000; | ||
for (let i = 0; i < records.length; i += batchSize) { | ||
const batch = records.slice(i, i + batchSize); | ||
const recordTrashData = batch.map((record) => ({ | ||
id: generateRecordTrashId(), | ||
table_id: tableId, | ||
record_id: record.id, | ||
snapshot: JSON.stringify(record), | ||
created_by: userId, | ||
})); | ||
|
||
const query = this.knex.insert(recordTrashData).into('record_trash').toQuery(); | ||
await prisma.$executeRawUnsafe(query); | ||
} | ||
}, | ||
{ | ||
timeout: this.thresholdConfig.bigTransactionTimeout, | ||
} | ||
); | ||
} | ||
|
||
@OnEvent(Events.OPERATION_FIELDS_DELETE, { async: true }) | ||
async fieldDeleteListener(payload: IDeleteFieldsPayload) { | ||
const { userId, tableId, fields, records, operationId } = payload; | ||
|
||
if (!operationId) return; | ||
|
||
await this.prismaService.tableTrash.create({ | ||
data: { | ||
id: operationId, | ||
tableId, | ||
createdBy: userId, | ||
resourceType: ResourceType.Field, | ||
snapshot: JSON.stringify({ fields, records }), | ||
}, | ||
}); | ||
} | ||
|
||
@OnEvent(Events.OPERATION_VIEW_DELETE, { async: true }) | ||
async viewDeleteListener(payload: IDeleteViewPayload) { | ||
const { operationId, tableId, viewId, userId } = payload; | ||
|
||
if (!operationId) return; | ||
|
||
await this.prismaService.tableTrash.create({ | ||
data: { | ||
id: operationId, | ||
tableId, | ||
createdBy: userId, | ||
resourceType: ResourceType.View, | ||
snapshot: JSON.stringify([viewId]), | ||
}, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,25 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { BaseModule } from '../base/base.module'; | ||
import { FieldOpenApiModule } from '../field/open-api/field-open-api.module'; | ||
import { RecordOpenApiModule } from '../record/open-api/record-open-api.module'; | ||
import { TableOpenApiModule } from '../table/open-api/table-open-api.module'; | ||
import { UserModule } from '../user/user.module'; | ||
import { ViewModule } from '../view/view.module'; | ||
import { TableTrashListener } from './listener/table-trash.listener'; | ||
import { TrashController } from './trash.controller'; | ||
import { TrashService } from './trash.service'; | ||
|
||
@Module({ | ||
imports: [UserModule, BaseModule, TableOpenApiModule], | ||
imports: [ | ||
UserModule, | ||
BaseModule, | ||
TableOpenApiModule, | ||
FieldOpenApiModule, | ||
RecordOpenApiModule, | ||
ViewModule, | ||
], | ||
controllers: [TrashController], | ||
providers: [TrashService], | ||
providers: [TrashService, TableTrashListener], | ||
exports: [TrashService], | ||
}) | ||
export class TrashModule {} |
Oops, something went wrong.