LuxelEspañol

Luxel v0.1 · Guide · part 1 of 2

Server

Everything a page shows and every rule it follows runs on the server. You write two kinds of handlers: defineLoad reads a view's state, defineEvent handles an event. The framework does the rest: authorization, object scopes, input checks, idempotency, transactions, version checks, and turning your changes into slot updates.

The files

server/contract.ts the contract: state paths, events with their policies, messages server/events.ts your handlers (luxel.config.ts → server.events) server/bindings.mjs what luxel-host needs from your app (luxel.config.ts → server.bindings) .luxel/server.ts generated by luxel build from the contract and the views; do not edit

Handlers import the generated module:

import { defineEvent, defineLoad, events, views } from '../.luxel/server.js';

views has one entry per view (views.contacts, views.secureTransfer), events one per event in camelCase (events.contactAdd for contact.add; when two views share an event name, the view is put in front). Every path, value type, input, message key and argument count is typed from the contract and the compiled views. A handler for an event the contract does not declare cannot be written.

The build bundles your handlers to .luxel/events.mjs; the host loads every defineLoad and defineEvent the module exports.

defineLoad

export const contactsLoad = defineLoad(views.contacts, async (ctx) => { const { rows, total, version } = await contactOps.page(ctx.tx as Db, ctx.session.userId as string, 1); return ctx.snapshot(version, { 'contacts.rows': rows, 'contacts.summary': ctx.plural('summary', total, String(total)), }); });

A load returns the state by path, either:

  • through ctx.snapshot(version, values, fill?): the values and the version they were read at, ideally from one statement, so the two always belong together; or
  • as a plain object of values: the view shows no versioned resource (a new, empty form) and its version is 0.

fill (the third argument of ctx.snapshot) holds data only the page shows and nothing updates: the options every row select of a collection lists.

A load runs per request for a private view. A view that shows server state and has events needs a load; the host refuses to start without one. A view with no state (a sign-in form) needs none. For a static view, the load runs once at start-up, without a session, and its result is shown to everyone: it must not read anyone's data.

The load context

MemberMeaning
ctx.session{ id, userId, roles }: userId is null for an anonymous session
ctx.txThe store's connection (see EventStore)
ctx.langThe page's language
ctx.message(key, ...args)A message of this view, as a text value
ctx.plural(key, count, ...args)The plural variant of key the page's language selects for count
ctx.snapshot(version, values, fill?)The state with its version

defineEvent

export const contactRemove = defineEvent( events.contactRemove, async (_input, ctx) => { await contactOps.remove(ctx.tx as Db, ctx.session.userId as string, ctx.param); const p = await contactOps.page(ctx.tx as Db, ctx.session.userId as string, 1); return ctx.set('contacts.rows', p.rows).success('removed').version(p.version); }, { lock: (ctx) => contactOps.lockBook(ctx.tx as Db, ctx.session.userId as string) }, );

(In the examples on this page, contactOps, accountOps and Db stand for your own database layer and its connection type.)

defineEvent(ref, handler, options?):

  • ref: the event, from the generated events.
  • handler(input, ctx): input holds the event's inputs, typed from the view (strings; rows as arrays of keyed objects). It returns changes (the chain on ctx) or a refusal (ctx.reject).
  • options: authorize, lock, compare (below).

The event context

Everything in the load context except snapshot, plus:

MemberMeaning
ctx.eventIdThe event's id (its idempotency key). A write's only: since VSA 0.9 a read has none (ADR-050)
ctx.seenThe version the person saw (what lock is compared with)
ctx.paramThe item key, for an action in a collection item; undefined otherwise
ctx.reject(code, message)A refusal: VALIDATION, INSUFFICIENT_FUNDS, FORBIDDEN or NOT_FOUND, with a message key reference or a string

And the changes, chained, each returning the chain:

ChangeEffect
set(path, value)The new value of a path; every zone bound to it changes. On a collection, replaces the list
item(path, key, fields)One item of a collection: only the fields given
insert(path, key, fields, after)A new item right after after ('' = first)
remove(path, key)Removes an item
move(path, key, after)Moves an item right after after ('' = first)
sync(path, before, now, seen)Brings a versioned collection from what the page showed at seen to now (Collections)
success(key, ...args)The message the action's status zone shows. Without it, the status zone is cleared
navigate(view)The page loads another view next
signIn({ userId, roles?, displayName? })Opens a new session for this user after the answer (Sessions)
signOut()Ends this session after the answer
version(v)The view's version after this answer

The framework maps paths to the zones that bind them, formats money (integer cents in, text out), dates and quantities, and puts the success message in the action's status zone. A handler never writes a zone id or a slot index.

Value types by leaf: text() takes a string or a message reference; money(), number() and quantity() take numbers (money: a safe integer of cents); date() an ISO yyyy-mm-dd string; numbers() an array of numbers; options() an array of { key, label }; a collection an array of items with their key.

Policies: who may load a view (Sprint 12)

Every view of the contract declares who may load it, the same way events declare who may send them. There is no default: a view without load is LUXEL-E058.

import { allow, publicLoad } from '@luxel-framework/compiler/config'; login: { state: {}, load: publicLoad(), events: { … } }, // a public route declares it contacts: { load: allow(['user']), state: { … }, events: { … } },

  • allow(roles): the session must hold one of the roles. Checked before the load runs: the state endpoint answers 403, a page answers 403, and the load never runs.
  • publicLoad(): anyone, signed in or not. Only on a view whose route is public (views.‹id>.public), and a public view must say it (both mismatches are LUXEL-E058).

Policies: who may send an event

Every event in the contract declares its policy. There is no default: an event without one is LUXEL-E044, and the gateway denies anything not allowed.

import { allow, LOCAL, publicEvent } from '@luxel-framework/compiler/config'; events: { 'session.login': publicEvent({ effect: 'read' }), 'contact.add': allow(['user']), 'contacts.page': allow(['user'], { effect: 'read', refs: { param: LOCAL } }), 'contact.remove': allow(['user'], { refs: { param: 'contact' } }), },

  • allow(roles, rest?): the session must hold one of the roles. A missing role answers FORBIDDEN.
  • publicEvent(rest?): any session may send it, signed in or not (sign-in, for example).
  • effect: 'write' (default) or 'read'.
  • A write runs in one transaction, claims its event id (idempotency) and checks versions.
  • A read (a search, a page, a recalculation) claims nothing and stores nothing.
  • A read has no event id (VSA 0.9). If its handler passes an id on to another service that wants one (a backend that logs or deduplicates by id), make one there (randomUUID()). The build refuses a read handler that reads ctx.eventId (LUXEL-E055). Since Sprint 12, code outside the handler is caught at run time: reading ctx.eventId in a read — or event.eventId in a hand-written adapter's handleEvent — throws LUXEL-E055: ‹view>/‹event> is a read, and a read has no event id…, so the mistake fails the request (500, logged) instead of sending undefined onwards. 'eventId' in ctx (or in event) is false for a read; test it first: const id = 'eventId' in event ? event.eventId : randomUUID();. (In Sprint 11 the internal pilot's bindings sent undefined to its backend for every row selection.)
  • refs: for each id the event carries, the kind of resource it names. Keys are param, the name of a select input, or '‹rows>.‹column>' for a select column of a rows input. Values are a resource kind (checked by its scope function) or LOCAL (the id names no resource, such as a page key or a row key of a form being filled). An id without a refs entry is LUXEL-E045.

Roles come from the session: the roles given to ctx.signIn, or those your session store returns.

authorize: an extra check

defineEvent(events.agencyToggle, handler, { authorize: ({ session, param }) => session.roles?.includes('operator') === true, });

authorize runs after the policy and the input checks. It can only refuse more (answering FORBIDDEN), never allow more.

Edit this page on GitHub

This page: 7.9 KB of JavaScript (Luxel's shared runtime), 0 KB from your app.

Measured when the site is built: the runtime files this page loads, compressed as they are sent.

Made by IntiOps. Luxel 0.1.0-beta.1 is a beta: no external security review yet.