select
To specify which attributes you want to return, you can use the select
method. This method accepts a list of attribute names for the collection as arguments.
const query = client.query('users').select(['id', 'name', 'email', 'dob']);
If the type you are selecting is a record, you may also select a specific attribute of the record by using dot notation. The result will be an object with just the selected keys.
const query = client
.query('users')
.select(['id', 'address.street', 'address.city']);
// {id: 'abc', address: {street: '123 Main St', city: 'New York'}}
If you do not call select on a query, all attributes are selected.
Selecting related entities
If you have defined a relation in your schema using RelationById
, RelationOne
, or RelationMany
, you can choose to select entities defined by the relation in a query.
For example, the following schema defines a relation between users
and messages
const schema = {
users: S.Schema({
id: S.Id(),
name: S.Id(),
email: S.String(),
}),
messages: S.Schema({
id: S.Id(),
text: S.String(),
sender_id: S.String(),
sender: S.RelationById('users', '$sender_id'),
}),
};
By default, a query on messages
will not include the sender
as an attribute. To include the sender, use the include
method in the query builder.
const query = client.query('messages').include('sender');
/*
{
id: '1',
text: 'hello world!',
sender_id: 'bob',
sender: { id: 'bob', name: 'Bob Jones', email: 'bob@triplit.com' },
};
*/