Node.js SDK Reference

The StrataDB Node.js SDK provides native bindings via NAPI-RS with full TypeScript support.

Installation

npm install stratadb
# or
yarn add stratadb

Quick Start

import { Strata } from 'stratadb';

// Open a database
const db = Strata.open('/path/to/data');

// Store and retrieve data
db.kvPut('greeting', 'Hello, World!');
console.log(db.kvGet('greeting'));  // "Hello, World!"

// Use transactions
db.begin();
try {
  db.kvPut('a', 1);
  db.kvPut('b', 2);
  db.commit();
} catch (e) {
  db.rollback();
  throw e;
}

// Vector search
db.vectorCreateCollection('embeddings', 384);
db.vectorUpsert('embeddings', 'doc-1', new Array(384).fill(0.1));
const results = db.vectorSearch('embeddings', new Array(384).fill(0.1), 5);

Opening a Database

Strata.open(path)

Open a database at the given path.

const db = Strata.open('/path/to/data');

Returns: Strata instance

Strata.cache()

Create an ephemeral in-memory database.

const db = Strata.cache();

KV Store

MethodReturnsDescription
kvPut(key, value)numberStore value, returns version
kvGet(key)JsonValue | nullGet value
kvDelete(key)booleanDelete key
kvList(prefix?)string[]List keys
kvHistory(key)VersionedValue[] | nullVersion history
kvGetVersioned(key)VersionedValue | nullGet with version info
kvListPaginated(prefix?, limit?, cursor?)KvListResultPaginated list

Example:

// Store complex values
db.kvPut('user:123', { name: 'Alice', age: 30 });

// Get with version info
const result = db.kvGetVersioned('user:123');
if (result) {
  console.log(`v${result.version}: ${JSON.stringify(result.value)}`);
}

// Paginate large key sets
const page = db.kvListPaginated('user:', 100);
for (const key of page.keys) {
  console.log(key);
}

State Cell

MethodReturnsDescription
stateSet(cell, value)numberSet value
stateGet(cell)JsonValue | nullGet value
stateInit(cell, value)numberInitialize if not exists
stateCas(cell, value, expected?)numberCompare-and-swap
stateDelete(cell)booleanDelete cell
stateList(prefix?)string[]List cells
stateHistory(cell)VersionedValue[] | nullVersion history

Example:

// Atomic counter
db.stateInit('counter', 0);
const current = db.stateGet('counter') as number;
try {
  db.stateCas('counter', current + 1, current);
} catch (e) {
  console.log('Concurrent modification!');
}

Event Log

MethodReturnsDescription
eventAppend(eventType, payload)numberAppend, returns sequence
eventGet(sequence)VersionedValue | nullGet by sequence
eventList(eventType, limit?, after?)VersionedValue[]List events
eventLen()numberTotal count

Example:

// Append events
const seq = db.eventAppend('user_action', { action: 'click', target: 'button' });

// Read events by type
const events = db.eventList('user_action', 10);
for (const event of events) {
  console.log(`[${event.timestamp}] ${JSON.stringify(event.value)}`);
}

JSON Store

MethodReturnsDescription
jsonSet(key, path, value)numberSet at JSONPath
jsonGet(key, path)JsonValue | nullGet at JSONPath
jsonDelete(key, path)numberDelete at JSONPath
jsonList(prefix?, limit?)string[]List documents
jsonHistory(key)VersionedValue[] | nullVersion history

Example:

// Store document
db.jsonSet('config', '$', { debug: true, timeout: 30 });

// Update nested path
db.jsonSet('config', '$.timeout', 60);

// Read specific path
const timeout = db.jsonGet('config', '$.timeout');

Vector Store

MethodReturnsDescription
vectorCreateCollection(name, dim, metric?)numberCreate collection
vectorDeleteCollection(name)booleanDelete collection
vectorListCollections()CollectionInfo[]List collections
vectorUpsert(coll, key, vector, metadata?)numberInsert/update
vectorGet(coll, key)object | nullGet vector
vectorDelete(coll, key)booleanDelete vector
vectorSearch(coll, query, k, metric?, filters?)VectorMatch[]Search
vectorBatchUpsert(coll, entries)number[]Batch insert

Example:

// Create and populate
db.vectorCreateCollection('docs', 384, 'cosine');
db.vectorUpsert('docs', 'doc-1', embedding, { title: 'Hello' });

// Search with filters
const results = db.vectorSearch(
  'docs',
  queryEmbedding,
  10,
  undefined,
  [{ field: 'category', op: 'eq', value: 'science' }]
);

Branches

MethodReturnsDescription
branchCreate(name)voidCreate branch
branchGet(name)BranchInfo | nullGet branch info
branchList()string[]List branches
branchExists(name)booleanCheck existence
branchDelete(name)voidDelete branch
branchFork(source, dest)ForkResultFork with data
branchDiff(a, b)DiffResultCompare branches
branchMerge(source, target, strategy?)MergeResultMerge
branchUse(name)voidSwitch branch
currentBranch()stringGet current
branchExport(name, path)ExportResultExport to bundle
branchImport(path)ImportResultImport from bundle

Spaces

MethodReturnsDescription
spaceCreate(name)voidCreate space
spaceList()string[]List spaces
spaceExists(name)booleanCheck existence
spaceDelete(name)voidDelete empty space
spaceUse(name)voidSwitch space
currentSpace()stringGet current

Transactions

MethodReturnsDescription
begin(readOnly?)voidStart transaction
commit()numberCommit, returns version
rollback()voidRollback transaction
inTransaction()booleanCheck if active
transactionInfo()TransactionInfo | nullGet transaction info

Example:

db.begin();
try {
  db.kvPut('a', 1);
  db.kvPut('b', 2);
  const version = db.commit();
  console.log(`Committed at version ${version}`);
} catch (e) {
  db.rollback();
  throw e;
}

search(query, k?, primitives?)

Search across multiple primitives.

const results = db.search('hello world', 20, ['kv', 'json']);
for (const hit of results) {
  console.log(`${hit.primitive}/${hit.entity}: ${hit.snippet}`);
}

Database Operations

MethodReturnsDescription
ping()stringCheck connectivity, returns version
info()DatabaseInfoGet database statistics
flush()voidFlush pending writes
compact()voidTrigger compaction

TypeScript Types

interface VersionedValue {
  value: JsonValue;
  version: number;
  timestamp: number;
}

interface KvListResult {
  keys: string[];
}

interface TransactionInfo {
  id: string;
  status: string;
  started_at: number;
}

interface MetadataFilter {
  field: string;
  op: 'eq' | 'ne' | 'gt' | 'gte' | 'lt' | 'lte' | 'in' | 'contains';
  value: JsonValue;
}

interface SearchHit {
  entity: string;
  primitive: string;
  score: number;
  rank: number;
  snippet: string;
}

type JsonValue = string | number | boolean | null | JsonValue[] | { [key: string]: JsonValue };