TypeScript / Node.js API
TypeScript/Node.js bindings for LatticeDB, an embedded knowledge graph database for AI/RAG applications.
Installation
npm install @hajewski/latticedb
The published package bundles the native shared library for Linux (x64, arm64), macOS (x64, arm64) and Windows (x64, arm64), so nothing else needs installing. From a source checkout, build it with zig build shared; the loader finds zig-out/lib (or set LATTICE_LIB_PATH).
Quick Start
import { Database } from "@hajewski/latticedb";
const db = new Database("knowledge.db", {
create: true,
enableVectors: true,
vectorDimensions: 4,
});
await db.open();
// Create nodes, edges, and index content
await db.createNodeFtsIndex("Person", "bio");
await db.write(async (txn) => {
const alice = await txn.createNode({
labels: ["Person"],
properties: { name: "Alice", age: 30 },
});
const bob = await txn.createNode({
labels: ["Person"],
properties: { name: "Bob", age: 25 },
});
await txn.createEdge(alice.id, bob.id, "KNOWS");
// Writing the indexed property is what makes it searchable
await txn.setProperty(alice.id, "bio", "Alice works on machine learning research");
await txn.setProperty(bob.id, "bio", "Bob studies deep learning and neural networks");
// Store vector embeddings
await txn.setVector(
alice.id,
"embedding",
new Float32Array([1.0, 0.0, 0.0, 0.0])
);
await txn.setVector(
bob.id,
"embedding",
new Float32Array([0.0, 1.0, 0.0, 0.0])
);
});
// Query with Cypher
const result = await db.query(
"MATCH (n:Person) WHERE n.age > 20 RETURN n.name, n.age"
);
for (const row of result.rows) {
console.log(row);
}
// Vector similarity search
const results = await db.vectorSearch(
new Float32Array([0.9, 0.1, 0.0, 0.0]),
{ k: 2 }
);
for (const r of results) {
console.log(`Node ${r.nodeId}: distance=${r.distance.toFixed(4)}`);
}
// Full-text search
const ftsResults = await db.ftsSearch("Person", "bio", "machine learning");
for (const r of ftsResults) {
console.log(`Node ${r.nodeId}: score=${r.score.toFixed(4)}`);
}
// Fuzzy search (typo-tolerant)
const fuzzyResults = await db.ftsSearchFuzzy("Person", "bio", "machin lerning");
for (const r of fuzzyResults) {
console.log(`Node ${r.nodeId}: score=${r.score.toFixed(4)}`);
}
await db.close();
API Reference
Database
const db = new Database(path: string, options?: DatabaseOptions);
interface DatabaseOptions {
create?: boolean; // Create if not exists (default: false)
readOnly?: boolean; // Open read-only (default: false)
cacheSizeMb?: number; // Cache size in MB; unset: LATTICE_BUFFER_POOL_MB, else 100
enableWal?: boolean; // log commits so they survive a crash (default: true)
enableAdjacencyCache?: boolean; // Accepted; no effect on queries (default: false)
enableVectors?: boolean; // Create a vector index if the file has none
enableVector?: boolean; // Deprecated compatibility alias
vectorDimensions?: number; // Vector dimensions (default: 128; 0 = the file's)
lock?: boolean; // Take the file lock (default: true)
syncMode?: 'full' | 'normal'; // Unset: LATTICE_SYNC_MODE, else 'full'
fullFsync?: boolean; // macOS F_FULLFSYNC; unset: LATTICE_FULL_FSYNC, else off
vectorMetric?: 'cosine' | 'euclidean' | 'dot_product'; // For a new index (default: cosine)
vectorM?: number; // HNSW connections per node, 2..32 (default: 16)
vectorEfConstruction?: number; // HNSW build candidate list, 1..4096 (default: 200)
changefeedRetention?: ChangefeedRetention; // 'all', 'off', 'last:N', 'age:7d' (default: the file's)
}
A database file records its vector index: dimensions, metric, vectorM and
vectorEfConstruction. A database that has an index opens with it whether or
not enableVectors is set, so reopening needs no vector options at all, also
after a crash. enableVectors creates an index if the database has none, and
vectorMetric, vectorM and vectorEfConstruction configure that new index;
once it exists they are fixed. Any vector option you do pass (and
vectorDimensions when enableVectors is set) must match the file, or open()
throws a LatticeError with code -18 (ConfigMismatch).
const db = new Database("vectors.db", {
create: true,
enableVectors: true,
vectorDimensions: 2,
vectorMetric: "euclidean",
});
await db.open();
await db.write(async (txn) => {
const a = await txn.createNode({ labels: ["Doc"] });
await txn.setVector(a.id, "embedding", new Float32Array([1, 0]));
const b = await txn.createNode({ labels: ["Doc"] });
await txn.setVector(b.id, "embedding", new Float32Array([0, 2]));
});
await db.close();
// No vector options: the file says how its index was built.
const reopened = new Database("vectors.db");
await reopened.open();
console.log(await reopened.vectorSearch(new Float32Array([1, 0]), { k: 2 }));
// [ { nodeId: 1n, distance: 0 }, { nodeId: 2n, distance: 2.2360680103302 } ]
await reopened.close();
The metric decides search distances, smallest first: cosine distance (vectors stored at unit length), L2 distance, or the negative dot product (vectors stored as given).
syncMode: 'normal' returns from a commit once the operating system has it: the
commit survives the process being killed, but a power cut or OS crash can lose
the most recent commits. The default, 'full', syncs the log on every commit.
fullFsync: true makes each sync on macOS ask the drive to flush its cache. A
value you pass wins over the LATTICE_SYNC_MODE and LATTICE_FULL_FSYNC
environment variables; they apply only when you leave the option unset. See
Durability and the Log.
enableWal: false logs nothing. Transactions, rollback, streams and the
changefeed work as with the log, but a commit reaches the file at the next
flush (close(), or when half the page cache holds changes) rather than when it
returns; see
Running without the log.
syncMode, fullFsync, the three vector index options and
changefeedRetention need a native library with lattice_open_v5; against an
older one, setting any of them throws with code -14 (Unsupported).
deserialize() takes them too, through lattice_deserialize_v5, with the same
fallback against an older library.
readOnly: true on a database that was not closed cleanly recovers it in
memory: the handle sees every committed transaction, and nothing is written to
the files. If that recovery needs more than 64 MB plus four times the size of
the log and journal, open() throws with code -10 (Full); open the database
read-write once to recover it on disk. A file that a newer release has marked as
not writable by this one opens only with readOnly: true; a writable open
throws with code -19 (NeedsNewerWriter) and writes nothing.
In-memory databases
const db = new Database(':memory:');
Pass :memory: as the path and the database has no files behind it. Nothing is
written to disk and nothing survives closing it, which suits a scratch database, a
test, or one you pulled out of object storage and will hand back as bytes.
It behaves like any other database — transactions, the write-ahead log, and serialization all work. The differences are that it disappears when closed, and that nothing locks it, since no other process can reach it.
The file lock
A database can only be open in one process at a time. Opening takes a lock on the
file, so a second process gets a LatticeError with
code === -16 (LATTICE_ERROR_DATABASE_LOCKED) rather than quietly corrupting your
data. A read-only handle shares the lock with other readers, but is still refused
while a writer holds the database, because what it would read is a stale file that
a checkpoint may be rewriting underneath it.
Pass lock: false only on filesystems where locking does not work. It does not
make concurrent access safe; it removes the thing that was going to tell you it
was not.
Methods
await db.open()- Open the database connectionawait db.close()- Close the database connectiondb.serialize()- Return the whole database as bytesawait db.read(fn)- Execute a read-only transactionawait db.write(fn)- Execute a read-write transactionawait db.query(cypher, params?)- Execute a Cypher queryawait db.getNodesByLabel(label)- IDs of every node with a labelawait db.vectorSearch(vector, options?)- k-NN vector searchawait db.ftsSearch(label, property, query, options?)- Full-text search of one declared indexawait db.ftsSearchFuzzy(label, property, query, options?)- Fuzzy full-text searchawait db.createNodeFtsIndex(label, property)/dropNodeFtsIndex(...)/hasNodeFtsIndex(...)- Manage node full-text indexesawait db.createEdgeFtsIndex(edgeType, property)/dropEdgeFtsIndex(...)/hasEdgeFtsIndex(...)- Manage relationship full-text indexes. A node index and a relationship index cannot share a name and property: the second is rejected as already existingawait db.createNodePropertyIndex(label, property)/dropNodePropertyIndex(...)- Manage explicit node property indexes, used for equality lookups and by Cypher for ranges, prefixes and sortingawait db.createEdgePropertyIndex(edgeType, property)/dropEdgePropertyIndex(...)- Manage explicit edge property indexesawait db.readStream(stream, options?)- Read durable stream records by cursor. WithtimeoutMs > 0the wait runs on a worker thread, so the event loop keeps running and a commit from the same process can end the waitawait db.getStreamOffset(stream, consumer)- Read a committed consumer offsetawait db.changes(options?)- Read the built-in graph changefeed. Rejects withLatticeChangesTrimmedErrorwhen retention has dropped the events afterafterSequence, and with code -14 (Unsupported) when the policy is'off'await db.changefeedStatus()- The changefeed's policy and range:{ retention, oldestSequence, lastSequence },oldestSequencenull when it holds no eventsawait db.trimChangefeed()- Apply the retention policy in full now; resolves to the number of events droppedawait db.cacheClear()- Clear the query cacheawait db.cacheStats()- Get cache hit/miss statisticsdb.isOpen()- Check if database is opendb.getPath()- Get database file path
deserialize(bytes, options?, copy = true), exported from the package root, opens a
database from bytes produced by serialize().
Node IDs, edge IDs and stream sequence numbers are bigint.
Transaction
Read Operations
await txn.getNode(nodeId)- Get a node by ID, returnsNodeornullawait txn.nodeExists(nodeId)- Check if a node existsawait txn.getProperty(nodeId, key)- Get a property value, ornullwhen the node does not have itawait txn.getVector(nodeId, key = "embedding")- The node's vector as aFloat32Array, ornullif it has none. Rejects with code -4 (NotFound) if the node does not exist. Under the cosine metric, the default, a committed vector reads back normalised to unit length ([3, 4]reads back as[0.6, 0.8]); an uncommitted one reads back as written.await txn.getOutgoingEdges(nodeId)- Get outgoing edges from a nodeawait txn.getIncomingEdges(nodeId)- Get incoming edges to a nodeawait txn.getOutgoingEdgesByType(nodeId, edgeType, ...)/getIncomingEdgesByType(...)- Edges of one typeawait txn.getNodesByLabel(label)- IDs of every node with a label, as this transaction sees themawait txn.query(cypher, params?)- Run Cypher inside this transactionawait txn.vectorSearch(...)/ftsSearch(...)/ftsSearchFuzzy(...)- Searches inside this transactionawait txn.findNodesByLabelProperty(label, property, value, limit?)- Indexed node equality lookupawait txn.findEdgesByTypeProperty(edgeType, property, value, limit?)- Indexed edge equality lookuptxn.isReadOnly()/txn.isActive()- Transaction state
Write Operations
await txn.createNode({ labels, properties })- Create a node. A property whose value isnull(orundefined) is left out, as in Cypher'sCREATE, and the returnedNodeleaves it out tooawait txn.deleteNode(nodeId, { detach })- Delete a node. A node that still has relationships is refused with code -17 (HasRelationships) and nothing is deleted, as Cypher'sDELETEdoes;{ detach: true }deletes its relationships with it, asDETACH DELETEdoes. Deleting a node that does not exist does nothing.await txn.setProperty(nodeId, key, value)- Set a property. Anullvalue removes the property, as Cypher'sSET n.x = nulldoes; removing one the node does not have does nothing. Before 0.18.1 it stored thenullawait txn.setVector(nodeId, key, vector)- Set the node's vector embedding. A node has one vector, which Cypher reads asn.embedding;keymust be"embedding"(or""), and any other key is rejected with an unsupported-operation error. A vector with NaN or infinite components is rejected as an invalid argument. On a database created without a vector index,setVector,batchInsertVectors(before it creates any node) andvectorSearchreject with aLatticeErrorwhose code isUnsupportedand whose message says so; before 0.18.1 the code wasIoError. Opening the database withenableVectors: trueandvectorDimensionsadds an index.await txn.removeVector(nodeId, key = "embedding")- Remove the node's vector. The node keeps its labels and properties, and vector search no longer returns it. Removing a vector the node does not have does nothing. Rejects with code -4 (NotFound) if the node does not exist.await txn.batchInsertVectors(label, vectors)- Insert vector-bearing nodes in one callawait txn.batchInsert(label, vectors)- Deprecated compatibility alias forbatchInsertVectorsawait txn.createEdge(sourceId, targetId, edgeType, options?)- Create an edge and return its stable edge ID on theEdge. Throws with code -4 (NotFound) unless both ends exist as the transaction sees them. A property whose value isnullis left outawait txn.deleteEdge(sourceId, targetId, edgeType)- Delete an edgeawait txn.setEdgeProperty(edgeId, key, value)- Set an edge property by stable edge ID. Anullvalue removes it, assetPropertydoesawait txn.getEdgeProperty(edgeId, key)- Get an edge property by stable edge IDawait txn.removeEdgeProperty(edgeId, key)- Remove an edge property by stable edge IDtxn.publishStream(stream, payload, kind?)- Publish a durable stream recordtxn.publishStreamGetSequence(stream, payload, kind?)- Publish and return the record's sequence (bigint)txn.setStreamOffset(stream, consumer, sequence)- Commit a durable consumer offsettxn.trimStream(stream, throughSequence)- Delete stream records through a sequencetxn.commit()/txn.rollback()- Commit or rollback. A commit that throws with code -7 (TxnAborted) has ended the transaction, androllback()afterwards does nothing; after any other failure it stays open forrollback()
Batch Insert
Insert many nodes with vectors in a single efficient call:
import { Database } from "@hajewski/latticedb";
const db = new Database("vectors.db", {
create: true,
enableVectors: true,
vectorDimensions: 128,
});
await db.open();
await db.write(async (txn) => {
const vectors = Array.from({ length: 1000 }, () =>
Float32Array.from({ length: 128 }, () => Math.random())
);
const nodeIds = await txn.batchInsertVectors("Document", vectors);
console.log(`Created ${nodeIds.length} nodes`);
});
await db.close();
Full-Text Search
Exact Search
const results = await db.ftsSearch("Person", "bio", "machine learning", { limit: 10 });
for (const r of results) {
console.log(`Node ${r.nodeId}: score=${r.score.toFixed(4)}`);
}
Fuzzy Search (Typo-Tolerant)
// Finds "machine learning" even with typos
const results = await db.ftsSearchFuzzy("Person", "bio", "machne lerning", { limit: 10 });
// Control fuzzy matching sensitivity
const precise = await db.ftsSearchFuzzy("Person", "bio", "machne", {
limit: 10,
maxDistance: 2, // Max edit distance (default: 0 = auto)
minTermLength: 4, // Min term length for fuzzy matching (default: 0 = auto)
});
Embeddings
LatticeDB includes a built-in hash embedding function and an HTTP client for external embedding services.
Hash Embeddings (Built-in)
Deterministic, no external service needed. Useful for testing or simple keyword-based similarity:
import { hashEmbed } from "@hajewski/latticedb/embedding";
const vec = hashEmbed("hello world", 128);
console.log(vec.length); // 128
HTTP Embedding Client
Connect to Ollama, OpenAI, or compatible APIs:
import { EmbeddingClient, EmbeddingApiFormat } from "@hajewski/latticedb/embedding";
// Ollama (default). The endpoint is the full URL the client POSTs to.
const client = new EmbeddingClient({
endpoint: "http://localhost:11434/api/embeddings",
});
const vec = client.embed("hello world");
client.close();
// OpenAI-compatible API
const openaiClient = new EmbeddingClient({
endpoint: "https://api.openai.com/v1/embeddings",
model: "text-embedding-3-small",
apiFormat: EmbeddingApiFormat.OpenAI,
apiKey: "sk-...",
});
const embedding = openaiClient.embed("hello world");
openaiClient.close();
The endpoint is POSTed to exactly as given; nothing is appended, so a server's
base URL alone (http://localhost:11434) sends the request to / and embed
throws. timeoutMs is how long embed waits for the response to begin: it bounds the wait for the server to start answering (default 30 seconds), not connecting or reading the rest of a response that has begun, and is not applied on Windows.
Edge Traversal
await db.read(async (txn) => {
const outgoing = await txn.getOutgoingEdges(nodeId);
for (const edge of outgoing) {
console.log(`${edge.id}: ${edge.sourceId} --[${edge.type}]--> ${edge.targetId}`);
}
const incoming = await txn.getIncomingEdges(nodeId);
for (const edge of incoming) {
console.log(`${edge.id}: ${edge.sourceId} --[${edge.type}]--> ${edge.targetId}`);
}
});
Edge Properties
Edge properties are addressed by stable edge ID. createEdge returns an Edge
object containing that ID, and traversal results include it.
await db.write(async (txn) => {
const edge = await txn.createEdge(alice.id, bob.id, "KNOWS");
await txn.setEdgeProperty(edge.id, "since", 2020);
const since = await txn.getEdgeProperty(edge.id, "since");
});
Durable Streams
await db.write(async (txn) => {
await txn.publishStream("jobs", { id: 1, status: "queued" }, "job.queued");
await txn.setStreamOffset("jobs", "worker-a", 1n);
});
const records = await db.readStream("jobs", { afterSequence: 0n, limit: 100 });
const changes = await db.changes({ afterSequence: 0n, limit: 100 });
changes() returns each transaction's net effect (a property set several times
is one event, from its value before the transaction to its last value), in an
order that can be replayed as it comes: the edges it deletes, the nodes that
exist after it, the edges that exist after it, then the nodes it deletes, each
group by ascending id. How much of the changefeed to keep is set with
changefeedRetention; see The Changefeed.
Cypher Queries
// Pattern matching
const result = await db.query("MATCH (n:Person) RETURN n.name");
// With parameters
const result = await db.query(
"MATCH (n:Person) WHERE n.name = $name RETURN n",
{ name: "Alice" }
);
// Vector similarity in Cypher
const result = await db.query(
"MATCH (n:Document) WHERE n.embedding <=> $vec < 0.5 RETURN n.title",
{ vec: new Float32Array([0.1, 0.2, 0.3, 0.4]) }
);
// Full-text search in Cypher
const result = await db.query(
'MATCH (n:Document) WHERE n.content @@ "machine learning" RETURN n.title'
);
// Data mutation
await db.query('CREATE (n:Person {name: "Charlie", age: 35})');
await db.query('MATCH (n:Person {name: "Charlie"}) SET n.age = 36');
await db.query('MATCH (n:Person {name: "Charlie"}) DETACH DELETE n');
Query Cache
// Get cache statistics
const stats = await db.cacheStats();
console.log(
`Entries: ${stats.entries}, Hits: ${stats.hits}, Misses: ${stats.misses}`
);
// Clear the cache
await db.cacheClear();
Checkpoints
const stats = await db.checkpoint('truncate'); // fold the log into the file now
await db.setAutoCheckpoint(10000); // commits checkpoint every 10,000 frames
await db.setAutoCheckpoint(0); // commits never checkpoint
'passive' gives way if a changed page is in use, 'full' writes every changed
page and leaves the log, and 'truncate' (the default) also empties it. The
stats report pagesFlushed, durationNs, checkpointLsn and walTruncated;
setAutoCheckpoint takes an optional minimum gap in milliseconds as its second
argument. Without the setter, the LATTICE_AUTO_CHECKPOINT_FRAMES environment variable sets the frame count; without either, the log may grow to an eighth of the page cache, from 1000 frames up to 64 MB (3,200 frames with the default 100 MB cache). A larger interval means fewer pauses, each longer, a longer -wal file and more to replay after a crash; see Checkpointing.
Supported Property Types
null- Null value. A property is never stored asnull: writing one removes the property (seesetProperty).nullinside an array or an object is keptboolean- Booleannumber- Integer or floatbigint- 64-bit integerstring- UTF-8 stringUint8Array- Binary dataFloat32Array- Vector- Arrays of any of these values, nested to any depth
- Plain objects with string keys, whose values are any of these, nested to any depth
Error Handling
import { Database, isLibraryAvailable } from "@hajewski/latticedb";
// Check if native library is available
if (!isLibraryAvailable()) {
console.error("LatticeDB native library not found");
process.exit(1);
}
try {
const db = new Database("test.db", { create: true });
await db.open();
// ...
await db.close();
} catch (error) {
console.error("Database error:", error);
}
Errors from the native library are LatticeErrors whose code is the C error
code; LatticeErrorCode, exported from the package root, names them (with
QueryErrorStage for LatticeQueryError.stage; both are plain enums, readable
at run time). For example -4 (NotFound, also from createEdge when either end
does not exist), -10 (Full, a read-only open whose crash recovery needs more
memory than it may use), -11 (VersionMismatch, a file in a format only a newer
release can read), -16 (DatabaseLocked, another process holds the file), -17
(HasRelationships, a plain deleteNode of a node with relationships), -18
(ConfigMismatch, open options that contradict the file's vector index) and -19
(NeedsNewerWriter, a writable open of a file only a newer release may write).
A failed Cypher query throws a LatticeQueryError, a LatticeError that also
says where the query failed:
import { Database, LatticeQueryError } from "@hajewski/latticedb";
try {
await db.query("RETURN 1/0");
} catch (e) {
if (e instanceof LatticeQueryError) {
console.log(e.message); // Division by zero: 1 / 0
console.log(e.code); // -1
console.log(e.stage); // 4 (execution)
console.log(e.diagnosticCode); // DivisionByZero
console.log(e.location); // { line: 1, column: 9, length: 1 }
}
}
stage is a QueryErrorStage: 1 parse, 2 semantic, 3 plan, 4 execution. Parse
and semantic failures (an unknown function is unknown_function) have code -6
and execution failures -1, except that a DELETE of a node with relationships
has code -17 and a relationship created to a node deleted earlier in the query
-4, as deleteNode and createEdge report them. A storage failure under a query
has code -2 and an allocation failure -13. location is undefined when the
failure does not belong to one place in the query.
Building from Source
Requires Node.js 18+ and the LatticeDB native library.
# From the latticedb root directory
zig build shared
# Build the TypeScript bindings
cd bindings/typescript
npm install
npm run build
# Run tests
npm test
Requirements
- Node.js 18+
- The native LatticeDB library (
liblattice.dylib/liblattice.so/lattice.dll), bundled in the published package