PgStore

The PgStore class provides vector search using PostgreSQL with pgvector extension.

Constructor Options

connectionString:

string
PostgreSQL connection URL

tableName?:

string
Table name for vector storage

dimension?:

number
Vector dimension (must match your embedding model)

Methods

createIndex()

indexName:

string
Name of the index to create

dimension:

number
Vector dimension size

metric?:

'cosine' | 'euclidean' | 'dotproduct'
Distance metric for similarity search

upsert()

vectors:

number[][]
Array of embedding vectors

metadata?:

Record<string, any>[]
Metadata for each vector

ids?:

string[]
Optional vector IDs (auto-generated if not provided)

query()

vector:

number[]
Query vector

topK?:

number
Number of results to return

filter?:

Record<string, any>
Metadata filters

minScore?:

number
Minimum similarity score threshold

describeIndex()

indexName:

string
Name of the index to describe

Returns:

interface IndexStats {
  dimension: number;
  count: number;
  metric: "cosine" | "euclidean" | "dotproduct";
}

deleteIndex()

indexName:

string
Name of the index to delete

disconnect()

Closes the database connection pool. Should be called when done using the store.

Response Types

Query results are returned in this format:

interface QueryResult {
  id: string;
  score: number;
  metadata: Record<string, any>;
}

Error Handling

The store throws typed errors that can be caught:

try {
  await store.query(queryVector);
} catch (error) {
  if (error instanceof VectorStoreError) {
    console.log(error.code); // 'connection_failed' | 'invalid_dimension' | etc
    console.log(error.details); // Additional error context
  }
}

MIT 2024 © Nextra.