Skip to content

KnexVectorRepository

Defined in: knex-vector-repository.ts:13

Extends KnexRepository with pgvector support for PostgreSQL. Subclasses must be connected to a pg client — vector methods throw GeneralError on other databases.

T extends Record<string, unknown>

D = Partial<T>

new KnexVectorRepository<T, D>(app): KnexVectorRepository<T, D>

Defined in: knex-repository.ts:42

MantleApplication

KnexVectorRepository<T, D>

KnexRepository.constructor

protected _trx: Transaction<any, any[]> | null = null

Defined in: knex-repository.ts:20

KnexRepository._trx


readonly columnCase: "camelCase" | "snake_case" = "camelCase"

Defined in: knex-repository.ts:35

Naming convention used to derive column names from entity field names. “camelCase” (default) uses entity field names verbatim as column names. “snake_case” converts every field automatically (userId -> user_id) so the entity/type can stay camelCase while the table uses snake_case columns.

KnexRepository.columnCase


readonly createdAtField: string = "createdAt"

Defined in: knex-repository.ts:26

Entity-side field written for auto-managed creation timestamps.

"createdAt"

KnexRepository.createdAtField


readonly distanceOperator: DistanceOperator = "<=>"

Defined in: knex-vector-repository.ts:18


readonly fieldMap: Record<string, string> = {}

Defined in: knex-repository.ts:40

Explicit entity-field-to-column overrides, applied before columnCase conversion. Use this for individual columns that don’t follow the table’s general convention.

KnexRepository.fieldMap


readonly idField: string = "id"

Defined in: knex-repository.ts:23

KnexRepository.idField


protected readonly knex: Knex

Defined in: knex-repository.ts:19

KnexRepository.knex


abstract readonly tableName: string

Defined in: knex-repository.ts:22

KnexRepository.tableName


readonly timestamps: boolean = true

Defined in: knex-repository.ts:24

KnexRepository.timestamps


readonly updatedAtField: string = "updatedAt"

Defined in: knex-repository.ts:28

Entity-side field written for auto-managed update timestamps.

"updatedAt"

KnexRepository.updatedAtField


readonly vectorColumn: string = "embedding"

Defined in: knex-vector-repository.ts:17

get db(): QueryBuilder

Defined in: knex-repository.ts:47

Raw query builder for this table, respecting any active transaction.

QueryBuilder

KnexRepository.db


get protected qb(): Knex<any, any[]> | Transaction<any, any[]>

Defined in: knex-repository.ts:79

Knex<any, any[]> | Transaction<any, any[]>

KnexRepository.qb

protected buildQuery(params?): QueryBuilder

Defined in: knex-repository.ts:94

QueryParams

QueryBuilder

KnexRepository.buildQuery


count(params?): Promise<number>

Defined in: knex-repository.ts:297

QueryParams

Promise<number>

VectorRepository.count

KnexRepository.count


deleteById(id): Promise<T>

Defined in: knex-repository.ts:272

Id

Promise<T>

VectorRepository.deleteById

KnexRepository.deleteById


deleteVector(id): Promise<T>

Defined in: knex-vector-repository.ts:119

Delete a record and its associated vector by id.

Id

Promise<T>

VectorRepository.deleteVector


describe(): RepositoryCapabilities

Defined in: knex-repository.ts:51

Optional capability introspection. All

RepositoryCapabilities

adapters implement it; user repositories may omit it.

VectorRepository.describe

KnexRepository.describe


findAll(params?): Promise<T[]>

Defined in: knex-repository.ts:152

QueryParams

Promise<T[]>

VectorRepository.findAll

KnexRepository.findAll


findById(id): Promise<T | null>

Defined in: knex-repository.ts:164

Id

Promise<T | null>

VectorRepository.findById

KnexRepository.findById


findSimilar(vector, topK, params?): Promise<T & object[]>

Defined in: knex-vector-repository.ts:37

Find the top-K records most similar to the given embedding vector. Results include a synthetic _score column with the computed pgvector distance — LOWER is more similar (it is a distance, not a similarity). Generates: SELECT *, $1::vector AS _score FROM

ORDER BY $1::vector LIMIT $2

number[]

number

QueryParams

Promise<T & object[]>

VectorRepository.findSimilar


protected mapDataToColumns(data): Record<string, unknown>

Defined in: knex-repository.ts:130

Renames the keys of a data payload from entity field names to column names.

Record<string, unknown>

Record<string, unknown>

KnexRepository.mapDataToColumns


protected mapRowToEntity(row): T

Defined in: knex-repository.ts:135

Renames the keys of a returned row from column names to entity field names.

Record<string, unknown>

T

KnexRepository.mapRowToEntity


patchById(id, data): Promise<T>

Defined in: knex-repository.ts:243

Id

D

Promise<T>

VectorRepository.patchById

KnexRepository.patchById


save(data): Promise<T>

Defined in: knex-repository.ts:175

D

Promise<T>

VectorRepository.save

KnexRepository.save


saveAll(data): Promise<T[]>

Defined in: knex-repository.ts:192

D[]

Promise<T[]>

VectorRepository.saveAll

KnexRepository.saveAll


protected toColumn(field): string

Defined in: knex-repository.ts:117

Translates an entity field name to its column name (fieldMap override, then columnCase convention).

string

string

KnexRepository.toColumn


protected toEntityField(column): string

Defined in: knex-repository.ts:123

Translates a column name back to its entity field name — the inverse of toColumn.

string

string

KnexRepository.toEntityField


updateById(id, data): Promise<T>

Defined in: knex-repository.ts:217

Id

D

Promise<T>

VectorRepository.updateById

KnexRepository.updateById


upsertVector(id, vector, data): Promise<T>

Defined in: knex-vector-repository.ts:82

Upsert a record with its embedding vector: updates the vector and data columns on an existing row (but not createdAt), or inserts a new one.

Deliberately an UPDATE followed by a conditional INSERT rather than a single INSERT ... ON CONFLICT DO UPDATE — Postgres validates NOT NULL constraints on the full candidate insert tuple before checking for a conflict, so a single-statement upsert that only lists the id/vector/timestamp columns fails on any other NOT NULL column (e.g. a required title) even when the conflicting row already has one. Not atomic: a concurrent upsertVector on the same id between the UPDATE and the INSERT could race to insert twice.

Id

number[]

Partial<T>

Promise<T>

VectorRepository.upsertVector


withTransaction<R>(callback): Promise<R>

Defined in: knex-repository.ts:69

Run a set of repository operations inside a single database transaction. The callback receives a transaction-scoped copy of this repository.

R

(repo) => Promise<R>

Promise<R>

KnexRepository.withTransaction


protected wrapError(err): Error

Defined in: knex-repository.ts:313

unknown

Error

KnexRepository.wrapError