Introducing a new way to review your PRs.

Read the announcement

frappe / frappe-ui

The all-in-one toolkit for modern frontends. High-quality components, app recipes and AI-ready docs.

View on GitHub

28 documents · Updated Nov 23, 2026

docs / data-access

Data access layer

The data access layer gives Vue applications one way to read and change data in a Frappe backend. It turns requests into reactive state, shares that state when two views address the same record, keeps useful data across reloads, and coordinates concurrent writes so an older response does not replace a newer result.

Data access model

The layer separates transport from data shape and shared state. Request composables handle one remote operation, while document and list composables add Frappe meaning and connect the result to shared stores.

Resource boundaries

useCall is the general request unit for a Frappe endpoint. It exposes data, progress, errors, cancellation, and submit or execute actions. useDoc and useList build on this unit for one document or a set of rows. useDoctype groups writes for a DocType, and useNewDoc represents a document before the server gives it a name. The older createResource, createDocumentResource, and createListResource API follows the same broad model and remains available for existing applications.

Choose the smallest boundary that matches the work. Use a general call for an endpoint that has no document or list identity. Use a document or list boundary when the result must stay in step with other views.

References: src/…/index.ts• L15-29 src/…/types.ts• L6-23 src/…/index.ts• L1-9

Frappe data shapes

The layer maps Frappe records to stable client-facing values. A document is identified by its DocType and name. A list is defined by its DocType and query, while its persisted cache identity comes from the caller's cache key. Its rows use the document name as their row identity. This lets a write to one record update both a document view and any list view that shows that record.

The public composables use Frappe's document API by default. A caller can still provide a different URL or base URL when an application uses a custom endpoint or a remote Frappe site.

References: src/…/useDoc.ts• L61-66 src/…/useList.ts• L49-66 src/…/types.ts• L26-45

Read lifecycle

Reads move from a request to reactive state and then to optional local storage. The layer can show an initial or cached value while a fresh request runs, then replace it with the server result when the request succeeds.

Request state

Each read reports whether it is loading, finished, aborted, or failed. It also exposes the resolved URL and parameters. Reactive parameters such as a document name, filter, or sort value can cause a read to run again when they change.

The result may be changed before or after a request through initial data and a transform. These features shape what a view sees; they do not change the identity used to coordinate shared records.

References: src/…/useCall.ts• L35-53 src/…/useDoc.ts• L80-112 src/…/useList.ts• L84-108

Cache layers

The layer has two cache boundaries with different jobs:

  • The in-memory document and list stores provide shared reactive state during the app session.
  • IndexedDB provides a local copy for cache-keyed calls and document or list state. A memory fallback is used when IndexedDB is not available.

Cache identity must describe the data being requested. A document uses its DocType and name. General calls and lists use the caller's cache key, with the data-access kind included so unrelated request types do not share a slot. A cached value is a starting point, not proof that the server accepted the latest state.

References: src/…/docStore.ts• L11-34 src/…/idbStore.ts• L3-24 src/…/utils.ts• L62-75 src/…/useList.ts• L79-82

Stale read policy

The default read policy favors current server data. When a cache key is present, a cached value can still provide immediate content while a request is in flight. staleOnError extends that behavior for suitable non-Frappe failures, such as a network outage. A Frappe response error remains visible instead of being hidden by stale data.

This policy belongs to the read boundary. It does not make a failed write safe, and it does not decide which concurrent write should update the shared stores.

References: src/…/useCall.ts• L219-242 src/…/useCall.ts• L278-280 src/…/useList.ts• L110-126 src/…/useDoc.ts• L177-195

Shared state boundaries

Shared state is the link between separate views that refer to the same Frappe data. Document state is keyed by DocType and name. List state is grouped by DocType so a row update or removal can reach every active list that contains that record.

Document and list state

useDoc reads and writes one shared document. useList owns a reactive set of rows and supports paging, filtering, sorting, and row changes. When a list write returns a document, the layer updates the matching document state and list rows. When a document write returns, the same two state paths are updated.

This means a view does not need to reload just because another view changed the same record. A full reload is still useful when a change affects membership, ordering, or fields not present in the current list.

See Document and list state for the detailed state rules.

References: src/…/useDoc.ts• L144-169 src/…/useList.ts• L176-238 src/…/listStore.ts• L20-66

Local persistence

Persistence is a recovery and first-paint aid, not the source of truth. Successful reads and accepted writes update the local copy. A later read may use that copy while it checks the server. Invalid data can be removed without sealing the document, so a later valid server write can restore it. A confirmed delete removes the local copy and prevents an older in-flight response from bringing the record back.

The document store also limits how long an in-memory value is treated as fresh. When it becomes stale, the next reader starts a refresh rather than assuming that the local value is current.

References: src/…/docStore.ts• L36-55 src/…/docStore.ts• L150-179 src/…/docStore.ts• L197-243

Read and write coordination

The layer treats reads and writes differently because they carry different meaning. A read reports what the server answered at one point in time. A successful write expresses a new value and must not be undone by a response from an older request.

Write ordering

Each request receives an order when it is sent. Shared stores compare that order per document. A later-dispatched successful write can therefore replace an earlier one even if the earlier network response arrives last. A read may update the store but does not claim a write order, because an old read must not block a save that is still in progress.

The rule applies across composable instances and across document and list views. It keeps the two views of one record aligned instead of letting each view choose its own last response.

The following diagram shows how a request result reaches both shared state paths and how the store gate protects them.

flowchart LR
  A[Request] --> B{Result}
  B -->|read| C[Document or list read]
  B -->|successful write| D[Write order]
  C --> E[Shared document state]
  C --> F[Shared list state]
  D --> G{Per-document gate}
  G -->|accepted| E
  G -->|accepted| F
  G -->|stale| H[Discard store update]

References: src/…/useFrappeFetch.ts• L40-75 src/…/useFrappeFetch.ts• L92-103 src/…/writeGate.ts• L1-25 src/…/docStore.ts• L64-89

Mutation outcomes

Writes use a small set of paths that match the Frappe data model:

  • A document update changes selected fields and publishes the returned document to document and list state.
  • A document delete removes the record from both state paths.
  • A list insert creates a new named document and can refresh the list.
  • A list update changes one named row without requiring a full list read.
  • A document or DocType method sends a server action and may publish returned documents.

List writes can run for different rows at the same time. Their aggregate loading state tells a view that work is in progress, while a target-specific loading check can show feedback on the affected row.

See Remote actions and mutations for action lifecycles, isolated calls, and write safety.

References: src/…/useAction.ts• L37-79 src/…/useList.ts• L176-248 src/…/useDoctype.ts• L10-27

Realtime refresh

When a socket is available, document and list resources can listen for a Frappe document update. The realtime boundary subscribes each socket to the needed DocTypes, restores those subscriptions after reconnect, and passes only matching document names to the resource. A document refreshes its record. A list refreshes only a row that it already has, which limits network work and preserves the list's query shape.

Each socket keeps its own subscription set, so a new socket subscribes even when another socket already watches the same DocType. Repeated listeners on one socket do not create duplicate DocType subscriptions. Realtime is an optional signal. Without a socket, the same resource continues as a normal request-based resource.

References: src/…/realtime.ts• L7-41 src/…/realtime.test.ts• L53-103 src/…/documentResource.js• L270-276 src/…/listResource.js• L267-274 src/…/socketAccess.ts• L7-19

Backend connection

The data layer connects to Frappe through a transport boundary. That boundary adds Frappe request defaults, maps response envelopes to useful values, and turns server failures into errors that the data composables can expose.

Request transport

The lower transport accepts relative URLs, dotted Frappe method names, or absolute URLs. It adds the request headers needed by Frappe and can use a configured base URL for local UI work against another site. It returns the Frappe message or document data in the shape expected by the calling composable.

Read Frappe request transport for the network path, authentication-facing settings, and error rules.

References: src/…/request.ts• L23-93 src/…/frappeRequest.ts• L79-136 src/…/useFrappeFetch.ts• L60-103

Configuration boundary

Applications can replace the resource fetcher and can set default endpoints for document and list operations. They can also supply request headers, a remote base URL, and handlers for server messages or errors. These settings are shared through the package configuration so all data-access paths use the same application policy.

Keep transport settings at this boundary. Do not make individual document or list consumers repeat authentication or error policy unless they need a deliberate exception.

References: src/…/config.ts• L11-38 src/…/config.ts• L40-57 src/…/resources.js• L56-60

Data access selection

The public data-access surfaces serve different levels of control. This table gives a first choice for a new feature.

NeedPreferred boundaryResult
Call a Frappe endpoint with custom parametersuseCallReactive request state and a direct response
Read and change one named documentuseDocShared document state plus update, delete, and method actions
Read or change rows for one DocTypeuseListShared rows with filters, paging, and row writes
Group DocType-wide writesuseDoctypeInsert, update, delete, and method actions without a list owner
Create a new document before it has a nameuseNewDocLocal draft state followed by a server insert
Keep an Options API application on its existing modelResource APIs and resourcesPluginThe earlier resource shape with the same request and cache ideas

The child pages describe each boundary in more detail. Start with Frappe request transport when the question is about the network. Start with Data-fetching composables when the question is about current composable APIs. Start with Document and list state when the question is about shared data. Start with Remote actions and mutations when the question is about writes or server methods.

Change checklist

When these files or sections are changed, remember to consider these:

  • When a Frappe response shape or endpoint path changes, check the transport mapping and every document, list, and method boundary that consumes it.
  • When cache identity changes, check both the in-memory shared key and the persisted key. A key collision can silently show one query's data in another query.
  • When a document write changes, check both document state and list-row state. They must receive the same accepted result.
  • When delete behavior changes, check local removal and the protection against an older in-flight response restoring the deleted record.
  • When request ordering changes, check the write gate and all paths that publish server results. A publish path outside the gate can reintroduce stale data.
  • When realtime event handling changes, check the event's DocType and name mapping and the refresh behavior for both single records and visible rows.
  • When realtime socket subscription changes, check per-socket de-duplication and re-subscription after reconnect. A new socket must establish its own server-side subscriptions.
  • When shared configuration changes, check the default resource fetcher, base URL, headers, and server-message policy together.

Important failure modes

TriggerConsequenceGuard
Two unrelated queries use the same cache keyA view can display data from the other queryBuild cache keys from all values that define the requested data
A transport failure occurs during a refreshThe view may lose its last value unless stale fallback is enabledUse staleOnError for views that need to remain useful offline, and still show the error state
A Frappe error response is treated as a normal network failureServer rejection can be hidden behind old dataKeep Frappe response errors distinct from transport errors
An older write response settles after a newer writeShared document or row state can move backwardRoute every server write through the per-document write gate
A list has rows with no document nameThe row cannot be matched for shared updates or deletesPreserve Frappe document identity in list results
A confirmed delete races with an older requestThe deleted document can return to a viewRemove it from both stores and seal its shared identity
A realtime event has the wrong DocType or nameThe wrong view refreshes or a needed view stays staleMatch both event identity fields before refreshing
A new socket does not repeat a DocType subscription after reconnectUpdates stop reaching resources on that socketKeep subscriptions per socket and restore all of them on each connect

Key files

FileWhy to read it
src/…/index.tsPublic entry point for the current data composables and their supported surface
src/…/useCall.tsGeneral request state, cache fallback, and the handoff to shared-store writes
src/…/useDoc.tsSingle-document reads, writes, methods, and links to document and list state
src/…/useList.tsList queries, paging, row updates, and list-owned writes
src/…/docStore.tsShared document identity, local recovery, freshness, and document write admission
src/…/listStore.tsPropagation of accepted document changes to active lists
src/…/writeGate.tsOrdering rule that prevents stale concurrent writes
src/…/useAction.tsIndependent submit lifecycles for inserts, updates, deletes, and methods
src/…/useFrappeFetch.tsFrappe response handling and request stamps used by shared state
src/…/frappeRequest.tsFrappe-aware transport behavior and server error mapping
src/…/config.tsApplication-wide transport and endpoint policy
src/…/index.tsPublic entry point for the earlier resource API, local persistence helpers, plugin, and realtime hook
src/…/realtime.ts and src/…/realtime.test.tsRealtime DocType subscriptions, reconnect behavior, and event filtering
src/…/resources.jsEarlier general resource boundary and its in-memory or local cache behavior
src/…/documentResource.js and src/…/listResource.jsEarlier document and list resource model for applications using the Options API