Transactions
vectlite supports atomic transactions for batched writes. All operations within a transaction are committed together or rolled back on failure.
Python
Use the context manager for automatic commit/rollback:
with db.transaction() as tx:
tx.upsert("doc1", emb1, {"source": "a"})
tx.upsert("doc2", emb2, {"source": "b"})
tx.delete("old_doc")
# Commits atomically on exit; rolls back on exception
Manual commit/rollback:
tx = db.transaction()
try:
tx.upsert("doc1", emb1, {"source": "a"})
tx.insert_many([
{"id": "d2", "vector": emb2, "metadata": {"source": "b"}},
{"id": "d3", "vector": emb3, "metadata": {"source": "c"}},
])
tx.commit()
except Exception:
tx.rollback()
raise
Node.js
const tx = db.transaction()
try {
tx.upsert('doc1', emb1, { source: 'a' })
tx.upsert('doc2', emb2, { source: 'b' })
tx.delete('old_doc')
tx.commit()
} catch (err) {
tx.rollback()
throw err
}
Batch Operations
Transactions support bulk inserts and upserts:
with db.transaction() as tx:
count = tx.insert_many([
{"id": "d1", "vector": emb1, "metadata": {"source": "a"}},
{"id": "d2", "vector": emb2, "metadata": {"source": "b"}},
])
print(f"Inserted {count} records")
Transaction Size
You can check how many operations are queued:
with db.transaction() as tx:
tx.upsert("doc1", emb1)
tx.upsert("doc2", emb2)
print(len(tx)) # 2