Returning Values

Fields

Select which fields to return.

.fields(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.

Selects which fields to return from PocketBase. expand() is not needed if fields() is used, we automatically include what to expand.

Modifiers:

  • *: Targets all keys from the specific depth level.
  • :excerpt(maxLength, withEllipsis?): Returns a short plain text version of the field string value.
const query = pbQuery<Post>()
  .fields([
    'title',                       // Basic field
    'content:excerpt(100,true)',   // Field with excerpt modifier
    'author',                      // Relation ID field
    'expand.author',               // Expanded relation field
    'expand.comments_via_post',    // Back-relation expansion
  ])
  .build(pb.filter);

console.log(query.fields); // Output: 'title,content:excerpt(100,true),author,expand.author,expand.comments_via_post'
console.log(query.expand); // Output: 'author,comments_via_post'

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

console.log(records);
// Output:
// [
//   {
//     author: 'authorId',
//     title: 'Football match this Saturday',
//     content: 'Lorem ipsum dolor sit amet.',
//     expand: {
//       author: {
//         name: 'John',
//       },
//       comments_via_post: [
//         { ... },
//       ],
//     },
//   },
// ]
With the key expand.* we can't automatically include what to expand, so you must specify it manually.
const query = pbQuery<Post>()
  .fields(['title', 'expand.*'])
  .expand(['author', 'comments_via_post'])
  .build(pb.filter);

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