@mastra/chroma 0.0.0-update-stores-peerDeps-20250723031338 → 0.0.0-usechat-duplicate-20251016110554
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 +380 -2
- package/README.md +47 -21
- package/dist/index.cjs +115 -49
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +116 -50
- package/dist/index.js.map +1 -0
- package/dist/vector/filter.d.ts +19 -0
- package/dist/vector/filter.d.ts.map +1 -0
- package/dist/vector/index.d.ts +70 -0
- package/dist/vector/index.d.ts.map +1 -0
- package/dist/vector/prompt.d.ts +6 -0
- package/dist/vector/prompt.d.ts.map +1 -0
- package/package.json +25 -10
- package/dist/_tsup-dts-rollup.d.cts +0 -102
- package/dist/_tsup-dts-rollup.d.ts +0 -102
- package/dist/index.d.cts +0 -2
- package/docker-compose.yaml +0 -7
- package/eslint.config.js +0 -6
- package/src/index.ts +0 -2
- package/src/vector/filter.test.ts +0 -418
- package/src/vector/filter.ts +0 -146
- package/src/vector/index.test.ts +0 -1550
- package/src/vector/index.ts +0 -340
- package/src/vector/prompt.ts +0 -72
- package/tsconfig.json +0 -5
- package/vitest.config.ts +0 -8
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
2
|
-
import type { BlacklistedRootOperators } from '@mastra/core/vector/filter';
|
|
3
|
-
import type { CreateIndexParams } from '@mastra/core/vector';
|
|
4
|
-
import type { DeleteIndexParams } from '@mastra/core/vector';
|
|
5
|
-
import type { DeleteVectorParams } from '@mastra/core/vector';
|
|
6
|
-
import type { DescribeIndexParams } from '@mastra/core/vector';
|
|
7
|
-
import type { IndexStats } from '@mastra/core/vector';
|
|
8
|
-
import type { LogicalOperatorValueMap } from '@mastra/core/vector/filter';
|
|
9
|
-
import { MastraVector } from '@mastra/core/vector';
|
|
10
|
-
import type { OperatorSupport } from '@mastra/core/vector/filter';
|
|
11
|
-
import type { OperatorValueMap } from '@mastra/core/vector/filter';
|
|
12
|
-
import type { QueryResult } from '@mastra/core/vector';
|
|
13
|
-
import type { QueryVectorParams } from '@mastra/core/vector';
|
|
14
|
-
import type { UpdateVectorParams } from '@mastra/core/vector';
|
|
15
|
-
import type { UpsertVectorParams } from '@mastra/core/vector';
|
|
16
|
-
import type { VectorFilter } from '@mastra/core/vector/filter';
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Vector store specific prompt that details supported operators and examples.
|
|
20
|
-
* This prompt helps users construct valid filters for Chroma Vector.
|
|
21
|
-
*/
|
|
22
|
-
declare const CHROMA_PROMPT = "When querying Chroma, you can ONLY use the operators listed below. Any other operators will be rejected.\nImportant: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.\nIf a user tries to give an explicit operator that is not supported, reject the filter entirely and let them know that the operator is not supported.\n\nBasic Comparison Operators:\n- $eq: Exact match (default when using field: value)\n Example: { \"category\": \"electronics\" }\n- $ne: Not equal\n Example: { \"category\": { \"$ne\": \"electronics\" } }\n- $gt: Greater than\n Example: { \"price\": { \"$gt\": 100 } }\n- $gte: Greater than or equal\n Example: { \"price\": { \"$gte\": 100 } }\n- $lt: Less than\n Example: { \"price\": { \"$lt\": 100 } }\n- $lte: Less than or equal\n Example: { \"price\": { \"$lte\": 100 } }\n\nArray Operators:\n- $in: Match any value in array\n Example: { \"category\": { \"$in\": [\"electronics\", \"books\"] } }\n- $nin: Does not match any value in array\n Example: { \"category\": { \"$nin\": [\"electronics\", \"books\"] } }\n\nLogical Operators:\n- $and: Logical AND\n Example: { \"$and\": [{ \"price\": { \"$gt\": 100 } }, { \"category\": \"electronics\" }] }\n- $or: Logical OR\n Example: { \"$or\": [{ \"price\": { \"$lt\": 50 } }, { \"category\": \"books\" }] }\n\nRestrictions:\n- Regex patterns are not supported\n- Element operators are not supported\n- Only $and and $or logical operators are supported\n- Nested fields are supported using dot notation\n- Multiple conditions on the same field are supported with both implicit and explicit $and\n- Empty arrays in $in/$nin will return no results\n- If multiple top-level fields exist, they're wrapped in $and\n- Only logical operators ($and, $or) can be used at the top level\n- All other operators must be used within a field condition\n Valid: { \"field\": { \"$gt\": 100 } }\n Valid: { \"$and\": [...] }\n Invalid: { \"$gt\": 100 }\n Invalid: { \"$in\": [...] }\n- Logical operators must contain field conditions, not direct operators\n Valid: { \"$and\": [{ \"field\": { \"$gt\": 100 } }] }\n Invalid: { \"$and\": [{ \"$gt\": 100 }] }\n- Logical operators ($and, $or):\n - Can only be used at top level or nested within other logical operators\n - Can not be used on a field level, or be nested inside a field\n - Can not be used inside an operator\n - Valid: { \"$and\": [{ \"field\": { \"$gt\": 100 } }] }\n - Valid: { \"$or\": [{ \"$and\": [{ \"field\": { \"$gt\": 100 } }] }] }\n - Invalid: { \"field\": { \"$and\": [{ \"$gt\": 100 }] } }\n - Invalid: { \"field\": { \"$or\": [{ \"$gt\": 100 }] } }\n - Invalid: { \"field\": { \"$gt\": { \"$and\": [{...}] } } }\n\nExample Complex Query:\n{\n \"$and\": [\n { \"category\": { \"$in\": [\"electronics\", \"computers\"] } },\n { \"price\": { \"$gte\": 100, \"$lte\": 1000 } },\n { \"$or\": [\n { \"inStock\": true },\n { \"preorder\": true }\n ]}\n ]\n}";
|
|
23
|
-
export { CHROMA_PROMPT }
|
|
24
|
-
export { CHROMA_PROMPT as CHROMA_PROMPT_alias_1 }
|
|
25
|
-
|
|
26
|
-
declare type ChromaBlacklisted = BlacklistedRootOperators | '$nor' | '$not';
|
|
27
|
-
|
|
28
|
-
declare type ChromaDocumentBlacklisted = Exclude<ChromaBlacklisted, '$contains'>;
|
|
29
|
-
|
|
30
|
-
declare type ChromaDocumentOperatorValueMap = ChromaOperatorValueMap;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Translator for Chroma filter queries.
|
|
34
|
-
* Maintains MongoDB-compatible syntax while ensuring proper validation
|
|
35
|
-
* and normalization of values.
|
|
36
|
-
*/
|
|
37
|
-
export declare class ChromaFilterTranslator extends BaseFilterTranslator<ChromaVectorFilter> {
|
|
38
|
-
protected getSupportedOperators(): OperatorSupport;
|
|
39
|
-
translate(filter?: ChromaVectorFilter): ChromaVectorFilter;
|
|
40
|
-
private translateNode;
|
|
41
|
-
private translateOperator;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
declare type ChromaLogicalOperatorValueMap = Omit<LogicalOperatorValueMap, '$nor' | '$not'>;
|
|
45
|
-
|
|
46
|
-
declare type ChromaOperatorValueMap = Omit<OperatorValueMap, '$exists' | '$elemMatch' | '$regex' | '$options'>;
|
|
47
|
-
|
|
48
|
-
declare interface ChromaQueryVectorParams extends QueryVectorParams<ChromaVectorFilter> {
|
|
49
|
-
documentFilter?: ChromaVectorDocumentFilter;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
declare interface ChromaUpsertVectorParams extends UpsertVectorParams {
|
|
53
|
-
documents?: string[];
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
declare class ChromaVector extends MastraVector<ChromaVectorFilter> {
|
|
57
|
-
private client;
|
|
58
|
-
private collections;
|
|
59
|
-
constructor({ path, auth, }: {
|
|
60
|
-
path: string;
|
|
61
|
-
auth?: {
|
|
62
|
-
provider: string;
|
|
63
|
-
credentials: string;
|
|
64
|
-
};
|
|
65
|
-
});
|
|
66
|
-
getCollection(indexName: string, throwIfNotExists?: boolean): Promise<any>;
|
|
67
|
-
private validateVectorDimensions;
|
|
68
|
-
upsert({ indexName, vectors, metadata, ids, documents }: ChromaUpsertVectorParams): Promise<string[]>;
|
|
69
|
-
private HnswSpaceMap;
|
|
70
|
-
createIndex({ indexName, dimension, metric }: CreateIndexParams): Promise<void>;
|
|
71
|
-
transformFilter(filter?: ChromaVectorFilter): ChromaVectorFilter;
|
|
72
|
-
query({ indexName, queryVector, topK, filter, includeVector, documentFilter, }: ChromaQueryVectorParams): Promise<QueryResult[]>;
|
|
73
|
-
listIndexes(): Promise<string[]>;
|
|
74
|
-
/**
|
|
75
|
-
* Retrieves statistics about a vector index.
|
|
76
|
-
*
|
|
77
|
-
* @param {string} indexName - The name of the index to describe
|
|
78
|
-
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
79
|
-
*/
|
|
80
|
-
describeIndex({ indexName }: DescribeIndexParams): Promise<IndexStats>;
|
|
81
|
-
deleteIndex({ indexName }: DeleteIndexParams): Promise<void>;
|
|
82
|
-
/**
|
|
83
|
-
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
84
|
-
* @param indexName - The name of the index containing the vector.
|
|
85
|
-
* @param id - The ID of the vector to update.
|
|
86
|
-
* @param update - An object containing the vector and/or metadata to update.
|
|
87
|
-
* @param update.vector - An optional array of numbers representing the new vector.
|
|
88
|
-
* @param update.metadata - An optional record containing the new metadata.
|
|
89
|
-
* @returns A promise that resolves when the update is complete.
|
|
90
|
-
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
91
|
-
*/
|
|
92
|
-
updateVector({ indexName, id, update }: UpdateVectorParams): Promise<void>;
|
|
93
|
-
deleteVector({ indexName, id }: DeleteVectorParams): Promise<void>;
|
|
94
|
-
}
|
|
95
|
-
export { ChromaVector }
|
|
96
|
-
export { ChromaVector as ChromaVector_alias_1 }
|
|
97
|
-
|
|
98
|
-
export declare type ChromaVectorDocumentFilter = VectorFilter<keyof ChromaDocumentOperatorValueMap, ChromaDocumentOperatorValueMap, ChromaLogicalOperatorValueMap, ChromaDocumentBlacklisted>;
|
|
99
|
-
|
|
100
|
-
export declare type ChromaVectorFilter = VectorFilter<keyof ChromaOperatorValueMap, ChromaOperatorValueMap, ChromaLogicalOperatorValueMap, ChromaBlacklisted>;
|
|
101
|
-
|
|
102
|
-
export { }
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
2
|
-
import type { BlacklistedRootOperators } from '@mastra/core/vector/filter';
|
|
3
|
-
import type { CreateIndexParams } from '@mastra/core/vector';
|
|
4
|
-
import type { DeleteIndexParams } from '@mastra/core/vector';
|
|
5
|
-
import type { DeleteVectorParams } from '@mastra/core/vector';
|
|
6
|
-
import type { DescribeIndexParams } from '@mastra/core/vector';
|
|
7
|
-
import type { IndexStats } from '@mastra/core/vector';
|
|
8
|
-
import type { LogicalOperatorValueMap } from '@mastra/core/vector/filter';
|
|
9
|
-
import { MastraVector } from '@mastra/core/vector';
|
|
10
|
-
import type { OperatorSupport } from '@mastra/core/vector/filter';
|
|
11
|
-
import type { OperatorValueMap } from '@mastra/core/vector/filter';
|
|
12
|
-
import type { QueryResult } from '@mastra/core/vector';
|
|
13
|
-
import type { QueryVectorParams } from '@mastra/core/vector';
|
|
14
|
-
import type { UpdateVectorParams } from '@mastra/core/vector';
|
|
15
|
-
import type { UpsertVectorParams } from '@mastra/core/vector';
|
|
16
|
-
import type { VectorFilter } from '@mastra/core/vector/filter';
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Vector store specific prompt that details supported operators and examples.
|
|
20
|
-
* This prompt helps users construct valid filters for Chroma Vector.
|
|
21
|
-
*/
|
|
22
|
-
declare const CHROMA_PROMPT = "When querying Chroma, you can ONLY use the operators listed below. Any other operators will be rejected.\nImportant: Don't explain how to construct the filter - use the specified operators and fields to search the content and return relevant results.\nIf a user tries to give an explicit operator that is not supported, reject the filter entirely and let them know that the operator is not supported.\n\nBasic Comparison Operators:\n- $eq: Exact match (default when using field: value)\n Example: { \"category\": \"electronics\" }\n- $ne: Not equal\n Example: { \"category\": { \"$ne\": \"electronics\" } }\n- $gt: Greater than\n Example: { \"price\": { \"$gt\": 100 } }\n- $gte: Greater than or equal\n Example: { \"price\": { \"$gte\": 100 } }\n- $lt: Less than\n Example: { \"price\": { \"$lt\": 100 } }\n- $lte: Less than or equal\n Example: { \"price\": { \"$lte\": 100 } }\n\nArray Operators:\n- $in: Match any value in array\n Example: { \"category\": { \"$in\": [\"electronics\", \"books\"] } }\n- $nin: Does not match any value in array\n Example: { \"category\": { \"$nin\": [\"electronics\", \"books\"] } }\n\nLogical Operators:\n- $and: Logical AND\n Example: { \"$and\": [{ \"price\": { \"$gt\": 100 } }, { \"category\": \"electronics\" }] }\n- $or: Logical OR\n Example: { \"$or\": [{ \"price\": { \"$lt\": 50 } }, { \"category\": \"books\" }] }\n\nRestrictions:\n- Regex patterns are not supported\n- Element operators are not supported\n- Only $and and $or logical operators are supported\n- Nested fields are supported using dot notation\n- Multiple conditions on the same field are supported with both implicit and explicit $and\n- Empty arrays in $in/$nin will return no results\n- If multiple top-level fields exist, they're wrapped in $and\n- Only logical operators ($and, $or) can be used at the top level\n- All other operators must be used within a field condition\n Valid: { \"field\": { \"$gt\": 100 } }\n Valid: { \"$and\": [...] }\n Invalid: { \"$gt\": 100 }\n Invalid: { \"$in\": [...] }\n- Logical operators must contain field conditions, not direct operators\n Valid: { \"$and\": [{ \"field\": { \"$gt\": 100 } }] }\n Invalid: { \"$and\": [{ \"$gt\": 100 }] }\n- Logical operators ($and, $or):\n - Can only be used at top level or nested within other logical operators\n - Can not be used on a field level, or be nested inside a field\n - Can not be used inside an operator\n - Valid: { \"$and\": [{ \"field\": { \"$gt\": 100 } }] }\n - Valid: { \"$or\": [{ \"$and\": [{ \"field\": { \"$gt\": 100 } }] }] }\n - Invalid: { \"field\": { \"$and\": [{ \"$gt\": 100 }] } }\n - Invalid: { \"field\": { \"$or\": [{ \"$gt\": 100 }] } }\n - Invalid: { \"field\": { \"$gt\": { \"$and\": [{...}] } } }\n\nExample Complex Query:\n{\n \"$and\": [\n { \"category\": { \"$in\": [\"electronics\", \"computers\"] } },\n { \"price\": { \"$gte\": 100, \"$lte\": 1000 } },\n { \"$or\": [\n { \"inStock\": true },\n { \"preorder\": true }\n ]}\n ]\n}";
|
|
23
|
-
export { CHROMA_PROMPT }
|
|
24
|
-
export { CHROMA_PROMPT as CHROMA_PROMPT_alias_1 }
|
|
25
|
-
|
|
26
|
-
declare type ChromaBlacklisted = BlacklistedRootOperators | '$nor' | '$not';
|
|
27
|
-
|
|
28
|
-
declare type ChromaDocumentBlacklisted = Exclude<ChromaBlacklisted, '$contains'>;
|
|
29
|
-
|
|
30
|
-
declare type ChromaDocumentOperatorValueMap = ChromaOperatorValueMap;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Translator for Chroma filter queries.
|
|
34
|
-
* Maintains MongoDB-compatible syntax while ensuring proper validation
|
|
35
|
-
* and normalization of values.
|
|
36
|
-
*/
|
|
37
|
-
export declare class ChromaFilterTranslator extends BaseFilterTranslator<ChromaVectorFilter> {
|
|
38
|
-
protected getSupportedOperators(): OperatorSupport;
|
|
39
|
-
translate(filter?: ChromaVectorFilter): ChromaVectorFilter;
|
|
40
|
-
private translateNode;
|
|
41
|
-
private translateOperator;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
declare type ChromaLogicalOperatorValueMap = Omit<LogicalOperatorValueMap, '$nor' | '$not'>;
|
|
45
|
-
|
|
46
|
-
declare type ChromaOperatorValueMap = Omit<OperatorValueMap, '$exists' | '$elemMatch' | '$regex' | '$options'>;
|
|
47
|
-
|
|
48
|
-
declare interface ChromaQueryVectorParams extends QueryVectorParams<ChromaVectorFilter> {
|
|
49
|
-
documentFilter?: ChromaVectorDocumentFilter;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
declare interface ChromaUpsertVectorParams extends UpsertVectorParams {
|
|
53
|
-
documents?: string[];
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
declare class ChromaVector extends MastraVector<ChromaVectorFilter> {
|
|
57
|
-
private client;
|
|
58
|
-
private collections;
|
|
59
|
-
constructor({ path, auth, }: {
|
|
60
|
-
path: string;
|
|
61
|
-
auth?: {
|
|
62
|
-
provider: string;
|
|
63
|
-
credentials: string;
|
|
64
|
-
};
|
|
65
|
-
});
|
|
66
|
-
getCollection(indexName: string, throwIfNotExists?: boolean): Promise<any>;
|
|
67
|
-
private validateVectorDimensions;
|
|
68
|
-
upsert({ indexName, vectors, metadata, ids, documents }: ChromaUpsertVectorParams): Promise<string[]>;
|
|
69
|
-
private HnswSpaceMap;
|
|
70
|
-
createIndex({ indexName, dimension, metric }: CreateIndexParams): Promise<void>;
|
|
71
|
-
transformFilter(filter?: ChromaVectorFilter): ChromaVectorFilter;
|
|
72
|
-
query({ indexName, queryVector, topK, filter, includeVector, documentFilter, }: ChromaQueryVectorParams): Promise<QueryResult[]>;
|
|
73
|
-
listIndexes(): Promise<string[]>;
|
|
74
|
-
/**
|
|
75
|
-
* Retrieves statistics about a vector index.
|
|
76
|
-
*
|
|
77
|
-
* @param {string} indexName - The name of the index to describe
|
|
78
|
-
* @returns A promise that resolves to the index statistics including dimension, count and metric
|
|
79
|
-
*/
|
|
80
|
-
describeIndex({ indexName }: DescribeIndexParams): Promise<IndexStats>;
|
|
81
|
-
deleteIndex({ indexName }: DeleteIndexParams): Promise<void>;
|
|
82
|
-
/**
|
|
83
|
-
* Updates a vector by its ID with the provided vector and/or metadata.
|
|
84
|
-
* @param indexName - The name of the index containing the vector.
|
|
85
|
-
* @param id - The ID of the vector to update.
|
|
86
|
-
* @param update - An object containing the vector and/or metadata to update.
|
|
87
|
-
* @param update.vector - An optional array of numbers representing the new vector.
|
|
88
|
-
* @param update.metadata - An optional record containing the new metadata.
|
|
89
|
-
* @returns A promise that resolves when the update is complete.
|
|
90
|
-
* @throws Will throw an error if no updates are provided or if the update operation fails.
|
|
91
|
-
*/
|
|
92
|
-
updateVector({ indexName, id, update }: UpdateVectorParams): Promise<void>;
|
|
93
|
-
deleteVector({ indexName, id }: DeleteVectorParams): Promise<void>;
|
|
94
|
-
}
|
|
95
|
-
export { ChromaVector }
|
|
96
|
-
export { ChromaVector as ChromaVector_alias_1 }
|
|
97
|
-
|
|
98
|
-
export declare type ChromaVectorDocumentFilter = VectorFilter<keyof ChromaDocumentOperatorValueMap, ChromaDocumentOperatorValueMap, ChromaLogicalOperatorValueMap, ChromaDocumentBlacklisted>;
|
|
99
|
-
|
|
100
|
-
export declare type ChromaVectorFilter = VectorFilter<keyof ChromaOperatorValueMap, ChromaOperatorValueMap, ChromaLogicalOperatorValueMap, ChromaBlacklisted>;
|
|
101
|
-
|
|
102
|
-
export { }
|
package/dist/index.d.cts
DELETED
package/docker-compose.yaml
DELETED
package/eslint.config.js
DELETED
package/src/index.ts
DELETED
|
@@ -1,418 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect, beforeEach } from 'vitest';
|
|
2
|
-
|
|
3
|
-
import type { ChromaVectorFilter } from './filter';
|
|
4
|
-
import { ChromaFilterTranslator } from './filter';
|
|
5
|
-
|
|
6
|
-
describe('ChromaFilterTranslator', () => {
|
|
7
|
-
let translator: ChromaFilterTranslator;
|
|
8
|
-
|
|
9
|
-
beforeEach(() => {
|
|
10
|
-
translator = new ChromaFilterTranslator();
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
// Basic Filter Operations
|
|
14
|
-
describe('basic operations', () => {
|
|
15
|
-
it('handles empty filters', () => {
|
|
16
|
-
expect(translator.translate({})).toEqual({});
|
|
17
|
-
expect(translator.translate(null)).toEqual(null);
|
|
18
|
-
expect(translator.translate(undefined)).toEqual(undefined);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
it('retains implicit equality', () => {
|
|
22
|
-
const filter: ChromaVectorFilter = { field: 'value' };
|
|
23
|
-
expect(translator.translate(filter)).toEqual({ field: 'value' });
|
|
24
|
-
});
|
|
25
|
-
|
|
26
|
-
it('converts multiple top-level fields to $and', () => {
|
|
27
|
-
const filter: ChromaVectorFilter = {
|
|
28
|
-
field1: 'value1',
|
|
29
|
-
field2: 'value2',
|
|
30
|
-
};
|
|
31
|
-
expect(translator.translate(filter)).toEqual({
|
|
32
|
-
$and: [{ field1: 'value1' }, { field2: 'value2' }],
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('handles multiple operators on same field', () => {
|
|
37
|
-
const filter: ChromaVectorFilter = {
|
|
38
|
-
price: { $gt: 100, $lt: 200 },
|
|
39
|
-
quantity: { $gte: 10, $lte: 20 },
|
|
40
|
-
};
|
|
41
|
-
expect(translator.translate(filter)).toEqual({
|
|
42
|
-
$and: [
|
|
43
|
-
{ price: { $gt: 100 } },
|
|
44
|
-
{ price: { $lt: 200 } },
|
|
45
|
-
{ quantity: { $gte: 10 } },
|
|
46
|
-
{ quantity: { $lte: 20 } },
|
|
47
|
-
],
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it('normalizes date values', () => {
|
|
52
|
-
const date = new Date('2024-01-01');
|
|
53
|
-
const filter: ChromaVectorFilter = { timestamp: { $gt: date } };
|
|
54
|
-
expect(translator.translate(filter)).toEqual({ timestamp: { $gt: date.toISOString() } });
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
// Array Operations
|
|
59
|
-
describe('array operations', () => {
|
|
60
|
-
it('handles arrays as $in operator', () => {
|
|
61
|
-
const filter: ChromaVectorFilter = { tags: ['tag1', 'tag2'] };
|
|
62
|
-
expect(translator.translate(filter)).toEqual({ tags: { $in: ['tag1', 'tag2'] } });
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it('handles empty array values', () => {
|
|
66
|
-
// $in with empty array is valid in Pinecone
|
|
67
|
-
expect(translator.translate({ tags: { $in: [] } })).toEqual({ tags: { $in: [] } });
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it('handles arrays as direct values', () => {
|
|
71
|
-
// Direct array value should be converted to $in
|
|
72
|
-
const filter: ChromaVectorFilter = { field: ['value1', 'value2'] };
|
|
73
|
-
expect(translator.translate(filter)).toEqual({
|
|
74
|
-
field: { $in: ['value1', 'value2'] },
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
// Empty direct array
|
|
78
|
-
const filter2 = { field: [] };
|
|
79
|
-
expect(translator.translate(filter2)).toEqual({ field: { $in: [] } });
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
describe('$in operator variations', () => {
|
|
83
|
-
it('handles $in with various values', () => {
|
|
84
|
-
// Empty array
|
|
85
|
-
expect(translator.translate({ field: { $in: [] } })).toEqual({ field: { $in: [] } });
|
|
86
|
-
|
|
87
|
-
// Single value
|
|
88
|
-
expect(translator.translate({ field: { $in: ['value'] } })).toEqual({ field: { $in: ['value'] } });
|
|
89
|
-
|
|
90
|
-
// Multiple values
|
|
91
|
-
expect(translator.translate({ field: { $in: [1, 'two', true] } })).toEqual({
|
|
92
|
-
field: { $in: [1, 'two', true] },
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
// With dates
|
|
96
|
-
const date = new Date('2024-01-01');
|
|
97
|
-
expect(translator.translate({ field: { $in: [date.toISOString()] } })).toEqual({
|
|
98
|
-
field: { $in: [date.toISOString()] },
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
// Logical Operators
|
|
105
|
-
describe('logical operators', () => {
|
|
106
|
-
it('handles logical operators', () => {
|
|
107
|
-
const filter: ChromaVectorFilter = {
|
|
108
|
-
$or: [{ status: { $eq: 'active' } }, { age: { $gt: 25 } }],
|
|
109
|
-
};
|
|
110
|
-
expect(translator.translate(filter)).toEqual({
|
|
111
|
-
$or: [{ status: { $eq: 'active' } }, { age: { $gt: 25 } }],
|
|
112
|
-
});
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
it('handles nested logical operators', () => {
|
|
116
|
-
const filter: ChromaVectorFilter = {
|
|
117
|
-
$and: [
|
|
118
|
-
{ status: { $eq: 'active' } },
|
|
119
|
-
{
|
|
120
|
-
$or: [{ category: { $in: ['A', 'B'] } }, { $and: [{ price: { $gt: 100 } }, { stock: { $lt: 50 } }] }],
|
|
121
|
-
},
|
|
122
|
-
],
|
|
123
|
-
};
|
|
124
|
-
expect(translator.translate(filter)).toEqual({
|
|
125
|
-
$and: [
|
|
126
|
-
{ status: { $eq: 'active' } },
|
|
127
|
-
{
|
|
128
|
-
$or: [{ category: { $in: ['A', 'B'] } }, { $and: [{ price: { $gt: 100 } }, { stock: { $lt: 50 } }] }],
|
|
129
|
-
},
|
|
130
|
-
],
|
|
131
|
-
});
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
it('handles nested arrays in logical operators', () => {
|
|
135
|
-
expect(
|
|
136
|
-
translator.translate({
|
|
137
|
-
$and: [{ field1: { $in: ['a', 'b'] } }, { field2: { $in: ['c', 'd'] } }],
|
|
138
|
-
}),
|
|
139
|
-
).toEqual({
|
|
140
|
-
$and: [
|
|
141
|
-
{ field1: { $in: ['a', 'b'] } },
|
|
142
|
-
{
|
|
143
|
-
field2: { $in: ['c', 'd'] },
|
|
144
|
-
},
|
|
145
|
-
],
|
|
146
|
-
});
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
it('handles complex nested conditions', () => {
|
|
150
|
-
const filter: ChromaVectorFilter = {
|
|
151
|
-
$or: [
|
|
152
|
-
{ age: { $gt: 25 } },
|
|
153
|
-
{
|
|
154
|
-
status: { $eq: 'active' },
|
|
155
|
-
theme: 'dark',
|
|
156
|
-
},
|
|
157
|
-
],
|
|
158
|
-
};
|
|
159
|
-
expect(translator.translate(filter)).toEqual({
|
|
160
|
-
$or: [
|
|
161
|
-
{ age: { $gt: 25 } },
|
|
162
|
-
{
|
|
163
|
-
$and: [{ status: { $eq: 'active' } }, { theme: 'dark' }],
|
|
164
|
-
},
|
|
165
|
-
],
|
|
166
|
-
});
|
|
167
|
-
});
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
// Nested Objects and Fields
|
|
171
|
-
describe('nested objects and fields', () => {
|
|
172
|
-
it('flattens nested objects to dot notation', () => {
|
|
173
|
-
const filter = {
|
|
174
|
-
user: {
|
|
175
|
-
profile: {
|
|
176
|
-
age: { $gt: 25 },
|
|
177
|
-
},
|
|
178
|
-
},
|
|
179
|
-
};
|
|
180
|
-
expect(translator.translate(filter)).toEqual({ 'user.profile.age': { $gt: 25 } });
|
|
181
|
-
});
|
|
182
|
-
|
|
183
|
-
it('preserves empty objects as exact match conditions', () => {
|
|
184
|
-
const filter: ChromaVectorFilter = {
|
|
185
|
-
metadata: {},
|
|
186
|
-
'user.profile': {},
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
expect(translator.translate(filter)).toEqual({
|
|
190
|
-
$and: [{ metadata: {} }, { 'user.profile': {} }],
|
|
191
|
-
});
|
|
192
|
-
});
|
|
193
|
-
|
|
194
|
-
it('handles empty objects in logical operators', () => {
|
|
195
|
-
const filter: ChromaVectorFilter = {
|
|
196
|
-
$or: [{}, { status: 'active' }],
|
|
197
|
-
};
|
|
198
|
-
|
|
199
|
-
expect(translator.translate(filter)).toEqual({
|
|
200
|
-
$or: [{}, { status: 'active' }],
|
|
201
|
-
});
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
it('preserves empty objects in nested structures', () => {
|
|
205
|
-
const filter = {
|
|
206
|
-
user: {
|
|
207
|
-
profile: {
|
|
208
|
-
settings: {},
|
|
209
|
-
},
|
|
210
|
-
},
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
expect(translator.translate(filter)).toEqual({
|
|
214
|
-
'user.profile.settings': {},
|
|
215
|
-
});
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
it('handles empty objects in comparison operators', () => {
|
|
219
|
-
const filter: ChromaVectorFilter = {
|
|
220
|
-
metadata: { $eq: {} },
|
|
221
|
-
};
|
|
222
|
-
|
|
223
|
-
expect(translator.translate(filter)).toEqual({
|
|
224
|
-
metadata: { $eq: {} },
|
|
225
|
-
});
|
|
226
|
-
});
|
|
227
|
-
|
|
228
|
-
it('handles empty objects in array operators', () => {
|
|
229
|
-
const filter: ChromaVectorFilter = {
|
|
230
|
-
tags: { $in: [{}] },
|
|
231
|
-
};
|
|
232
|
-
|
|
233
|
-
expect(translator.translate(filter)).toEqual({
|
|
234
|
-
tags: { $in: [{}] },
|
|
235
|
-
});
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
it('handles multiple empty nested fields', () => {
|
|
239
|
-
const filter = {
|
|
240
|
-
metadata: {},
|
|
241
|
-
settings: {},
|
|
242
|
-
config: { nested: {} },
|
|
243
|
-
};
|
|
244
|
-
|
|
245
|
-
expect(translator.translate(filter)).toEqual({
|
|
246
|
-
$and: [{ metadata: {} }, { settings: {} }, { 'config.nested': {} }],
|
|
247
|
-
});
|
|
248
|
-
});
|
|
249
|
-
});
|
|
250
|
-
|
|
251
|
-
// Operator Validation
|
|
252
|
-
describe('operator validation', () => {
|
|
253
|
-
describe('logical operator validation', () => {
|
|
254
|
-
it('allows $and and $or at root level', () => {
|
|
255
|
-
const validFilters = [
|
|
256
|
-
{
|
|
257
|
-
$and: [{ field1: 'value1' }, { field2: 'value2' }],
|
|
258
|
-
},
|
|
259
|
-
{
|
|
260
|
-
$or: [{ field1: 'value1' }, { field2: 'value2' }],
|
|
261
|
-
},
|
|
262
|
-
{
|
|
263
|
-
$and: [{ field1: 'value1' }],
|
|
264
|
-
$or: [{ field2: 'value2' }],
|
|
265
|
-
},
|
|
266
|
-
];
|
|
267
|
-
|
|
268
|
-
validFilters.forEach(filter => {
|
|
269
|
-
expect(() => translator.translate(filter)).not.toThrow();
|
|
270
|
-
});
|
|
271
|
-
});
|
|
272
|
-
|
|
273
|
-
it('allows nested $and and $or within other logical operators', () => {
|
|
274
|
-
const validFilters = [
|
|
275
|
-
{
|
|
276
|
-
$and: [
|
|
277
|
-
{ field1: 'value1' },
|
|
278
|
-
{
|
|
279
|
-
$or: [{ field2: 'value2' }, { field3: 'value3' }],
|
|
280
|
-
},
|
|
281
|
-
],
|
|
282
|
-
},
|
|
283
|
-
{
|
|
284
|
-
$or: [
|
|
285
|
-
{ field1: 'value1' },
|
|
286
|
-
{
|
|
287
|
-
$and: [{ field2: 'value2' }, { field3: 'value3' }],
|
|
288
|
-
},
|
|
289
|
-
],
|
|
290
|
-
},
|
|
291
|
-
];
|
|
292
|
-
|
|
293
|
-
validFilters.forEach(filter => {
|
|
294
|
-
expect(() => translator.translate(filter)).not.toThrow();
|
|
295
|
-
});
|
|
296
|
-
});
|
|
297
|
-
|
|
298
|
-
it('throws error for logical operators in field-level conditions', () => {
|
|
299
|
-
const invalidFilters = [
|
|
300
|
-
{
|
|
301
|
-
field: {
|
|
302
|
-
$and: [{ $eq: 'value1' }, { $eq: 'value2' }],
|
|
303
|
-
},
|
|
304
|
-
},
|
|
305
|
-
{
|
|
306
|
-
field: {
|
|
307
|
-
$or: [{ $eq: 'value1' }, { $eq: 'value2' }],
|
|
308
|
-
},
|
|
309
|
-
},
|
|
310
|
-
{
|
|
311
|
-
nested: {
|
|
312
|
-
field: {
|
|
313
|
-
$and: [{ $eq: 'value1' }, { $eq: 'value2' }],
|
|
314
|
-
},
|
|
315
|
-
},
|
|
316
|
-
},
|
|
317
|
-
];
|
|
318
|
-
|
|
319
|
-
invalidFilters.forEach(filter => {
|
|
320
|
-
expect(() => translator.translate(filter)).toThrow(/cannot be used at field level/);
|
|
321
|
-
});
|
|
322
|
-
});
|
|
323
|
-
|
|
324
|
-
it('throws error for direct operators in logical operator arrays', () => {
|
|
325
|
-
const invalidFilters = [
|
|
326
|
-
{
|
|
327
|
-
$and: [{ $eq: 'value' }, { $gt: 100 }],
|
|
328
|
-
},
|
|
329
|
-
{
|
|
330
|
-
$or: [{ $in: ['value1', 'value2'] }],
|
|
331
|
-
},
|
|
332
|
-
{
|
|
333
|
-
$and: [{ field1: 'value1' }, { $or: [{ $eq: 'value2' }] }],
|
|
334
|
-
},
|
|
335
|
-
];
|
|
336
|
-
|
|
337
|
-
invalidFilters.forEach(filter => {
|
|
338
|
-
expect(() => translator.translate(filter)).toThrow(/must contain field conditions/);
|
|
339
|
-
});
|
|
340
|
-
});
|
|
341
|
-
|
|
342
|
-
it('throws error for unsupported logical operators', () => {
|
|
343
|
-
const invalidFilters: any = [
|
|
344
|
-
{
|
|
345
|
-
$not: { field: 'value' },
|
|
346
|
-
},
|
|
347
|
-
{
|
|
348
|
-
$nor: [{ field: 'value' }],
|
|
349
|
-
},
|
|
350
|
-
{
|
|
351
|
-
$and: [{ field1: 'value1' }, { $nor: [{ field2: 'value2' }] }],
|
|
352
|
-
},
|
|
353
|
-
{
|
|
354
|
-
field: { $not: { $eq: 'value' } },
|
|
355
|
-
},
|
|
356
|
-
];
|
|
357
|
-
|
|
358
|
-
invalidFilters.forEach(filter => {
|
|
359
|
-
expect(() => translator.translate(filter)).toThrow(/Unsupported operator/);
|
|
360
|
-
});
|
|
361
|
-
});
|
|
362
|
-
});
|
|
363
|
-
|
|
364
|
-
it('ensure all operator filters are supported', () => {
|
|
365
|
-
const supportedFilters = [
|
|
366
|
-
{ field: { $eq: 'value' } },
|
|
367
|
-
{ field: { $ne: 'value' } },
|
|
368
|
-
{ field: { $gt: 'value' } },
|
|
369
|
-
{ field: { $gte: 'value' } },
|
|
370
|
-
{ field: { $lt: 'value' } },
|
|
371
|
-
{ field: { $lte: 'value' } },
|
|
372
|
-
{ field: { $in: ['value'] } },
|
|
373
|
-
{ $and: [{ field: { $eq: 'value' } }] },
|
|
374
|
-
{ $or: [{ field: { $eq: 'value' } }] },
|
|
375
|
-
];
|
|
376
|
-
supportedFilters.forEach(filter => {
|
|
377
|
-
expect(() => translator.translate(filter)).not.toThrow();
|
|
378
|
-
});
|
|
379
|
-
});
|
|
380
|
-
|
|
381
|
-
it('throws error for unsupported operators', () => {
|
|
382
|
-
const unsupportedFilters: any = [
|
|
383
|
-
{ field: { $regex: 'pattern' } },
|
|
384
|
-
{ field: { $contains: 'value' } },
|
|
385
|
-
{ field: { $exists: true } },
|
|
386
|
-
{ field: { $elemMatch: { $gt: 5 } } },
|
|
387
|
-
{ field: { $nor: [{ $eq: 'value' }] } },
|
|
388
|
-
{ field: { $not: [{ $eq: 'value' }] } },
|
|
389
|
-
{ field: { $regex: 'pattern', $options: 'i' } },
|
|
390
|
-
{ field: { $all: [{ $eq: 'value' }] } },
|
|
391
|
-
];
|
|
392
|
-
|
|
393
|
-
unsupportedFilters.forEach(filter => {
|
|
394
|
-
expect(() => translator.translate(filter)).toThrow(/Unsupported operator/);
|
|
395
|
-
});
|
|
396
|
-
});
|
|
397
|
-
|
|
398
|
-
it('throws error for regex operators', () => {
|
|
399
|
-
const filter = { field: /pattern/i };
|
|
400
|
-
expect(() => translator.translate(filter)).toThrow();
|
|
401
|
-
});
|
|
402
|
-
it('throws error for non-logical operators at top level', () => {
|
|
403
|
-
const invalidFilters: any = [{ $gt: 100 }, { $in: ['value1', 'value2'] }, { $eq: true }];
|
|
404
|
-
|
|
405
|
-
invalidFilters.forEach(filter => {
|
|
406
|
-
expect(() => translator.translate(filter)).toThrow(/Invalid top-level operator/);
|
|
407
|
-
});
|
|
408
|
-
});
|
|
409
|
-
|
|
410
|
-
it('allows logical operators at top level', () => {
|
|
411
|
-
const validFilters = [{ $and: [{ field: 'value' }] }, { $or: [{ field: 'value' }] }];
|
|
412
|
-
|
|
413
|
-
validFilters.forEach(filter => {
|
|
414
|
-
expect(() => translator.translate(filter)).not.toThrow();
|
|
415
|
-
});
|
|
416
|
-
});
|
|
417
|
-
});
|
|
418
|
-
});
|