( filter: Filter, tableName: string, columnTypeMap: ColumnTypeMap )
| 124 | } |
| 125 | |
| 126 | function buildFilterClauseInternal( |
| 127 | filter: Filter, |
| 128 | tableName: string, |
| 129 | columnTypeMap: ColumnTypeMap |
| 130 | ): SQL | undefined { |
| 131 | const conditions: SQL[] = [] |
| 132 | |
| 133 | for (const [field, condition] of Object.entries(filter)) { |
| 134 | if (condition === undefined) { |
| 135 | continue |
| 136 | } |
| 137 | |
| 138 | // This represents a case where the filter is a logical OR of multiple filters |
| 139 | // e.g. { $or: [{ status: 'active' }, { status: 'pending' }] } |
| 140 | if (field === '$or' && Array.isArray(condition)) { |
| 141 | const orClause = buildLogicalClause(condition as Filter[], tableName, 'OR', columnTypeMap) |
| 142 | if (orClause) { |
| 143 | conditions.push(orClause) |
| 144 | } |
| 145 | continue |
| 146 | } |
| 147 | |
| 148 | // This represents a case where the filter is a logical AND of multiple filters |
| 149 | // e.g. { $and: [{ status: 'active' }, { status: 'pending' }] } |
| 150 | if (field === '$and' && Array.isArray(condition)) { |
| 151 | const andClause = buildLogicalClause(condition as Filter[], tableName, 'AND', columnTypeMap) |
| 152 | if (andClause) { |
| 153 | conditions.push(andClause) |
| 154 | } |
| 155 | continue |
| 156 | } |
| 157 | |
| 158 | // Skip arrays for regular fields - arrays are only valid for $or and $and. |
| 159 | // If we encounter an array here, it's likely malformed input (e.g., { name: [filter1, filter2] }) |
| 160 | // which doesn't have a clear semantic meaning, so we skip it. |
| 161 | if (Array.isArray(condition)) { |
| 162 | continue |
| 163 | } |
| 164 | |
| 165 | // Build SQL conditions for this field. Returns array of SQL fragments for each operator. |
| 166 | const fieldConditions = buildFieldCondition( |
| 167 | tableName, |
| 168 | field, |
| 169 | condition as JsonValue | ConditionOperators, |
| 170 | columnTypeMap.get(field) |
| 171 | ) |
| 172 | conditions.push(...fieldConditions) |
| 173 | } |
| 174 | |
| 175 | if (conditions.length === 0) return undefined |
| 176 | if (conditions.length === 1) return conditions[0] |
| 177 | |
| 178 | return sql.join(conditions, sql.raw(' AND ')) |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Builds an ORDER BY clause from a sort object. |
no test coverage detected