Skip to content

Commit cef4ec5

Browse files
committed
refactor: extract table creation logic for improved readability
1 parent 3e00879 commit cef4ec5

File tree

5 files changed

+40
-37
lines changed

5 files changed

+40
-37
lines changed

apps/nestjs-backend/src/features/base-node/base-node.service.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import type {
2121
IDuplicateTableRo,
2222
ICreateDashboardRo,
2323
ICreateFolderNodeRo,
24-
ICreateTableWithDefault,
2524
IDuplicateDashboardRo,
2625
IUpdateBaseNodeRo,
2726
IBaseNodePresenceFlushPayload,
2827
IBaseNodeResourceMeta,
2928
IBaseNodeResourceMetaWithId,
29+
ICreateTableRo,
3030
} from '@teable/openapi';
3131
import { BaseNodeResourceType } from '@teable/openapi';
3232
import { Knex } from 'knex';
@@ -68,6 +68,7 @@ import type { IClsStore } from '../../types/cls';
6868
import { updateOrder } from '../../utils/update-order';
6969
import { DashboardService } from '../dashboard/dashboard.service';
7070
import { TableOpenApiService } from '../table/open-api/table-open-api.service';
71+
import { prepareCreateTableRo } from '../table/open-api/table.pipe.helper';
7172
import { TableDuplicateService } from '../table/table-duplicate.service';
7273
import { BaseNodeFolderService } from './folder/base-node-folder.service';
7374

@@ -461,10 +462,9 @@ export class BaseNodeService {
461462
return { id: folder.id, name: folder.name };
462463
}
463464
case BaseNodeResourceType.Table: {
464-
const table = await this.tableOpenApiService.createTable(
465-
baseId,
466-
ro as ICreateTableWithDefault
467-
);
465+
const preparedRo = prepareCreateTableRo(ro as ICreateTableRo);
466+
const table = await this.tableOpenApiService.createTable(baseId, preparedRo);
467+
468468
return {
469469
id: table.id,
470470
name: table.name,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { IFieldVo } from '@teable/core';
2+
import { HttpErrorCode, PRIMARY_SUPPORTED_TYPES } from '@teable/core';
3+
import type { ICreateTableRo, ICreateTableWithDefault } from '@teable/openapi';
4+
import { CustomHttpException } from '../../../custom.exception';
5+
import { DEFAULT_FIELDS, DEFAULT_VIEWS, DEFAULT_RECORD_DATA } from '../constant';
6+
7+
export const prepareCreateTableRo = (tableRo: ICreateTableRo): ICreateTableWithDefault => {
8+
const fieldRos = tableRo.fields && tableRo.fields.length ? tableRo.fields : DEFAULT_FIELDS;
9+
// make sure first field to be the primary field;
10+
(fieldRos[0] as IFieldVo).isPrimary = true;
11+
if (!PRIMARY_SUPPORTED_TYPES.has(fieldRos[0].type)) {
12+
throw new CustomHttpException(
13+
`Field type ${fieldRos[0].type} is not supported as primary field`,
14+
HttpErrorCode.VALIDATION_ERROR,
15+
{
16+
localization: {
17+
i18nKey: 'httpErrors.field.primaryFieldNotSupported',
18+
},
19+
}
20+
);
21+
}
22+
23+
return {
24+
...tableRo,
25+
fields: fieldRos,
26+
views: tableRo.views && tableRo.views.length ? tableRo.views : DEFAULT_VIEWS,
27+
records: tableRo.records ? tableRo.records : DEFAULT_RECORD_DATA,
28+
};
29+
};
Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,11 @@
11
import type { ArgumentMetadata, PipeTransform } from '@nestjs/common';
22
import { Injectable } from '@nestjs/common';
3-
import { HttpErrorCode, PRIMARY_SUPPORTED_TYPES, type IFieldVo } from '@teable/core';
43
import type { ICreateTableRo } from '@teable/openapi';
5-
import { CustomHttpException } from '../../../custom.exception';
6-
import { DEFAULT_FIELDS, DEFAULT_RECORD_DATA, DEFAULT_VIEWS } from '../constant';
4+
import { prepareCreateTableRo } from './table.pipe.helper';
75

86
@Injectable()
97
export class TablePipe implements PipeTransform {
108
async transform(value: ICreateTableRo, _metadata: ArgumentMetadata) {
11-
return this.prepareDefaultRo(value);
12-
}
13-
14-
async prepareDefaultRo(tableRo: ICreateTableRo): Promise<ICreateTableRo> {
15-
const fieldRos = tableRo.fields && tableRo.fields.length ? tableRo.fields : DEFAULT_FIELDS;
16-
// make sure first field to be the primary field;
17-
(fieldRos[0] as IFieldVo).isPrimary = true;
18-
if (!PRIMARY_SUPPORTED_TYPES.has(fieldRos[0].type)) {
19-
throw new CustomHttpException(
20-
`Field type ${fieldRos[0].type} is not supported as primary field`,
21-
HttpErrorCode.VALIDATION_ERROR,
22-
{
23-
localization: {
24-
i18nKey: 'httpErrors.field.primaryFieldNotSupported',
25-
},
26-
}
27-
);
28-
}
29-
30-
return {
31-
...tableRo,
32-
fields: fieldRos,
33-
views: tableRo.views && tableRo.views.length ? tableRo.views : DEFAULT_VIEWS,
34-
records: tableRo.records ? tableRo.records : DEFAULT_RECORD_DATA,
35-
};
9+
return prepareCreateTableRo(value);
3610
}
3711
}

apps/nextjs-app/src/features/app/blocks/base/base-side-bar/BaseNodeTree.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,8 @@ export const BaseNodeTree = () => {
434434
}, [treeItems]);
435435

436436
useEffect(() => {
437-
if (Object.keys(treeItemsRef.current).length === 0) return;
438-
const nodes = Object.values(treeItemsRef.current);
437+
if (Object.keys(treeItems).length === 0) return;
438+
const nodes = Object.values(treeItems);
439439
const { resourceType, resourceId } = parseNodeUrl({ baseId, url: urlPath, urlParams }) ?? {};
440440
const node = nodes.find(
441441
(node) => node.resourceType === resourceType && node.resourceId === resourceId
@@ -452,7 +452,7 @@ export const BaseNodeTree = () => {
452452
parentResourceId: baseId,
453453
});
454454
}
455-
}, [urlPath, urlParams, baseId, updateUserLastVisit]);
455+
}, [treeItems, urlPath, urlParams, baseId, updateUserLastVisit]);
456456

457457
useEffect(() => {
458458
if (!Object.keys(treeItems).length) return;

packages/openapi/src/base-node/create.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const CREATE_BASE_NODE = '/base/{baseId}/node';
1212
const createBaseNodeSchema = z.object({
1313
resourceType: z.nativeEnum(BaseNodeResourceType),
1414
parentId: z.string().nullable().optional(),
15-
name: z.string().trim(),
15+
name: z.string().trim().min(1),
1616
});
1717

1818
export type ICreateBaseNode = z.infer<typeof createBaseNodeSchema>;

0 commit comments

Comments
 (0)