Skip to content

Guide: CRUD and list

This page covers create (insert), read (list, count), update, and delete in Vecnest.


Create (insert)

You already know addText and insert from the JavaScript API. Summary:

// With embeddings (Transformers.js)
await db.addText('Some text.', { source: 'doc1' });

// Raw vectors
await db.insert([0.1, -0.2, ...], { label: 'custom' });

// Batch (low-level)
import { openDB, insertBatch } from 'vecnest';
const db = await openDB('my-db');
const ids = await insertBatch(db, [
  { vector: [0.1, ...], metadata: { title: 'A' } },
  { vector: [0.2, ...], metadata: { title: 'B' } },
]);

Read: list and count

List vectors

list(opts) returns stored vectors, newest first, with optional pagination.

const items = await db.list({ limit: 50, offset: 0 });
// Each item: { id, vector, metadata, created }

Example: render a document list:

const docs = await db.list({ limit: 100 });
for (const d of docs) {
  console.log(d.id, d.metadata?.text ?? '(no text)', new Date(d.created));
}

Count

count() returns the total number of vectors.

const total = await db.count();
console.log(`${total} vectors in database`);

Storage estimate

VecnestDB.getStorageEstimate() is a static method. It returns { usage, quota } in bytes for the current origin (all site storage, not just Vecnest).

const { usage, quota } = await VecnestDB.getStorageEstimate();
console.log(`Used: ${(usage / 1024 / 1024).toFixed(2)} MB`);
console.log(`Quota: ${(quota / 1024 / 1024 / 1024).toFixed(2)} GB`);

Update

update(id, updates) updates a vector and/or its metadata by ID.

// Update metadata only
await db.update(123, { metadata: { category: 'updated', tags: ['a', 'b'] } });

Metadata is merged with existing keys. You can overwrite specific fields without losing others.

Update the vector too (e.g. after re-embedding text):

import { embed } from 'vecnest';

const newText = 'Updated document content.';
const vector = await embed(newText);
await db.update(123, {
  vector,
  metadata: { ...existingMetadata, text: newText },
});

When the vector changes, Vecnest triggers an asynchronous HNSW index rebuild so search stays accurate.


Delete

delete(id, opts?) removes a vector by ID.

await db.delete(123);

Options:

  • rebuildHnsw (default true) — Rebuild the HNSW index after delete. Set to false to skip (e.g. bulk deletes before a single manual rebuild).
await db.delete(123, { rebuildHnsw: false });

After deletes (or bulk updates), you can manually rebuild the index:

await db.rebuildIndex(384); // 384 = vector dimensions (e.g. MiniLM)

Example: list, edit, delete

const docs = await db.list({ limit: 50 });
for (const doc of docs) {
  console.log(doc.id, doc.metadata?.text);

  // Edit
  await db.update(doc.id, { metadata: { ...doc.metadata, edited: true } });

  // Or delete
  await db.delete(doc.id);
}

Next steps