11import { Injectable } from '@nestjs/common' ;
22import { WidgetTypeEnum } from '../../enums/widget-type.enum.js' ;
3+ import { QueryOrderingEnum } from '../../enums/query-ordering.enum.js' ;
34import { checkFieldAutoincrement } from '../../helpers/check-field-autoincrement.js' ;
45import { TableWidgetEntity } from '../widget/table-widget.entity.js' ;
56import { 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:
72752. search_fields: Columns that should be searchable (text fields like name, email, title)
73763. readonly_fields: Columns that should not be editable (like auto_increment, timestamps)
74774. 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
7782Available 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