Skip to content

Commit

Permalink
fix: record history functionality in SQLite database (#873)
Browse files Browse the repository at this point in the history
* fix: record history functionality in SQLite database

* fix: the error issue when dragging Kanban cards to an empty stack

* chore: remove transcation
  • Loading branch information
Sky-FE authored Sep 1, 2024
1 parent fbec49a commit b39c912
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Injectable } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
import type { ISelectFieldOptions } from '@teable/core';
import { FieldType } from '@teable/core';
import type { Prisma, Field } from '@teable/db-main-prisma';
import { FieldType, generateRecordHistoryId } from '@teable/core';
import { PrismaService } from '@teable/db-main-prisma';
import type { Field } from '@teable/db-main-prisma';
import { Knex } from 'knex';
import { isString } from 'lodash';
import { InjectModel } from 'nest-knexjs';
import { Events, RecordUpdateEvent } from '../events';

// eslint-disable-next-line @typescript-eslint/naming-convention
const SELECT_FIELD_TYPE_SET = new Set([FieldType.SingleSelect, FieldType.MultipleSelect]);

@Injectable()
export class RecordHistoryListener {
constructor(private readonly prismaService: PrismaService) {}
constructor(
private readonly prismaService: PrismaService,
@InjectModel('CUSTOM_KNEX') private readonly knex: Knex
) {}

@OnEvent(Events.TABLE_RECORD_UPDATE, { async: true })
async recordUpdateListener(event: RecordUpdateEvent) {
const { payload, context } = event;
const { user } = context;
const { tableId, oldField: _oldField } = payload;
let records = payload.record;
const userId = user?.id;

if (!Array.isArray(records)) {
records = [records];
}
const payloadRecord = payload.record;
const records = !Array.isArray(payloadRecord) ? [payloadRecord] : payloadRecord;

const fieldIdSet = new Set<string>();

Expand Down Expand Up @@ -63,7 +66,15 @@ export class RecordHistoryListener {

for (let i = 0; i < totalCount; i += batchSize) {
const batch = records.slice(i, i + batchSize);
const recordHistoryList: Prisma.RecordHistoryCreateManyInput[] = [];
const recordHistoryList: {
id: string;
table_id: string;
record_id: string;
field_id: string;
before: string;
after: string;
created_by: string;
}[] = [];

batch.forEach((record) => {
const { id: recordId, fields } = record;
Expand All @@ -82,9 +93,10 @@ export class RecordHistoryListener {
}

recordHistoryList.push({
tableId,
recordId,
fieldId,
id: generateRecordHistoryId(),
table_id: tableId,
record_id: recordId,
field_id: fieldId,
before: JSON.stringify({
meta: {
type: oldField.type,
Expand All @@ -103,14 +115,14 @@ export class RecordHistoryListener {
},
data: newValue,
}),
createdBy: userId as string,
created_by: userId as string,
});
});
});

await this.prismaService.recordHistory.createMany({
data: recordHistoryList,
});
const query = this.knex.insert(recordHistoryList).into('record_history').toQuery();

await this.prismaService.$executeRawUnsafe(query);
}
}

Expand Down
2 changes: 1 addition & 1 deletion apps/nestjs-backend/test/record.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ describe('OpenAPI RecordController (e2e)', () => {
});
});

describe.skipIf(globalThis.testConfig.driver === DriverClient.Sqlite)('record history', () => {
describe('record history', () => {
let mainTable: ITableFullVo;
let foreignTable: ITableFullVo;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,16 @@ export const KanbanContainer = () => {
};

// Drag a card to the end of another stack
if (targetCardId == null && targetIndex !== 0) {
const lastTargetCardId = targetCards?.[targetIndex - 1]?.id;
if (lastTargetCardId != null) {
recordRo.order = {
viewId,
anchorId: lastTargetCardId,
position: 'after',
};
if (targetCardId == null) {
if (targetIndex !== 0) {
const lastTargetCardId = targetCards?.[targetIndex - 1]?.id;
if (lastTargetCardId != null) {
recordRo.order = {
viewId,
anchorId: lastTargetCardId,
position: 'after',
};
}
}
} else {
recordRo.order = {
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/utils/id-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export enum IdPrefix {
OAuthClient = 'clt',

Window = 'win',

RecordHistory = 'rhi',
}

const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
Expand Down Expand Up @@ -146,3 +148,7 @@ export function generateLicenseId() {
export function generateClientId() {
return IdPrefix.OAuthClient + getRandomString(16).toLocaleLowerCase();
}

export function generateRecordHistoryId() {
return IdPrefix.RecordHistory + getRandomString(24);
}

0 comments on commit b39c912

Please sign in to comment.