@mastra/pg 0.11.1-alpha.1 → 0.11.1-alpha.2
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 +7 -7
- package/CHANGELOG.md +14 -0
- package/dist/_tsup-dts-rollup.d.cts +39 -6
- package/dist/_tsup-dts-rollup.d.ts +39 -6
- package/dist/index.cjs +94 -8
- package/dist/index.js +95 -9
- package/package.json +4 -4
- package/src/storage/index.test.ts +355 -1
- package/src/storage/index.ts +119 -5
- package/src/vector/filter.test.ts +12 -12
- package/src/vector/filter.ts +36 -7
- package/src/vector/index.test.ts +2 -2
- package/src/vector/index.ts +4 -4
- package/src/vector/sql-builder.ts +2 -1
package/src/vector/filter.ts
CHANGED
|
@@ -1,5 +1,34 @@
|
|
|
1
1
|
import { BaseFilterTranslator } from '@mastra/core/vector/filter';
|
|
2
|
-
import type {
|
|
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
|
+
>;
|
|
3
32
|
|
|
4
33
|
/**
|
|
5
34
|
* Translates MongoDB-style filters to PG compatible filters.
|
|
@@ -11,7 +40,7 @@ import type { FieldCondition, VectorFilter, OperatorSupport } from '@mastra/core
|
|
|
11
40
|
* - Can take either a single condition or an array of conditions
|
|
12
41
|
*
|
|
13
42
|
*/
|
|
14
|
-
export class PGFilterTranslator extends BaseFilterTranslator {
|
|
43
|
+
export class PGFilterTranslator extends BaseFilterTranslator<PGVectorFilter> {
|
|
15
44
|
protected override getSupportedOperators(): OperatorSupport {
|
|
16
45
|
return {
|
|
17
46
|
...BaseFilterTranslator.DEFAULT_OPERATORS,
|
|
@@ -19,7 +48,7 @@ export class PGFilterTranslator extends BaseFilterTranslator {
|
|
|
19
48
|
};
|
|
20
49
|
}
|
|
21
50
|
|
|
22
|
-
translate(filter?:
|
|
51
|
+
translate(filter?: PGVectorFilter): PGVectorFilter {
|
|
23
52
|
if (this.isEmpty(filter)) {
|
|
24
53
|
return filter;
|
|
25
54
|
}
|
|
@@ -27,7 +56,7 @@ export class PGFilterTranslator extends BaseFilterTranslator {
|
|
|
27
56
|
return this.translateNode(filter);
|
|
28
57
|
}
|
|
29
58
|
|
|
30
|
-
private translateNode(node:
|
|
59
|
+
private translateNode(node: PGVectorFilter, currentPath: string = ''): any {
|
|
31
60
|
// Helper to wrap result with path if needed
|
|
32
61
|
const withPath = (result: any) => (currentPath ? { [currentPath]: result } : result);
|
|
33
62
|
|
|
@@ -49,14 +78,14 @@ export class PGFilterTranslator extends BaseFilterTranslator {
|
|
|
49
78
|
const entries = Object.entries(node as Record<string, any>);
|
|
50
79
|
const result: Record<string, any> = {};
|
|
51
80
|
|
|
52
|
-
if ('$options' in node && !('$regex' in node)) {
|
|
81
|
+
if (node && '$options' in node && !('$regex' in node)) {
|
|
53
82
|
throw new Error('$options is not valid without $regex');
|
|
54
83
|
}
|
|
55
84
|
|
|
56
85
|
// Handle special regex object format
|
|
57
|
-
if ('$regex' in node) {
|
|
86
|
+
if (node && '$regex' in node) {
|
|
58
87
|
const options = (node as any).$options || '';
|
|
59
|
-
return withPath(this.translateRegexPattern(node.$regex, options));
|
|
88
|
+
return withPath(this.translateRegexPattern((node as any).$regex, options));
|
|
60
89
|
}
|
|
61
90
|
|
|
62
91
|
// Process remaining entries
|
package/src/vector/index.test.ts
CHANGED
|
@@ -1288,7 +1288,7 @@ describe('PgVector', () => {
|
|
|
1288
1288
|
vectorDB.query({
|
|
1289
1289
|
indexName,
|
|
1290
1290
|
queryVector: [1, 0, 0],
|
|
1291
|
-
filter: { price: { $invalid: 100 } },
|
|
1291
|
+
filter: { price: { $invalid: 100 } } as any,
|
|
1292
1292
|
}),
|
|
1293
1293
|
).rejects.toThrow('Unsupported operator: $invalid');
|
|
1294
1294
|
});
|
|
@@ -1567,7 +1567,7 @@ describe('PgVector', () => {
|
|
|
1567
1567
|
vectorDB.query({
|
|
1568
1568
|
indexName,
|
|
1569
1569
|
queryVector: [1, 0, 0],
|
|
1570
|
-
filter: { price: { $invalid: 100 } },
|
|
1570
|
+
filter: { price: { $invalid: 100 } } as any,
|
|
1571
1571
|
}),
|
|
1572
1572
|
).rejects.toThrow('Unsupported operator: $invalid');
|
|
1573
1573
|
});
|
package/src/vector/index.ts
CHANGED
|
@@ -12,12 +12,12 @@ import type {
|
|
|
12
12
|
DeleteVectorParams,
|
|
13
13
|
UpdateVectorParams,
|
|
14
14
|
} from '@mastra/core/vector';
|
|
15
|
-
import type { VectorFilter } from '@mastra/core/vector/filter';
|
|
16
15
|
import { Mutex } from 'async-mutex';
|
|
17
16
|
import pg from 'pg';
|
|
18
17
|
import xxhash from 'xxhash-wasm';
|
|
19
18
|
|
|
20
19
|
import { PGFilterTranslator } from './filter';
|
|
20
|
+
import type { PGVectorFilter } from './filter';
|
|
21
21
|
import { buildFilterQuery } from './sql-builder';
|
|
22
22
|
import type { IndexConfig, IndexType } from './types';
|
|
23
23
|
|
|
@@ -31,7 +31,7 @@ export interface PGIndexStats extends IndexStats {
|
|
|
31
31
|
};
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
interface PgQueryVectorParams extends QueryVectorParams {
|
|
34
|
+
interface PgQueryVectorParams extends QueryVectorParams<PGVectorFilter> {
|
|
35
35
|
minScore?: number;
|
|
36
36
|
/**
|
|
37
37
|
* HNSW search parameter. Controls the size of the dynamic candidate
|
|
@@ -56,7 +56,7 @@ interface PgDefineIndexParams {
|
|
|
56
56
|
indexConfig: IndexConfig;
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
export class PgVector extends MastraVector {
|
|
59
|
+
export class PgVector extends MastraVector<PGVectorFilter> {
|
|
60
60
|
private pool: pg.Pool;
|
|
61
61
|
private describeIndexCache: Map<string, PGIndexStats> = new Map();
|
|
62
62
|
private createdIndexes = new Map<string, number>();
|
|
@@ -153,7 +153,7 @@ export class PgVector extends MastraVector {
|
|
|
153
153
|
return this.schema ? `"${parseSqlIdentifier(this.schema, 'schema name')}"` : undefined;
|
|
154
154
|
}
|
|
155
155
|
|
|
156
|
-
transformFilter(filter?:
|
|
156
|
+
transformFilter(filter?: PGVectorFilter) {
|
|
157
157
|
const translator = new PGFilterTranslator();
|
|
158
158
|
return translator.translate(filter);
|
|
159
159
|
}
|
|
@@ -8,6 +8,7 @@ import type {
|
|
|
8
8
|
RegexOperator,
|
|
9
9
|
VectorFilter,
|
|
10
10
|
} from '@mastra/core/vector/filter';
|
|
11
|
+
import type { PGVectorFilter } from './filter';
|
|
11
12
|
|
|
12
13
|
type OperatorType =
|
|
13
14
|
| BasicOperator
|
|
@@ -250,7 +251,7 @@ function escapeLikePattern(str: string): string {
|
|
|
250
251
|
return str.replace(/([%_\\])/g, '\\$1');
|
|
251
252
|
}
|
|
252
253
|
|
|
253
|
-
export function buildFilterQuery(filter:
|
|
254
|
+
export function buildFilterQuery(filter: PGVectorFilter, minScore: number, topK: number): FilterResult {
|
|
254
255
|
const values = [minScore, topK];
|
|
255
256
|
|
|
256
257
|
function buildCondition(key: string, value: any, parentPath: string): string {
|