Returning Values

Expand

Expands information from related collections.

.expand(keys)

Since v0.3.0

Starter, Once - This can only be used once, at the start.

Accepts a single key or an array of keys.

Expands information from related collections. expand() is not needed if fields() is used, we automatically include what to expand. If used together with fields(), it overrides the automatic expansion.

Notes:

  • Supports up to 6-levels depth nested relations expansion.
  • The expanded relations will be appended to the record under the expand property (e.g. "expand": { "relField1": { ... }, ... }).
  • Only the relations to which the request user has permissions to view will be expanded.

Read more about expand in the official documentation.

const query = pbQuery<Post>()
  .expand([
    'author',
    'comments_via_post',
  ])
  .build(pb.filter);

console.log(query.fields); // Output: ''
console.log(query.expand); // Output: 'author,comments_via_post'

const records = await pb.collection('posts').getList(1, 20, query);

console.log(records);
// Output:
// [
//   {
//     expand: {
//       author: { ... },
//         comments_via_post: [
//           { ... },
//         ],
//       },
//     ...,
//   },
// ]