MCPcopy
hub / github.com/simstudioai/sim / buildSortClause

Function buildSortClause

apps/sim/lib/table/sql.ts:206–228  ·  view source on GitHub ↗
(
  sort: Sort,
  tableName: string,
  columns: ColumnDefinition[]
)

Source from the content-addressed store, hash-verified

204 * // Returns: ORDER BY (user_table_rows.data->>'salary')::numeric DESC NULLS LAST
205 */
206export function buildSortClause(
207 sort: Sort,
208 tableName: string,
209 columns: ColumnDefinition[]
210): SQL | undefined {
211 const clauses: SQL[] = []
212 const columnTypeMap = buildColumnTypeMap(columns)
213
214 for (const [field, direction] of Object.entries(sort)) {
215 validateFieldName(field)
216
217 if (direction !== 'asc' && direction !== 'desc') {
218 throw new TableQueryValidationError(
219 `Invalid sort direction "${direction}". Must be "asc" or "desc".`
220 )
221 }
222
223 const columnType = columnTypeMap.get(field)
224 clauses.push(buildSortFieldClause(tableName, field, direction, columnType))
225 }
226
227 return clauses.length > 0 ? sql.join(clauses, sql.raw(', ')) : undefined
228}
229
230/**
231 * Validates a field name to prevent SQL injection.

Callers 2

buildRowOrderBySqlFunction · 0.90
sql.test.tsFile · 0.90

Calls 6

buildColumnTypeMapFunction · 0.85
buildSortFieldClauseFunction · 0.85
joinMethod · 0.80
validateFieldNameFunction · 0.70
getMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected