Skip to content

Commit bc62aa7

Browse files
fix[table-block]: fix filtering on built in timestamp columns
1 parent 6f514c1 commit bc62aa7

2 files changed

Lines changed: 34 additions & 4 deletions

File tree

apps/sim/app/api/table/utils.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,10 @@ async function checkTableAccess(tableId: string, userId: string): Promise<TableA
156156
return { hasAccess: true, table }
157157
}
158158

159-
return { hasAccess: false, reason: 'User does not have access to this table' }
159+
return {
160+
hasAccess: false,
161+
reason: 'User does not have access to this table',
162+
}
160163
}
161164

162165
/**
@@ -175,7 +178,10 @@ async function checkTableWriteAccess(tableId: string, userId: string): Promise<T
175178
return { hasAccess: true, table }
176179
}
177180

178-
return { hasAccess: false, reason: 'User does not have write access to this table' }
181+
return {
182+
hasAccess: false,
183+
reason: 'User does not have write access to this table',
184+
}
179185
}
180186

181187
/**
@@ -281,3 +287,10 @@ export function normalizeColumn(col: ColumnDefinition): ColumnDefinition {
281287
...(col.workflowGroupId ? { workflowGroupId: col.workflowGroupId } : {}),
282288
}
283289
}
290+
291+
export function isBuiltInDateField(field: string): boolean {
292+
if (field === 'created_at' || field === 'updated_at') {
293+
return true
294+
}
295+
return false
296+
}

apps/sim/lib/table/sql.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
JsonValue,
1818
Sort,
1919
} from '@/lib/table/types'
20+
import { isBuiltInDateField } from '../../app/api/table/utils'
2021

2122
/**
2223
* Error thrown when caller-supplied filter or sort input is malformed.
@@ -380,7 +381,9 @@ function buildFieldCondition(
380381

381382
case '$ncontains':
382383
conditions.push(
383-
buildLikeClause(tableName, field, value as string, 'contains', { negate: true })
384+
buildLikeClause(tableName, field, value as string, 'contains', {
385+
negate: true,
386+
})
384387
)
385388
break
386389

@@ -458,8 +461,15 @@ function buildLogicalClause(
458461

459462
/** Builds JSONB containment clause: `data @> '{"field": value}'::jsonb` (uses GIN index) */
460463
function buildContainmentClause(tableName: string, field: string, value: JsonValue): SQL {
464+
if (isBuiltInDateField(field)) {
465+
return sql`${sql.raw(`${tableName}.${field}`)} = ${value}::timestamptz`
466+
}
467+
461468
const jsonObj = JSON.stringify({ [field]: value })
462469
return sql`${sql.raw(`${tableName}.data`)} @> ${jsonObj}::jsonb`
470+
471+
// const jsonObj = JSON.stringify({ [field]: value })
472+
// return sql`${sql.raw(`${tableName}.data`)} @> ${jsonObj}::jsonb`
463473
}
464474

465475
/**
@@ -485,10 +495,17 @@ function buildComparisonClause(
485495
value: number | string,
486496
columnType: ColumnType | undefined
487497
): SQL {
498+
if (isBuiltInDateField(field)) {
499+
columnType = 'date'
500+
}
501+
488502
const escapedField = field.replace(/'/g, "''")
489503
const cast = jsonbCastForType(columnType) ?? 'numeric'
490504
validateComparisonValue(field, columnType, cast, value)
491-
const cell = sql.raw(`(${tableName}.data->>'${escapedField}')::${cast}`)
505+
506+
const cell = isBuiltInDateField(field)
507+
? sql.raw(`${tableName}.${field}`)
508+
: sql.raw(`(${tableName}.data->>'${escapedField}')::${cast}`)
492509
return cast === 'timestamptz'
493510
? sql`${cell} ${sql.raw(operator)} ${value}::timestamptz`
494511
: sql`${cell} ${sql.raw(operator)} ${value}`

0 commit comments

Comments
 (0)