@mastra/opensearch 0.11.7 → 0.11.8-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/CHANGELOG.md +9 -0
- package/package.json +17 -4
- package/.turbo/turbo-build.log +0 -4
- package/docker-compose.yaml +0 -23
- package/eslint.config.js +0 -6
- package/src/index.ts +0 -1
- package/src/vector/filter.test.ts +0 -661
- package/src/vector/filter.ts +0 -479
- package/src/vector/index.test.ts +0 -1558
- package/src/vector/index.ts +0 -436
- package/src/vector/prompt.ts +0 -82
- package/tsconfig.build.json +0 -9
- package/tsconfig.json +0 -5
- package/tsup.config.ts +0 -17
- package/vitest.config.ts +0 -11
package/src/vector/index.ts
DELETED
|
@@ -1,436 +0,0 @@
|
|
|
1
|
-
import { MastraError, ErrorDomain, ErrorCategory } from '@mastra/core/error';
|
|
2
|
-
import type {
|
|
3
|
-
CreateIndexParams,
|
|
4
|
-
DeleteIndexParams,
|
|
5
|
-
DeleteVectorParams,
|
|
6
|
-
DescribeIndexParams,
|
|
7
|
-
IndexStats,
|
|
8
|
-
QueryResult,
|
|
9
|
-
QueryVectorParams,
|
|
10
|
-
UpdateVectorParams,
|
|
11
|
-
UpsertVectorParams,
|
|
12
|
-
} from '@mastra/core/vector';
|
|
13
|
-
import { MastraVector } from '@mastra/core/vector';
|
|
14
|
-
import { Client as OpenSearchClient } from '@opensearch-project/opensearch';
|
|
15
|
-
import { OpenSearchFilterTranslator } from './filter';
|
|
16
|
-
import type { OpenSearchVectorFilter } from './filter';
|
|
17
|
-
|
|
18
|
-
const METRIC_MAPPING = {
|
|
19
|
-
cosine: 'cosinesimil',
|
|
20
|
-
euclidean: 'l2',
|
|
21
|
-
dotproduct: 'innerproduct',
|
|
22
|
-
} as const;
|
|
23
|
-
|
|
24
|
-
const REVERSE_METRIC_MAPPING = {
|
|
25
|
-
cosinesimil: 'cosine',
|
|
26
|
-
l2: 'euclidean',
|
|
27
|
-
innerproduct: 'dotproduct',
|
|
28
|
-
} as const;
|
|
29
|
-
|
|
30
|
-
type OpenSearchVectorParams = QueryVectorParams<OpenSearchVectorFilter>;
|
|
31
|
-
|
|
32
|
-
export class OpenSearchVector extends MastraVector<OpenSearchVectorFilter> {
|
|
33
|
-
private client: OpenSearchClient;
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Creates a new OpenSearchVector client.
|
|
37
|
-
*
|
|
38
|
-
* @param {string} url - The url of the OpenSearch node.
|
|
39
|
-
*/
|
|
40
|
-
constructor({ url }: { url: string }) {
|
|
41
|
-
super();
|
|
42
|
-
this.client = new OpenSearchClient({ node: url });
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Creates a new collection with the specified configuration.
|
|
47
|
-
*
|
|
48
|
-
* @param {string} indexName - The name of the collection to create.
|
|
49
|
-
* @param {number} dimension - The dimension of the vectors to be stored in the collection.
|
|
50
|
-
* @param {'cosine' | 'euclidean' | 'dotproduct'} [metric=cosine] - The metric to use to sort vectors in the collection.
|
|
51
|
-
* @returns {Promise<void>} A promise that resolves when the collection is created.
|
|
52
|
-
*/
|
|
53
|
-
async createIndex({ indexName, dimension, metric = 'cosine' }: CreateIndexParams): Promise<void> {
|
|
54
|
-
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
55
|
-
throw new MastraError({
|
|
56
|
-
id: 'STORAGE_OPENSEARCH_VECTOR_CREATE_INDEX_INVALID_ARGS',
|
|
57
|
-
domain: ErrorDomain.STORAGE,
|
|
58
|
-
category: ErrorCategory.USER,
|
|
59
|
-
text: 'Dimension must be a positive integer',
|
|
60
|
-
details: { indexName, dimension },
|
|
61
|
-
});
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
try {
|
|
65
|
-
await this.client.indices.create({
|
|
66
|
-
index: indexName,
|
|
67
|
-
body: {
|
|
68
|
-
settings: { index: { knn: true } },
|
|
69
|
-
mappings: {
|
|
70
|
-
properties: {
|
|
71
|
-
metadata: { type: 'object' },
|
|
72
|
-
id: { type: 'keyword' },
|
|
73
|
-
embedding: {
|
|
74
|
-
type: 'knn_vector',
|
|
75
|
-
dimension: dimension,
|
|
76
|
-
method: {
|
|
77
|
-
name: 'hnsw',
|
|
78
|
-
space_type: METRIC_MAPPING[metric],
|
|
79
|
-
engine: 'faiss',
|
|
80
|
-
parameters: { ef_construction: 128, m: 16 },
|
|
81
|
-
},
|
|
82
|
-
},
|
|
83
|
-
},
|
|
84
|
-
},
|
|
85
|
-
},
|
|
86
|
-
});
|
|
87
|
-
} catch (error: any) {
|
|
88
|
-
const message = error?.message || error?.toString();
|
|
89
|
-
if (message && message.toLowerCase().includes('already exists')) {
|
|
90
|
-
// Fetch collection info and check dimension
|
|
91
|
-
await this.validateExistingIndex(indexName, dimension, metric);
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
throw new MastraError(
|
|
95
|
-
{
|
|
96
|
-
id: 'STORAGE_OPENSEARCH_VECTOR_CREATE_INDEX_FAILED',
|
|
97
|
-
domain: ErrorDomain.STORAGE,
|
|
98
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
99
|
-
details: { indexName, dimension, metric },
|
|
100
|
-
},
|
|
101
|
-
error,
|
|
102
|
-
);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Lists all indexes.
|
|
108
|
-
*
|
|
109
|
-
* @returns {Promise<string[]>} A promise that resolves to an array of indexes.
|
|
110
|
-
*/
|
|
111
|
-
async listIndexes(): Promise<string[]> {
|
|
112
|
-
try {
|
|
113
|
-
const response = await this.client.cat.indices({ format: 'json' });
|
|
114
|
-
const indexes = response.body
|
|
115
|
-
.map((record: { index?: string }) => record.index)
|
|
116
|
-
.filter((index: string | undefined) => index !== undefined);
|
|
117
|
-
|
|
118
|
-
return indexes;
|
|
119
|
-
} catch (error) {
|
|
120
|
-
throw new MastraError(
|
|
121
|
-
{
|
|
122
|
-
id: 'STORAGE_OPENSEARCH_VECTOR_LIST_INDEXES_FAILED',
|
|
123
|
-
domain: ErrorDomain.STORAGE,
|
|
124
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
125
|
-
},
|
|
126
|
-
error,
|
|
127
|
-
);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Retrieves statistics about a vector index.
|
|
133
|
-
*
|
|
134
|
-
* @param {string} indexName - The name of the index to describe
|
|
135
|
-
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
136
|
-
*/
|
|
137
|
-
async describeIndex({ indexName }: DescribeIndexParams): Promise<IndexStats> {
|
|
138
|
-
const { body: indexInfo } = await this.client.indices.get({ index: indexName });
|
|
139
|
-
const mappings = indexInfo[indexName]?.mappings;
|
|
140
|
-
const embedding: any = mappings?.properties?.embedding;
|
|
141
|
-
const spaceType = embedding.method.space_type as keyof typeof REVERSE_METRIC_MAPPING;
|
|
142
|
-
|
|
143
|
-
const { body: countInfo } = await this.client.count({ index: indexName });
|
|
144
|
-
|
|
145
|
-
return {
|
|
146
|
-
dimension: Number(embedding.dimension),
|
|
147
|
-
count: Number(countInfo.count),
|
|
148
|
-
metric: REVERSE_METRIC_MAPPING[spaceType],
|
|
149
|
-
};
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Deletes the specified index.
|
|
154
|
-
*
|
|
155
|
-
* @param {string} indexName - The name of the index to delete.
|
|
156
|
-
* @returns {Promise<void>} A promise that resolves when the index is deleted.
|
|
157
|
-
*/
|
|
158
|
-
async deleteIndex({ indexName }: DeleteIndexParams): Promise<void> {
|
|
159
|
-
try {
|
|
160
|
-
await this.client.indices.delete({ index: indexName });
|
|
161
|
-
} catch (error) {
|
|
162
|
-
const mastraError = new MastraError(
|
|
163
|
-
{
|
|
164
|
-
id: 'STORAGE_OPENSEARCH_VECTOR_DELETE_INDEX_FAILED',
|
|
165
|
-
domain: ErrorDomain.STORAGE,
|
|
166
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
167
|
-
details: { indexName },
|
|
168
|
-
},
|
|
169
|
-
error,
|
|
170
|
-
);
|
|
171
|
-
this.logger?.error(mastraError.toString());
|
|
172
|
-
this.logger?.trackException(mastraError);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Inserts or updates vectors in the specified collection.
|
|
178
|
-
*
|
|
179
|
-
* @param {string} indexName - The name of the collection to upsert into.
|
|
180
|
-
* @param {number[][]} vectors - An array of vectors to upsert.
|
|
181
|
-
* @param {Record<string, any>[]} [metadata] - An optional array of metadata objects corresponding to each vector.
|
|
182
|
-
* @param {string[]} [ids] - An optional array of IDs corresponding to each vector. If not provided, new IDs will be generated.
|
|
183
|
-
* @returns {Promise<string[]>} A promise that resolves to an array of IDs of the upserted vectors.
|
|
184
|
-
*/
|
|
185
|
-
async upsert({ indexName, vectors, metadata = [], ids }: UpsertVectorParams): Promise<string[]> {
|
|
186
|
-
const vectorIds = ids || vectors.map(() => crypto.randomUUID());
|
|
187
|
-
const operations = [];
|
|
188
|
-
|
|
189
|
-
try {
|
|
190
|
-
// Get index stats to check dimension
|
|
191
|
-
const indexInfo = await this.describeIndex({ indexName });
|
|
192
|
-
|
|
193
|
-
// Validate vector dimensions
|
|
194
|
-
this.validateVectorDimensions(vectors, indexInfo.dimension);
|
|
195
|
-
|
|
196
|
-
for (let i = 0; i < vectors.length; i++) {
|
|
197
|
-
const operation = {
|
|
198
|
-
index: {
|
|
199
|
-
_index: indexName,
|
|
200
|
-
_id: vectorIds[i],
|
|
201
|
-
},
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
const document = {
|
|
205
|
-
id: vectorIds[i],
|
|
206
|
-
embedding: vectors[i],
|
|
207
|
-
metadata: metadata[i] || {},
|
|
208
|
-
};
|
|
209
|
-
|
|
210
|
-
operations.push(operation);
|
|
211
|
-
operations.push(document);
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
if (operations.length > 0) {
|
|
215
|
-
await this.client.bulk({ body: operations, refresh: true });
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
return vectorIds;
|
|
219
|
-
} catch (error) {
|
|
220
|
-
throw new MastraError(
|
|
221
|
-
{
|
|
222
|
-
id: 'STORAGE_OPENSEARCH_VECTOR_UPSERT_FAILED',
|
|
223
|
-
domain: ErrorDomain.STORAGE,
|
|
224
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
225
|
-
details: { indexName, vectorCount: vectors?.length || 0 },
|
|
226
|
-
},
|
|
227
|
-
error,
|
|
228
|
-
);
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* Queries the specified collection using a vector and optional filter.
|
|
234
|
-
*
|
|
235
|
-
* @param {string} indexName - The name of the collection to query.
|
|
236
|
-
* @param {number[]} queryVector - The vector to query with.
|
|
237
|
-
* @param {number} [topK] - The maximum number of results to return.
|
|
238
|
-
* @param {Record<string, any>} [filter] - An optional filter to apply to the query. For more on filters in OpenSearch, see the filtering reference: https://opensearch.org/docs/latest/query-dsl/
|
|
239
|
-
* @param {boolean} [includeVectors=false] - Whether to include the vectors in the response.
|
|
240
|
-
* @returns {Promise<QueryResult[]>} A promise that resolves to an array of query results.
|
|
241
|
-
*/
|
|
242
|
-
async query({
|
|
243
|
-
indexName,
|
|
244
|
-
queryVector,
|
|
245
|
-
filter,
|
|
246
|
-
topK = 10,
|
|
247
|
-
includeVector = false,
|
|
248
|
-
}: OpenSearchVectorParams): Promise<QueryResult[]> {
|
|
249
|
-
try {
|
|
250
|
-
const translatedFilter = this.transformFilter(filter);
|
|
251
|
-
|
|
252
|
-
const response = await this.client.search({
|
|
253
|
-
index: indexName,
|
|
254
|
-
body: {
|
|
255
|
-
query: {
|
|
256
|
-
bool: {
|
|
257
|
-
must: { knn: { embedding: { vector: queryVector, k: topK } } },
|
|
258
|
-
filter: translatedFilter ? [translatedFilter] : [],
|
|
259
|
-
},
|
|
260
|
-
},
|
|
261
|
-
_source: ['id', 'metadata', 'embedding'],
|
|
262
|
-
},
|
|
263
|
-
});
|
|
264
|
-
|
|
265
|
-
const results = response.body.hits.hits.map((hit: any) => {
|
|
266
|
-
const source = hit._source || {};
|
|
267
|
-
return {
|
|
268
|
-
id: String(source.id || ''),
|
|
269
|
-
score: typeof hit._score === 'number' ? hit._score : 0,
|
|
270
|
-
metadata: source.metadata || {},
|
|
271
|
-
...(includeVector && { vector: source.embedding as number[] }),
|
|
272
|
-
};
|
|
273
|
-
});
|
|
274
|
-
|
|
275
|
-
return results;
|
|
276
|
-
} catch (error) {
|
|
277
|
-
throw new MastraError(
|
|
278
|
-
{
|
|
279
|
-
id: 'STORAGE_OPENSEARCH_VECTOR_QUERY_FAILED',
|
|
280
|
-
domain: ErrorDomain.STORAGE,
|
|
281
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
282
|
-
details: { indexName, topK },
|
|
283
|
-
},
|
|
284
|
-
error,
|
|
285
|
-
);
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
/**
|
|
290
|
-
* Validates the dimensions of the vectors.
|
|
291
|
-
*
|
|
292
|
-
* @param {number[][]} vectors - The vectors to validate.
|
|
293
|
-
* @param {number} dimension - The dimension of the vectors.
|
|
294
|
-
* @returns {void}
|
|
295
|
-
*/
|
|
296
|
-
private validateVectorDimensions(vectors: number[][], dimension: number) {
|
|
297
|
-
if (vectors.some(vector => vector.length !== dimension)) {
|
|
298
|
-
throw new Error('Vector dimension does not match index dimension');
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
/**
|
|
303
|
-
* Transforms the filter to the OpenSearch DSL.
|
|
304
|
-
*
|
|
305
|
-
* @param {OpenSearchVectorFilter} filter - The filter to transform.
|
|
306
|
-
* @returns {Record<string, any>} The transformed filter.
|
|
307
|
-
*/
|
|
308
|
-
private transformFilter(filter?: OpenSearchVectorFilter): any {
|
|
309
|
-
const translator = new OpenSearchFilterTranslator();
|
|
310
|
-
return translator.translate(filter);
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
/**
|
|
314
|
-
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
315
|
-
* @param indexName - The name of the index containing the vector.
|
|
316
|
-
* @param id - The ID of the vector to update.
|
|
317
|
-
* @param update - An object containing the vector and/or metadata to update.
|
|
318
|
-
* @param update.vector - An optional array of numbers representing the new vector.
|
|
319
|
-
* @param update.metadata - An optional record containing the new metadata.
|
|
320
|
-
* @returns A promise that resolves when the update is complete.
|
|
321
|
-
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
322
|
-
*/
|
|
323
|
-
async updateVector({ indexName, id, update }: UpdateVectorParams): Promise<void> {
|
|
324
|
-
let existingDoc;
|
|
325
|
-
try {
|
|
326
|
-
if (!update.vector && !update.metadata) {
|
|
327
|
-
throw new Error('No updates provided');
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
// First get the current document to merge with updates
|
|
331
|
-
const { body } = await this.client
|
|
332
|
-
|
|
333
|
-
.get({
|
|
334
|
-
index: indexName,
|
|
335
|
-
id: id,
|
|
336
|
-
})
|
|
337
|
-
.catch(() => {
|
|
338
|
-
throw new Error(`Document with ID ${id} not found in index ${indexName}`);
|
|
339
|
-
});
|
|
340
|
-
|
|
341
|
-
if (!body || !body._source) {
|
|
342
|
-
throw new Error(`Document with ID ${id} has no source data in index ${indexName}`);
|
|
343
|
-
}
|
|
344
|
-
existingDoc = body;
|
|
345
|
-
} catch (error) {
|
|
346
|
-
throw new MastraError(
|
|
347
|
-
{
|
|
348
|
-
id: 'STORAGE_OPENSEARCH_VECTOR_UPDATE_VECTOR_FAILED',
|
|
349
|
-
domain: ErrorDomain.STORAGE,
|
|
350
|
-
category: ErrorCategory.USER,
|
|
351
|
-
details: { indexName, id },
|
|
352
|
-
},
|
|
353
|
-
error,
|
|
354
|
-
);
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
const source = existingDoc._source;
|
|
358
|
-
const updatedDoc: Record<string, any> = {
|
|
359
|
-
id: source?.id || id,
|
|
360
|
-
};
|
|
361
|
-
|
|
362
|
-
try {
|
|
363
|
-
// Update vector if provided
|
|
364
|
-
if (update.vector) {
|
|
365
|
-
// Get index stats to check dimension
|
|
366
|
-
console.log(`1`);
|
|
367
|
-
const indexInfo = await this.describeIndex({ indexName });
|
|
368
|
-
|
|
369
|
-
// Validate vector dimensions
|
|
370
|
-
console.log(`2`);
|
|
371
|
-
this.validateVectorDimensions([update.vector], indexInfo.dimension);
|
|
372
|
-
|
|
373
|
-
updatedDoc.embedding = update.vector;
|
|
374
|
-
} else if (source?.embedding) {
|
|
375
|
-
updatedDoc.embedding = source.embedding;
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
// Update metadata if provided
|
|
379
|
-
if (update.metadata) {
|
|
380
|
-
updatedDoc.metadata = update.metadata;
|
|
381
|
-
} else {
|
|
382
|
-
updatedDoc.metadata = source?.metadata || {};
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
// Update the document
|
|
386
|
-
console.log(`3`);
|
|
387
|
-
await this.client.index({
|
|
388
|
-
index: indexName,
|
|
389
|
-
id: id,
|
|
390
|
-
body: updatedDoc,
|
|
391
|
-
refresh: true,
|
|
392
|
-
});
|
|
393
|
-
} catch (error) {
|
|
394
|
-
throw new MastraError(
|
|
395
|
-
{
|
|
396
|
-
id: 'STORAGE_OPENSEARCH_VECTOR_UPDATE_VECTOR_FAILED',
|
|
397
|
-
domain: ErrorDomain.STORAGE,
|
|
398
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
399
|
-
details: { indexName, id },
|
|
400
|
-
},
|
|
401
|
-
error,
|
|
402
|
-
);
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
/**
|
|
407
|
-
* Deletes a vector by its ID.
|
|
408
|
-
* @param indexName - The name of the index containing the vector.
|
|
409
|
-
* @param id - The ID of the vector to delete.
|
|
410
|
-
* @returns A promise that resolves when the deletion is complete.
|
|
411
|
-
* @throws Will throw an error if the deletion operation fails.
|
|
412
|
-
*/
|
|
413
|
-
async deleteVector({ indexName, id }: DeleteVectorParams): Promise<void> {
|
|
414
|
-
try {
|
|
415
|
-
await this.client.delete({
|
|
416
|
-
index: indexName,
|
|
417
|
-
id: id,
|
|
418
|
-
refresh: true,
|
|
419
|
-
});
|
|
420
|
-
} catch (error: unknown) {
|
|
421
|
-
// Don't throw error if document doesn't exist (404)
|
|
422
|
-
if (error && typeof error === 'object' && 'statusCode' in error && error.statusCode === 404) {
|
|
423
|
-
return;
|
|
424
|
-
}
|
|
425
|
-
throw new MastraError(
|
|
426
|
-
{
|
|
427
|
-
id: 'STORAGE_OPENSEARCH_VECTOR_DELETE_VECTOR_FAILED',
|
|
428
|
-
domain: ErrorDomain.STORAGE,
|
|
429
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
430
|
-
details: { indexName, id },
|
|
431
|
-
},
|
|
432
|
-
error,
|
|
433
|
-
);
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
}
|
package/src/vector/prompt.ts
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Vector store prompt for OpenSearch. This prompt details supported filter operators, syntax, and usage examples.
|
|
3
|
-
* Use this as a guide for constructing valid filters for OpenSearch vector queries in Mastra.
|
|
4
|
-
*/
|
|
5
|
-
export const OPENSEARCH_PROMPT = `When querying OpenSearch, you can ONLY use the operators listed below. Any other operators will be rejected.
|
|
6
|
-
Important: Do not explain how to construct the filter—use the specified operators and fields to search the content and return relevant results.
|
|
7
|
-
If a user tries to use an unsupported operator, reject the filter entirely and let them know that the operator is not supported.
|
|
8
|
-
|
|
9
|
-
Basic Comparison Operators:
|
|
10
|
-
- $eq: Exact match (default for field: value)
|
|
11
|
-
Example: { "category": "electronics" }
|
|
12
|
-
- $ne: Not equal
|
|
13
|
-
Example: { "category": { "$ne": "electronics" } }
|
|
14
|
-
- $gt: Greater than
|
|
15
|
-
Example: { "price": { "$gt": 100 } }
|
|
16
|
-
- $gte: Greater than or equal
|
|
17
|
-
Example: { "price": { "$gte": 100 } }
|
|
18
|
-
- $lt: Less than
|
|
19
|
-
Example: { "price": { "$lt": 100 } }
|
|
20
|
-
- $lte: Less than or equal
|
|
21
|
-
Example: { "price": { "$lte": 100 } }
|
|
22
|
-
|
|
23
|
-
Array Operators:
|
|
24
|
-
- $in: Match any value in array
|
|
25
|
-
Example: { "category": { "$in": ["electronics", "books"] } }
|
|
26
|
-
- $nin: Does not match any value in array
|
|
27
|
-
Example: { "category": { "$nin": ["electronics", "books"] } }
|
|
28
|
-
- $all: Match all values in array
|
|
29
|
-
Example: { "tags": { "$all": ["premium", "sale"] } }
|
|
30
|
-
|
|
31
|
-
Logical Operators:
|
|
32
|
-
- $and: Logical AND (implicit when using multiple conditions)
|
|
33
|
-
Example: { "$and": [{ "price": { "$gt": 100 } }, { "category": "electronics" }] }
|
|
34
|
-
- $or: Logical OR
|
|
35
|
-
Example: { "$or": [{ "price": { "$lt": 50 } }, { "category": "books" }] }
|
|
36
|
-
- $not: Logical NOT
|
|
37
|
-
Example: { "$not": { "category": "electronics" } }
|
|
38
|
-
|
|
39
|
-
Element Operators:
|
|
40
|
-
- $exists: Check if field exists
|
|
41
|
-
Example: { "rating": { "$exists": true } }
|
|
42
|
-
|
|
43
|
-
Regex Operator:
|
|
44
|
-
- $regex: Match using a regular expression (ECMAScript syntax)
|
|
45
|
-
Example: { "name": { "$regex": "^Sam.*son$" } }
|
|
46
|
-
Note: Regex queries are supported for string fields only. Use valid ECMAScript patterns; invalid patterns will throw an error.
|
|
47
|
-
|
|
48
|
-
Restrictions:
|
|
49
|
-
- Nested fields are supported using dot notation (e.g., "address.city").
|
|
50
|
-
- Multiple conditions on the same field are supported (e.g., { "price": { "$gte": 100, "$lte": 1000 } }).
|
|
51
|
-
- Only logical operators ($and, $or, $not) can be used at the top level.
|
|
52
|
-
- All other operators must be used within a field condition.
|
|
53
|
-
Valid: { "field": { "$gt": 100 } }
|
|
54
|
-
Valid: { "$and": [...] }
|
|
55
|
-
Invalid: { "$gt": 100 }
|
|
56
|
-
- Logical operators must contain field conditions, not direct operators.
|
|
57
|
-
Valid: { "$and": [{ "field": { "$gt": 100 } }] }
|
|
58
|
-
Invalid: { "$and": [{ "$gt": 100 }] }
|
|
59
|
-
- $not operator:
|
|
60
|
-
- Must be an object
|
|
61
|
-
- Cannot be empty
|
|
62
|
-
- Can be used at field level or top level
|
|
63
|
-
- Valid: { "$not": { "field": "value" } }
|
|
64
|
-
- Valid: { "field": { "$not": { "$eq": "value" } } }
|
|
65
|
-
- Array operators work on array fields only.
|
|
66
|
-
- Empty arrays in conditions are handled gracefully.
|
|
67
|
-
- Regex queries are case-sensitive by default; use patterns accordingly.
|
|
68
|
-
|
|
69
|
-
Example Complex Query:
|
|
70
|
-
{
|
|
71
|
-
"$and": [
|
|
72
|
-
{ "category": { "$in": ["electronics", "computers"] } },
|
|
73
|
-
{ "price": { "$gte": 100, "$lte": 1000 } },
|
|
74
|
-
{ "tags": { "$all": ["premium"] } },
|
|
75
|
-
{ "rating": { "$exists": true, "$gt": 4 } },
|
|
76
|
-
{ "$or": [
|
|
77
|
-
{ "stock": { "$gt": 0 } },
|
|
78
|
-
{ "preorder": true }
|
|
79
|
-
]},
|
|
80
|
-
{ "name": { "$regex": "^Sam.*son$" } }
|
|
81
|
-
]
|
|
82
|
-
}`;
|
package/tsconfig.build.json
DELETED
package/tsconfig.json
DELETED
package/tsup.config.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { generateTypes } from '@internal/types-builder';
|
|
2
|
-
import { defineConfig } from 'tsup';
|
|
3
|
-
|
|
4
|
-
export default defineConfig({
|
|
5
|
-
entry: ['src/index.ts'],
|
|
6
|
-
format: ['esm', 'cjs'],
|
|
7
|
-
clean: true,
|
|
8
|
-
dts: false,
|
|
9
|
-
splitting: true,
|
|
10
|
-
treeshake: {
|
|
11
|
-
preset: 'smallest',
|
|
12
|
-
},
|
|
13
|
-
sourcemap: true,
|
|
14
|
-
onSuccess: async () => {
|
|
15
|
-
await generateTypes(process.cwd());
|
|
16
|
-
},
|
|
17
|
-
});
|