Manta Developers
GitHub
Menu

TodoMVC

The classic TodoMVC app, with its todos in a Manta datastore and its counts derived by rules.

TodoMVC is the same small todo app implemented in every framework. The Manta version follows the TodoMVC app spec and uses the Manta SDK for everything around it:

  • the UI is built from the Manta design system in @manta-eu/react
  • todos are stored in the Manta knowledge graph and read and written through LDKit, with no hand-written SPARQL
  • Datalog rules in the datastore keep the counts up to date
  • a Manta chat beside the list can manage the todos for you

The code is at github.com/manta-eu/manta-todomvc, under the MIT license. It uses only the published @manta-eu/* packages.

Try it live at todomvc.hiremanta.com. The site has no sign-in, and every visitor shares one list.

Run it

You need Node 24 or newer, pnpm, and Manta access: a datastore that holds only the demo’s data, and a custom app with Machine access and SPARQL writes limited to that datastore. The quick start says how to get them. Limiting the app to that datastore also lets it get the visitor tokens the chat needs.

git clone https://github.com/manta-eu/manta-todomvc.git
cd manta-todomvc
pnpm install
cp .env.example .env.local

Put the custom app’s client ID and secret, and the datastore id, in .env.local. Push the vocabulary and the shapes, ask us to push the rules, then start the dev server on http://localhost:3010:

pnpm dev

The repository’s readme has each command and a table of the errors you can see on the way.

A tour of the code

The demo takes the same steps as the quick start, with three differences:

  • It has no sign-in. The dev server signs writes in with the custom app’s machine token, and everything else with a read-only visitor token.
  • It writes through LDKit lenses in the browser, not through SPARQL updates in its own server.
  • It writes its LDKit schemas by hand instead of generating them with manta-codegen.

Describe the data

manta/ontology.ttl declares todomvc:Todo, with a title, a completed flag, an order and a creation time, and todomvc:TodoCounts for the counts. manta/shapes.ttl holds their SHACL shapes. Both are pushed with manta ontology push and manta shapes push, the same commands as in the quick start.

The LDKit schemas are written by hand in src/todos/todo-schema.ts, not generated. A unit test keeps them in step with the vocabulary and the shapes.

export const TodoSchema = {
  "@type": todomvc.Todo,
  title: todomvc.title,
  completed: { "@id": todomvc.completed, "@type": xsd.boolean },
  order: { "@id": todomvc.order, "@type": xsd.integer },
  created: { "@id": todomvc.created, "@type": xsd.dateTime },
} as const;

Derive the counts with rules

The footer’s “items left”, “Clear completed” and the toggle-all box do not count the list in the browser. They read one node, todomvc:counts, that the rules in manta/rules.dlog derive:

todomvc:activeCount[todomvc:counts, ?count] :-
    AGGREGATE(
        todomvc:Todo[?todo],
        todomvc:completed[?todo, false]
        BIND COUNT(?todo) AS ?count
    ) .

The datastore updates the counts in the same transaction as each write, so a read right after a write sees the new numbers. A custom app cannot push rules yet: an operator pushes them for you. See Rules.

Read the todos

src/todos/graph-todo-store.ts reads with two lenses on the SPARQL connection that EmbeddedChatProviders sets up:

const readLens = createLens(TodoSchema, readOptions);
const countsLens = createLens(TodoCountsSchema, readOptions);

const todos = await readLens.find();
const counts = await countsLens.findByIri(todomvc.counts);

readOptions comes from useSparqlOptions, in src/graph-todo-providers.tsx.

Write the todos

The same file builds each write with lens.insert and lens.update, and sends it to the datastore’s /sparql-update endpoint. Everything one action changes, such as a toggle all or a clear completed, goes out as one request. A delete binds each todo’s IRI in the query, because the endpoint refuses LDKit’s catch-all delete. New todos get IRIs from the SDK’s mintInstanceIri, and the app never parses them.

Let the chat change the todos

The embedded chat beside the list can add, complete, rename and remove todos. It does not write SPARQL. The page lends it app tools, and each one calls the same action as the UI, so the agent’s changes take the same write path, show in the list at once, and update the counts through the rules. src/chat/todo-tools.ts holds seven tools. This is one of them:

setTodosCompleted: {
  description: "Mark the todos with these ids as completed, or as not completed.",
  parameters: {
    type: "object",
    properties: { ids: idsParameter, completed: { type: "boolean" } },
    required: ["ids", "completed"],
  },
  execute: (input) => {
    const completed = booleanIn(input, "completed");
    const todos = findTodos(currentTodos(), stringsIn(input, "ids"));
    const changing = todos.filter((todo) => todo.completed !== completed);
    for (const todo of changing) actions.toggle(todo.id);
    return { changed: changing.length, completed };
  },
},

Each tool checks its input, because the browser does not check it against parameters. An unknown id makes the tool throw, and the agent gets the error and can call listTodos for the ids. The tool sets a state instead of flipping it, so a second call changes nothing more. The list and the chat share one useTodos state, in src/todo-page.tsx.

The chat opens a WebSocket under /agents. The dev server and the Worker proxy it, and sign its one-time ticket request in with a visitor token from customAppUserAccessToken({ anonymousId }), so the page still holds no real token. Don’t use the machine token here: it acts for no person, so Manta opens no chat for it and answers the ticket request with 403.

Test it without Manta

The end-to-end tests send the app’s /sparql and /sparql-update requests to an in-process Oxigraph store, so the real LDKit queries run without a Manta stack. Oxigraph cannot run Datalog, so after each write a SPARQL update stands in for the rules. See e2e/mock-manta.ts.