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

Backend TypeScript client (@hakodb/backend)

hakobackend-ts is the TypeScript client for hakobackend, paired with the HTTP + WebSocket wire. It is not the native FFI client; that is @hakodb/client.

npm i @hakodb/backend
import { hakobackend, collection, doc, getDocs } from "@hakodb/backend";

const snap = await getDocs(collection(hakobackend as any, "users"));

Set VITE_BACKEND_URL (Vite) or NEXT_PUBLIC_BACKEND_URL (Next.js).

Legacy mapping

LegacyThis client
RethinkClientHakoBackendClient (same methods)
socket.io transportnative WebSocket /ws (auto-reconnect + resubscribe)
setDoc(..., {merge:true}) via PUT ?merge=merge:true → PATCH, else PUT
Errors as plain text{error, code?} JSON parsed into messages
getSystemInfo() → {status, type}mapped from {status, db}
aggregatessame calls; count reads count_count key
Firebase token in setIdentityany bearer token (local JWT, etc.)
—+ register/login/logout/me (local BFF)
—dispose() to release the socket

id is required on batch/transaction ops (400 otherwise). Unknown batch type values are rejected — use set/add/update/delete/get.

Consumer migration notes (0.1.x → 0.2.x/0.3.x)

These wire breaks fail at runtime. Document data is any, so typecheck will not catch them.

  • Timestamps are strings now. <=0.1.x hydrated ISO strings into {toDate, toMillis, toISOString} objects; >=0.2.x passes ISO strings through. Guard reads: typeof v === 'string' ? new Date(v) : v.toDate(). For writes use Timestamp.fromDate(d) (serializes to ISO) or serverTimestamp() for a server stamp.
  • Merge is a method, not a flag. merge:true → PATCH, else PUT; updateDoc is always PATCH. A 0.1.x client has no ?merge= flag server-side — merge writes risk full-replace. Upgrade the client together with the code that writes.
  • Realtime is native WS. socket.io → WebSocket /ws ({type:'subscribe',key,collection,options,group,db,token}); the client resubscribes automatically.
  • Errors are JSON. Plain-text bodies → {error, code?}; the client throws Error with the server message. Match on text, not status assumptions.
  • Sentinels key __type__ (double underscore): FieldValue.* shapes resolve gateway-side per HTTP_CONTRACT §4. Single-underscore payloads store as plain data with no error.

Support matrix

LibBackend features covered
0.1.xcore CRUD/queries/batch/tx/aggregates, WS realtime, local auth
0.2.x+ FieldValue/Timestamp sentinels, ?db=, managed files, POST /api/indexes, pass-through hydration
0.3.x+ token bridge, watch engine, React entry (@hakodb/backend/react), archive (relocateDocs/load/unload/unloaded, getField), vector (vectorIndexCreate/vectorSearch KNN), doc onSnapshot overload

Timestamps stay ISO strings client-side (parseable); the old object-wrapper hydrator is gone — see hydrate.test.ts for the regression contract.

React

The framework-free core stays dependency-free; React is a peer dependency.

import { HakoBackendClient } from "@hakodb/backend";
import { HakoProvider, useCollection, useDoc } from "@hakodb/backend/react";
import { bindTokenSource } from "@hakodb/backend";

const client = new HakoBackendClient();

// Any auth system plugs in via a token source (Firebase, local, OIDC —
// the lib never imports an auth SDK):
const unbind = bindTokenSource(client, {
  getToken: () => auth.currentUser?.getIdToken() ?? Promise.resolve(null),
  subscribe: (cb) => auth.onAuthStateChanged(() => cb()),
});

function App() {
  return (
    <HakoProvider client={client}>
      <Posts />
    </HakoProvider>
  );
}

function Posts() {
  const { data, loading, error } = useCollection(client.collection("posts"));
  // ...
}

useCollection(query | null) and useDoc(ref | null) return { data, loading, error, snapshot }. Concurrent identical subscriptions share one bootstrap fetch.