|
| 1 | +import type { FilterGroup, SingleFilter } from "@fn-sphere/filter"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { filterFnList } from "./schema"; |
| 4 | + |
| 5 | +const SQL_OPERATORS: Record<string, string> = { |
| 6 | + equals: "=", |
| 7 | + notEquals: "!=", |
| 8 | + greaterThan: ">", |
| 9 | + greaterThanOrEqual: ">=", |
| 10 | + lessThan: "<", |
| 11 | + lessThanOrEqual: "<=", |
| 12 | + contains: "LIKE", |
| 13 | + notContains: "NOT LIKE", |
| 14 | + startsWith: "LIKE", |
| 15 | + notStartsWith: "NOT LIKE", |
| 16 | + in: "IN", |
| 17 | + notIn: "NOT IN", |
| 18 | + isNull: "IS NULL", |
| 19 | + isNotNull: "IS NOT NULL", |
| 20 | + isEmpty: "= ''", |
| 21 | + isNotEmpty: "!= ''", |
| 22 | + before: "<", |
| 23 | + after: ">", |
| 24 | +}; |
| 25 | + |
| 26 | +const checkUnaryFilter = (filterName: string) => { |
| 27 | + const filterSchema = filterFnList.find((fn) => fn.name === filterName); |
| 28 | + if (!filterSchema) throw new Error("Unknown filter! " + filterName); |
| 29 | + const filterDefine = |
| 30 | + typeof filterSchema.define === "function" |
| 31 | + ? filterSchema.define(z.any()) |
| 32 | + : filterSchema.define; |
| 33 | + const parameters = filterDefine._zod.def.input as z.ZodTuple; |
| 34 | + return parameters._zod.def.items.length <= 1; |
| 35 | +}; |
| 36 | + |
| 37 | +function escapeSQL(value: string): string { |
| 38 | + return value.replace(/'/g, "''"); |
| 39 | +} |
| 40 | + |
| 41 | +function transformSingleFilter(filter: SingleFilter): string | null { |
| 42 | + const path = filter.path?.[0]; |
| 43 | + const operator = filter.name ? SQL_OPERATORS[filter.name] : undefined; |
| 44 | + const value = filter.args[0]; |
| 45 | + |
| 46 | + if (!filter.name || path === undefined || operator === undefined) { |
| 47 | + return null; |
| 48 | + } |
| 49 | + const isUnaryFilter = checkUnaryFilter(filter.name); |
| 50 | + if (value === undefined && !isUnaryFilter) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + // Handle array values for IN/NOT IN |
| 55 | + if (Array.isArray(value)) { |
| 56 | + const items = value |
| 57 | + .map((v) => (typeof v === "string" ? `'${escapeSQL(v)}'` : v)) |
| 58 | + .join(", "); |
| 59 | + return `${path} ${operator} (${items})`; |
| 60 | + } |
| 61 | + |
| 62 | + // Unary operators (IS NULL, IS NOT NULL, = '', != '') |
| 63 | + if (value === undefined) { |
| 64 | + return `${path} ${operator}`; |
| 65 | + } |
| 66 | + |
| 67 | + // LIKE patterns for contains/startsWith |
| 68 | + if (typeof value === "string") { |
| 69 | + const escaped = escapeSQL(value); |
| 70 | + if (filter.name === "contains" || filter.name === "notContains") { |
| 71 | + return `${path} ${operator} '%${escaped}%'`; |
| 72 | + } |
| 73 | + if (filter.name === "startsWith" || filter.name === "notStartsWith") { |
| 74 | + return `${path} ${operator} '${escaped}%'`; |
| 75 | + } |
| 76 | + return `${path} ${operator} '${escaped}'`; |
| 77 | + } |
| 78 | + |
| 79 | + if (value instanceof Date) { |
| 80 | + return `${path} ${operator} '${value.toISOString().split("T")[0]}'`; |
| 81 | + } |
| 82 | + |
| 83 | + return `${path} ${operator} ${value}`; |
| 84 | +} |
| 85 | + |
| 86 | +function transformFilterGroup(filterGroup: FilterGroup): string | null { |
| 87 | + if (!filterGroup.conditions.length) return ""; |
| 88 | + |
| 89 | + const conditions = filterGroup.conditions.map((condition) => { |
| 90 | + if (condition.type === "Filter") { |
| 91 | + return transformSingleFilter(condition); |
| 92 | + } else { |
| 93 | + return transformFilterGroup(condition); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + const operator = filterGroup.op.toUpperCase() as Uppercase<FilterGroup["op"]>; |
| 98 | + const result = conditions.filter((i) => i !== null).join(` ${operator} `); |
| 99 | + if (!result) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + return `(${result})`; |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Transforms a FilterGroup object into a SQL WHERE clause. |
| 108 | + * |
| 109 | + * @example |
| 110 | + * ```ts |
| 111 | + * filterRuleToSQL({ |
| 112 | + * type: "FilterGroup", |
| 113 | + * op: "and", |
| 114 | + * conditions: [{ |
| 115 | + * type: "Filter", |
| 116 | + * path: ["title"], |
| 117 | + * name: "equals", |
| 118 | + * args: ["hello world"] |
| 119 | + * }] |
| 120 | + * }) |
| 121 | + * // "WHERE (title = 'hello world')" |
| 122 | + * ``` |
| 123 | + */ |
| 124 | +export const filterRuleToSQL = (filterGroup: FilterGroup) => { |
| 125 | + const where = transformFilterGroup(filterGroup); |
| 126 | + if (!where) return ""; |
| 127 | + return `WHERE ${where}`; |
| 128 | +}; |
0 commit comments