|
| 1 | +import { QueryNode, And, Or, Eq, Gt, Gte, Lt, Lte, Neq, Contains } from './query-node'; |
| 2 | + |
| 3 | +export interface QueryOptions { |
| 4 | + /** |
| 5 | + * Function to transform property names (e.g. camelCase to snake_case) |
| 6 | + */ |
| 7 | + namingStrategy?: (prop: string) => string; |
| 8 | +} |
| 9 | + |
| 10 | +/** |
| 11 | + * Translates a MongoDB-like query object to a QueryNode tree. |
| 12 | + * Example: { name: "Fabio", age: { $gt: 18 } } |
| 13 | + */ |
| 14 | +export class ObjectToQueryNodeTranslator { |
| 15 | + |
| 16 | + static translate(query: any, options?: QueryOptions): QueryNode | null { |
| 17 | + if (!query || Object.keys(query).length === 0) return null; |
| 18 | + |
| 19 | + const nodes: QueryNode[] = []; |
| 20 | + |
| 21 | + for (const key of Object.keys(query)) { |
| 22 | + const value = query[key]; |
| 23 | + const fieldName = options?.namingStrategy ? options.namingStrategy(key) : key; |
| 24 | + |
| 25 | + if (key === '$or') { |
| 26 | + if (Array.isArray(value)) { |
| 27 | + const subNodes = value.map(v => this.translate(v, options)).filter(n => n !== null) as QueryNode[]; |
| 28 | + if (subNodes.length > 0) { |
| 29 | + let combined = subNodes[0]; |
| 30 | + for (let i = 1; i < subNodes.length; i++) { |
| 31 | + combined = new Or(combined, subNodes[i]); |
| 32 | + } |
| 33 | + nodes.push(combined); |
| 34 | + } |
| 35 | + } |
| 36 | + continue; |
| 37 | + } |
| 38 | + |
| 39 | + if (key === '$and') { |
| 40 | + if (Array.isArray(value)) { |
| 41 | + const subNodes = value.map(v => this.translate(v, options)).filter(n => n !== null) as QueryNode[]; |
| 42 | + subNodes.forEach(n => nodes.push(n)); |
| 43 | + } |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + // Primitive value check (e.g. { field: "value" } -> implies Eq) |
| 48 | + if (typeof value !== 'object' || value === null || Array.isArray(value)) { |
| 49 | + nodes.push(new Eq(fieldName, value)); |
| 50 | + continue; |
| 51 | + } |
| 52 | + |
| 53 | + // Operator check (e.g. { field: { $gt: 10 } }) |
| 54 | + for (const op of Object.keys(value)) { |
| 55 | + const opVal = value[op]; |
| 56 | + switch (op) { |
| 57 | + case '$eq': nodes.push(new Eq(fieldName, opVal)); break; |
| 58 | + case '$neq': nodes.push(new Neq(fieldName, opVal)); break; |
| 59 | + case '$gt': nodes.push(new Gt(fieldName, opVal)); break; |
| 60 | + case '$gte': nodes.push(new Gte(fieldName, opVal)); break; |
| 61 | + case '$lt': nodes.push(new Lt(fieldName, opVal)); break; |
| 62 | + case '$lte': nodes.push(new Lte(fieldName, opVal)); break; |
| 63 | + case '$contains': nodes.push(new Contains(fieldName, opVal)); break; |
| 64 | + default: |
| 65 | + // If it's not a known operator, maybe it's just a nested object Eq? |
| 66 | + // For now let's assume valid operators or fallback to Eq? |
| 67 | + // Actually in mongo {a: {b:1}} is exact match. |
| 68 | + // EntglDb is flat mostly. Let's throw or ignore. |
| 69 | + console.warn(`Unknown operator ${op} for field ${fieldName}`); |
| 70 | + break; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if (nodes.length === 0) return null; |
| 76 | + if (nodes.length === 1) return nodes[0]; |
| 77 | + |
| 78 | + // Combine all implicit ANDs |
| 79 | + let result = nodes[0]; |
| 80 | + for (let i = 1; i < nodes.length; i++) { |
| 81 | + result = new And(result, nodes[i]); |
| 82 | + } |
| 83 | + return result; |
| 84 | + } |
| 85 | +} |
0 commit comments