Client
`query`
`vars`

Variables

Variables in Triplit allow you to pass in preset values into queries. They consist of a scope (to prevent collisions) and a dot (.) separated path to reference data in the variable.

Query variables

Query variables are prefixed with the query scope and are accessible just to the query they are defined on. They are defined with the vars method in the query builder. For example:

const baseQuery = client.query('employees').where([
  ['team', '=', 'Delivery Crew'],
  ['name', '=', '$query.name'],
]);
const fryQuery = baseQuery.vars({ name: 'Philip J. Fry' });
const leelaQuery = baseQuery.vars({ name: 'Turanga Leela' });

This can help prevent writing the same query multiple times with different values.

Global variables

Global variables are prefixed with the global scope and are accessible to all queries in the database. They are defined in the client constructor or via the updateGlobalVariables method. For example:

const client = new TriplitClient({ variables: { name: 'Philip J. Fry' } });
let query = client.query('employees').where('name', '=', '$global.name'); // resolves to 'Philip J. Fry'
 
client.db.updateGlobalVariables({ name: 'Turanga Leela' });
query = client.query('employees').where('name', '=', '$global.name'); // resolves to 'Turanga Leela'

Session variables

Session variables are prefixed with the session scope and are accessible to all queries in that database session. When authenticating with a Triplit server, the server will assign all claims on the JWT to session variables.

Role variables

When determining access control rules with roles, you can use role variables to reference values from a client's token in your permission definitions. Role variables are prefixed with the role scope.