@mastra/pg 1.22.3-alpha.1 → 1.22.3-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/dist/index.js CHANGED
@@ -794,19 +794,18 @@ var PgVector = class extends MastraVector {
794
794
  }
795
795
  async ensureNamespaceSchema(indexName, client) {
796
796
  const { tableName, parsedIndexName } = this.getTableName(indexName);
797
- const schemaName = this.schema ? parseSqlIdentifier(this.schema, "schema name") : "public";
798
797
  if ((await client.query(`SELECT 1
799
- FROM information_schema.columns
800
- WHERE table_schema = $1 AND table_name = $2 AND column_name = 'vector_id'`, [schemaName, parsedIndexName])).rowCount === 0) return;
798
+ FROM pg_attribute
799
+ WHERE attrelid = to_regclass($1)
800
+ AND attname = 'vector_id'
801
+ AND attnum > 0
802
+ AND NOT attisdropped`, [tableName])).rowCount === 0) return;
801
803
  await client.query(`ALTER TABLE ${tableName} ADD COLUMN IF NOT EXISTS namespace VARCHAR(255) NOT NULL DEFAULT '${DEFAULT_NAMESPACE}'`);
802
804
  const legacyConstraints = await client.query(`SELECT c.conname
803
805
  FROM pg_constraint c
804
- JOIN pg_class t ON t.oid = c.conrelid
805
- JOIN pg_namespace n ON n.oid = t.relnamespace
806
- WHERE n.nspname = $1
807
- AND t.relname = $2
806
+ WHERE c.conrelid = to_regclass($1)
808
807
  AND c.contype = 'u'
809
- AND pg_get_constraintdef(c.oid) = 'UNIQUE (vector_id)'`, [schemaName, parsedIndexName]);
808
+ AND pg_get_constraintdef(c.oid) = 'UNIQUE (vector_id)'`, [tableName]);
810
809
  for (const { conname } of legacyConstraints.rows) {
811
810
  const parsedConstraintName = parseSqlIdentifier(conname, "constraint name");
812
811
  await client.query(`ALTER TABLE ${tableName} DROP CONSTRAINT "${parsedConstraintName}"`);
@@ -1444,7 +1443,9 @@ var PgVector = class extends MastraVector {
1444
1443
  return (await client.query(`
1445
1444
  SELECT DISTINCT t.table_name
1446
1445
  FROM information_schema.tables t
1447
- WHERE t.table_schema = $1
1446
+ WHERE t.table_schema = ANY(
1447
+ CASE WHEN $1::text IS NULL THEN current_schemas(false) ELSE ARRAY[$1::text] END
1448
+ )
1448
1449
  AND EXISTS (
1449
1450
  SELECT 1
1450
1451
  FROM information_schema.columns c
@@ -1469,7 +1470,7 @@ var PgVector = class extends MastraVector {
1469
1470
  AND c.column_name = 'metadata'
1470
1471
  AND c.data_type = 'jsonb'
1471
1472
  );
1472
- `, [this.schema || "public"])).rows.map((row) => row.table_name);
1473
+ `, [this.schema ?? null])).rows.map((row) => row.table_name);
1473
1474
  } catch (e) {
1474
1475
  const mastraError = new MastraError({
1475
1476
  id: createVectorErrorId("PG", "LIST_INDEXES", "FAILED"),
@@ -1504,15 +1505,18 @@ var PgVector = class extends MastraVector {
1504
1505
  async describeIndexMetadata({ indexName }) {
1505
1506
  const client = await this.pool.connect();
1506
1507
  try {
1507
- const { tableName } = this.getTableName(indexName);
1508
+ const { tableName, parsedIndexName } = this.getTableName(indexName);
1508
1509
  const tableExists = await client.query(`
1509
- SELECT udt_name
1510
- FROM information_schema.columns
1511
- WHERE table_schema = $1
1512
- AND table_name = $2
1513
- AND udt_name IN ('vector', 'halfvec', 'bit', 'sparsevec')
1510
+ SELECT t.typname AS udt_name
1511
+ FROM pg_attribute a
1512
+ JOIN pg_type t ON t.oid = a.atttypid
1513
+ WHERE a.attrelid = to_regclass($1)
1514
+ AND a.attname = 'embedding'
1515
+ AND a.attnum > 0
1516
+ AND NOT a.attisdropped
1517
+ AND t.typname IN ('vector', 'halfvec', 'bit', 'sparsevec')
1514
1518
  LIMIT 1;
1515
- `, [this.schema || "public", indexName]);
1519
+ `, [tableName]);
1516
1520
  if (tableExists.rows.length === 0) throw new Error(`Vector table ${tableName} does not exist`);
1517
1521
  const udtName = tableExists.rows[0].udt_name;
1518
1522
  const vectorType = udtName === "halfvec" ? "halfvec" : udtName === "bit" ? "bit" : udtName === "sparsevec" ? "sparsevec" : "vector";
@@ -1531,12 +1535,11 @@ var PgVector = class extends MastraVector {
1531
1535
  JOIN pg_class c ON i.indexrelid = c.oid
1532
1536
  JOIN pg_am am ON c.relam = am.oid
1533
1537
  JOIN pg_opclass opclass ON i.indclass[0] = opclass.oid
1534
- JOIN pg_namespace n ON c.relnamespace = n.oid
1535
1538
  WHERE c.relname = $1
1536
- AND n.nspname = $2;
1539
+ AND i.indrelid = to_regclass($2);
1537
1540
  `;
1538
1541
  const dimResult = await client.query(dimensionQuery, [tableName]);
1539
- const { index_method, index_def, operator_class } = (await client.query(indexQuery, [`${indexName}_vector_idx`, this.schema || "public"])).rows[0] || {
1542
+ const { index_method, index_def, operator_class } = (await client.query(indexQuery, [`${parsedIndexName}_vector_idx`, tableName])).rows[0] || {
1540
1543
  index_method: "flat",
1541
1544
  index_def: "",
1542
1545
  operator_class: "cosine"
@@ -4474,6 +4477,25 @@ var AgentsPG = class AgentsPG extends AgentsStorage {
4474
4477
  }, error);
4475
4478
  }
4476
4479
  }
4480
+ async getVersions(ids) {
4481
+ if (ids.length === 0) return [];
4482
+ try {
4483
+ const tableName = getTableName$5({
4484
+ indexName: TABLE_AGENT_VERSIONS,
4485
+ schemaName: getSchemaName$5(this.#schema)
4486
+ });
4487
+ const placeholders = ids.map((_, i) => `$${i + 1}`).join(", ");
4488
+ return (await this.#db.client.manyOrNone(`SELECT * FROM ${tableName} WHERE id IN (${placeholders})`, ids)).map((row) => this.parseVersionRow(row));
4489
+ } catch (error) {
4490
+ if (error instanceof MastraError) throw error;
4491
+ throw new MastraError({
4492
+ id: createStorageErrorId("PG", "GET_VERSIONS", "FAILED"),
4493
+ domain: ErrorDomain.STORAGE,
4494
+ category: ErrorCategory.THIRD_PARTY,
4495
+ details: { count: ids.length }
4496
+ }, error);
4497
+ }
4498
+ }
4477
4499
  async getVersionByNumber(agentId, versionNumber) {
4478
4500
  try {
4479
4501
  const tableName = getTableName$5({
@@ -20098,6 +20120,25 @@ var SkillsPG = class SkillsPG extends SkillsStorage {
20098
20120
  }, error);
20099
20121
  }
20100
20122
  }
20123
+ async getVersions(ids) {
20124
+ if (ids.length === 0) return [];
20125
+ try {
20126
+ const tableName = getTableName$5({
20127
+ indexName: TABLE_SKILL_VERSIONS,
20128
+ schemaName: getSchemaName$5(this.#schema)
20129
+ });
20130
+ const placeholders = ids.map((_, i) => `$${i + 1}`).join(", ");
20131
+ return (await this.#db.client.manyOrNone(`SELECT * FROM ${tableName} WHERE id IN (${placeholders})`, ids)).map((row) => this.parseVersionRow(row));
20132
+ } catch (error) {
20133
+ if (error instanceof MastraError) throw error;
20134
+ throw new MastraError({
20135
+ id: createStorageErrorId("PG", "GET_SKILL_VERSIONS", "FAILED"),
20136
+ domain: ErrorDomain.STORAGE,
20137
+ category: ErrorCategory.THIRD_PARTY,
20138
+ details: { count: ids.length }
20139
+ }, error);
20140
+ }
20141
+ }
20101
20142
  async getVersionByNumber(skillId, versionNumber) {
20102
20143
  try {
20103
20144
  const tableName = getTableName$5({