@mastra/pg 0.14.5 → 0.14.6-alpha.1

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