Defining queries
query
Define a public query function that can be called from clients.Argument validation object using validators from
convex/values. Maps argument names to their validators.Return value validator. Helps catch bugs and provides better type safety.
internalQuery
Define an internal query that can only be called from other Convex functions, not directly from clients.- Queries called from actions or scheduled functions
- Shared query logic that shouldn’t be exposed to clients
- Administrative queries
Query context
QueryCtx
The context object passed to query handlers.Read-only database interface. See Database API for details.
Authentication interface to get the current user’s identity.
Read-only file storage interface. See Storage API for details.
Call another query function within the same read snapshot.Note: Often you can extract shared logic into a helper function instead.
runQuery incurs overhead of running argument and return value validation.Function shorthand
You can also define queries using function shorthand syntax:Best practices
Use indexes for efficient queries
Always use
.withIndex() instead of .filter() when querying by specific fields. Filters scan all documents, while indexes efficiently skip non-matching documents.Add argument validation
For security, always add argument validation to public queries in production apps.
Limit result sets
Use
.take(n), .first(), .unique(), or pagination instead of .collect() when result sets can grow unbounded.Queries are reactive
When used with
useQuery on the client, queries automatically re-run when their results change.