Feature by feature
Chapter 4 of 11
Rules, queries and files
Access rules sit on your data types instead of inside route handlers. Saved EdgeQL files turn those rules into named endpoints, direct file uploads and typed client functions.
Route-level permission filters and hand-written client types drift as soon as a second endpoint or screen ships. One forgotten owner check on an export route exposes Ada’s private notes to Ben, and one renamed column crashes the client at runtime.
Try it
Napalm attaches access rules directly to each type, so even an unfiltered query leaks nothing. Sign up as Ada to save a note, then sign in as Ben in a private window and query every note.
select Note { title }Rules on the type#
Pillar at work: Caller-bound
Moving the rule onto the type closes every route at once. Access policies walk links in the same EdgeQL language as queries, so one rule on Note and Attachment (ext::files::File) governs every read, write, search and download link.
global caller := global ext::auth::ClientTokenIdentity; type Note { required owner: ext::auth::Identity; required title: str; required body: str; access policy owner_has_full_access allow all using (.owner ?= global caller);} type Attachment extending ext::files::File { required owner: ext::auth::Identity; access policy owner_has_full_access allow all using (.owner ?= global caller);}Policies compile directly into every query
Saved queries traverse linked rows under open access policies in one snapshot while unpolicied types stay closed.
Every stored type starts closed, so a type without a matching rule returns zero records. Napalm verifies the session credential before binding global caller, so no request can impersonate Ada.
Rules in every statement#
Pillar at work: Checked and fault-tested
Declaring the rule on Note and Attachment works because Napalm compiles that rule straight into each SQL statement. PostgreSQL attaches that ownership filter to every query, download link, search, live subscription, update and delete from Ben. If Ben tries to insert a note naming Ada as the owner, PostgreSQL refuses the write before anything saves.
Non-human callers hit the exact same rule. Because a backend service key belongs to no user, global caller is empty on worker calls and a worker sees zero records until you add a policy checking service globals. When Ada approves an AI agent instead, global caller carries Ada and the agent stays inside her exact access.
As you add rules for teammates or services, run napalm people reach notes ben@example.com to count reachable rows or explain-access to trace why a note stayed hidden without reading field values.
Saved queries and typed clients#
Pillar at work: Compiled
Production apps save every read and write as an .edgeql file in dbschema/queries. Four lines load Ada’s notes list.
select Note { id, title, body }order by .titleA saved query traverses links, backlinks and counts across any depth and compiles into one SQL statement without N+1 round trips or torn reads. Pushing deploys each file at POST /query/{name}. Callers send only JSON variables while the query text stays on the server, so user input cannot alter the query.
curl https://notes.napalmdata.com/query/addNote \ -H "Authorization: Bearer $SESSION" \ -H "Content-Type: application/json" \ -d '{ "variables": { "title": "Printers", "body": "Call the vendor on Monday" } }'addNote by name using typed variables.From those same files, napalm generate writes typed functions across eight languages, plus commands in your project’s CLI and tools on your MCP server.
napalm schema push notesnpm run generateFiles as records#
Pillar at work: One typed graph
Adding PDF or image attachments usually splits permissions between database rows and S3 presigning code. In Napalm, Attachment extends ext::files::File to own metadata and access rules while raw bytes live in dedicated object storage.
select (insert Attachment { owner := global caller, file_name := <str>$name, file_size := <int64>$size,}) { id, file_upload }Direct-to-bucket uploads under schema rules
Inserting a File record checks insert policies and returns a one-time upload plan straight to your dedicated object bucket, verifying leading magic bytes before marking the record Ready.
Browser and mobile uploads stream bytes directly to your cluster’s private bucket without passing through engines. On completion, Napalm inspects the uploaded bytes rather than trusting upload headers and checks any file_format constraint before marking the record Ready.
select Attachment { id, file_name, file_size, file_status, file_download_url }order by .file_nameBen never receives a link to Ada’s PDFs because the URL is signed inside the policy-filtered query. Stored objects never overwrite in place, so point-in-time restores and data-cloned branches always resolve the exact file bytes from that moment.
Checks in CI and production#
Because Napalm knows every deployed query, every push validates your whole query folder before migrating data. Passing --review in CI writes the migration plan for pull requests and fails the build if a change would break any saved query.
napalm schema push notes --review > review.mdOnce every screen calls deployed endpoints by name, one command turns off raw EdgeQL strings for browser and mobile sessions while keeping full query access open for backend service keys.
napalm endpoint notes --passthrough keysQuery-checked pushes
Schema pushes check every saved query before migrating data and block breaking pull requests in CI.
Deployed-query lockdown
Closing raw EdgeQL passthrough limits user sessions strictly to deployed query endpoints.
Under live traffic, napalm insights notes reports per-query latency, replica read share, the heaviest access rule on each statement and suggested indexes.
Next, keep Ada and Ben’s screens updated live as notes change.