DreamDB

React example

A grid of images and their labels, read straight from object storage. No API route, no server component, no state library.

The hook

Open the Space once, then resolve the image track into URLs and the label track into a lookup:

jsx
import { useEffect, useState } from "react";
import { Space, ZERO_SPATIAL_KEY, bytesToBase32 } from "@dreamlake/dreamdb";

export function useGallery(refUri) {
  const [items, setItems] = useState([]);

  useEffect(() => {
    let cancelled = false;

    (async () => {
      const space = await Space.fromUri(refUri, null);
      const imageTrack = space.trackFor("image");

      const [entries, labels] = await Promise.all([
        space.resolveObjectIndex(imageTrack),
        space.readScalarColumn(space.trackFor("label"), null),
      ]);

      const next = entries.map(([anchor, , size, hash]) => ({
        anchor: String(anchor),
        size,
        label: labels.get(String(anchor)),
        url: [
          space.connectorBase,
          imageTrack.timeline,
          imageTrack.modality,
          ZERO_SPATIAL_KEY,
          bytesToBase32(hash),
        ].join("/"),
      }));

      if (!cancelled) setItems(next);
    })();

    return () => { cancelled = true; };
  }, [refUri]);

  return items;
}

The cancelled flag matters in StrictMode, where the effect runs twice in development.

The component

jsx
import { useGallery } from "./useGallery";

export function Gallery({ refUri }) {
  const items = useGallery(refUri);

  if (!items.length) return <p>Loading…</p>;

  return (
    <ul className="grid">
      {items.map((item) => (
        <li key={item.anchor}>
          <img src={item.url} alt={item.label ?? ""} loading="lazy" />
          <figcaption>{item.label}</figcaption>
        </li>
      ))}
    </ul>
  );
}
jsx
<Gallery refUri="https://bucket.example/refs/gallery" />

Every URL is content-addressed and immutable, so loading="lazy" plus the browser cache is the whole image-loading strategy. Nothing needs revoking, because nothing allocated a Blob.

Adding search

useGallery returns every record. To narrow it, run the search through SQL (npm 0.5.6+), not queryVector: in 0.5.8 queryVector returns anchors as plain numbers that can lose nanosecond precision, so they cannot be matched against item.anchor. SQL _anchor cells are exact bigints:

jsx
import { SqlSession } from "@dreamlake/dreamdb";

const session = await SqlSession.open("gallery", "https://bucket.example");
const result = await session.query(
  "SELECT _anchor FROM vector_search('embedding', $1, 24, 8)",
  [queryVector],
);

// Best first; String() of a bigint is exact, like item.anchor.
const ranked = result.batches.flatMap((batch) => batch.columns[0].map(String));
const byAnchor = new Map(items.map((item) => [item.anchor, item]));
const shown = ranked.map((anchor) => byAnchor.get(anchor)).filter(Boolean);

shown is in hit order. A hit with no image entry has no item and is skipped. Semantic search shows how to produce queryVector.

Bundler setup

Vite needs vite-plugin-wasm for the default import. Next.js needs experiments.asyncWebAssembly, or keep DreamDB on the server. Details in Install.