@mastra/libsql 0.0.1-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.
@@ -0,0 +1,117 @@
1
+ import { BaseFilterTranslator } from '@mastra/core/vector/filter';
2
+ import type { FieldCondition, VectorFilter, OperatorSupport } from '@mastra/core/vector/filter';
3
+
4
+ /**
5
+ * Translates MongoDB-style filters to LibSQL 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 LibSQLFilterTranslator extends BaseFilterTranslator {
15
+ protected override getSupportedOperators(): OperatorSupport {
16
+ return {
17
+ ...BaseFilterTranslator.DEFAULT_OPERATORS,
18
+ regex: [],
19
+ custom: ['$contains', '$size'],
20
+ };
21
+ }
22
+
23
+ translate(filter?: VectorFilter): VectorFilter {
24
+ if (this.isEmpty(filter)) {
25
+ return filter;
26
+ }
27
+ this.validateFilter(filter);
28
+ return this.translateNode(filter);
29
+ }
30
+
31
+ private translateNode(node: VectorFilter | FieldCondition, currentPath: string = ''): any {
32
+ if (this.isRegex(node)) {
33
+ throw new Error('Direct regex pattern format is not supported in LibSQL');
34
+ }
35
+ // Helper to wrap result with path if needed
36
+ const withPath = (result: any) => (currentPath ? { [currentPath]: result } : result);
37
+
38
+ // Handle primitives
39
+ if (this.isPrimitive(node)) {
40
+ return withPath({ $eq: this.normalizeComparisonValue(node) });
41
+ }
42
+
43
+ // Handle arrays
44
+ if (Array.isArray(node)) {
45
+ return withPath({ $in: this.normalizeArrayValues(node) });
46
+ }
47
+
48
+ // Handle regex
49
+ // TODO: Look more into regex support for LibSQL
50
+ // if (node instanceof RegExp) {
51
+ // return withPath(this.translateRegexPattern(node.source, node.flags));
52
+ // }
53
+
54
+ const entries = Object.entries(node as Record<string, any>);
55
+ const result: Record<string, any> = {};
56
+
57
+ // if ('$options' in node && !('$regex' in node)) {
58
+ // throw new Error('$options is not valid without $regex');
59
+ // }
60
+
61
+ // TODO: Look more into regex support for LibSQL
62
+ // // Handle special regex object format
63
+ // if ('$regex' in node) {
64
+ // const options = (node as any).$options || '';
65
+ // return withPath(this.translateRegexPattern(node.$regex, options));
66
+ // }
67
+
68
+ // Process remaining entries
69
+ for (const [key, value] of entries) {
70
+ // // Skip options as they're handled with $regex
71
+ // if (key === '$options') continue;
72
+
73
+ const newPath = currentPath ? `${currentPath}.${key}` : key;
74
+
75
+ if (this.isLogicalOperator(key)) {
76
+ result[key] = Array.isArray(value)
77
+ ? value.map((filter: VectorFilter) => this.translateNode(filter))
78
+ : this.translateNode(value);
79
+ } else if (this.isOperator(key)) {
80
+ if (this.isArrayOperator(key) && !Array.isArray(value) && key !== '$elemMatch') {
81
+ result[key] = [value];
82
+ } else if (this.isBasicOperator(key) && Array.isArray(value)) {
83
+ result[key] = JSON.stringify(value);
84
+ } else {
85
+ result[key] = value;
86
+ }
87
+ } else if (typeof value === 'object' && value !== null) {
88
+ // Handle nested objects
89
+ const hasOperators = Object.keys(value).some(k => this.isOperator(k));
90
+ if (hasOperators) {
91
+ result[newPath] = this.translateNode(value);
92
+ } else {
93
+ Object.assign(result, this.translateNode(value, newPath));
94
+ }
95
+ } else {
96
+ result[newPath] = this.translateNode(value);
97
+ }
98
+ }
99
+
100
+ return result;
101
+ }
102
+
103
+ // TODO: Look more into regex support for LibSQL
104
+ // private translateRegexPattern(pattern: string, options: string = ''): any {
105
+ // if (!options) return { $regex: pattern };
106
+
107
+ // const flags = options
108
+ // .split('')
109
+ // .filter(f => 'imsux'.includes(f))
110
+ // .join('');
111
+
112
+ // return {
113
+ // $regex: pattern,
114
+ // $options: flags,
115
+ // };
116
+ // }
117
+ }