@mastra/libsql 0.13.0 → 0.13.1-alpha.0
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.
- package/.turbo/turbo-build.log +1 -1
- package/CHANGELOG.md +8 -0
- package/dist/index.cjs +46 -33
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +46 -33
- package/dist/index.js.map +1 -1
- package/dist/vector/index.d.ts.map +1 -1
- package/dist/vector/sql-builder.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/vector/index.test.ts +22 -1
- package/src/vector/index.ts +1 -1
- package/src/vector/sql-builder.ts +51 -33
package/dist/index.js
CHANGED
|
@@ -76,11 +76,11 @@ var LibSQLFilterTranslator = class extends BaseFilterTranslator {
|
|
|
76
76
|
};
|
|
77
77
|
var createBasicOperator = (symbol) => {
|
|
78
78
|
return (key, value) => {
|
|
79
|
-
const
|
|
79
|
+
const jsonPath = getJsonPath(key);
|
|
80
80
|
return {
|
|
81
81
|
sql: `CASE
|
|
82
|
-
WHEN ? IS NULL THEN json_extract(metadata,
|
|
83
|
-
ELSE json_extract(metadata,
|
|
82
|
+
WHEN ? IS NULL THEN json_extract(metadata, ${jsonPath}) IS ${symbol === "=" ? "" : "NOT"} NULL
|
|
83
|
+
ELSE json_extract(metadata, ${jsonPath}) ${symbol} ?
|
|
84
84
|
END`,
|
|
85
85
|
needsValue: true,
|
|
86
86
|
transformValue: () => {
|
|
@@ -91,16 +91,19 @@ var createBasicOperator = (symbol) => {
|
|
|
91
91
|
};
|
|
92
92
|
var createNumericOperator = (symbol) => {
|
|
93
93
|
return (key) => {
|
|
94
|
-
const
|
|
94
|
+
const jsonPath = getJsonPath(key);
|
|
95
95
|
return {
|
|
96
|
-
sql: `CAST(json_extract(metadata,
|
|
96
|
+
sql: `CAST(json_extract(metadata, ${jsonPath}) AS NUMERIC) ${symbol} ?`,
|
|
97
97
|
needsValue: true
|
|
98
98
|
};
|
|
99
99
|
};
|
|
100
100
|
};
|
|
101
|
-
var validateJsonArray = (key) =>
|
|
102
|
-
|
|
103
|
-
|
|
101
|
+
var validateJsonArray = (key) => {
|
|
102
|
+
const jsonPath = getJsonPath(key);
|
|
103
|
+
return `json_valid(json_extract(metadata, ${jsonPath}))
|
|
104
|
+
AND json_type(json_extract(metadata, ${jsonPath})) = 'array'`;
|
|
105
|
+
};
|
|
106
|
+
var pattern = /json_extract\(metadata, '\$\.(?:"[^"]*"(?:\."[^"]*")*|[^']+)'\)/g;
|
|
104
107
|
function buildElemMatchConditions(value) {
|
|
105
108
|
const conditions = Object.entries(value).map(([field, fieldValue]) => {
|
|
106
109
|
if (field.startsWith("$")) {
|
|
@@ -109,12 +112,13 @@ function buildElemMatchConditions(value) {
|
|
|
109
112
|
return { sql: elemSql, values };
|
|
110
113
|
} else if (typeof fieldValue === "object" && !Array.isArray(fieldValue)) {
|
|
111
114
|
const { sql, values } = buildCondition(field, fieldValue);
|
|
112
|
-
const
|
|
115
|
+
const jsonPath = parseJsonPathKey(field);
|
|
116
|
+
const elemSql = sql.replace(pattern, `json_extract(elem.value, '$.${jsonPath}')`);
|
|
113
117
|
return { sql: elemSql, values };
|
|
114
118
|
} else {
|
|
115
|
-
const
|
|
119
|
+
const jsonPath = parseJsonPathKey(field);
|
|
116
120
|
return {
|
|
117
|
-
sql: `json_extract(elem.value, '
|
|
121
|
+
sql: `json_extract(elem.value, '$.${jsonPath}') = ?`,
|
|
118
122
|
values: [fieldValue]
|
|
119
123
|
};
|
|
120
124
|
}
|
|
@@ -130,7 +134,7 @@ var FILTER_OPERATORS = {
|
|
|
130
134
|
$lte: createNumericOperator("<="),
|
|
131
135
|
// Array Operators
|
|
132
136
|
$in: (key, value) => {
|
|
133
|
-
const
|
|
137
|
+
const jsonPath = getJsonPath(key);
|
|
134
138
|
const arr = Array.isArray(value) ? value : [value];
|
|
135
139
|
if (arr.length === 0) {
|
|
136
140
|
return { sql: "1 = 0", needsValue: true, transformValue: () => [] };
|
|
@@ -139,12 +143,12 @@ var FILTER_OPERATORS = {
|
|
|
139
143
|
return {
|
|
140
144
|
sql: `(
|
|
141
145
|
CASE
|
|
142
|
-
WHEN ${validateJsonArray(
|
|
146
|
+
WHEN ${validateJsonArray(key)} THEN
|
|
143
147
|
EXISTS (
|
|
144
|
-
SELECT 1 FROM json_each(json_extract(metadata,
|
|
148
|
+
SELECT 1 FROM json_each(json_extract(metadata, ${jsonPath})) as elem
|
|
145
149
|
WHERE elem.value IN (SELECT value FROM json_each(?))
|
|
146
150
|
)
|
|
147
|
-
ELSE json_extract(metadata,
|
|
151
|
+
ELSE json_extract(metadata, ${jsonPath}) IN (${paramPlaceholders})
|
|
148
152
|
END
|
|
149
153
|
)`,
|
|
150
154
|
needsValue: true,
|
|
@@ -152,7 +156,7 @@ var FILTER_OPERATORS = {
|
|
|
152
156
|
};
|
|
153
157
|
},
|
|
154
158
|
$nin: (key, value) => {
|
|
155
|
-
const
|
|
159
|
+
const jsonPath = getJsonPath(key);
|
|
156
160
|
const arr = Array.isArray(value) ? value : [value];
|
|
157
161
|
if (arr.length === 0) {
|
|
158
162
|
return { sql: "1 = 1", needsValue: true, transformValue: () => [] };
|
|
@@ -161,12 +165,12 @@ var FILTER_OPERATORS = {
|
|
|
161
165
|
return {
|
|
162
166
|
sql: `(
|
|
163
167
|
CASE
|
|
164
|
-
WHEN ${validateJsonArray(
|
|
168
|
+
WHEN ${validateJsonArray(key)} THEN
|
|
165
169
|
NOT EXISTS (
|
|
166
|
-
SELECT 1 FROM json_each(json_extract(metadata,
|
|
170
|
+
SELECT 1 FROM json_each(json_extract(metadata, ${jsonPath})) as elem
|
|
167
171
|
WHERE elem.value IN (SELECT value FROM json_each(?))
|
|
168
172
|
)
|
|
169
|
-
ELSE json_extract(metadata,
|
|
173
|
+
ELSE json_extract(metadata, ${jsonPath}) NOT IN (${paramPlaceholders})
|
|
170
174
|
END
|
|
171
175
|
)`,
|
|
172
176
|
needsValue: true,
|
|
@@ -174,7 +178,7 @@ var FILTER_OPERATORS = {
|
|
|
174
178
|
};
|
|
175
179
|
},
|
|
176
180
|
$all: (key, value) => {
|
|
177
|
-
const
|
|
181
|
+
const jsonPath = getJsonPath(key);
|
|
178
182
|
let sql;
|
|
179
183
|
const arrayValue = Array.isArray(value) ? value : [value];
|
|
180
184
|
if (arrayValue.length === 0) {
|
|
@@ -182,13 +186,13 @@ var FILTER_OPERATORS = {
|
|
|
182
186
|
} else {
|
|
183
187
|
sql = `(
|
|
184
188
|
CASE
|
|
185
|
-
WHEN ${validateJsonArray(
|
|
189
|
+
WHEN ${validateJsonArray(key)} THEN
|
|
186
190
|
NOT EXISTS (
|
|
187
191
|
SELECT value
|
|
188
192
|
FROM json_each(?)
|
|
189
193
|
WHERE value NOT IN (
|
|
190
194
|
SELECT value
|
|
191
|
-
FROM json_each(json_extract(metadata,
|
|
195
|
+
FROM json_each(json_extract(metadata, ${jsonPath}))
|
|
192
196
|
)
|
|
193
197
|
)
|
|
194
198
|
ELSE FALSE
|
|
@@ -207,7 +211,7 @@ var FILTER_OPERATORS = {
|
|
|
207
211
|
};
|
|
208
212
|
},
|
|
209
213
|
$elemMatch: (key, value) => {
|
|
210
|
-
const
|
|
214
|
+
const jsonPath = getJsonPath(key);
|
|
211
215
|
if (typeof value !== "object" || Array.isArray(value)) {
|
|
212
216
|
throw new Error("$elemMatch requires an object with conditions");
|
|
213
217
|
}
|
|
@@ -215,10 +219,10 @@ var FILTER_OPERATORS = {
|
|
|
215
219
|
return {
|
|
216
220
|
sql: `(
|
|
217
221
|
CASE
|
|
218
|
-
WHEN ${validateJsonArray(
|
|
222
|
+
WHEN ${validateJsonArray(key)} THEN
|
|
219
223
|
EXISTS (
|
|
220
224
|
SELECT 1
|
|
221
|
-
FROM json_each(json_extract(metadata,
|
|
225
|
+
FROM json_each(json_extract(metadata, ${jsonPath})) as elem
|
|
222
226
|
WHERE ${conditions.map((c) => c.sql).join(" AND ")}
|
|
223
227
|
)
|
|
224
228
|
ELSE FALSE
|
|
@@ -230,9 +234,9 @@ var FILTER_OPERATORS = {
|
|
|
230
234
|
},
|
|
231
235
|
// Element Operators
|
|
232
236
|
$exists: (key) => {
|
|
233
|
-
const
|
|
237
|
+
const jsonPath = getJsonPath(key);
|
|
234
238
|
return {
|
|
235
|
-
sql: `json_extract(metadata,
|
|
239
|
+
sql: `json_extract(metadata, ${jsonPath}) IS NOT NULL`,
|
|
236
240
|
needsValue: false
|
|
237
241
|
};
|
|
238
242
|
},
|
|
@@ -251,12 +255,12 @@ var FILTER_OPERATORS = {
|
|
|
251
255
|
needsValue: false
|
|
252
256
|
}),
|
|
253
257
|
$size: (key, paramIndex) => {
|
|
254
|
-
const
|
|
258
|
+
const jsonPath = getJsonPath(key);
|
|
255
259
|
return {
|
|
256
260
|
sql: `(
|
|
257
261
|
CASE
|
|
258
|
-
WHEN json_type(json_extract(metadata,
|
|
259
|
-
json_array_length(json_extract(metadata,
|
|
262
|
+
WHEN json_type(json_extract(metadata, ${jsonPath})) = 'array' THEN
|
|
263
|
+
json_array_length(json_extract(metadata, ${jsonPath})) = $${paramIndex}
|
|
260
264
|
ELSE FALSE
|
|
261
265
|
END
|
|
262
266
|
)`,
|
|
@@ -375,7 +379,14 @@ function isFilterResult(obj) {
|
|
|
375
379
|
}
|
|
376
380
|
var parseJsonPathKey = (key) => {
|
|
377
381
|
const parsedKey = parseFieldKey(key);
|
|
378
|
-
|
|
382
|
+
if (parsedKey.includes(".")) {
|
|
383
|
+
return parsedKey.split(".").map((segment) => `"${segment}"`).join(".");
|
|
384
|
+
}
|
|
385
|
+
return parsedKey;
|
|
386
|
+
};
|
|
387
|
+
var getJsonPath = (key) => {
|
|
388
|
+
const jsonPathKey = parseJsonPathKey(key);
|
|
389
|
+
return `'$.${jsonPathKey}'`;
|
|
379
390
|
};
|
|
380
391
|
function escapeLikePattern(str) {
|
|
381
392
|
return str.replace(/([%_\\])/g, "\\$1");
|
|
@@ -400,8 +411,9 @@ function buildCondition(key, value, parentPath) {
|
|
|
400
411
|
return handleLogicalOperator(key, value);
|
|
401
412
|
}
|
|
402
413
|
if (!value || typeof value !== "object") {
|
|
414
|
+
const jsonPath = getJsonPath(key);
|
|
403
415
|
return {
|
|
404
|
-
sql: `json_extract(metadata,
|
|
416
|
+
sql: `json_extract(metadata, ${jsonPath}) = ?`,
|
|
405
417
|
values: [value]
|
|
406
418
|
};
|
|
407
419
|
}
|
|
@@ -547,7 +559,8 @@ var LibSQLVector = class extends MastraVector {
|
|
|
547
559
|
topK = 10,
|
|
548
560
|
filter,
|
|
549
561
|
includeVector = false,
|
|
550
|
-
minScore =
|
|
562
|
+
minScore = -1
|
|
563
|
+
// Default to -1 to include all results (cosine similarity ranges from -1 to 1)
|
|
551
564
|
}) {
|
|
552
565
|
try {
|
|
553
566
|
if (!Number.isInteger(topK) || topK <= 0) {
|