-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathquery-filter.ts
32 lines (25 loc) · 891 Bytes
/
query-filter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import type { QuerySchema } from '@@/schemas/query'
import type { SelectStatement } from 'sql-bricks'
import type { z } from 'zod'
const { in: $in, and, eq } = SqlBricks
export type Query = z.infer<typeof QuerySchema>
export function query2filter(query: Query) {
const filter = []
if (query.id)
filter.push(eq('index1', query.id))
Object.keys(logsMap).forEach((key) => {
// @ts-expect-error todo
if (query[key]) {
// @ts-expect-error todo
filter.push($in(logsMap[key], query[key].split(',')))
}
})
return filter.length ? and(...filter) : []
}
export function appendTimeFilter(sql: SelectStatement, query: Query): unknown {
if (query.startAt)
sql.where(SqlBricks.gte('timestamp', SqlBricks(`toDateTime(${query.startAt})`)))
if (query.endAt)
sql.where(SqlBricks.lte('timestamp', SqlBricks(`toDateTime(${query.endAt})`)))
return sql
}