Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

TypeScript client

mushroomdb-client is a typed wrapper over the HTTP API and its WebSocket stream. It talks to a running mushroomdb serve; it does not embed the engine.

npm install mushroomdb-client

It needs Node 18 or later, and runs in a browser. In a Node without a global WebSocket, install ws as well if you use subscriptions.

Quick start

import { MushroomClient } from 'mushroomdb-client';

const client = new MushroomClient('http://127.0.0.1:8080', { token: 'your-token' });

// A read.
const result = await client.query(
  'MATCH (p:Person) RETURN p.id AS id, p.name AS name LIMIT 10',
);
console.log(result.columns); // ['id', 'name']
console.log(result.rows);    // [['person-01', 'Alice'], ...]

// A read with a parameter.
const one = await client.query(
  'MATCH (p:Person {id: $pid}) RETURN p.name AS name',
  { params: { pid: 'person-01' } },
);

// Why are these two linked?
const why = await client.explain('person-01', 'proj-01');

The token is sent as an Authorization: Bearer header, and on the WebSocket URL as ?token= for subscriptions. Leave the second argument out for a server started without one.

Methods

Each method calls one route. The route’s page has the request and the response in full.

CallWhat it doesRoute
client.query(Run a Cypher read or writePOST /query
client.ingest(Insert many nodes of one labelPOST /ingest
client.stats(Node, edge and rule countsGET /stats
client.suggest(Rules the store proposes from its own valuesGET /suggest
client.explain(Why two nodes are linkedGET /explain
client.createRule(Declare a rulePOST /rules
client.node(One node’s label and properties; null when it does not existGET /node/{key}
client.neighborhood(What is within a few hops of a nodeGET /node/{key}/neighborhood
client.algo(PageRank, connected components or degree, by namePOST /algo/pagerank, /wcc, /degree
client.renameNode(Change a node’s keyPOST /nodes/{key}/rename
client.upsertEdge(Create an edge, and its endpoints if they are missingPOST /edges/upsert
client.subscribe(Stream rule and write eventsGET /subscribe
client.health(Server liveness and node/edge counts (when authenticated)GET /health
client.metrics(Detailed runtime metrics including slow-query logGET /metrics
client.findSimilar(Approximate nearest-neighbour vector searchPOST /find_similar
client.createNode(Insert a node by label and keyPOST /nodes
client.deleteNode(Remove a node; throws when it does not existDELETE /node/{key}
client.nodeEdges(All edges attached to a nodeGET /node/{key}/edges
client.nodeHistory(Per-commit change log for a nodeGET /node/{key}/history
client.edgeHistory(Edge presence log between two nodesGET /history/edge
client.wasLinked(Whether two nodes were linked at a given commitGET /history/was_linked
client.createEdge(Insert an explicit (non-derived) edgePOST /edges
client.deleteEdge(Remove an explicit edgeDELETE /edges/{etype}/{src}/{dst}
client.setNodeProp(Set or overwrite one property on a nodePUT /node/{key}/prop/{field}
client.removeNodeProp(Delete one property from a nodeDELETE /node/{key}/prop/{field}
client.backup(Copy the store to a directory and verify itPOST /backup
client.indexes(Every index, as SHOW INDEXES lists itGET /schema/indexes
client.createIndex(Declare a range index on a label’s propertyPOST /schema/indexes
client.dropIndex(Drop an index by nameDELETE /schema/indexes/{name}
client.constraints(Every unique constraint, as SHOW CONSTRAINTS lists itGET /schema/constraints
client.createConstraint(Declare a unique constraintPOST /schema/constraints
client.dropConstraint(Withdraw a unique constraint by nameDELETE /schema/constraints/{name}
client.beginTransaction(Open a transaction and return its handlePOST /tx/begin
client.transaction(Run a callback in a transaction; commit when it resolves, roll back when it throwsPOST /tx/begin, POST /tx/{tx_id}/commit, POST /tx/{tx_id}/rollback
client.watch(Stream the raw mutation log over WebSocketGET /watch

Algorithm configs

client.algo returns the report for the selected algorithm. Configs are optional; client.algo('wcc') and client.algo('wcc', undefined) use the server defaults. All three configs support edge_type, budget_ms, weight_prop and min_weight. PageRank uses edge weights; components and degree use the weight fields only to filter edges, and degree still counts edges.

The compiler checks a config variable’s declared keys. A PageRankConfig variable cannot be passed to wcc or degree, and a DegreeConfig cannot be passed to wcc, even if its current value sets only shared fields. Declare a narrower shared type when the same config is intended for several algorithms:

const shared: { edge_type?: string | null; budget_ms?: number } = {
  edge_type: 'KNOWS', budget_ms: 1000,
};
await client.algo('wcc', shared);
await client.algo('degree', shared);

This is a compile-time check; any, casts and annotations that erase extra keys can bypass it. The server separately refuses unknown fields and requires an object body.

Transactions

import { TransactionConflictError } from 'mushroomdb-client';

// The callback form commits when the callback resolves and rolls back,
// rethrowing, when it throws.
const seenCount = await client.transaction(async (tx) => {
  await tx.execute('CREATE (:Person {id: $id})', { id: 'p1' });
  const seen = await tx.execute('MATCH (p:Person {id: $id}) RETURN p.id AS id', { id: 'p1' });
  return seen.rows.length; // sees its own write
});

// The explicit form.
const tx = await client.beginTransaction();
try {
  await tx.execute('CREATE (:Person {id: $id})', { id: 'p2' });
  const commitNumber = await tx.commit();
} catch (err) {
  if (err instanceof TransactionConflictError) {
    // a node it read or wrote changed after begin: retry the whole unit of work
  }
  await tx.rollback();
}

tx.execute(cypher, params?) returns the same { columns, rows } as client.query. tx.commit() resolves to the commit number. A conflict at commit throws TransactionConflictError, which a retry of the whole transaction can fix; a commit a unique constraint refuses throws ConstraintViolationError (the server’s 409 with code: "constraint", also thrown by an auto-commit write such as client.query), which a retry cannot fix, and the transaction is over; a request that raced another on the same transaction (the server answers 409 with code: "busy") throws a plain MushroomError with code === "busy" and leaves the handle open, so it can be retried; a transaction the server no longer knows (expired, finished, or lost in a restart) throws TransactionExpiredError. All three extend MushroomError. The client does not retry. If a rollback fails after the callback threw, the callback’s error is still the one rethrown, with the rollback failure on its rollbackError property. After any other failed commit the client asks the server (GET /tx/{tx_id}) whether it still holds the transaction: the handle stays usable only if it does (a commit that reached the engine always ends it). If commit() fails at the network level, or the server is still running the commit, its outcome is unknown and the handle refuses further use; a failed rollback() can be retried.

What it does not wrap

All HTTP routes and WebSocket endpoints have a method on the client.

Creating a node, setting a property and deleting can also be done through client.query, with a Cypher CREATE, SET or DELETE.

Query options

client.query(cypher, opts?) accepts three optional fields beyond params:

OptionTypeWhat it does
as_ofnumberTime-travel: run the query on the graph as it was at this WAL commit index (0 to commit_seq − 1, read-only). A commit index from a nodeHistory/edgeHistory entry is the usual source.
maskstring[]Node-key allow-list: the query sees only nodes whose key is in this set. Makes the query read-only.
namespacestringRestrict results to this namespace, intersecting with mask and any role. Makes the query read-only.

Dates, durations and binary

Five value types travel as one-key JSON objects, and a CellValue can be any of them:

TypeFormExample
Date{ $date }{ $date: '2024-01-15' }
Local date-time{ $ldt }{ $ldt: '2024-01-15T12:34:56.789012' }
Date-time{ $dt }{ $dt: '2024-01-15T12:34:56.789012+05:30' }
Duration{ $dur }{ $dur: 'P1Y2M3DT4H5M6.789S' }
Binary{ $binary }{ $binary: '3q2+7w==' } (base64, at most 1 MiB)

The text is ISO 8601, and a date-time keeps the UTC offset it was written with (within ±18:00). asDate, asLocalDateTime, asDateTime, asDuration and asBinary narrow a cell to its text, or return undefined for any other cell; binaryBytes decodes a binary cell to a Uint8Array.

import { asDate } from 'mushroomdb-client';

const { rows } = await client.query('MATCH (e:Event) RETURN e.on AS on');
const day = asDate(rows[0][0]); // '2024-01-15', or undefined if the cell is not a date

These types are not yet storable: a write that carries one is refused, naming the type, until the format that holds them lands.

Errors

A response that is not a success throws MushroomError. Its detail, which is also its message, is what the server said went wrong. Empty or plain-text error bodies (e.g. 404 with no JSON, or 413) surface as MushroomError("HTTP <status>") rather than a raw parse error.

import { MushroomError } from 'mushroomdb-client';

try {
  await client.query('BAD CYPHER');
} catch (err) {
  if (err instanceof MushroomError) {
    console.error('Query failed:', err.detail);
  }
}

Subscriptions

const handle = await client.subscribe(
  { rules: ['skill_fit'], writes: true },
  (ev) => {
    switch (ev.type) {
      case 'edge_fired':
        console.log(`${ev.rule}: ${ev.src_key} -> ${ev.dst_key} (commit ${ev.commit_seq})`);
        break;
      case 'lagged':
        console.warn(`missed ${ev.missed} events; re-read what you depend on`);
        break;
    }
  },
);

// Later:
await handle.close();
  • A subscription sees only writes made through the server it is connected to.
  • It does not reconnect. When the connection drops, no more events arrive; subscribe again.
  • A lagged event means the server’s queue for this subscriber overflowed and some events were not sent. The stream goes on.
  • In a Node without a global WebSocket, pass the ws constructor as wsConstructor in the first argument.

Every option of every method is in the package’s own README.