Sink/server/utils/query-filter.ts
wudi 7cadbfe2b1 refactor: Migrate project files and directories to the app directory using the new compatibilityVersion: 4 feature
- Moved `app.config.ts` to the `app` directory
- Migrated `.vue` files and other assets to the `app` directory
- Updated import paths to reflect the new file locations
2025-03-24 11:05:59 +08:00

32 lines
891 B
TypeScript

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
}