0.1.0 • Published 5 months ago
cosdata-sdk v0.1.0
COSDATA Vector Store (Node.js SDK)
A TypeScript/JavaScript SDK for interacting with the COSDATA Vector Database API.
Installation
npm install cosdata-sdk
Usage
Basic Usage
import { createClient } from 'cosdata-sdk';
// Initialize the client
const client = createClient({
host: 'http://127.0.0.1:8443',
username: 'admin',
password: 'admin'
});
// Create a collection
const collection = await client.createCollection({
name: 'my_vectors',
dimension: 768,
description: 'My vector collection'
});
// Create an index
const index = await collection.createIndex({
distanceMetric: 'cosine'
});
// Insert vectors
const vectors = [
{ id: 1, values: [0.1, 0.2, 0.3, /* ... */] },
{ id: 2, values: [0.2, 0.3, 0.4, /* ... */] },
// ...
];
// Using a transaction
await index.transaction(async (txn) => {
await txn.upsert(vectors);
});
// Query vectors
const results = await index.query({
vector: [0.1, 0.2, 0.3, /* ... */],
nnCount: 5
});
// Fetch a specific vector
const vector = await index.fetchVector(1);
// Get collection info
const info = await collection.getInfo();
// List all collections
const collections = await client.collections();
Transaction Management
The SDK provides transaction management for batch operations:
// Manual transaction management
const txn = index.createTransaction();
try {
await txn.upsert(vectors);
await txn.commit();
} catch (error) {
await txn.abort();
throw error;
}
// Automatic transaction management (recommended)
await index.transaction(async (txn) => {
await txn.upsert(vectors);
// Transaction is automatically committed on success
// or aborted on error
});
API Reference
Client
createClient(options)
: Create a new client instanceclient.createCollection(options)
: Create a new collectionclient.getCollection(name)
: Get an existing collectionclient.collection(name)
: Alias for getCollectionclient.listCollections()
: List all collectionsclient.collections()
: Get all collections as Collection objects
Collection
collection.createIndex(options)
: Create a new indexcollection.index(distanceMetric)
: Get or create an indexcollection.getInfo()
: Get collection information
Index
index.createTransaction()
: Create a new transactionindex.transaction(callback)
: Execute operations in a transactionindex.query(options)
: Search for similar vectorsindex.fetchVector(id)
: Fetch a specific vector by ID
Transaction
transaction.upsert(vectors)
: Insert or update vectorstransaction.commit()
: Commit the transactiontransaction.abort()
: Abort the transaction
License
MIT
0.1.0
5 months ago