@mastra/pg 0.1.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,106 @@
1
+ import { BaseFilterTranslator, FieldCondition, Filter, OperatorSupport } from '@mastra/core/filter';
2
+
3
+ /**
4
+ * Translates MongoDB-style filters to PG compatible filters.
5
+ *
6
+ * Key differences from MongoDB:
7
+ *
8
+ * Logical Operators ($and, $or, $nor):
9
+ * - Can be used at the top level or nested within fields
10
+ * - Can take either a single condition or an array of conditions
11
+ *
12
+ */
13
+ export class PGFilterTranslator extends BaseFilterTranslator {
14
+ protected override getSupportedOperators(): OperatorSupport {
15
+ return {
16
+ ...BaseFilterTranslator.DEFAULT_OPERATORS,
17
+ custom: ['$contains', '$size'],
18
+ };
19
+ }
20
+
21
+ translate(filter: Filter): Filter {
22
+ if (this.isEmpty(filter)) {
23
+ return filter;
24
+ }
25
+ this.validateFilter(filter);
26
+ return this.translateNode(filter);
27
+ }
28
+
29
+ private translateNode(node: Filter | FieldCondition, currentPath: string = ''): any {
30
+ // Helper to wrap result with path if needed
31
+ const withPath = (result: any) => (currentPath ? { [currentPath]: result } : result);
32
+
33
+ // Handle primitives
34
+ if (this.isPrimitive(node)) {
35
+ return withPath({ $eq: this.normalizeComparisonValue(node) });
36
+ }
37
+
38
+ // Handle arrays
39
+ if (Array.isArray(node)) {
40
+ return withPath({ $in: this.normalizeArrayValues(node) });
41
+ }
42
+
43
+ // Handle regex
44
+ if (node instanceof RegExp) {
45
+ return withPath(this.translateRegexPattern(node.source, node.flags));
46
+ }
47
+
48
+ const entries = Object.entries(node as Record<string, any>);
49
+ const result: Record<string, any> = {};
50
+
51
+ if ('$options' in node && !('$regex' in node)) {
52
+ throw new Error('$options is not valid without $regex');
53
+ }
54
+
55
+ // Handle special regex object format
56
+ if ('$regex' in node) {
57
+ const options = (node as any).$options || '';
58
+ return withPath(this.translateRegexPattern(node.$regex, options));
59
+ }
60
+
61
+ // Process remaining entries
62
+ for (const [key, value] of entries) {
63
+ // Skip options as they're handled with $regex
64
+ if (key === '$options') continue;
65
+
66
+ const newPath = currentPath ? `${currentPath}.${key}` : key;
67
+
68
+ if (this.isLogicalOperator(key)) {
69
+ result[key] = Array.isArray(value)
70
+ ? value.map((filter: Filter) => this.translateNode(filter))
71
+ : this.translateNode(value);
72
+ } else if (this.isOperator(key)) {
73
+ if (this.isArrayOperator(key) && !Array.isArray(value) && key !== '$elemMatch') {
74
+ result[key] = [value];
75
+ } else if (this.isBasicOperator(key) && Array.isArray(value)) {
76
+ result[key] = JSON.stringify(value);
77
+ } else {
78
+ result[key] = value;
79
+ }
80
+ } else if (typeof value === 'object' && value !== null) {
81
+ // Handle nested objects
82
+ const hasOperators = Object.keys(value).some(k => this.isOperator(k));
83
+ if (hasOperators) {
84
+ result[newPath] = this.translateNode(value);
85
+ } else {
86
+ Object.assign(result, this.translateNode(value, newPath));
87
+ }
88
+ } else {
89
+ result[newPath] = this.translateNode(value);
90
+ }
91
+ }
92
+
93
+ return result;
94
+ }
95
+
96
+ private translateRegexPattern(pattern: string, options: string = ''): any {
97
+ if (!options) return { $regex: pattern };
98
+
99
+ const flags = options
100
+ .split('')
101
+ .filter(f => 'imsux'.includes(f))
102
+ .join('');
103
+
104
+ return { $regex: flags ? `(?${flags})${pattern}` : pattern };
105
+ }
106
+ }