Vector search with sqlite-vec
Enable sqlite-vec for vector search in React Native or NitroUI.
Vectors are numeric arrays, often called embeddings. The sqlite-vec extension adds vec0 virtual tables to SQLite so you can store vectors and find nearby matches. Its SQL works in either host once the native extension is registered. See supported platforms.
Install the companion for your runtime. Both expose the same functions for creating vector tables and finding neighbors.
Enable vector search in React Native
Install react-native-nitro-sqlite-vec alongside react-native-nitro-sqlite:
npm install --save-exact react-native-nitro-sqlite@alpha react-native-nitro-sqlite-vec@alphaEnable it for each platform you ship using the iOS or Android configuration page, then rebuild the native app. Check availability with isVecAvailable(db) before running vector queries. A JavaScript update alone cannot enable the extension.
Expo projects can enable Vec during prebuild with the Vec config plugin. For macOS, use the 10.x setup.
NitroUI
Install nitro-sqlite-vec alongside nitro-sqlite. Both must be direct app dependencies:
bun add --exact nitro-sqlite@alpha nitro-sqlite-vec@alphaPrepare and rebuild the native app after adding Vec, just as when installing the SQLite adapter. Vec links to the SQLite engine in the adapter.
Call registerVectorExtensions() once at startup, before opening your first connection. Registration applies to connections opened afterward and lasts for the rest of the process.
import { open } from 'nitro-sqlite'
import { registerVectorExtensions, vecVersion } from 'nitro-sqlite-vec'
registerVectorExtensions()
const db = open({ name: 'vectors.sqlite' })
console.log(vecVersion(db))
db.close()Create and search a vector table
The example below uses React Native imports. On NitroUI, import the same helpers from nitro-sqlite-vec and open from nitro-sqlite after registering the extension.
import { open } from 'react-native-nitro-sqlite'
import {
createVectorTable,
isVecAvailable,
knnSearch,
vecVersion,
} from 'react-native-nitro-sqlite-vec'
const db = open({ name: 'vectors.sqlite' })
if (!isVecAvailable(db)) {
db.close()
throw new Error('Enable sqlite-vec in the native build')
}
console.log(vecVersion(db))
createVectorTable(db, 'embeddings', { dimensions: 3 })
db.execute('INSERT INTO embeddings (rowid, embedding) VALUES (?, ?)', [
1,
'[0.1, 0.2, 0.3]',
])
const matches = knnSearch(db, 'embeddings', [0.1, 0.2, 0.25], 10)
// Each match has rowid and distance.
db.close()vecVersion(db) calls vec_version() and returns its version string. isVecAvailable(db) returns false if that call fails. createVectorTable(db, table, options) creates a vec0 virtual table if it does not exist. knnSearch(db, table, query, k, options?) returns an array of { rowid, distance } matches, ordered by distance; query can be a numeric array or a JSON vector string.
All four helpers are synchronous and take an open Connection:
| Export | Signature |
|---|---|
vecVersion | (db: Connection) => string |
isVecAvailable | (db: Connection) => boolean |
createVectorTable | (db: Connection, table: string, options: CreateVectorTableOptions) => void |
knnSearch | (db: Connection, table: string, query: string | number[], k: number, options?: KnnSearchOptions) => KnnMatch[] |
isVecAvailable() catches any error from vecVersion(), so false means its availability query failed, not necessarily that the build flag is absent. The other helpers let query errors propagate from db.execute().
CreateVectorTableOptions has dimensions: number, optional type: VectorColumnType, optional distanceMetric: VectorDistanceMetric, and optional column: string. VectorColumnType is 'float' | 'int8' | 'bit'; 'float' is the default. VectorDistanceMetric is 'L2' | 'cosine' | 'L1'; sqlite-vec's default is L2. The column defaults to 'embedding'. KnnSearchOptions has an optional column: string with the same default. For int8 and bit tables, construct vectors with sqlite-vec's vec_int8() and vec_bit() SQL functions as appropriate.
The package's five named type exports are VectorColumnType, VectorDistanceMetric, CreateVectorTableOptions, KnnSearchOptions, and KnnMatch. KnnMatch has rowid: number, distance: number, and a [column: string]: unknown index signature. The current knnSearch() helper selects only rowid and distance; write SQL through the core connection when you need more columns.
The helpers interpolate table and column identifiers into SQL. Only pass identifiers you control. They bind the query vector and k as parameters. The core connection query methods can also run sqlite-vec SQL directly for features these small helpers do not wrap.