Skip to content

Commit 4b2e41f

Browse files
committed
Add ordering and ordering_field to AIGeneratedTableSettings for enhanced table sorting
1 parent a14f1b6 commit 4b2e41f

1 file changed

Lines changed: 16 additions & 1 deletion

File tree

backend/src/entities/ai/ai.service.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Injectable } from '@nestjs/common';
22
import { WidgetTypeEnum } from '../../enums/widget-type.enum.js';
3+
import { QueryOrderingEnum } from '../../enums/query-ordering.enum.js';
34
import { checkFieldAutoincrement } from '../../helpers/check-field-autoincrement.js';
45
import { TableWidgetEntity } from '../widget/table-widget.entity.js';
56
import { TableInformation } from './ai-data-entities/types/ai-module-types.js';
@@ -12,6 +13,8 @@ interface AIGeneratedTableSettings {
1213
search_fields: string[];
1314
readonly_fields: string[];
1415
columns_view: string[];
16+
ordering: string;
17+
ordering_field: string;
1518
widgets: Array<{
1619
field_name: string;
1720
widget_type: string;
@@ -72,7 +75,9 @@ For each table, provide:
7275
2. search_fields: Columns that should be searchable (text fields like name, email, title)
7376
3. readonly_fields: Columns that should not be editable (like auto_increment, timestamps)
7477
4. columns_view: All columns in preferred display order
75-
5. widgets: For each column, suggest the best widget type from: ${widgetTypes}
78+
5. ordering: Default sort order - either "ASC" or "DESC" (use "DESC" for tables with timestamps to show newest first)
79+
6. ordering_field: Column name to sort by default (prefer created_at, updated_at, or primary key)
80+
7. widgets: For each column, suggest the best widget type from: ${widgetTypes}
7681
7782
Available widget types and when to use them:
7883
- Password: for password fields
@@ -111,6 +116,8 @@ Respond ONLY with valid JSON in this exact format (no markdown, no explanations)
111116
"search_fields": ["name", "email"],
112117
"readonly_fields": ["id", "created_at"],
113118
"columns_view": ["id", "name", "email", "created_at"],
119+
"ordering": "DESC",
120+
"ordering_field": "created_at",
114121
"widgets": [
115122
{
116123
"field_name": "column_name",
@@ -148,6 +155,8 @@ Respond ONLY with valid JSON in this exact format (no markdown, no explanations)
148155
settings.search_fields = this.filterValidColumns(tableSettings.search_fields, validColumnNames);
149156
settings.readonly_fields = this.filterValidColumns(tableSettings.readonly_fields, validColumnNames);
150157
settings.columns_view = this.filterValidColumns(tableSettings.columns_view, validColumnNames);
158+
settings.ordering = this.mapOrdering(tableSettings.ordering);
159+
settings.ordering_field = validColumnNames.includes(tableSettings.ordering_field) ? tableSettings.ordering_field : null;
151160
settings.table_widgets = tableSettings.widgets
152161
.filter((w) => validColumnNames.includes(w.field_name))
153162
.map((widgetData) => {
@@ -167,6 +176,12 @@ Respond ONLY with valid JSON in this exact format (no markdown, no explanations)
167176
return columns?.filter((col) => validColumnNames.includes(col)) || [];
168177
}
169178

179+
private mapOrdering(ordering: string): QueryOrderingEnum | null {
180+
if (ordering === 'ASC') return QueryOrderingEnum.ASC;
181+
if (ordering === 'DESC') return QueryOrderingEnum.DESC;
182+
return null;
183+
}
184+
170185
private mapWidgetType(widgetType: string): WidgetTypeEnum | undefined {
171186
const widgetTypeMap = new Map<string, WidgetTypeEnum>([
172187
['Password', WidgetTypeEnum.Password],

0 commit comments

Comments
 (0)