Rune DB and Vect Event
Run a server-side Rune DB with a Rune-Web frontend, embed providers in existing apps, and commit documents and durable events together.
Declare reusable providers
A rune-db datasource stores documents by collection and key. A vect-event datasource shares its store and transaction boundary. Native storage uses redb with MessagePack; browser-local storage uses IndexedDB. This feature requires a compiler built from current source.
#!RUNE
# Import providers into an app without importing another @App.
@DataSource/main
type = rune-db
database = workshop
native_path = .vectrune/workshop.rdb
@Datasource/activity
type = vect-event
store = main
Run a server-side database with Rune-Web
Recommended: run one server on port 3000. @App type = rune-db owns the native file and @Frontend type = rune-web serves the page. The browser datasource uses mode = remote and endpoint = /, so database requests use the page’s current origin. Both localhost and 127.0.0.1 work, including a --port override, without CORS configuration. Data remains on the server, rather than in IndexedDB.
Save a note, add a view, reload, then load it. Another browser and the CLI read the same server record. Save intentionally replaces this demo note, resets its views to zero and appends note.saved in one atomic batch.
Read new events advances the tab’s cursor through pages of up to 20 events. Replay events starts at zero. The server retains the events; the tab cursor resets on reload. The interface shows event cards, with the raw response under Inspect the response data.
#!RUNE
# One process, one port: the UI and database protocol share the same origin.
import "ui.rune"
@App
name = Shared Rune DB notes
type = rune-db
source = main
host = 127.0.0.1
port = 3000
# The native host owns this file.
@DataSource/main
type = rune-db
native_path = .vectrune/notes.rdb
# Browser requests go back to this server, never to IndexedDB.
@DataSource/server
type = rune-db
mode = remote
endpoint = /
@DataSource/activity
type = vect-event
store = server
vectrune examples/data/storage/fullstack/single-server.rune
# Open http://127.0.0.1:3000 (or http://localhost:3000), then save a note.
vectrune db get notes hello --endpoint http://127.0.0.1:3000
vectrune events tail notes --endpoint http://127.0.0.1:3000Two listeners from one command
Use --secondary-app to launch independent HTTP apps in the same Vectrune process. Each file keeps its own @App, datasource settings, routes and port. The primary frontend listens on 3000 and the secondary database on 7333. Multiple positional Rune files still merge documents; they do not start separate apps.
Repeat --secondary-app for additional apps. --port and --host override only the primary. Ctrl-C shuts down the group, --watch reloads it, and a bind failure aborts startup before any app starts serving. The option requires Rune app files/directories and cannot be combined with output, input, transform, merge or desktop-target flags.
You can still run app.rune and database.rune in two terminals. Stop the previous version first: the examples share the same native database file.
#!RUNE
# Split-server frontend. Start database.rune with --secondary-app or separately.
import "ui.rune"
@App
name = Shared Rune DB notes (split server)
type = REST
host = 127.0.0.1
port = 3000
@DataSource/server
type = rune-db
mode = remote
endpoint = http://127.0.0.1:7333
@DataSource/activity
type = vect-event
store = server
vectrune examples/data/storage/fullstack/app.rune --secondary-app examples/data/storage/fullstack/database.rune
# The split database endpoint is port 7333:
vectrune db get notes hello --endpoint http://127.0.0.1:7333CORS for the split-server example
Separate ports have different origins. The split database explicitly allows http://127.0.0.1:3000 and http://localhost:3000 using allow_origins, an array of exact HTTP(S) origins. The single-origin allow_origin setting remains supported; use one setting at a time. Wildcard origins are not accepted.
If you change the split frontend port, update this list too. The browser’s origin must match by scheme, hostname and port. The recommended same-server example avoids this cross-origin configuration entirely.
#!RUNE
# Native server owns this file. The browser and CLI both connect over HTTP.
@DataSource/main
type = rune-db
native_path = .vectrune/notes.rdb
@App
name = Shared notes database
type = rune-db
source = main
host = 127.0.0.1
port = 7333
allow_origins = ["http://127.0.0.1:3000", "http://localhost:3000"]
The complete Rune-Web frontend
Both hosts import ui.rune and style.rune, so the same note editor, view counter and event history work in either mode. The remote rune-db provider selects the native server; vect-event references that store. Top-level db/events assignments resume after I/O completes, with catch failed reporting errors and re-enabling the controls. The shared styles and all imports are included in the downloadable ZIP.
This unauthenticated example binds to loopback for local development. For external access, configure a server token and HTTPS termination, and supply browser credentials at runtime through __RuneWeb.setDataSourceToken. Use a trusted backend for application authorization; CORS only controls browser origins. Do not embed privileged tokens in downloadable Rune source.
#!RUNE
# Shared by the same-server and split-server entrypoints.
import "style.rune"
@Frontend
type = rune-web
path = %ROOT%
page = notes
@Page/notes
title = Rune DB · Shared notes workshop
style = notes
logic = notes
view:
main .workshop:
header .intro:
p .eyebrow "RUNE DB / INTERACTIVE WORKSHOP"
h1 "One note. A shared source of truth."
p .lead "Write in this browser. Read it from another. Your Rune server keeps the note and its history."
div .connection:
span .connection-dot aria-hidden=true "●"
span "This browser → Rune server → database file"
div .layout:
section .panel:
div .section-heading:
span .step "01"
div:
h2 "Write a note"
p .muted "Save first, then try the counter or open another browser."
label for=note-title "Note title"
input #note-title type=text value="{draft}" change="setDraft(this.value)" maxlength=200
div .actions:
button .primary disabled="{busy}" click="save()" "Save note"
button .secondary disabled="{busy}" click="load()" "Load note"
p .hint "Save replaces this demo note and resets its views to zero. Load reads the latest version from the server."
div .saved-note:
p .eyebrow "ON THE SERVER"
p .saved-title "Saved title: {title}"
div .counter-row:
p .counter "Views: {views}"
button .secondary disabled="{busy || !hasNote}" click="incrementViews()" "Add one view"
p .hint "Each click adds one on the server, even when other browsers are updating it."
p .commit "Last commit: {lastCommit}"
section .panel:
div .section-heading:
span .step "02"
div:
h2 "Follow the history"
p .muted "Saving a note also records an event in the same commit."
div .actions:
button .primary disabled="{busy}" click="readEvents()" "Read new events"
button .secondary disabled="{busy}" click="replayEvents()" "Replay events"
p .hint "Read advances one page. Replay starts at the beginning. Each page holds up to 20 events."
div .stream-meta:
span "Resume after: {cursor}"
span "Events in this page: {eventItems.length}"
if eventItems.length == 0:
div .empty-state:
h3 "No events in this page"
p "Save a note, then read its event. Use Replay events to revisit earlier saves."
for event in eventItems:
article .event-card:
div .event-heading:
strong "{event.kind}"
span .event-sequence "#{event.sequence}"
p "{event.value.title}"
p .hint "Commit {event.commit} · note {event.value.key}"
details .raw-data:
summary "Inspect the response data"
pre "{eventText}"
p .status role=status aria-live=polite "{status}"
footer .footer:
p "Try it: save → add a view → reload → load. Open a second browser to read the same note."
p "Notes and events survive reloads. The event cursor belongs to this tab and resets on reload."
@Logic/notes
state:
draft = "Hello from Rune-Web"
title = "No note loaded"
views = 0
status = "Save a note to start"
busy = false
hasNote = false
lastCommit = "No commit yet"
eventItems = []
cursor = "0"
eventText = "No events read"
action setDraft(value):
draft = value
action save:
busy = true
status = "Saving the note and its event..."
operations = [
{ op: "put", collection: "notes", key: "hello", value: { title: draft, views: 0 } },
{ op: "append", stream: "notes", kind: "note.saved", value: { key: "hello", title: draft } }
]
packet = { op: "batch", operations }
receipt = db server packet catch failed
busy = false
hasNote = true
lastCommit = receipt.commit
title = receipt.results[0].value.title
views = receipt.results[0].value.views
status = "Note and event committed together on the server"
action load:
busy = true
status = "Loading the shared note..."
packet = { op: "get", collection: "notes", key: "hello" }
record = db server packet catch failed
busy = false
hasNote = record != null
lastCommit = record.revision ?? "No commit yet"
title = record.value.title ?? "No saved note"
views = record.value.views ?? 0
status = record ? "Loaded from server" : "No saved note yet. Save one to begin."
action incrementViews:
busy = true
status = "Updating the view counter..."
packet = { op: "increment", collection: "notes", key: "hello", path: ["views"], by: 1 }
receipt = db server packet catch failed
busy = false
lastCommit = receipt.commit
views = receipt.results[0].patch.value
status = "View count incremented atomically on the server"
action readEvents:
busy = true
status = "Reading the event stream..."
packet = { op: "read", stream: "notes", after: cursor, limit: 20 }
page = events activity packet catch failed
busy = false
eventItems = page.events
cursor = page.next
eventText = json.stringify(page.events)
status = "Read one page of server events"
action replayEvents:
busy = true
status = "Reading the event stream..."
packet = { op: "read", stream: "notes", after: "0", limit: 20 }
page = events activity packet catch failed
busy = false
eventItems = page.events
cursor = page.next
eventText = json.stringify(page.events)
status = "Replayed the first page of server events"
action failed(error):
busy = false
status = error.message
Atomic checkout without a server
The local examples use a separate checkout.rdb file. Each checkout run intentionally seeds stock at 10, then atomically decrements it and appends purchase.completed. The expected revision comes from the seed receipt; a concurrent conflicting update rejects the entire batch. Stock and event receipts share a commit position.
Run the replay script twice: the first invocation prints purchases and stores its cursor; the second prints zero events if there are no new purchases. Run checkout again, then replay to resume after the saved position. Each call reads one bounded page. Persist the cursor after processing; a crash before saving can repeat events, so real handlers should tolerate duplicates.
#!VECT
# Each run resets this demo product to stock 10, then purchases one item.
packet = { op: "put", collection: "products", key: "tea", value: { name: "Rune tea", stock: 10 } }
seed = db.request("local.rune", "main", packet)
revision = seed.results[0].revision
# Stock and the domain event commit together, or neither does.
operations = [
{ op: "increment", collection: "products", key: "tea", path: ["stock"], by: -1, expected: revision },
{ op: "append", stream: "purchases", kind: "purchase.completed", value: { product: "tea", quantity: 1 } }
]
packet = { op: "batch", operations }
receipt = db.request("local.rune", "main", packet)
stdio -> "Stock remaining: {receipt.results[0].patch.value}"
stdio -> "Stock commit: {receipt.commit}; event commit: {receipt.results[1].commit}"
packet = { op: "get", collection: "products", key: "tea" }
record = db.request("local.rune", "main", packet)
stdio -> "Stored product: {json.stringify(record)}"
vectrune examples/data/storage/checkout.vect
vectrune examples/data/storage/replay.vect
vectrune examples/data/storage/replay.vectResume a durable event consumer
local.rune declares main as a rune-db provider and activity as a vect-event view with store = main. The script and its provider file are included in the example ZIP. Run consumers sequentially: this teaching cursor does not coordinate concurrent workers or provide exactly-once delivery.
#!VECT
# Run checkout.vect first. Keep a cursor on disk to resume between invocations.
# Start from zero if this is the first consumer run; cursor positions are strings.
packet = { op: "get", collection: "consumers", key: "purchase-printer" }
saved = db.request("local.rune", "main", packet)
after = saved.value.after ?? "0"
packet = { op: "read", stream: "purchases", after, limit: 20 }
page = db.request("local.rune", "activity", packet)
for event in page.events:
stdio -> "Purchase {event.sequence}: {json.stringify(event.value)}"
# Save only after processing this bounded page. Repeat to consume another page.
packet = { op: "put", collection: "consumers", key: "purchase-printer", value: { after: page.next } }
saved = db.request("local.rune", "main", packet)
stdio -> "Resume after: {page.next}; events this page: {page.events.length}"
Run an event-only server
This host uses its own file and port 7332, so it can run alongside the database examples. It exposes append, replay and append-only batches; document operations are rejected. after is an exclusive decimal-string cursor. Tail waits for committed events until Ctrl-C.
#!RUNE
# A separate file lets both standalone examples run simultaneously.
@DataSource/event_store
type = rune-db
native_path = .vectrune/events.rdb
@DataSource/activity
type = vect-event
store = event_store
@App
name = Vect Event Workshop
type = vect-event
source = activity
host = 127.0.0.1
port = 7332
# Terminal 1:
vectrune examples/data/storage/event-server.rune
# Terminal 2:
vectrune events append orders order.created --value '{"id":"order-1"}' --endpoint http://localhost:7332
vectrune events read orders --after 0 --limit 20 --endpoint http://localhost:7332
vectrune events tail orders --after 0 --endpoint http://localhost:7332Launch a standalone server
Run this server, then use CLI commands from a second terminal. A rune-db host serves document and stream operations; a vect-event host accepts stream operations only. Stop an owning server before directly opening its native file.
#!RUNE
import "data.rune"
@App
name = Rune DB Workshop
type = rune-db
source = main
host = 127.0.0.1
port = 7331
vectrune examples/data/storage/db-server.rune
# In another terminal:
vectrune db put notes hello --value '{"count":0}' --endpoint http://localhost:7331
vectrune db increment notes hello --path '["count"]' --by 1 --endpoint http://localhost:7331
vectrune db get notes hello --endpoint http://localhost:7331
vectrune events tail '$changes' --endpoint http://localhost:7331Compose with an existing app
Import the provider declarations into a REST app and call db/events with an operation packet. Writes and their change entries commit atomically. The packet API includes get, get_many, put, delete, increment, bounded prefix scans, append, replay and fixed atomic batches.
#!RUNE
import "data.rune"
@App
name = Embedded Rune DB API
type = REST
port = 3000
@Route/POST /notes/{id}
run:
parse-json
packet = { op: "put", collection: "notes", key: id, value: body }
receipt = db main packet
respond 201 receipt
@Route/GET /notes/{id}
run:
packet = { op: "get", collection: "notes", key: id }
record = db main packet
respond 200 record
@Route/GET /changes
run:
packet = { op: "read", stream: "$changes", limit: 20 }
page = events activity packet
respond 200 page
Keep browser data across reloads
This example saves a note in IndexedDB. Save, reload the page, then load it again. Browser data is local to the origin and does not automatically replicate to the native store. Top-level Rune-Web action I/O resumes after transaction completion; catch handles failures. Use the server-side example above when data must be shared across browsers or devices.
#!RUNE
# Browser-local data: IndexedDB, independent of the native server's file.
@App
name = Rune DB Browser Workshop
type = REST
port = 3000
@DataSource/browser
type = rune-db
database = rune-storage-workshop
@Frontend
type = rune-web
path = %ROOT%
page = storage
@Page/storage
title = Persistent browser data
logic = storage
view:
main:
h1 "Rune DB in the browser"
p "{status}"
p "Saved title: {title}"
button click="save()" "Save a note"
button click="load()" "Load saved note"
p "Reload this page, then load the note. IndexedDB keeps it locally."
@Logic/storage
state:
status = "Ready"
title = "No note loaded"
action save:
packet = { op: "put", collection: "notes", key: "hello", value: { title: "Hello from IndexedDB" } }
receipt = db browser packet catch failed
status = "Saved after transaction commit"
action load:
packet = { op: "get", collection: "notes", key: "hello" }
record = db browser packet catch failed
title = record.value.title ?? "No saved note"
status = "Loaded from IndexedDB"
action failed(error):
status = error.message
vectrune examples/data/storage/web.runeScope and limits
This is a small experimental core: no SQL, joins, consumer groups, automatic replication, branching or commit-block syntax. Names and cursors have defined bounds; values are limited to 1 MiB, packets to 8 MiB and atomic batches to 256 mutations. Revisions and positions are decimal strings.
For full packet forms, retention limitations, authentication and benchmark evidence, read knowledge/runtime/storage.md and examples/data/storage/README.md in the repository. The library db, events, db.request and @DataSource entries identify the supported hosts.
- get, put, delete and increment address one document by collection and key. Use expected = "0" for create-only, or a returned revision for optimistic concurrency.
- scan (CLI alias: query) returns an ordered key-prefix page, not SQL or a property-filter query. Continue with items/next and --after.
- append/read operate on ordered streams. Document writes also record $changes in the same transaction. Explicit appends to $changes are rejected.
- batch accepts 1–256 put/delete/increment/append operations in one store. A failed operation rolls back all document, revision and event changes.