@atom-forge/expose · Server output
Expose decides what a server sends to the client. A view names every field that may go out; everything else stays behind—including the column someone adds to the table next month. The compiler knows the exact shape of what left.
What it is
A view is a plain function: it receives the source row and returns the part it is responsible for, naming every field. expose runs one or more views on a row and merges their parts; compose joins views into a reusable one; fieldsOf builds views for fields copied as they are.
Because a view is just a function, Expose works with any database layer and any transport. It pairs naturally with AtomForge RPC: RPC carries the data, Expose decides what goes into it.
A field reaches the client only when a view names it. New columns, password hashes and internal flags stay on the server until someone deliberately adds them.
The result type contains only the named fields. Reading output.passwordHash is a compile error, not a runtime surprise.
Build small views per concern—identity, contact, links—and compose them into the shapes each endpoint needs. A later view can extend a key instead of replacing it.
null and undefined become null without running a view, so optional relations need no extra check at the call site.
Use fieldsOf for plain fields and write a view by hand for computed or nested data. Never spread the source row.
Keep the views per entity in their own files and collect them into a plain views object.
Check permissions first, then return expose(row, views.user.card) instead of the raw row.
An honest fit check
The library cannot stop code from returning a row without calling it. A project that needs this guarantee adds its own guard test.
A view says what goes out, not who may get it. Authorization still happens before the call.
Spreading the source or returning a nested row as it is lets everything out. Run nested data through its own view.
From the Expose documentation. passwordHash is neither in the result nor on its type.
import { compose, expose } from '@atom-forge/expose';
const identity = (source: { id: string; name: string }) => ({ id: source.id, name: source.name });
const contact = (source: { email: string }) => ({ email: source.email });
const card = compose(identity, contact);
const row = { id: 'u1', name: 'Ada', email: 'ada@example.com', passwordHash: 'secret' };
expose(row, card);
// { id: 'u1', name: 'Ada', email: 'ada@example.com' }The type lists what may go out by name; the call picks the fields. Never pass it names that come from a request.
import { expose, fieldsOf } from '@atom-forge/expose';
type PublicUser = { id: string; name: string; email: string };
const fields = fieldsOf<PublicUser>();
expose(row, fields('id', 'name'));
// { id: 'u1', name: 'Ada' }