Cosdata Node.js SDK
A TypeScript/JavaScript SDK for interacting with the Cosdata Vector Database.
Installation
npm install cosdata-sdk
Quick Start
import { createClient } from 'cosdata-sdk';
const client = createClient({
host: 'http://127.0.0.1:8443',
username: 'admin',
password: 'test_key',
verifySSL: false
});
const collection = await client.createCollection({
name: 'my_collection',
dimension: 128,
dense_vector: {
enabled: true,
dimension: 128,
auto_create_index: false
}
});
const index = await collection.createIndex({
name: 'my_collection_dense_index',
distance_metric: 'cosine',
quantization_type: 'auto',
sample_threshold: 100,
num_layers: 16,
max_cache_size: 1024,
ef_construction: 128,
ef_search: 64,
neighbors_count: 10,
level_0_neighbors_count: 20
});
function generateRandomVector(dimension: number): number[] {
return Array.from({ length: dimension }, () => Math.random());
}
const vectors = Array.from({ length: 100 }, (_, i) => ({
id: `vec_${i}`,
dense_values: generateRandomVector(128),
document_id: `doc_${i}`
}));
const txn = collection.transaction();
await txn.batch_upsert_vectors(vectors);
await txn.commit();
const results = await collection.getSearch().dense({
query_vector: generateRandomVector(128),
top_k: 5,
return_raw_text: true
});
const exists = await collection.getVectors().exists('vec_1');
console.log('Vector exists:', exists);
const collectionInfo = await collection.getInfo();
console.log('Collection info:', collectionInfo);
const collections = await client.listCollections();
console.log('Available collections:', collections);
const currentVersion = await collection.getVersions().getCurrent();
console.log('Current version:', currentVersion);
await collection.delete();
API Reference
Client
The main client for interacting with the Vector Database API.
const client = createClient({
host: 'http://127.0.0.1:8443',
username: 'admin',
password: 'test_key',
verifySSL: false
});
Methods:
createCollection(options: { name: string, dimension: number, dense_vector?: { enabled: boolean, dimension: number, auto_create_index: boolean }, sparse_vector?: { enabled: boolean, auto_create_index: boolean }, tf_idf_options?: { enabled: boolean } }): Promise<Collection>
listCollections(): Promise<string[]>
getCollection(name: string): Promise<Collection>
Collection
The Collection class provides access to all collection-specific operations.
const collection = await client.createCollection({
name: 'my_collection',
dimension: 128,
dense_vector: {
enabled: true,
dimension: 128,
auto_create_index: false
}
});
Methods:
createIndex(options: { name: string, distance_metric: string, quantization_type: string, sample_threshold: number, num_layers: number, max_cache_size: number, ef_construction: number, ef_search: number, neighbors_count: number, level_0_neighbors_count: number }): Promise<Index>
getInfo(): Promise<CollectionInfo>
- Returns collection information:
interface CollectionInfo {
name: string;
description?: string;
dense_vector?: {
enabled: boolean;
dimension: number;
auto_create_index: boolean;
};
sparse_vector?: {
enabled: boolean;
auto_create_index: boolean;
};
metadata_schema?: Record<string, any>;
config?: {
max_vectors?: number;
replication_factor?: number;
};
}
delete(): Promise<void>
transaction(): Transaction
getVectors(): Vectors
getSearch(): Search
getVersions(): Versions
Transaction
The Transaction class provides methods for vector operations.
const txn = collection.transaction();
await txn.batch_upsert_vectors(vectors);
await txn.commit();
Methods:
upsert_vector(vector: Vector): Promise<void>
batch_upsert_vectors(vectors: Vector[]): Promise<void>
commit(): Promise<void>
abort(): Promise<void>
Search
The Search class provides methods for vector similarity search.
const results = await collection.getSearch().dense({
query_vector: vector,
top_k: 5,
return_raw_text: true
});
Methods:
dense(options: { query_vector: number[], top_k?: number, return_raw_text?: boolean }): Promise<SearchResponse>
sparse(options: { query_terms: number[][], top_k?: number, early_terminate_threshold?: number, return_raw_text?: boolean }): Promise<SearchResponse>
- Returns search results with the same interface as dense search
text(options: { query_text: string, top_k?: number, return_raw_text?: boolean }): Promise<SearchResponse>
- Performs text search using TF-IDF and returns results with the same interface as dense search
Vectors
The Vectors class provides methods for vector operations.
const exists = await collection.getVectors().exists('vec_1');
const vector = await collection.getVectors().get('vec_1');
Methods:
get(vector_id: string): Promise<any>
exists(vector_id: string): Promise<boolean>
delete(vector_id: string): Promise<void>
Versions
The Versions class provides methods for version management.
const currentVersion = await collection.getVersions().getCurrent();
const allVersions = await collection.getVersions().list();
Methods:
getCurrent(): Promise<Version>
list(): Promise<ListVersionsResponse>
getByHash(versionHash: string): Promise<Version>
- Returns version information for a specific hash
Best Practices
-
Connection Management
- Use
createClient()
to initialize the client
- Reuse the client instance across your application
- The client automatically handles authentication and token management
-
Vector Operations
- Use transactions for batch operations
- Always call
commit()
after successful operations
- Use
abort()
in case of errors
- Maximum batch size is 200 vectors per transaction
-
Error Handling
- All operations return promises that reject on failure
- Use try/catch blocks for error handling
- Always clean up resources (delete collections) after testing
-
Performance
- Adjust index parameters based on your use case
- Use appropriate vector dimensions
- Consider batch sizes for large operations
-
Version Management
- Use versions to track collection evolution
- Clean up old versions when no longer needed
License
This project is licensed under the MIT License - see the LICENSE file for details.