@mastra/pg 0.0.0-commonjs-20250227130920

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