@mastra/pg 0.3.5-alpha.0 → 0.10.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.
@@ -6,9 +6,6 @@ import type {
6
6
  QueryVectorParams,
7
7
  CreateIndexParams,
8
8
  UpsertVectorParams,
9
- ParamsToArgs,
10
- QueryVectorArgs,
11
- CreateIndexArgs,
12
9
  DescribeIndexParams,
13
10
  DeleteIndexParams,
14
11
  DeleteVectorParams,
@@ -47,23 +44,17 @@ interface PgQueryVectorParams extends QueryVectorParams {
47
44
  probes?: number;
48
45
  }
49
46
 
50
- type PgQueryVectorArgs = [...QueryVectorArgs, number?, number?, number?];
51
-
52
47
  interface PgCreateIndexParams extends CreateIndexParams {
53
48
  indexConfig?: IndexConfig;
54
49
  buildIndex?: boolean;
55
50
  }
56
51
 
57
- type PgCreateIndexArgs = [...CreateIndexArgs, IndexConfig?, boolean?];
58
-
59
52
  interface PgDefineIndexParams {
60
53
  indexName: string;
61
54
  metric: 'cosine' | 'euclidean' | 'dotproduct';
62
55
  indexConfig: IndexConfig;
63
56
  }
64
57
 
65
- type PgDefineIndexArgs = [string, 'cosine' | 'euclidean' | 'dotproduct', IndexConfig];
66
-
67
58
  export class PgVector extends MastraVector {
68
59
  private pool: pg.Pool;
69
60
  private describeIndexCache: Map<string, PGIndexStats> = new Map();
@@ -75,48 +66,15 @@ export class PgVector extends MastraVector {
75
66
  private vectorExtensionInstalled: boolean | undefined = undefined;
76
67
  private schemaSetupComplete: boolean | undefined = undefined;
77
68
 
78
- /**
79
- * @deprecated Passing connectionString as a string is deprecated.
80
- * Use the object parameter instead. This signature will be removed on May 20th, 2025.
81
- */
82
- constructor(connectionString: string);
83
- constructor(config: {
69
+ constructor({
70
+ connectionString,
71
+ schemaName,
72
+ pgPoolOptions,
73
+ }: {
84
74
  connectionString: string;
85
75
  schemaName?: string;
86
76
  pgPoolOptions?: Omit<pg.PoolConfig, 'connectionString'>;
87
- });
88
- constructor(
89
- config:
90
- | string
91
- | {
92
- connectionString: string;
93
- schemaName?: string;
94
- pgPoolOptions?: Omit<pg.PoolConfig, 'connectionString'>;
95
- },
96
- ) {
97
- let connectionString: string;
98
- let pgPoolOptions: Omit<pg.PoolConfig, 'connectionString'> | undefined;
99
- let schemaName: string | undefined;
100
-
101
- if (typeof config === 'string') {
102
- // DEPRECATION WARNING
103
- console.warn(
104
- `DEPRECATION WARNING: Passing connectionString as a string to PgVector constructor is deprecated.
105
-
106
- Please use an object parameter instead:
107
- new PgVector({ connectionString })
108
-
109
- The string signature will be removed on May 20th, 2025.`,
110
- );
111
- connectionString = config;
112
- schemaName = undefined;
113
- pgPoolOptions = undefined;
114
- } else {
115
- connectionString = config.connectionString;
116
- schemaName = config.schemaName;
117
- pgPoolOptions = config.pgPoolOptions;
118
- }
119
-
77
+ }) {
120
78
  if (!connectionString || connectionString.trim() === '') {
121
79
  throw new Error(
122
80
  'PgVector: connectionString must be provided and cannot be empty. Passing an empty string may cause fallback to local Postgres defaults.',
@@ -176,23 +134,23 @@ export class PgVector extends MastraVector {
176
134
  return translator.translate(filter);
177
135
  }
178
136
 
179
- async getIndexInfo(...args: ParamsToArgs<DescribeIndexParams>): Promise<PGIndexStats> {
180
- const params = this.normalizeArgs<DescribeIndexParams>('getIndexInfo', args);
181
- const { indexName } = params;
137
+ async getIndexInfo({ indexName }: DescribeIndexParams): Promise<PGIndexStats> {
182
138
  if (!this.describeIndexCache.has(indexName)) {
183
139
  this.describeIndexCache.set(indexName, await this.describeIndex({ indexName }));
184
140
  }
185
141
  return this.describeIndexCache.get(indexName)!;
186
142
  }
187
143
 
188
- async query(...args: ParamsToArgs<PgQueryVectorParams> | PgQueryVectorArgs): Promise<QueryResult[]> {
189
- const params = this.normalizeArgs<PgQueryVectorParams, PgQueryVectorArgs>('query', args, [
190
- 'minScore',
191
- 'ef',
192
- 'probes',
193
- ]);
194
- const { indexName, queryVector, topK = 10, filter, includeVector = false, minScore = 0, ef, probes } = params;
195
-
144
+ async query({
145
+ indexName,
146
+ queryVector,
147
+ topK = 10,
148
+ filter,
149
+ includeVector = false,
150
+ minScore = 0,
151
+ ef,
152
+ probes,
153
+ }: PgQueryVectorParams): Promise<QueryResult[]> {
196
154
  if (!Number.isInteger(topK) || topK <= 0) {
197
155
  throw new Error('topK must be a positive integer');
198
156
  }
@@ -251,10 +209,7 @@ export class PgVector extends MastraVector {
251
209
  }
252
210
  }
253
211
 
254
- async upsert(...args: ParamsToArgs<UpsertVectorParams>): Promise<string[]> {
255
- const params = this.normalizeArgs<UpsertVectorParams>('upsert', args);
256
-
257
- const { indexName, vectors, metadata, ids } = params;
212
+ async upsert({ indexName, vectors, metadata, ids }: UpsertVectorParams): Promise<string[]> {
258
213
  const tableName = this.getTableName(indexName);
259
214
 
260
215
  // Start a transaction
@@ -286,7 +241,7 @@ export class PgVector extends MastraVector {
286
241
  if (match) {
287
242
  const [, expected, actual] = match;
288
243
  throw new Error(
289
- `Vector dimension mismatch: Index "${params.indexName}" expects ${expected} dimensions but got ${actual} dimensions. ` +
244
+ `Vector dimension mismatch: Index "${indexName}" expects ${expected} dimensions but got ${actual} dimensions. ` +
290
245
  `Either use a matching embedding model or delete and recreate the index with the new dimension.`,
291
246
  );
292
247
  }
@@ -298,8 +253,13 @@ export class PgVector extends MastraVector {
298
253
  }
299
254
 
300
255
  private hasher = xxhash();
301
- private async getIndexCacheKey(params: CreateIndexParams & { type: IndexType | undefined }) {
302
- const input = params.indexName + params.dimension + params.metric + (params.type || 'ivfflat'); // ivfflat is default
256
+ private async getIndexCacheKey({
257
+ indexName,
258
+ dimension,
259
+ metric,
260
+ type,
261
+ }: CreateIndexParams & { type: IndexType | undefined }) {
262
+ const input = indexName + dimension + metric + (type || 'ivfflat'); // ivfflat is default
303
263
  return (await this.hasher).h32(input);
304
264
  }
305
265
  private cachedIndexExists(indexName: string, newKey: number) {
@@ -357,13 +317,13 @@ export class PgVector extends MastraVector {
357
317
  await this.setupSchemaPromise;
358
318
  }
359
319
 
360
- async createIndex(...args: ParamsToArgs<PgCreateIndexParams> | PgCreateIndexArgs): Promise<void> {
361
- const params = this.normalizeArgs<PgCreateIndexParams, PgCreateIndexArgs>('createIndex', args, [
362
- 'indexConfig',
363
- 'buildIndex',
364
- ]);
365
-
366
- const { indexName, dimension, metric = 'cosine', indexConfig = {}, buildIndex = true } = params;
320
+ async createIndex({
321
+ indexName,
322
+ dimension,
323
+ metric = 'cosine',
324
+ indexConfig = {},
325
+ buildIndex = true,
326
+ }: PgCreateIndexParams): Promise<void> {
367
327
  const tableName = this.getTableName(indexName);
368
328
 
369
329
  // Validate inputs
@@ -418,27 +378,7 @@ export class PgVector extends MastraVector {
418
378
  });
419
379
  }
420
380
 
421
- /**
422
- * @deprecated This function is deprecated. Use buildIndex instead
423
- * This function will be removed on May 20th, 2025
424
- */
425
- async defineIndex(
426
- indexName: string,
427
- metric: 'cosine' | 'euclidean' | 'dotproduct' = 'cosine',
428
- indexConfig: IndexConfig,
429
- ): Promise<void> {
430
- console.warn('defineIndex is deprecated. Use buildIndex instead. This function will be removed on May 20th, 2025');
431
- return this.buildIndex({ indexName, metric, indexConfig });
432
- }
433
-
434
- async buildIndex(...args: ParamsToArgs<PgDefineIndexParams> | PgDefineIndexArgs): Promise<void> {
435
- const params = this.normalizeArgs<PgDefineIndexParams, PgDefineIndexArgs>('buildIndex', args, [
436
- 'metric',
437
- 'indexConfig',
438
- ]);
439
-
440
- const { indexName, metric = 'cosine', indexConfig } = params;
441
-
381
+ async buildIndex({ indexName, metric = 'cosine', indexConfig }: PgDefineIndexParams): Promise<void> {
442
382
  const client = await this.pool.connect();
443
383
  try {
444
384
  await this.setupIndex({ indexName, metric, indexConfig }, client);
@@ -571,13 +511,10 @@ export class PgVector extends MastraVector {
571
511
  /**
572
512
  * Retrieves statistics about a vector index.
573
513
  *
574
- * @param params - The parameters for describing an index
575
- * @param params.indexName - The name of the index to describe
514
+ * @param {string} indexName - The name of the index to describe
576
515
  * @returns A promise that resolves to the index statistics including dimension, count and metric
577
516
  */
578
- async describeIndex(...args: ParamsToArgs<DescribeIndexParams>): Promise<PGIndexStats> {
579
- const params = this.normalizeArgs<DescribeIndexParams>('describeIndex', args);
580
- const { indexName } = params;
517
+ async describeIndex({ indexName }: DescribeIndexParams): Promise<PGIndexStats> {
581
518
  const client = await this.pool.connect();
582
519
  try {
583
520
  const tableName = this.getTableName(indexName);
@@ -673,9 +610,7 @@ export class PgVector extends MastraVector {
673
610
  }
674
611
  }
675
612
 
676
- async deleteIndex(...args: ParamsToArgs<DeleteIndexParams>): Promise<void> {
677
- const params = this.normalizeArgs<DeleteIndexParams>('deleteIndex', args);
678
- const { indexName } = params;
613
+ async deleteIndex({ indexName }: DeleteIndexParams): Promise<void> {
679
614
  const client = await this.pool.connect();
680
615
  try {
681
616
  const tableName = this.getTableName(indexName);
@@ -690,9 +625,7 @@ export class PgVector extends MastraVector {
690
625
  }
691
626
  }
692
627
 
693
- async truncateIndex(...args: ParamsToArgs<DeleteIndexParams>): Promise<void> {
694
- const params = this.normalizeArgs<DeleteIndexParams>('truncateIndex', args);
695
- const { indexName } = params;
628
+ async truncateIndex({ indexName }: DeleteIndexParams): Promise<void> {
696
629
  const client = await this.pool.connect();
697
630
  try {
698
631
  const tableName = this.getTableName(indexName);
@@ -709,31 +642,6 @@ export class PgVector extends MastraVector {
709
642
  await this.pool.end();
710
643
  }
711
644
 
712
- /**
713
- * @deprecated Use {@link updateVector} instead. This method will be removed on May 20th, 2025.
714
- *
715
- * Updates a vector by its ID with the provided vector and/or metadata.
716
- * @param indexName - The name of the index containing the vector.
717
- * @param id - The ID of the vector to update.
718
- * @param update - An object containing the vector and/or metadata to update.
719
- * @param update.vector - An optional array of numbers representing the new vector.
720
- * @param update.metadata - An optional record containing the new metadata.
721
- * @returns A promise that resolves when the update is complete.
722
- * @throws Will throw an error if no updates are provided or if the update operation fails.
723
- */
724
- async updateIndexById(
725
- indexName: string,
726
- id: string,
727
- update: { vector?: number[]; metadata?: Record<string, any> },
728
- ): Promise<void> {
729
- this.logger.warn(
730
- `Deprecation Warning: updateIndexById() is deprecated.
731
- Please use updateVector() instead.
732
- updateIndexById() will be removed on May 20th, 2025.`,
733
- );
734
- await this.updateVector({ indexName, id, update });
735
- }
736
-
737
645
  /**
738
646
  * Updates a vector by its ID with the provided vector and/or metadata.
739
647
  * @param indexName - The name of the index containing the vector.
@@ -744,9 +652,7 @@ export class PgVector extends MastraVector {
744
652
  * @returns A promise that resolves when the update is complete.
745
653
  * @throws Will throw an error if no updates are provided or if the update operation fails.
746
654
  */
747
- async updateVector(...args: ParamsToArgs<UpdateVectorParams>): Promise<void> {
748
- const params = this.normalizeArgs<UpdateVectorParams>('updateVector', args);
749
- const { indexName, id, update } = params;
655
+ async updateVector({ indexName, id, update }: UpdateVectorParams): Promise<void> {
750
656
  if (!update.vector && !update.metadata) {
751
657
  throw new Error('No updates provided');
752
658
  }
@@ -790,24 +696,6 @@ export class PgVector extends MastraVector {
790
696
  }
791
697
  }
792
698
 
793
- /**
794
- * @deprecated Use {@link deleteVector} instead. This method will be removed on May 20th, 2025.
795
- *
796
- * Deletes a vector by its ID.
797
- * @param indexName - The name of the index containing the vector.
798
- * @param id - The ID of the vector to delete.
799
- * @returns A promise that resolves when the deletion is complete.
800
- * @throws Will throw an error if the deletion operation fails.
801
- */
802
- async deleteIndexById(indexName: string, id: string): Promise<void> {
803
- this.logger.warn(
804
- `Deprecation Warning: deleteIndexById() is deprecated.
805
- Please use deleteVector() instead.
806
- deleteIndexById() will be removed on May 20th, 2025.`,
807
- );
808
- await this.deleteVector({ indexName, id });
809
- }
810
-
811
699
  /**
812
700
  * Deletes a vector by its ID.
813
701
  * @param indexName - The name of the index containing the vector.
@@ -815,9 +703,7 @@ export class PgVector extends MastraVector {
815
703
  * @returns A promise that resolves when the deletion is complete.
816
704
  * @throws Will throw an error if the deletion operation fails.
817
705
  */
818
- async deleteVector(...args: ParamsToArgs<DeleteVectorParams>): Promise<void> {
819
- const params = this.normalizeArgs<DeleteVectorParams>('deleteVector', args);
820
- const { indexName, id } = params;
706
+ async deleteVector({ indexName, id }: DeleteVectorParams): Promise<void> {
821
707
  const client = await this.pool.connect();
822
708
  try {
823
709
  const tableName = this.getTableName(indexName);