|
6 | 6 | * LICENSE file in the root directory of this source tree. |
7 | 7 | */ |
8 | 8 |
|
9 | | -import { ObjectQLContext, IObjectQL, ObjectConfig, Driver, UnifiedQuery, ActionContext, HookAPI, RetrievalHookContext, MutationHookContext, UpdateHookContext, ValidationContext, ValidationError, ValidationRuleResult, FormulaContext, FilterExpression } from '@objectql/types'; |
| 9 | +import { ObjectQLContext, IObjectQL, ObjectConfig, Driver, UnifiedQuery, ActionContext, HookAPI, RetrievalHookContext, MutationHookContext, UpdateHookContext, ValidationContext, ValidationError, ValidationRuleResult, FormulaContext, Filter } from '@objectql/types'; |
10 | 10 | import type { ObjectStackKernel } from '@objectstack/runtime'; |
11 | 11 | import type { QueryAST, FilterNode, SortNode } from '@objectstack/spec'; |
12 | 12 | import { Validator } from './validator'; |
@@ -43,16 +43,119 @@ export class ObjectRepository { |
43 | 43 | } |
44 | 44 |
|
45 | 45 | /** |
46 | | - * Translates ObjectQL FilterExpression to ObjectStack FilterNode format |
| 46 | + * Translates ObjectQL Filter (FilterCondition) to ObjectStack FilterNode format |
| 47 | + * |
| 48 | + * Converts modern object-based syntax to legacy array-based syntax: |
| 49 | + * Input: { age: { $gte: 18 }, $or: [{ status: "active" }, { role: "admin" }] } |
| 50 | + * Output: [["age", ">=", 18], "or", [["status", "=", "active"], "or", ["role", "=", "admin"]]] |
47 | 51 | */ |
48 | | - private translateFilters(filters?: FilterExpression[]): FilterNode | undefined { |
49 | | - if (!filters || filters.length === 0) { |
| 52 | + private translateFilters(filters?: Filter): FilterNode | undefined { |
| 53 | + if (!filters || Object.keys(filters).length === 0) { |
50 | 54 | return undefined; |
51 | 55 | } |
52 | 56 |
|
53 | | - // FilterExpression[] is already compatible with FilterNode format |
54 | | - // Just pass through as-is |
55 | | - return filters as FilterNode; |
| 57 | + return this.convertFilterToNode(filters); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Recursively converts FilterCondition to FilterNode array format |
| 62 | + */ |
| 63 | + private convertFilterToNode(filter: Filter): FilterNode { |
| 64 | + const nodes: any[] = []; |
| 65 | + |
| 66 | + // Process logical operators first |
| 67 | + if (filter.$and) { |
| 68 | + const andNodes = filter.$and.map(f => this.convertFilterToNode(f)); |
| 69 | + nodes.push(...this.interleaveWithOperator(andNodes, 'and')); |
| 70 | + } |
| 71 | + |
| 72 | + if (filter.$or) { |
| 73 | + const orNodes = filter.$or.map(f => this.convertFilterToNode(f)); |
| 74 | + if (nodes.length > 0) { |
| 75 | + nodes.push('and'); |
| 76 | + } |
| 77 | + nodes.push(...this.interleaveWithOperator(orNodes, 'or')); |
| 78 | + } |
| 79 | + |
| 80 | + if (filter.$not) { |
| 81 | + // NOT operator: convert to array of negated conditions |
| 82 | + // This is a simplification - proper implementation would need driver support |
| 83 | + const notNode = this.convertFilterToNode(filter.$not); |
| 84 | + if (nodes.length > 0) { |
| 85 | + nodes.push('and'); |
| 86 | + } |
| 87 | + // Wrap in array to indicate it's a NOT group |
| 88 | + nodes.push(['not', notNode]); |
| 89 | + } |
| 90 | + |
| 91 | + // Process field conditions |
| 92 | + for (const [field, value] of Object.entries(filter)) { |
| 93 | + if (field.startsWith('$')) { |
| 94 | + continue; // Skip logical operators (already processed) |
| 95 | + } |
| 96 | + |
| 97 | + if (nodes.length > 0) { |
| 98 | + nodes.push('and'); |
| 99 | + } |
| 100 | + |
| 101 | + // Handle field value |
| 102 | + if (value === null || value === undefined) { |
| 103 | + nodes.push([field, '=', value]); |
| 104 | + } else if (typeof value === 'object' && !Array.isArray(value) && !(value instanceof Date)) { |
| 105 | + // Explicit operators |
| 106 | + for (const [op, opValue] of Object.entries(value)) { |
| 107 | + if (nodes.length > 0 && nodes[nodes.length - 1] !== 'and') { |
| 108 | + nodes.push('and'); |
| 109 | + } |
| 110 | + |
| 111 | + const legacyOp = this.mapOperatorToLegacy(op); |
| 112 | + nodes.push([field, legacyOp, opValue]); |
| 113 | + } |
| 114 | + } else { |
| 115 | + // Implicit equality |
| 116 | + nodes.push([field, '=', value]); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return nodes.length === 1 ? nodes[0] : nodes; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Interleaves filter nodes with a logical operator |
| 125 | + */ |
| 126 | + private interleaveWithOperator(nodes: FilterNode[], operator: string): any[] { |
| 127 | + if (nodes.length === 0) return []; |
| 128 | + if (nodes.length === 1) return [nodes[0]]; |
| 129 | + |
| 130 | + const result: any[] = [nodes[0]]; |
| 131 | + for (let i = 1; i < nodes.length; i++) { |
| 132 | + result.push(operator, nodes[i]); |
| 133 | + } |
| 134 | + return result; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Maps modern $-prefixed operators to legacy format |
| 139 | + */ |
| 140 | + private mapOperatorToLegacy(operator: string): string { |
| 141 | + const mapping: Record<string, string> = { |
| 142 | + '$eq': '=', |
| 143 | + '$ne': '!=', |
| 144 | + '$gt': '>', |
| 145 | + '$gte': '>=', |
| 146 | + '$lt': '<', |
| 147 | + '$lte': '<=', |
| 148 | + '$in': 'in', |
| 149 | + '$nin': 'nin', |
| 150 | + '$contains': 'contains', |
| 151 | + '$startsWith': 'startswith', |
| 152 | + '$endsWith': 'endswith', |
| 153 | + '$null': 'is_null', |
| 154 | + '$exist': 'is_not_null', |
| 155 | + '$between': 'between', |
| 156 | + }; |
| 157 | + |
| 158 | + return mapping[operator] || operator.replace('$', ''); |
56 | 159 | } |
57 | 160 |
|
58 | 161 | /** |
|
0 commit comments