
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
TinyVecDB is a high performance, lightweight, embedded vector database for similarity search.
This document provides a comprehensive overview of the TinyVecDB Node.js API.
npm install tinyvecdb
TinyVecDB is an embedded vector database that emphasizes speed, low memory usage, and simplicity. The core of TinyVecDB is written in C, and this library provides a Node.js binding to that engine. The key concepts are:
import {
TinyVecClient,
type TinyVecInsertion,
type TinyVecSearchOptions,
} from "tinyvecdb";
async function example() {
// Connect to database (will create the file if it doesn't exist)
const client = TinyVecClient.connect("./vectors.db", { dimensions: 512 });
// Create sample vectors
const insertions: TinyVecInsertion[] = [];
for (let i = 0; i < 50; i++) {
// Using Float32Array (more efficient)
const vec = new Float32Array(512);
for (let j = 0; j < 512; j++) {
vec[j] = Math.random();
}
// Or using standard JavaScript arrays
// const vec = Array.from({ length: 512 }, () => Math.random());
insertions.push({
vector: vec,
metadata: { name: `item-${i}`, category: "example" },
});
}
// Insert vectors
const inserted = await client.insert(insertions);
console.log("Inserted:", inserted);
// Create a query vector
const queryVector = new Float32Array(512);
for (let i = 0; i < 512; i++) {
queryVector[i] = Math.random();
}
// Search for similar vectors (without filtering)
const results = await client.search(queryVector, 5);
// Example results:
/*
[
{
id: 8,
similarity: 0.801587700843811,
metadata: { name: 'item-8', category: 'example' }
},
{
id: 16,
similarity: 0.7834401726722717,
metadata: { name: 'item-16', category: 'example' }
},
{
id: 5,
similarity: 0.7815409898757935,
metadata: { name: 'item-5', category: 'example' }
},
...
]
*/
// Search with filtering
const searchOptions: TinyVecSearchOptions = {
filter: { category: { $eq: "example" } },
};
const filteredResults = await client.search(queryVector, 5, searchOptions);
// Delete items by ID
const deleteByIdResult = await client.deleteByIds([1, 2, 3]);
console.log("Delete by ID result:", deleteByIdResult);
// Example output: Delete by ID result: { deletedCount: 3, success: true }
// Delete by metadata filter
const deleteByFilterResult = await client.deleteByFilter({
filter: { category: { $eq: "example" } },
});
console.log("Delete by filter result:", deleteByFilterResult);
// Example output: Delete by filter result: { deletedCount: 47, success: true }
// Get database statistics
const stats = await client.getIndexStats();
console.log(
`Database has ${stats.vectors} vectors with ${stats.dimensions} dimensions`
);
}
The main class you'll interact with is TinyVecClient. It provides all methods for managing the vector database.
TinyVecClient.connect(path, config?)Creates and connects to a TinyVecDB database.
Parameters:
path: string - Path to the database fileconfig?: TinyVecConfig (optional) - Configuration optionsExample:
const client = TinyVecClient.connect("./vectors.db", { dimensions: 512 });
async insert(vectors)Inserts vectors with metadata into the database. Each metadata item must be a JSON-serializable object.
Parameters:
vectors: TinyVecInsertion[] - Array of vectors to insertReturns:
Promise<number> - The number of vectors successfully insertedExample:
const vector = new Float32Array(512).fill(0.1);
const count = await client.insert([
{
vector,
metadata: {
documentId: "doc1",
title: "Example Document",
category: "reference",
},
},
]);
async search(queryVector, topK, searchOptions?)Searches for the most similar vectors to the query vector, with optional filtering.
Parameters:
queryVector: NumericArraytype NumericArray =
| number[]
| Float32Array
| Float64Array
| Int8Array
| Int16Array
| Int32Array
| Uint8Array
| Uint16Array
| Uint32Array;
Internally, it will be converted to a Float32Array for similarity calculations.topK: number - Number of results to returnsearchOptions?: TinyVecSearchOptions (optional) - Contains filter criteria for the searchReturns:
Promise<TinyVecResult[]> - Array of search resultsExample:
// Search without filtering
const results = await client.search(queryVector, 10);
// Search with filtering
const searchOptions: TinyVecSearchOptions = {
filter: { year: { $eq: 2024 } },
};
const filteredResults = await client.search(queryVector, 10, searchOptions);
async deleteByIds(ids)Deletes vectors by their IDs.
Parameters:
ids: number[] - Array of vector IDs to deleteReturns:
Promise<TinyVecDeletionResult> - Object containing deletion count and success statusExample:
const result = await client.deleteByIds([1, 2, 3]);
console.log(
`Deleted ${result.deletedCount} vectors. Success: ${result.success}`
);
// Example output: Deleted 3 vectors. Success: true
async deleteByFilter(searchOptions)Deletes vectors that match the given filter criteria.
Parameters:
searchOptions: TinyVecSearchOptions - Contains filter criteria for deletionReturns:
Promise<TinyVecDeletionResult> - Object containing deletion count and success statusExample:
const searchOptions: TinyVecSearchOptions = {
filter: { year: { $eq: 2024 } },
};
const result = await client.deleteByFilter(searchOptions);
console.log(
`Deleted ${result.deletedCount} vectors. Success: ${result.success}`
);
async updateById(items)Updates existing database entries with new metadata and/or vector values.
Parameters:
items: Array<UpdateItem> - Array of items to update, each containing ID and optional metadata/vector dataReturns:
UpdateResult - Object containing update count and success statusExample:
import { TinyVecClient, type UpdateItem } from "tinyvecdb";
// Create update items
const updateItems: UpdateItem[] = [
{
id: 1,
vector: new Float32Array(
Array(128)
.fill()
.map(() => Math.random())
),
metadata: { category: "electronics", inStock: true },
},
{
id: 42,
metadata: { price: 99.99, featured: true },
},
{
id: 75,
vector: new Float32Array(
Array(128)
.fill()
.map(() => Math.random())
),
},
];
// Update entries in the database
const result = await client.updateById(updateItems);
console.log(
`Updated ${result.updatedCount} entries. Success: ${result.success}`
);
async getPaginated(config)Retrieves database entries with pagination support.
Parameters:
config: PaginationConfig - Configuration object containing skip and limit parameters for paginationReturns:
Array<PaginationItem> - Array of paginated items, each containing ID, metadata, and vector dataExample:
import { TinyVecClient, type PaginationConfig } from "tinyvecdb";
// Create a pagination configuration
const config: PaginationConfig = {
skip: 0,
limit: 10,
};
// Retrieve paginated results
const paginatedItems = await client.getPaginated(config);
console.log(`Retrieved ${paginatedItems.length} items`);
// Process the results
for (const item of paginatedItems) {
console.log(`ID: ${item.id}, Metadata:`, item.metadata);
}
// Get the next page
const nextPageConfig: PaginationConfig = {
skip: 10,
limit: 10,
};
const nextPage = await client.getPaginated(nextPageConfig);
console.log(`Next page has ${nextPage.length} items`);
The PaginationConfig type requires two properties:
skip: Number of items to skip (must be non-negative)limit: Maximum number of items to return (must be positive)Each returned pagination item contains:
id: Unique identifier for the vectormetadata: Associated metadata (as a JSON-serializable value)vector: The actual vector data as an arrayasync getIndexStats()Retrieves statistics about the database.
Parameters:
Returns:
Promise<TinyVecStats> - Database statisticsExample:
const stats = await client.getIndexStats();
console.log(
`Database has ${stats.vectors} vectors with ${stats.dimensions} dimensions`
);
TinyVecDeletionResultResult from delete operations.
Properties:
deletedCount: number - Number of vectors deletedsuccess: boolean - Whether the operation was successfulExample:
const result = await client.deleteByIds([1, 2, 3]);
if (result.success) {
console.log(`Successfully deleted ${result.deletedCount} vectors`);
} else {
console.log("Deletion operation failed");
}
TinyVecConfigConfiguration for the vector database.
Properties:
dimensions: number - The dimensionality of vectors to be storedExample:
const config: TinyVecConfig = { dimensions: 512 };
TinyVecInsertionInterface representing a vector to be inserted.
Properties:
vector: NumericArray - The vector datametadata: Record<string, any> - JSON-serializable metadata associated with the vectorExample:
const insertion: TinyVecInsertion = {
vector: new Float32Array(512).fill(0.1),
metadata: { category: "example" },
};
TinyVecSearchOptionsOptions for search queries, including filtering.
Properties:
filter: object - Filter criteria in MongoDB-like query syntaxAvailable filter operators:
$eq: Matches values equal to a specified value$gt: Matches values greater than a specified value$gte: Matches values greater than or equal to a specified value$in: Matches any values specified in an array$lt: Matches values less than a specified value$lte: Matches values less than or equal to a specified value$ne: Matches values not equal to a specified value$nin: Matches none of the values specified in an arrayFilters can be nested for complex queries.
Example:
// Simple filter
const searchOptions: TinyVecSearchOptions = {
filter: { make: { $eq: "Toyota" } },
};
// Complex nested filter
const complexSearchOptions: TinyVecSearchOptions = {
filter: {
category: {
subcategory: {
value: { $gt: 200 },
},
},
tags: { $in: ["premium", "featured"] },
},
};
TinyVecResultInterface representing a search result.
Properties:
similarity: number - Cosine similarity scoreindex: number - ID of the matched vectormetadata: Record<string, any> | null - Metadata associated with the matched vectorExample:
// Results from a search query
for (const result of results) {
console.log(
`ID: ${result.index}, Similarity: ${result.similarity}, Metadata:`,
result.metadata
);
}
// Example output:
// ID: 34, Similarity: 0.8109967303276062, Metadata: { category: 'example', name: 'item-34' }
// ID: 46, Similarity: 0.789353609085083, Metadata: { category: 'example', name: 'item-46' }
// ID: 22, Similarity: 0.7870827913284302, Metadata: { category: 'example', name: 'item-22' }
TinyVecStatsInterface representing database statistics.
Properties:
vectors: number - Number of vectors in the databasedimensions: number - Dimensionality of the vectorsExample:
const stats = await client.getIndexStats();
console.log(`Vector count: ${stats.vectors}, Dimensions: ${stats.dimensions}`);
If you're using TinyVecDB in a Next.js application, you'll need to add the following configuration to your next.config.js file to ensure proper functionality:
/** @type {import('next').NextConfig} */
const nextConfig = {
// This prevents Next.js from bundling TinyVecDB and allows it to work correctly on the server
serverExternalPackages: ["tinyvecdb"],
// Your other Next.js configuration options...
};
module.exports = nextConfig;
This configuration is required because TinyVecDB uses native Node.js addons which cannot be bundled by Next.js. The serverExternalPackages option tells Next.js to load TinyVecDB directly from the Node.js environment on the server instead of trying to bundle it.
experimental.serverComponentsExternalPackages insteadFAQs
TinyVecDB is a high performance, lightweight, embedded vector database for similarity search.
The npm package tinyvecdb receives a total of 3 weekly downloads. As such, tinyvecdb popularity was classified as not popular.
We found that tinyvecdb demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.