@mastra/pg 0.2.6-alpha.3 → 0.2.6-alpha.4

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.
@@ -1,23 +1,23 @@
1
1
 
2
- > @mastra/pg@0.2.6-alpha.3 build /home/runner/work/mastra/mastra/stores/pg
2
+ > @mastra/pg@0.2.6-alpha.4 build /home/runner/work/mastra/mastra/stores/pg
3
3
  > tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting
4
4
 
5
5
  CLI Building entry: src/index.ts
6
6
  CLI Using tsconfig: tsconfig.json
7
7
  CLI tsup v8.4.0
8
8
  TSC Build start
9
- TSC ⚡️ Build success in 10734ms
9
+ TSC ⚡️ Build success in 10085ms
10
10
  DTS Build start
11
11
  CLI Target: es2022
12
12
  Analysis will use the bundled TypeScript version 5.8.2
13
13
  Writing package typings: /home/runner/work/mastra/mastra/stores/pg/dist/_tsup-dts-rollup.d.ts
14
14
  Analysis will use the bundled TypeScript version 5.8.2
15
15
  Writing package typings: /home/runner/work/mastra/mastra/stores/pg/dist/_tsup-dts-rollup.d.cts
16
- DTS ⚡️ Build success in 11829ms
16
+ DTS ⚡️ Build success in 10753ms
17
17
  CLI Cleaning output folder
18
18
  ESM Build start
19
19
  CJS Build start
20
- CJS dist/index.cjs 36.78 KB
21
- CJS ⚡️ Build success in 1386ms
22
- ESM dist/index.js 36.37 KB
23
- ESM ⚡️ Build success in 1387ms
20
+ ESM dist/index.js 36.43 KB
21
+ ESM ⚡️ Build success in 1230ms
22
+ CJS dist/index.cjs 36.85 KB
23
+ CJS ⚡️ Build success in 1230ms
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @mastra/pg
2
2
 
3
+ ## 0.2.6-alpha.4
4
+
5
+ ### Patch Changes
6
+
7
+ - e91bee7: Added helper method for both createindex and buildIndex
8
+
3
9
  ## 0.2.6-alpha.3
4
10
 
5
11
  ### Patch Changes
@@ -211,6 +211,7 @@ declare class PgVector extends MastraVector {
211
211
  */
212
212
  defineIndex(indexName: string, metric: "cosine" | "euclidean" | "dotproduct" | undefined, indexConfig: IndexConfig): Promise<void>;
213
213
  buildIndex(...args: ParamsToArgs<PgDefineIndexParams> | PgDefineIndexArgs): Promise<void>;
214
+ private setupIndex;
214
215
  listIndexes(): Promise<string[]>;
215
216
  describeIndex(indexName: string): Promise<PGIndexStats>;
216
217
  deleteIndex(indexName: string): Promise<void>;
@@ -211,6 +211,7 @@ declare class PgVector extends MastraVector {
211
211
  */
212
212
  defineIndex(indexName: string, metric: "cosine" | "euclidean" | "dotproduct" | undefined, indexConfig: IndexConfig): Promise<void>;
213
213
  buildIndex(...args: ParamsToArgs<PgDefineIndexParams> | PgDefineIndexArgs): Promise<void>;
214
+ private setupIndex;
214
215
  listIndexes(): Promise<string[]>;
215
216
  describeIndex(indexName: string): Promise<PGIndexStats>;
216
217
  deleteIndex(indexName: string): Promise<void>;
package/dist/index.cjs CHANGED
@@ -454,7 +454,7 @@ var PgVector = class extends vector.MastraVector {
454
454
  await client.query("SELECT pg_advisory_unlock($1)", [lockId]);
455
455
  }
456
456
  if (buildIndex) {
457
- await this.buildIndex({ indexName, metric, indexConfig });
457
+ await this.setupIndex({ indexName, metric, indexConfig }, client);
458
458
  }
459
459
  } catch (error) {
460
460
  console.error("Failed to create vector table:", error);
@@ -477,38 +477,44 @@ var PgVector = class extends vector.MastraVector {
477
477
  const { indexName, metric = "cosine", indexConfig } = params;
478
478
  const client = await this.pool.connect();
479
479
  try {
480
- const hash = crypto$1.createHash("sha256").update("build:" + indexName).digest("hex");
481
- const lockId = BigInt("0x" + hash.slice(0, 8)) % BigInt(2 ** 31);
482
- const acquired = await client.query("SELECT pg_try_advisory_lock($1)", [lockId]);
483
- if (!acquired.rows[0].pg_try_advisory_lock) {
484
- const exists = await client.query(
485
- `
480
+ await this.setupIndex({ indexName, metric, indexConfig }, client);
481
+ } finally {
482
+ client.release();
483
+ }
484
+ }
485
+ async setupIndex({ indexName, metric, indexConfig }, client) {
486
+ const hash = crypto$1.createHash("sha256").update("build:" + indexName).digest("hex");
487
+ const lockId = BigInt("0x" + hash.slice(0, 8)) % BigInt(2 ** 31);
488
+ const acquired = await client.query("SELECT pg_try_advisory_lock($1)", [lockId]);
489
+ if (!acquired.rows[0].pg_try_advisory_lock) {
490
+ const exists = await client.query(
491
+ `
486
492
  SELECT 1 FROM pg_class c
487
493
  JOIN pg_namespace n ON n.oid = c.relnamespace
488
494
  WHERE c.relname = $1
489
495
  AND n.nspname = 'public'
490
496
  `,
491
- [`${indexName}_vector_idx`]
492
- );
493
- if (exists.rows.length > 0) {
494
- console.log(`Index ${indexName}_vector_idx already exists, skipping creation`);
495
- this.indexCache.delete(indexName);
496
- return;
497
- }
498
- await client.query("SELECT pg_advisory_lock($1)", [lockId]);
497
+ [`${indexName}_vector_idx`]
498
+ );
499
+ if (exists.rows.length > 0) {
500
+ console.log(`Index ${indexName}_vector_idx already exists, skipping creation`);
501
+ this.indexCache.delete(indexName);
502
+ return;
499
503
  }
500
- try {
501
- await client.query(`DROP INDEX IF EXISTS ${indexName}_vector_idx`);
502
- if (indexConfig.type === "flat") {
503
- this.indexCache.delete(indexName);
504
- return;
505
- }
506
- const metricOp = metric === "cosine" ? "vector_cosine_ops" : metric === "euclidean" ? "vector_l2_ops" : "vector_ip_ops";
507
- let indexSQL;
508
- if (indexConfig.type === "hnsw") {
509
- const m = indexConfig.hnsw?.m ?? 8;
510
- const efConstruction = indexConfig.hnsw?.efConstruction ?? 32;
511
- indexSQL = `
504
+ await client.query("SELECT pg_advisory_lock($1)", [lockId]);
505
+ }
506
+ try {
507
+ await client.query(`DROP INDEX IF EXISTS ${indexName}_vector_idx`);
508
+ if (indexConfig.type === "flat") {
509
+ this.indexCache.delete(indexName);
510
+ return;
511
+ }
512
+ const metricOp = metric === "cosine" ? "vector_cosine_ops" : metric === "euclidean" ? "vector_l2_ops" : "vector_ip_ops";
513
+ let indexSQL;
514
+ if (indexConfig.type === "hnsw") {
515
+ const m = indexConfig.hnsw?.m ?? 8;
516
+ const efConstruction = indexConfig.hnsw?.efConstruction ?? 32;
517
+ indexSQL = `
512
518
  CREATE INDEX IF NOT EXISTS ${indexName}_vector_idx
513
519
  ON ${indexName}
514
520
  USING hnsw (embedding ${metricOp})
@@ -517,28 +523,25 @@ var PgVector = class extends vector.MastraVector {
517
523
  ef_construction = ${efConstruction}
518
524
  )
519
525
  `;
526
+ } else {
527
+ let lists;
528
+ if (indexConfig.ivf?.lists) {
529
+ lists = indexConfig.ivf.lists;
520
530
  } else {
521
- let lists;
522
- if (indexConfig.ivf?.lists) {
523
- lists = indexConfig.ivf.lists;
524
- } else {
525
- const size = (await client.query(`SELECT COUNT(*) FROM ${indexName}`)).rows[0].count;
526
- lists = Math.max(100, Math.min(4e3, Math.floor(Math.sqrt(size) * 2)));
527
- }
528
- indexSQL = `
531
+ const size = (await client.query(`SELECT COUNT(*) FROM ${indexName}`)).rows[0].count;
532
+ lists = Math.max(100, Math.min(4e3, Math.floor(Math.sqrt(size) * 2)));
533
+ }
534
+ indexSQL = `
529
535
  CREATE INDEX IF NOT EXISTS ${indexName}_vector_idx
530
536
  ON ${indexName}
531
537
  USING ivfflat (embedding ${metricOp})
532
538
  WITH (lists = ${lists});
533
539
  `;
534
- }
535
- await client.query(indexSQL);
536
- this.indexCache.delete(indexName);
537
- } finally {
538
- await client.query("SELECT pg_advisory_unlock($1)", [lockId]);
539
540
  }
541
+ await client.query(indexSQL);
542
+ this.indexCache.delete(indexName);
540
543
  } finally {
541
- client.release();
544
+ await client.query("SELECT pg_advisory_unlock($1)", [lockId]);
542
545
  }
543
546
  }
544
547
  async listIndexes() {
package/dist/index.js CHANGED
@@ -447,7 +447,7 @@ var PgVector = class extends MastraVector {
447
447
  await client.query("SELECT pg_advisory_unlock($1)", [lockId]);
448
448
  }
449
449
  if (buildIndex) {
450
- await this.buildIndex({ indexName, metric, indexConfig });
450
+ await this.setupIndex({ indexName, metric, indexConfig }, client);
451
451
  }
452
452
  } catch (error) {
453
453
  console.error("Failed to create vector table:", error);
@@ -470,38 +470,44 @@ var PgVector = class extends MastraVector {
470
470
  const { indexName, metric = "cosine", indexConfig } = params;
471
471
  const client = await this.pool.connect();
472
472
  try {
473
- const hash = createHash("sha256").update("build:" + indexName).digest("hex");
474
- const lockId = BigInt("0x" + hash.slice(0, 8)) % BigInt(2 ** 31);
475
- const acquired = await client.query("SELECT pg_try_advisory_lock($1)", [lockId]);
476
- if (!acquired.rows[0].pg_try_advisory_lock) {
477
- const exists = await client.query(
478
- `
473
+ await this.setupIndex({ indexName, metric, indexConfig }, client);
474
+ } finally {
475
+ client.release();
476
+ }
477
+ }
478
+ async setupIndex({ indexName, metric, indexConfig }, client) {
479
+ const hash = createHash("sha256").update("build:" + indexName).digest("hex");
480
+ const lockId = BigInt("0x" + hash.slice(0, 8)) % BigInt(2 ** 31);
481
+ const acquired = await client.query("SELECT pg_try_advisory_lock($1)", [lockId]);
482
+ if (!acquired.rows[0].pg_try_advisory_lock) {
483
+ const exists = await client.query(
484
+ `
479
485
  SELECT 1 FROM pg_class c
480
486
  JOIN pg_namespace n ON n.oid = c.relnamespace
481
487
  WHERE c.relname = $1
482
488
  AND n.nspname = 'public'
483
489
  `,
484
- [`${indexName}_vector_idx`]
485
- );
486
- if (exists.rows.length > 0) {
487
- console.log(`Index ${indexName}_vector_idx already exists, skipping creation`);
488
- this.indexCache.delete(indexName);
489
- return;
490
- }
491
- await client.query("SELECT pg_advisory_lock($1)", [lockId]);
490
+ [`${indexName}_vector_idx`]
491
+ );
492
+ if (exists.rows.length > 0) {
493
+ console.log(`Index ${indexName}_vector_idx already exists, skipping creation`);
494
+ this.indexCache.delete(indexName);
495
+ return;
492
496
  }
493
- try {
494
- await client.query(`DROP INDEX IF EXISTS ${indexName}_vector_idx`);
495
- if (indexConfig.type === "flat") {
496
- this.indexCache.delete(indexName);
497
- return;
498
- }
499
- const metricOp = metric === "cosine" ? "vector_cosine_ops" : metric === "euclidean" ? "vector_l2_ops" : "vector_ip_ops";
500
- let indexSQL;
501
- if (indexConfig.type === "hnsw") {
502
- const m = indexConfig.hnsw?.m ?? 8;
503
- const efConstruction = indexConfig.hnsw?.efConstruction ?? 32;
504
- indexSQL = `
497
+ await client.query("SELECT pg_advisory_lock($1)", [lockId]);
498
+ }
499
+ try {
500
+ await client.query(`DROP INDEX IF EXISTS ${indexName}_vector_idx`);
501
+ if (indexConfig.type === "flat") {
502
+ this.indexCache.delete(indexName);
503
+ return;
504
+ }
505
+ const metricOp = metric === "cosine" ? "vector_cosine_ops" : metric === "euclidean" ? "vector_l2_ops" : "vector_ip_ops";
506
+ let indexSQL;
507
+ if (indexConfig.type === "hnsw") {
508
+ const m = indexConfig.hnsw?.m ?? 8;
509
+ const efConstruction = indexConfig.hnsw?.efConstruction ?? 32;
510
+ indexSQL = `
505
511
  CREATE INDEX IF NOT EXISTS ${indexName}_vector_idx
506
512
  ON ${indexName}
507
513
  USING hnsw (embedding ${metricOp})
@@ -510,28 +516,25 @@ var PgVector = class extends MastraVector {
510
516
  ef_construction = ${efConstruction}
511
517
  )
512
518
  `;
519
+ } else {
520
+ let lists;
521
+ if (indexConfig.ivf?.lists) {
522
+ lists = indexConfig.ivf.lists;
513
523
  } else {
514
- let lists;
515
- if (indexConfig.ivf?.lists) {
516
- lists = indexConfig.ivf.lists;
517
- } else {
518
- const size = (await client.query(`SELECT COUNT(*) FROM ${indexName}`)).rows[0].count;
519
- lists = Math.max(100, Math.min(4e3, Math.floor(Math.sqrt(size) * 2)));
520
- }
521
- indexSQL = `
524
+ const size = (await client.query(`SELECT COUNT(*) FROM ${indexName}`)).rows[0].count;
525
+ lists = Math.max(100, Math.min(4e3, Math.floor(Math.sqrt(size) * 2)));
526
+ }
527
+ indexSQL = `
522
528
  CREATE INDEX IF NOT EXISTS ${indexName}_vector_idx
523
529
  ON ${indexName}
524
530
  USING ivfflat (embedding ${metricOp})
525
531
  WITH (lists = ${lists});
526
532
  `;
527
- }
528
- await client.query(indexSQL);
529
- this.indexCache.delete(indexName);
530
- } finally {
531
- await client.query("SELECT pg_advisory_unlock($1)", [lockId]);
532
533
  }
534
+ await client.query(indexSQL);
535
+ this.indexCache.delete(indexName);
533
536
  } finally {
534
- client.release();
537
+ await client.query("SELECT pg_advisory_unlock($1)", [lockId]);
535
538
  }
536
539
  }
537
540
  async listIndexes() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/pg",
3
- "version": "0.2.6-alpha.3",
3
+ "version": "0.2.6-alpha.4",
4
4
  "description": "Postgres provider for Mastra - includes both vector and db storage capabilities",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -262,7 +262,7 @@ export class PgVector extends MastraVector {
262
262
  }
263
263
 
264
264
  if (buildIndex) {
265
- await this.buildIndex({ indexName, metric, indexConfig });
265
+ await this.setupIndex({ indexName, metric, indexConfig }, client);
266
266
  }
267
267
  } catch (error: any) {
268
268
  console.error('Failed to create vector table:', error);
@@ -293,52 +293,59 @@ export class PgVector extends MastraVector {
293
293
 
294
294
  const client = await this.pool.connect();
295
295
  try {
296
- // Use a different hash prefix for buildIndex locks to avoid conflicts with createIndex
297
- const hash = createHash('sha256')
298
- .update('build:' + indexName)
299
- .digest('hex');
300
- const lockId = BigInt('0x' + hash.slice(0, 8)) % BigInt(2 ** 31);
301
- const acquired = await client.query('SELECT pg_try_advisory_lock($1)', [lockId]);
296
+ await this.setupIndex({ indexName, metric, indexConfig }, client);
297
+ } finally {
298
+ client.release();
299
+ }
300
+ }
302
301
 
303
- if (!acquired.rows[0].pg_try_advisory_lock) {
304
- // Check if index already exists
305
- const exists = await client.query(
306
- `
302
+ private async setupIndex({ indexName, metric, indexConfig }: PgDefineIndexParams, client: pg.PoolClient) {
303
+ // Use a different hash prefix for buildIndex locks to avoid conflicts with createIndex
304
+ const hash = createHash('sha256')
305
+ .update('build:' + indexName)
306
+ .digest('hex');
307
+ const lockId = BigInt('0x' + hash.slice(0, 8)) % BigInt(2 ** 31);
308
+ const acquired = await client.query('SELECT pg_try_advisory_lock($1)', [lockId]);
309
+
310
+ if (!acquired.rows[0].pg_try_advisory_lock) {
311
+ // Check if index already exists
312
+ const exists = await client.query(
313
+ `
307
314
  SELECT 1 FROM pg_class c
308
315
  JOIN pg_namespace n ON n.oid = c.relnamespace
309
316
  WHERE c.relname = $1
310
317
  AND n.nspname = 'public'
311
318
  `,
312
- [`${indexName}_vector_idx`],
313
- );
314
-
315
- if (exists.rows.length > 0) {
316
- console.log(`Index ${indexName}_vector_idx already exists, skipping creation`);
317
- this.indexCache.delete(indexName); // Still clear cache since we checked
318
- return;
319
- }
319
+ [`${indexName}_vector_idx`],
320
+ );
320
321
 
321
- // Index doesn't exist, wait for lock
322
- await client.query('SELECT pg_advisory_lock($1)', [lockId]);
322
+ if (exists.rows.length > 0) {
323
+ console.log(`Index ${indexName}_vector_idx already exists, skipping creation`);
324
+ this.indexCache.delete(indexName); // Still clear cache since we checked
325
+ return;
323
326
  }
324
327
 
325
- try {
326
- await client.query(`DROP INDEX IF EXISTS ${indexName}_vector_idx`);
328
+ // Index doesn't exist, wait for lock
329
+ await client.query('SELECT pg_advisory_lock($1)', [lockId]);
330
+ }
327
331
 
328
- if (indexConfig.type === 'flat') {
329
- this.indexCache.delete(indexName);
330
- return;
331
- }
332
+ try {
333
+ await client.query(`DROP INDEX IF EXISTS ${indexName}_vector_idx`);
334
+
335
+ if (indexConfig.type === 'flat') {
336
+ this.indexCache.delete(indexName);
337
+ return;
338
+ }
332
339
 
333
- const metricOp =
334
- metric === 'cosine' ? 'vector_cosine_ops' : metric === 'euclidean' ? 'vector_l2_ops' : 'vector_ip_ops';
340
+ const metricOp =
341
+ metric === 'cosine' ? 'vector_cosine_ops' : metric === 'euclidean' ? 'vector_l2_ops' : 'vector_ip_ops';
335
342
 
336
- let indexSQL: string;
337
- if (indexConfig.type === 'hnsw') {
338
- const m = indexConfig.hnsw?.m ?? 8;
339
- const efConstruction = indexConfig.hnsw?.efConstruction ?? 32;
343
+ let indexSQL: string;
344
+ if (indexConfig.type === 'hnsw') {
345
+ const m = indexConfig.hnsw?.m ?? 8;
346
+ const efConstruction = indexConfig.hnsw?.efConstruction ?? 32;
340
347
 
341
- indexSQL = `
348
+ indexSQL = `
342
349
  CREATE INDEX IF NOT EXISTS ${indexName}_vector_idx
343
350
  ON ${indexName}
344
351
  USING hnsw (embedding ${metricOp})
@@ -347,29 +354,26 @@ export class PgVector extends MastraVector {
347
354
  ef_construction = ${efConstruction}
348
355
  )
349
356
  `;
357
+ } else {
358
+ let lists: number;
359
+ if (indexConfig.ivf?.lists) {
360
+ lists = indexConfig.ivf.lists;
350
361
  } else {
351
- let lists: number;
352
- if (indexConfig.ivf?.lists) {
353
- lists = indexConfig.ivf.lists;
354
- } else {
355
- const size = (await client.query(`SELECT COUNT(*) FROM ${indexName}`)).rows[0].count;
356
- lists = Math.max(100, Math.min(4000, Math.floor(Math.sqrt(size) * 2)));
357
- }
358
- indexSQL = `
362
+ const size = (await client.query(`SELECT COUNT(*) FROM ${indexName}`)).rows[0].count;
363
+ lists = Math.max(100, Math.min(4000, Math.floor(Math.sqrt(size) * 2)));
364
+ }
365
+ indexSQL = `
359
366
  CREATE INDEX IF NOT EXISTS ${indexName}_vector_idx
360
367
  ON ${indexName}
361
368
  USING ivfflat (embedding ${metricOp})
362
369
  WITH (lists = ${lists});
363
370
  `;
364
- }
365
-
366
- await client.query(indexSQL);
367
- this.indexCache.delete(indexName);
368
- } finally {
369
- await client.query('SELECT pg_advisory_unlock($1)', [lockId]);
370
371
  }
372
+
373
+ await client.query(indexSQL);
374
+ this.indexCache.delete(indexName);
371
375
  } finally {
372
- client.release();
376
+ await client.query('SELECT pg_advisory_unlock($1)', [lockId]);
373
377
  }
374
378
  }
375
379