Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/src/common/data-injection.tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export enum UseCaseType {
FIND_TABLES_IN_CONNECTION_V2 = 'FIND_TABLES_IN_CONNECTION_V2',
GET_ALL_TABLE_ROWS = 'GET_ALL_TABLE_ROWS',
GET_TABLE_STRUCTURE = 'GET_TABLE_STRUCTURE',
GET_TABLE_STRUCTURE_WITHOUT_CACHE = 'GET_TABLE_STRUCTURE_WITHOUT_CACHE',
ADD_ROW_IN_TABLE = 'ADD_ROW_IN_TABLE',
UPDATE_ROW_IN_TABLE = 'UPDATE_ROW_IN_TABLE',
BULK_UPDATE_ROWS_IN_TABLE = 'BULK_UPDATE_ROWS_IN_TABLE',
Expand Down
39 changes: 39 additions & 0 deletions backend/src/entities/table/table.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import {
IGetRowByPrimaryKey,
IGetTableRows,
IGetTableStructure,
IGetTableStructureWithoutCache,
IImportCSVFinTable,
IUpdateRowInTable,
} from './use-cases/table-use-cases.interface.js';
Expand All @@ -90,6 +91,8 @@ export class TableController {
private readonly getTableRowsUseCase: IGetTableRows,
@Inject(UseCaseType.GET_TABLE_STRUCTURE)
private readonly getTableStructureUseCase: IGetTableStructure,
@Inject(UseCaseType.GET_TABLE_STRUCTURE_WITHOUT_CACHE)
private readonly getTableStructureWithoutCacheUseCase: IGetTableStructureWithoutCache,
@Inject(UseCaseType.ADD_ROW_IN_TABLE)
private readonly addRowInTableUseCase: IAddRowInTable,
@Inject(UseCaseType.UPDATE_ROW_IN_TABLE)
Expand Down Expand Up @@ -344,6 +347,42 @@ export class TableController {
return await this.getTableStructureUseCase.execute(inputData, InTransactionEnum.OFF);
}

@ApiOperation({
summary: 'Get table structure without cache. API+',
description:
'Return table structure fetched directly from the database, bypassing the structure cache. Support access with api key.',
})
@ApiResponse({
status: 200,
description: 'Table structure found.',
type: TableStructureDs,
})
@ApiQuery({ name: 'tableName', required: true })
@UseGuards(TableReadGuard)
@Get('/table/structure/no-cache/:connectionId')
async getTableStructureWithoutCache(
@QueryTableName() tableName: string,
@UserId() userId: string,
@SlugUuid('connectionId') connectionId: string,
@MasterPassword() masterPwd: string,
): Promise<TableStructureDs> {
if (!connectionId) {
throw new HttpException(
{
message: Messages.CONNECTION_ID_MISSING,
},
HttpStatus.BAD_REQUEST,
);
}
const inputData: GetTableStructureDs = {
connectionId: connectionId,
masterPwd: masterPwd,
tableName: tableName,
userId: userId,
};
return await this.getTableStructureWithoutCacheUseCase.execute(inputData, InTransactionEnum.OFF);
}

@ApiOperation({
summary: 'Add new row in table. API+',
description: 'Add new row in table. Support access with api key.',
Expand Down
6 changes: 6 additions & 0 deletions backend/src/entities/table/table.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { FindTablesInConnectionV2UseCase } from './use-cases/find-tables-in-conn
import { GetRowByPrimaryKeyUseCase } from './use-cases/get-row-by-primary-key.use.case.js';
import { GetTableRowsUseCase } from './use-cases/get-table-rows.use.case.js';
import { GetTableStructureUseCase } from './use-cases/get-table-structure.use.case.js';
import { GetTableStructureWithoutCacheUseCase } from './use-cases/get-table-structure-without-cache.use.case.js';
import { ImportCSVInTableUseCase } from './use-cases/import-csv-in-table-user.case.js';
import { UpdateRowInTableUseCase } from './use-cases/update-row-in-table.use.case.js';

Expand Down Expand Up @@ -67,6 +68,10 @@ import { UpdateRowInTableUseCase } from './use-cases/update-row-in-table.use.cas
provide: UseCaseType.GET_TABLE_STRUCTURE,
useClass: GetTableStructureUseCase,
},
{
provide: UseCaseType.GET_TABLE_STRUCTURE_WITHOUT_CACHE,
useClass: GetTableStructureWithoutCacheUseCase,
},
{
provide: UseCaseType.ADD_ROW_IN_TABLE,
useClass: AddRowInTableUseCase,
Expand Down Expand Up @@ -115,6 +120,7 @@ export class TableModule {
{ path: '/table/rows/:connectionId', method: RequestMethod.GET },
{ path: '/table/rows/find/:connectionId', method: RequestMethod.POST },
{ path: '/table/structure/:connectionId', method: RequestMethod.GET },
{ path: '/table/structure/no-cache/:connectionId', method: RequestMethod.GET },
Comment on lines 122 to +123
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

Fix route ordering to prevent path conflict.

The route /table/structure/no-cache/:connectionId (line 123) should be registered before /table/structure/:connectionId (line 122). With the current order, requests to /table/structure/no-cache/{id} will match the more generic pattern first, treating "no-cache" as a connection ID.

🔧 Proposed fix
 			.forRoutes(
 				{ path: '/table/columns/:connectionId', method: RequestMethod.GET },
 				{ path: '/table/rows/:connectionId', method: RequestMethod.GET },
 				{ path: '/table/:connectionId', method: RequestMethod.GET },
 				{ path: '/connection/tables/:connectionId', method: RequestMethod.GET },
 				{ path: '/connection/tables/v2/:connectionId', method: RequestMethod.GET },
 				{ path: '/table/rows/:connectionId', method: RequestMethod.GET },
 				{ path: '/table/rows/find/:connectionId', method: RequestMethod.POST },
-				{ path: '/table/structure/:connectionId', method: RequestMethod.GET },
 				{ path: '/table/structure/no-cache/:connectionId', method: RequestMethod.GET },
+				{ path: '/table/structure/:connectionId', method: RequestMethod.GET },
 				{ path: '/table/row/:connectionId', method: RequestMethod.POST },
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
{ path: '/table/structure/:connectionId', method: RequestMethod.GET },
{ path: '/table/structure/no-cache/:connectionId', method: RequestMethod.GET },
.forRoutes(
{ path: '/table/columns/:connectionId', method: RequestMethod.GET },
{ path: '/table/rows/:connectionId', method: RequestMethod.GET },
{ path: '/table/:connectionId', method: RequestMethod.GET },
{ path: '/connection/tables/:connectionId', method: RequestMethod.GET },
{ path: '/connection/tables/v2/:connectionId', method: RequestMethod.GET },
{ path: '/table/rows/:connectionId', method: RequestMethod.GET },
{ path: '/table/rows/find/:connectionId', method: RequestMethod.POST },
{ path: '/table/structure/no-cache/:connectionId', method: RequestMethod.GET },
{ path: '/table/structure/:connectionId', method: RequestMethod.GET },
{ path: '/table/row/:connectionId', method: RequestMethod.POST },
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@backend/src/entities/table/table.module.ts` around lines 122 - 123, The route
ordering in the routes array registers { path: '/table/structure/:connectionId',
method: RequestMethod.GET } before the more specific { path:
'/table/structure/no-cache/:connectionId', method: RequestMethod.GET }, causing
"no-cache" to be treated as a connectionId; fix this by swapping their order so
'/table/structure/no-cache/:connectionId' appears before
'/table/structure/:connectionId' in the module's route registrations (the array
containing these path entries) so the specific route matches first.

{ path: '/table/row/:connectionId', method: RequestMethod.POST },
{ path: '/table/row/:connectionId', method: RequestMethod.PUT },
{ path: '/table/row/:connectionId', method: RequestMethod.DELETE },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common';
import { getDataAccessObject } from '@rocketadmin/shared-code/dist/src/data-access-layer/shared/create-data-access-object.js';
import { ForeignKeyWithAutocompleteColumnsDS } from '@rocketadmin/shared-code/dist/src/data-access-layer/shared/data-structures/foreign-key-with-autocomplete-columns.ds.js';
import AbstractUseCase from '../../../common/abstract-use.case.js';
import { IGlobalDatabaseContext } from '../../../common/application/global-database-context.interface.js';
import { BaseType } from '../../../common/data-injection.tokens.js';
import { ExceptionOperations } from '../../../exceptions/custom-exceptions/exception-operation.js';
import { UnknownSQLException } from '../../../exceptions/custom-exceptions/unknown-sql-exception.js';
import { Messages } from '../../../exceptions/text/messages.js';
import { CedarPermissionsService } from '../../cedar-authorization/cedar-permissions.service.js';
import { buildFoundTableWidgetDs } from '../../widget/utils/build-found-table-widget-ds.js';
import { GetTableStructureDs } from '../application/data-structures/get-table-structure-ds.js';
import { TableStructureDs } from '../table-datastructures.js';
import { attachForeignColumnNames } from '../utils/attach-foreign-column-names.util.js';
import { extractForeignKeysFromWidgets } from '../utils/extract-foreign-keys-from-widgets.util.js';
import { filterForeignKeysByReadPermission } from '../utils/filter-foreign-keys-by-permission.util.js';
import { formFullTableStructure } from '../utils/form-full-table-structure.js';
import { getUserEmailForAgent, validateConnection } from '../utils/validate-connection.util.js';
import { IGetTableStructureWithoutCache } from './table-use-cases.interface.js';

@Injectable()
export class GetTableStructureWithoutCacheUseCase
extends AbstractUseCase<GetTableStructureDs, TableStructureDs>
implements IGetTableStructureWithoutCache
{
constructor(
@Inject(BaseType.GLOBAL_DB_CONTEXT)
protected _dbContext: IGlobalDatabaseContext,
private readonly cedarPermissions: CedarPermissionsService,
) {
super();
}

protected async implementation(inputData: GetTableStructureDs): Promise<TableStructureDs> {
const { connectionId, masterPwd, tableName, userId } = inputData;
const foundConnection = await this._dbContext.connectionRepository.findAndDecryptConnection(
connectionId,
masterPwd,
);
validateConnection(foundConnection);

try {
const dao = getDataAccessObject(foundConnection);
const foundTalesInConnection = await dao.getTablesFromDB();
if (!foundTalesInConnection.find((el) => el.tableName === tableName)) {
Comment on lines +44 to +45
throw new HttpException(
{
message: Messages.TABLE_NOT_FOUND,
},
HttpStatus.BAD_REQUEST,
);
}
const userEmail = await getUserEmailForAgent(foundConnection, userId, this._dbContext.userRepository);

// eslint-disable-next-line prefer-const
let [tableSettings, personalTableSettings, tablePrimaryColumns, tableForeignKeys, tableStructure, tableWidgets] =
await Promise.all([
this._dbContext.tableSettingsRepository.findTableSettings(connectionId, tableName),
this._dbContext.personalTableSettingsRepository.findUserTableSettings(userId, connectionId, tableName),
dao.getTablePrimaryColumns(tableName, userEmail),
dao.getTableForeignKeys(tableName, userEmail),
dao.getTableStructureWithoutCache(tableName, userEmail),
this._dbContext.tableWidgetsRepository.findTableWidgets(connectionId, tableName),
]);
const foreignKeysFromWidgets = extractForeignKeysFromWidgets(tableWidgets);
Comment on lines +53 to +65

tableForeignKeys = tableForeignKeys.concat(foreignKeysFromWidgets);
let transformedTableForeignKeys: Array<ForeignKeyWithAutocompleteColumnsDS> = [];
tableForeignKeys = await filterForeignKeysByReadPermission(
tableForeignKeys,
userId,
connectionId,
masterPwd,
this.cedarPermissions,
);

if (tableForeignKeys && tableForeignKeys.length > 0) {
transformedTableForeignKeys = await Promise.all(
tableForeignKeys.map((el) =>
attachForeignColumnNames(
el,
userEmail,
connectionId,
dao,
this._dbContext.tableSettingsRepository.findTableSettings.bind(this._dbContext.tableSettingsRepository),
).catch(() => el as ForeignKeyWithAutocompleteColumnsDS),
),
);
}
const readonly_fields = tableSettings?.readonly_fields?.length > 0 ? tableSettings.readonly_fields : [];
const formedTableStructure = formFullTableStructure(tableStructure, tableSettings);
return {
structure: formedTableStructure,
primaryColumns: tablePrimaryColumns,
foreignKeys: transformedTableForeignKeys,
readonly_fields: readonly_fields,
table_widgets: tableWidgets?.length > 0 ? tableWidgets.map((widget) => buildFoundTableWidgetDs(widget)) : [],
list_fields: personalTableSettings?.list_fields ? personalTableSettings.list_fields : [],
display_name: tableSettings?.display_name ? tableSettings.display_name : null,
excluded_fields: tableSettings?.excluded_fields ? tableSettings.excluded_fields : [],
};
} catch (e) {
if (e instanceof HttpException) {
throw e;
}
throw new UnknownSQLException(e.message, ExceptionOperations.FAILED_TO_GET_TABLE_STRUCTURE);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ export interface IGetTableStructure {
execute(inputData: GetTableStructureDs, inTransaction: InTransactionEnum): Promise<TableStructureDs>;
}

export interface IGetTableStructureWithoutCache {
execute(inputData: GetTableStructureDs, inTransaction: InTransactionEnum): Promise<TableStructureDs>;
}

export interface IAddRowInTable {
execute(inputData: AddRowInTableDs, inTransaction: InTransactionEnum): Promise<TableRowRODs>;
}
Expand Down
Loading
Loading