Queries, permissions, and roles
This page explains the SDK values that shape list requests and access control. Query turns filters, ordering, paging, and related conditions into request values. ID, Permission, and Role provide the shared inputs that identify resources and express who can use them. For the wider value model, see SDK value types and query building and Models and enumerations. For service examples, see Databases and TablesDB records and VectorsDB documents and queries.
Query construction
The Query value is a portable description of one list condition or list control. A service accepts query strings, Query values, a fluent builder, or nested arrays of these inputs, then normalizes them into the query string array expected by the request. Appwrite applies the conditions to the selected resource type on the server.
The request flow connects query creation in application code to a filtered list response.
flowchart LR
A[Application input] --> B[Query values]
B --> C[List service request]
C --> D[Server-side filtering and ordering]
D --> E[Resource list]
List request inputs
List methods use queries as one part of a larger request. The same query value can be used with different list services, but each service defines which resource attributes and operators are valid for its endpoint.
| Input | Purpose |
|---|---|
| Filter query | Keeps resources that match an attribute condition. |
| Selection query | Limits the returned attributes. |
| Ordering query | Sorts results in ascending, descending, or random order. |
| Cursor query | Starts a page before or after a resource ID. |
| Limit or offset query | Controls page size or position. |
| Search or total option | Adds service-level text search or controls count calculation when the list method supports it. |
Each query becomes a string value in the service request. List endpoints commonly accept up to 100 queries with a maximum length of 4096 characters per query, although the endpoint documentation remains the source for its exact limits. (See Storage.listFiles and Functions.listExecutions.)
References: src/query.ts• L10-38 src/…/storage.ts• L18-40 src/…/functions.ts• L17-37
Query value shape
The query format has a method name plus an optional attribute and values. Scalar input becomes a one-item values array, while array input keeps its array shape. Compound queries contain parsed query objects rather than nested JSON strings, so they can express one server-side condition tree.
The SDK uses a JSON serializer that preserves large integer values when it builds query strings. This keeps the wire value aligned with the input type when a query needs a bigint.
References: src/query.ts• L1-38 src/query.ts• L285-325
Fluent query inputs
Query.Builder collects query fragments in call order and returns the string[] used by list requests. It provides the main query families as chainable methods, and Query.page adds validated one-based pagination as a limit and offset pair. normalizeQueries also accepts direct query values and nested arrays, so callers can assemble query input without manually converting each fragment.
References: src/query.ts• L741-753 src/query.ts• L795-806 src/query.ts• L1227-1264
Query operators
Query helpers group the server's list operations by the kind of result they produce. Use basic comparison and text helpers for common filters, composition helpers for shared conditions, and specialized helpers only when the resource field supports their data type. The target service still decides which operators and attributes it supports.
Comparison and text operators
Comparison operators match equality, ranges, null state, or text boundaries. The matching negative operators cover the inverse cases. Date helpers apply the same comparisons to the reserved creation and update time fields, which gives list code a clear time-based form without repeating those field names.
| Operator group | Result |
|---|---|
| Equality and range | Matches equal, unequal, less-than, greater-than, or bounded values. |
| Null and existence | Matches missing or present values and attributes. |
| Text | Matches regular expressions, substrings, prefixes, suffixes, or full-text search. |
| Date | Filters creation or update time before, after, or between dates. |
References: src/query.ts• L40-132 src/query.ts• L199-283
Collection and compound operators
Collection operators distinguish between a field that contains one value and a field that contains a set of values. Compound operators combine query strings with and or or, while elemMatch applies several conditions to elements of an array. Relationship queries apply filter, order, selection, and paging rules to populated related resources. These helpers let a list request state whether conditions apply across a resource, within one array element, or on related resources.
References: src/query.ts• L134-227 src/query.ts• L285-325
List shaping and relational operators
List-shaping operators control the result rather than only filtering it. Selection, ordering, cursors, limits, offsets, grouping, distinct results, and aggregate values let a supported list endpoint return a focused or summarized result. Join operators add rows from another table or collection and use an on condition to relate the two sources.
These operators depend on server support and on the endpoint's field and table rules. Use aliases when the result must expose an aggregate or joined source under a stable name.
References: src/query.ts• L108-132 src/query.ts• L519-726
Spatial and vector operators
Specialized operators support location and vector fields. Distance helpers compare coordinates to a distance, geometry helpers compare spatial relationships, and vector helpers compare similarity by a selected metric. Coverage and spatial-equality helpers add further geometry relations. These helpers describe advanced matching; the target resource must expose a compatible field and server capability.
References: src/query.ts• L328-517 src/query.ts• L728-738
Query and update operators
Query builds conditions for reading lists. Operator is a separate value family for changing an attribute during an update, such as incrementing a number, editing an array, or toggling a boolean. Do not use an update operator as a list filter.
References: src/query.ts• L10-38 src/operator.ts• L17-55 src/operator.ts• L57-141
Resource identifiers
The ID helper gives resource-creation calls a consistent choice between a caller-owned identifier and an SDK-generated identifier. The selected ID becomes the stable handle used by later reads, updates, deletes, cursor queries, and access rules.
Custom and unique IDs
ID.custom returns the supplied value unchanged. It supports a known or business-defined ID that the caller can reproduce. ID.unique creates a hexadecimal value from the current time and random hexadecimal padding. Its padding length can be adjusted when a caller needs a different size, but the generated value is not a stable replacement for a caller-defined ID.
Resource services document this choice for IDs such as files, rows, teams, and other created resources. The service still validates the final ID against its own allowed format and length.
References: src/id.ts• L1-27 src/…/storage.ts• L89-106 src/…/tables-db.ts• L411-420 src/…/teams.ts• L80-94
Permissions and roles
Permissions and roles form two linked parts of access control. A permission states an action such as read or update. A role states which users or groups receive that action. Resource creation and update methods accept permission strings, so the helpers let application code build those values without hand-writing the access-control syntax.
Permission actions
Permission creates one action string for a supplied role. The action set covers reading, writing, creating, updating, and deleting a resource. The helper does not decide who receives access; it combines the action with the role supplied by the caller.
| Action | Access meaning |
|---|---|
read | The role can read the resource. |
write | The role can write to the resource. |
create | The role can create the resource. |
update | The role can update the resource. |
delete | The role can delete the resource. |
References: src/permission.ts• L1-22
Role targets
Role creates the audience part of a permission. Broad roles cover anyone, authenticated users, or guests. Narrow roles identify one user, a team, a team member, or a user label. User and team roles can add a status or team role when access must target a subset of that audience.
| Role helper | Audience |
|---|---|
any() | Authenticated and unauthenticated users. |
users(status?) | Users with an optional verified or unverified status. |
guests() | Users without a session. |
user(id, status?) | One user with an optional status. |
team(id, role?) | A team or members with one team role. |
member(id) | One team member. |
label(name) | Users with one label. |
The access value is built by composing the role with the action. For example, Permission.read(Role.team(teamId)) expresses read access for a team, while Permission.update(Role.user(userId)) targets one user. The resulting strings belong in a resource method's permissions array.
References: src/role.ts• L1-100 src/permission.ts• L1-22 src/…/storage.ts• L99-106 src/…/tables-db.ts• L413-420
Change checklist
When these files or sections are changed, remember to consider these:
- When a query method, builder method, or value shape changes, check every service list method that sends
queriesand every endpoint-specific attribute contract. - When query normalization or page pagination changes, check callers that pass builders, nested query arrays, or one-based page values because these inputs become the final request array.
- When aggregation, grouping, or join operators change, check the endpoint response shape and its table, alias, and field-name rules.
- When an ID format changes, check create methods that document custom IDs and later methods that use the ID in paths or cursors.
- When a permission action or role format changes, check all resource create and update methods that accept
permissions, plus any team role inputs that use the same access terms. - When compound query serialization changes, check nested
and,or,elemMatch,relationship, andhavingvalues because they must remain query objects on the wire.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A list query names an attribute or operator that the endpoint does not support. | The server rejects the request or returns no useful matches. | Use the attribute and operator set documented by the target list method. |
| A compound query is passed as an unparsed nested string. | The server receives the wrong query tree and cannot apply the intended composition. | Build compound values through Query.and, Query.or, Query.elemMatch, Query.relationship, or Query.having. |
| A page number or page size is less than one. | Query.page rejects the pagination input. | Pass one-based page and positive per-page values. |
| A generated or custom ID violates the resource ID rules. | Resource creation fails before later operations can use the ID. | Keep IDs within the service's allowed characters and length. |
A caller uses ID.unique where a repeatable ID is required. | A later run creates a different resource instead of addressing the intended one. | Use ID.custom when the caller must reproduce the ID. |
| A permission combines the wrong action or role. | A user or group can lose required access or gain unintended access. | Build the action with Permission and the audience with Role; review the resulting permission string. |
Role.guests() is used for an authenticated user. | The authenticated user does not match the guest audience. | Use Role.users() or a narrower user or team role when a session is required. |
Key files
| File | Why to read it |
|---|---|
| src/query.ts | Defines the query format, input normalization, builder, and filters, controls, composition, aggregate, join, and specialized operators used by list APIs. |
| src/id.ts | Defines the custom and generated resource ID choices. |
| src/permission.ts | Defines the supported access actions. |
| src/role.ts | Defines the audiences that can receive access. |
| src/…/tables-db.ts | Shows query and permission inputs together on row list and row creation flows. |
| src/…/storage.ts | Shows list filtering and permission inputs on file operations. |
| src/index.ts | Shows the public SDK exports for these helpers. |