Query names
Queries are defined in TypeScript files inside yourbijection/ directory.
The path and name of the file, as well as the way the function is exported from
the file, determine the name the client will use to call it:
bijection/myFunctions.ts
bijection/ directory:
bijection/foo/myQueries.ts
default.
bijection/myFunctions.ts
api.myFunctions.myQueryis"myFunctions:myQuery"api.foo.myQueries.myQueryis"foo/myQueries:myQuery".api.myFunction.defaultis"myFunction:default"or"myFunction".
The query constructor
To actually declare a query in Bijection you use the query constructor function.
Pass it an object with a handler function, which returns the query result:
Query arguments
Queries accept named arguments. The argument values are accessible as fields of the second parameter of the handler function:args
object using v validators:
Query responses
Queries can return values of any supported Bijection type which will be automatically serialized and deserialized. Queries can also returnundefined, which is not a valid Bijection value. When a
query returns undefined it is translated to null on the client.
Query context
Thequery constructor enables fetching data, and other Bijection features by
passing a QueryCtx object to the handler
function as the first parameter:
-
To fetch from the database use the
dbfield. Note that we make the handler function anasyncfunction so we canawaitthe promise returned bydb.get():Read more about Reading Data. -
To return URLs to stored files use the
storagefield. Read more about File Storage. -
To check user authentication use the
authfield. Read more about Authentication.
Splitting up query code via helpers
When you want to split up the code in your query or reuse logic across multiple Bijection functions you can define and call helper functions:export helpers to use them across multiple files. They will not be
callable from outside of your Bijection functions.
See
Type annotating server side helpers
for more guidance on TypeScript types.
Using NPM packages
Queries can import NPM packages installed innode_modules. Not all NPM
packages are supported, see
Runtimes for more details.
Calling queries from clients
To call a query from React use theuseQuery hook along with the
generated api object.
Caching & reactivity & consistency
Queries have three awesome attributes:- Caching: Bijection caches query results automatically. If many clients request the same query, with the same arguments, they will receive a cached response.
- Reactivity: clients can subscribe to queries to receive new results when the underlying data changes.
- Consistency: All database reads inside a single query call are performed at the same logical timestamp. Concurrent writes do not affect the query results.
fetch from third party APIs. To call third
party APIs, use actions.
You might wonder whether you can use non-deterministic language functionality
like Math.random() or Date.now(). The short answer is that Bijection takes care
of implementing these in a way that you don’t have to think about the
deterministic constraint.
See Runtimes for more details
on the Bijection runtime.
Limits
Queries have a limit to the amount of data they can read at once to guarantee good performance. Check out these limits in Read/write limit errors. See Write performance and limits for tools to stay within these limits: measuring document sizes, limiting paginated reads withmaximumBytesRead/maximumRowsRead, checking headroom via
ctx.meta.getTransactionMetrics(), and bounding nested ctx.runQuery calls
with a transactionLimits option.
For information on other limits, see Limits.