Skip to content

Commit

Permalink
refactor: remove get table full (#673)
Browse files Browse the repository at this point in the history
* refactor: remove the functionality of fetching table content (view, field, record) from the API

* chore: public convertProjection method in record.service

* fix: multiple socket connections and resolve readonly user role confusion after snapshot changes
  • Loading branch information
boris-w committed Jun 25, 2024
1 parent d6dc4fc commit a2f48f4
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 132 deletions.
2 changes: 1 addition & 1 deletion apps/nestjs-backend/src/features/record/record.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ export class RecordService {
return this.prismaService.txClient().$executeRawUnsafe(updateRecordSql);
}

private convertProjection(fieldKeys?: string[]) {
convertProjection(fieldKeys?: string[]) {
return fieldKeys?.reduce<Record<string, boolean>>((acc, cur) => {
acc[cur] = true;
return acc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import { Body, Controller, Delete, Get, Param, Post, Put, Query } from '@nestjs/common';
import type { ITableFullVo, ITableListVo, ITableVo } from '@teable/openapi';
import {
getTableQuerySchema,
IGetTableQuery,
tableRoSchema,
ICreateTableWithDefault,
dbTableNameRoSchema,
Expand Down Expand Up @@ -46,10 +44,9 @@ export class TableController {
@Get(':tableId')
async getTable(
@Param('baseId') baseId: string,
@Param('tableId') tableId: string,
@Query(new ZodValidationPipe(getTableQuerySchema)) query: IGetTableQuery
@Param('tableId') tableId: string
): Promise<ITableVo> {
return await this.tableOpenApiService.getTable(baseId, tableId, query);
return await this.tableOpenApiService.getTable(baseId, tableId);
}

@Permissions('table|read')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import type {
ICreateRecordsRo,
ICreateTableRo,
ICreateTableWithDefault,
IGetTableQuery,
ITableFullVo,
ITablePermissionVo,
ITableVo,
Expand Down Expand Up @@ -161,11 +160,7 @@ export class TableOpenApiService {
return await this.tableService.createTable(baseId, tableRo);
}

async getTable(baseId: string, tableId: string, query: IGetTableQuery): Promise<ITableVo> {
const { viewId, fieldKeyType, includeContent } = query;
if (includeContent) {
return await this.tableService.getFullTable(baseId, tableId, viewId, fieldKeyType);
}
async getTable(baseId: string, tableId: string): Promise<ITableVo> {
return await this.tableService.getTableMeta(baseId, tableId);
}

Expand Down
35 changes: 1 addition & 34 deletions apps/nestjs-backend/src/features/table/table.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { BadRequestException, Injectable, Logger, NotFoundException } from '@nestjs/common';
import type { IOtOperation, ISnapshotBase } from '@teable/core';
import {
FieldKeyType,
generateTableId,
getRandomString,
getUniqName,
Expand All @@ -10,7 +9,7 @@ import {
} from '@teable/core';
import type { Prisma } from '@teable/db-main-prisma';
import { PrismaService } from '@teable/db-main-prisma';
import type { ICreateTableRo, ITableFullVo, ITableVo } from '@teable/openapi';
import type { ICreateTableRo, ITableVo } from '@teable/openapi';
import { Knex } from 'knex';
import { InjectModel } from 'nest-knexjs';
import { ClsService } from 'nestjs-cls';
Expand All @@ -22,9 +21,6 @@ import type { IClsStore } from '../../types/cls';
import { convertNameToValidCharacter } from '../../utils/name-conversion';
import { Timing } from '../../utils/timing';
import { BatchService } from '../calculation/batch.service';
import { FieldService } from '../field/field.service';
import { RecordService } from '../record/record.service';
import { ViewService } from '../view/view.service';

@Injectable()
export class TableService implements IReadonlyAdapterService {
Expand All @@ -34,9 +30,6 @@ export class TableService implements IReadonlyAdapterService {
private readonly cls: ClsService<IClsStore>,
private readonly prismaService: PrismaService,
private readonly batchService: BatchService,
private readonly viewService: ViewService,
private readonly fieldService: FieldService,
private readonly recordService: RecordService,
@InjectDbProvider() private readonly dbProvider: IDbProvider,
@InjectModel('CUSTOM_KNEX') private readonly knex: Knex
) {}
Expand Down Expand Up @@ -198,32 +191,6 @@ export class TableService implements IReadonlyAdapterService {
};
}

async getFullTable(
baseId: string,
tableId: string,
viewId?: string,
fieldKeyType: FieldKeyType = FieldKeyType.Name
): Promise<ITableFullVo> {
const tableMeta = await this.getTableMeta(baseId, tableId);
const fields = await this.fieldService.getFieldsByQuery(tableId, { viewId });
const views = await this.viewService.getViews(tableId);
const { records } = await this.recordService.getRecords(tableId, {
viewId,
skip: 0,
take: 50,
fieldKeyType,
});

return {
...tableMeta,
description: tableMeta.description ?? undefined,
icon: tableMeta.icon ?? undefined,
fields,
views,
records,
};
}

async getDefaultViewId(tableId: string) {
const viewRaw = await this.prismaService.view.findFirst({
where: { tableId, deletedTime: null },
Expand Down
55 changes: 32 additions & 23 deletions apps/nestjs-backend/src/share-db/share-db.adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,21 @@ export class ShareDbAdapter extends ShareDb.DB {
callback: (error: any | null, ids: string[], extra?: any) => void
) {
try {
await this.cls.runWith(this.cls.get(), async () => {
this.cls.set('cookie', options.cookie);
this.cls.set('shareViewId', options.shareId);
const [docType, collectionId] = collection.split('_');

const queryResult = await this.getReadonlyService(docType as IdPrefix).getDocIdsByQuery(
collectionId,
query
);
callback(null, queryResult.ids, queryResult.extra);
});
await this.cls.runWith(
{
...this.cls.get(),
cookie: options.cookie,
shareViewId: options.shareId,
},
async () => {
const [docType, collectionId] = collection.split('_');
const queryResult = await this.getReadonlyService(docType as IdPrefix).getDocIdsByQuery(
collectionId,
query
);
callback(null, queryResult.ids, queryResult.extra);
}
);
} catch (e) {
this.logger.error(e);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -201,18 +205,23 @@ export class ShareDbAdapter extends ShareDb.DB {
options: any,
callback: (err: unknown, data?: Snapshot) => void
) {
await this.cls.runWith(this.cls.get(), async () => {
this.cls.set('cookie', options.agentCustom.cookie);
this.cls.set('shareViewId', options.agentCustom.shareId);
this.getSnapshotBulk(collection, [id], projection, options, (err, data) => {
if (err) {
callback(err);
} else {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
callback(null, data![id]);
}
});
});
await this.cls.runWith(
{
...this.cls.get(),
cookie: options.agentCustom.cookie,
shareViewId: options.agentCustom.shareId,
},
async () => {
this.getSnapshotBulk(collection, [id], projection, options, (err, data) => {
if (err) {
callback(err);
} else {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
callback(null, data![id]);
}
});
}
);
}

// Get operations between [from, to) non-inclusively. (Ie, the range should
Expand Down
15 changes: 3 additions & 12 deletions apps/nestjs-backend/test/table-import.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
notify as apiNotify,
analyzeFile as apiAnalyzeFile,
importTableFromFile as apiImportTableFromFile,
getTableById as apiGetTableById,
createBase as apiCreateBase,
createSpace as apiCreateSpace,
deleteBase as apiDeleteBase,
Expand All @@ -21,7 +20,7 @@ import {
import * as XLSX from 'xlsx';
import { CsvImporter } from '../src/features/import/open-api/import.class';

import { initApp, deleteTable } from './utils/init-app';
import { initApp, deleteTable, getTable as apiGetTableById } from './utils/init-app';

enum TestFileFormat {
'CSV' = 'csv',
Expand Down Expand Up @@ -265,17 +264,11 @@ describe('OpenAPI ImportController (e2e)', () => {
name: field.name,
}));

const res = await apiGetTableById(baseId, table.data[0].id, {
includeContent: true,
});
await apiGetTableById(baseId, table.data[0].id);

bases.push([baseId, id]);

expect(createdFields).toEqual(assertHeaders);
expect(res).toMatchObject({
status: 200,
statusText: 'OK',
});
}
);
});
Expand Down Expand Up @@ -349,9 +342,7 @@ describe('OpenAPI ImportController (e2e)', () => {

await delay(1000);

const {
data: { records },
} = await apiGetTableById(baseId, tableId, {
const { records } = await apiGetTableById(baseId, tableId, {
includeContent: true,
});

Expand Down
24 changes: 17 additions & 7 deletions apps/nestjs-backend/test/utils/init-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import type {
IRecordsVo,
IUpdateRecordRo,
ITableFullVo,
IGetTableQuery,
ITableVo,
} from '@teable/openapi';
import {
axios,
Expand Down Expand Up @@ -163,17 +161,29 @@ export async function deleteTable(baseId: string, tableId: string, expectStatus?
}
}

type IMakeOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;

export async function getTable(
baseId: string,
tableId: string,
query: IGetTableQuery = {}
): Promise<ITableVo> {
const result = await apiGetTableById(baseId, tableId, query);

query?: { includeContent?: boolean; viewId?: string }
): Promise<IMakeOptional<ITableFullVo, 'records' | 'views' | 'fields'>> {
const result = await apiGetTableById(baseId, tableId);
if (query?.includeContent) {
const { records } = await getRecords(tableId);
const fields = await getFields(tableId, query.viewId);
const views = await getViews(tableId);
return {
...result.data,
records,
views,
fields,
};
}
return result.data;
}

async function getCookie(email: string, password: string) {
export async function getCookie(email: string, password: string) {
const sessionResponse = await apiSignin({ email, password });
return {
access_token: sessionResponse.data,
Expand Down
9 changes: 4 additions & 5 deletions apps/nestjs-backend/test/view.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
import type { INestApplication } from '@nestjs/common';
import type { IColumn, IFieldRo, IFieldVo, IViewRo } from '@teable/core';
import { FieldType, Relationship, ViewType } from '@teable/core';
import type { ICreateTableRo, ITableFullVo, ITableVo } from '@teable/openapi';
import type { ICreateTableRo, ITableFullVo } from '@teable/openapi';
import {
updateViewDescription,
updateViewName,
getViewFilterLinkRecords,
getTableById,
updateViewShareMeta,
enableShareView,
} from '@teable/openapi';
Expand All @@ -21,6 +20,7 @@ import {
createTable,
getViews,
getView,
getTable,
} from './utils/init-app';

const defaultViews = [
Expand Down Expand Up @@ -140,7 +140,7 @@ describe('OpenAPI ViewController (e2e)', () => {
});

describe('/api/table/{tableId}/view/:viewId/filter-link-records (GET)', () => {
let table: ITableVo;
let table: ITableFullVo;
let linkTable1: ITableFullVo;
let linkTable2: ITableFullVo;

Expand Down Expand Up @@ -235,8 +235,7 @@ describe('OpenAPI ViewController (e2e)', () => {
records: linkTable2RecordRo,
});

const tableData = await getTableById(baseId, fullTable.id, { includeContent: true });
table = tableData.data;
table = (await getTable(baseId, fullTable.id, { includeContent: true })) as ITableFullVo;
});

afterAll(async () => {
Expand Down
35 changes: 29 additions & 6 deletions apps/nextjs-app/src/backend/api/rest/table.ssr.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IFieldVo, IRecord } from '@teable/core';
import type { IFieldVo, IGetFieldsQuery, IRecord, IViewVo } from '@teable/core';
import { FieldKeyType } from '@teable/core';
import type {
AcceptInvitationLinkRo,
Expand All @@ -11,17 +11,21 @@ import type {
ShareViewGetVo,
ITableFullVo,
ITableListVo,
IRecordsVo,
ITableVo,
} from '@teable/openapi';
import {
ACCEPT_INVITATION_LINK,
GET_BASE,
GET_BASE_ALL,
GET_DEFAULT_VIEW_ID,
GET_FIELD_LIST,
GET_RECORDS_URL,
GET_RECORD_URL,
GET_SPACE,
GET_TABLE,
GET_TABLE_LIST,
GET_VIEW_LIST,
SHARE_VIEW_GET,
SPACE_COLLABORATE_LIST,
UPDATE_NOTIFICATION_STATUS,
Expand All @@ -38,21 +42,40 @@ export class SsrApi {
this.axios = getAxios();
}

async getTable(baseId: string, tableId: string, viewId?: string) {
return this.axios
.get<ITableFullVo>(urlBuilder(GET_TABLE, { baseId, tableId }), {
async getTable(baseId: string, tableId: string, viewId?: string): Promise<ITableFullVo> {
const { records } = await this.axios
.get<IRecordsVo>(urlBuilder(GET_RECORDS_URL, { baseId, tableId }), {
params: {
viewId,
fieldKeyType: FieldKeyType.Id,
},
})
.then(({ data }) => data);

const fields = await this.getFields(tableId, { viewId });
const views = await this.axios
.get<IViewVo[]>(urlBuilder(GET_VIEW_LIST, { tableId }))
.then(({ data }) => data);
const table = await this.axios
.get<ITableVo>(urlBuilder(GET_TABLE, { baseId, tableId }), {
params: {
includeContent: true,
viewId,
fieldKeyType: FieldKeyType.Id,
},
})
.then(({ data }) => data);
return {
...table,
records,
views,
fields,
};
}

async getFields(tableId: string) {
async getFields(tableId: string, query?: IGetFieldsQuery) {
return this.axios
.get<IFieldVo[]>(urlBuilder(GET_FIELD_LIST, { tableId }))
.get<IFieldVo[]>(urlBuilder(GET_FIELD_LIST, { tableId }), { params: query })
.then(({ data }) => data);
}

Expand Down
Loading

0 comments on commit a2f48f4

Please sign in to comment.