Skip to the page

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.

Ben’s query
select Note { title }
Ben gets none of Ada’s notes, even when asking for her note’s id directly.

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.

dbschema/default.gel
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);}
The same three-line rule governs both Note records and uploaded Attachment files.

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.

Your terminal
napalm explain-access notes 0192c3f4-6b1a-7d8e-9f00-1a2b3c4d5e6f --as ben@example.com
Object default::Note 0192c3f4-6b1a-7d8e-9f00-1a2b3c4d5e6f
Access × read × change × delete
owner_has_full_access allows all
× .owner ?= global caller
global caller was set
Trace every rule on Ada’s note as Ben’s session without adding debug routes or weakening policies.

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.

dbschema/queries/myNotes.edgeql
select Note { id, title, body }order by .title
The query never filters by owner because the rule on Note already scopes the records.

A 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.

Terminal
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" } }'
Ada’s session calls 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.

Terminal
napalm schema push notesnpm run generate
Push the schema and queries, then generate typed client functions.

Files 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.

dbschema/queries/addFile.edgeql
select (insert Attachment {  owner := global caller,  file_name := <str>$name,  file_size := <int64>$size,}) { id, file_upload }
Setting file_size checks the insert rule and returns a one-time upload plan straight to object storage.

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.

dbschema/queries/myFiles.edgeql
select Attachment { id, file_name, file_size, file_status, file_download_url }order by .file_name
Signed download URLs come back only on Attachment records the caller’s select rule allows.

Ben 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.

Terminal
napalm schema push notes --review > review.md
Block pull requests in CI whenever a schema change would break a query.

Once 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.

Terminal
napalm endpoint notes --passthrough keys
Restrict user sessions to deployed queries while service keys keep full access.

Query-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.

Type a word or two to search every docs page.