How to get a dict result when querying on an index?

I have an index which a bunch of values I would like to return e.g.:

CreateIndex({
  name: 'users_data',
  source: Collection('users'),
  values: [
    { field: [ 'data', 'first_name'] },
    { field: [ 'data', 'last_name'] },
    { field: [ 'data', 'email'] },
    { field: [ 'data', 'mobile'] },
    { field: [ 'data', 'nationality'] },
    { field: [ 'data', 'role'] }
  ],
  unique: true,
  serialized: true,
})

If I run the a paginated match query the results are like so::

    {
      data: [
        [
          'Chauncey',
          "O'Kon",
          'adrianna_gibson@gmail.com',
          '1-960-902-4300 x0708',
          'Iran',
          'user'
        ],
        [
          'Dominic',
          'Erdman',
          'carson52@gmail.com',
          '(263) 209-3505',
          'El Salvador',
          'user'
        ],
        [
          'Justyn',
          'Torp',
          'marlin.greenfelder@hotmail.com',
          '297-751-7469 x9155',
          'Chad',
          'admin'
        ]
      ]
    }

Query:

    Map(Paginate(Match(Index('users_data'))), Lambda('ref', Var('ref')))

Is it possible to query it so the results are formatted like a dict e.g.:

{
  data: [
    {
      first_name: 'name'
      ...
    }
  ]
}

Honestly in my ideal scenario I would just like it to look like when I retrieve a document via get just witht he fields I specified.

Almost there! When you use Map you can return an object from the Lambda in the shape you want. Also, since your Index has 6 values defined, the Lambda will accept an array of 6 arguments.

Map(
  Paginate(Match(Index('users_data'))), 
  Lambda(
    ['first_name', 'last_name', 'email', 'mobile', 'nationality', 'role'],
    {
      first_name: Var('first_name'),
      last_name: Var('last_name'),
      email: Var('email'),
      mobile: Var('mobile'),
      nationality: Var('nationality'),
      role: Var('role'),
    }
  )
)

Be certain you want to perform general compute on Fauna

Here’s the catch: While it is convenient for your application to accept results in the exact form you want it, now you have to pay for it:

  • Latency: the extra time to compute the transformation as well as transferring the additional payload over the internet
  • Cost: Compute operations to transform your data

In this particular case, it might not be so bad. Each invocation of the Lambda would be 1/50th a Compute Op, and Var doesn’t add toward Compute Ops. For example, for each default page size of 64 user Documents, you’d probably see 2 additional Compute Ops than you would if you just returned the Page without transformation. Perhaps this is acceptable.

In the general case, I would advocate for not performing transformations in Fauna queries unless you are using that data in other operations within the same transaction. It’s much more efficient to transform data like this in your driver.

But as is always true, it’s up to you and your use case. Maybe you want to make the API more generic across services, or integration with an existing GraphQL service, or any number of things. Just keep these things in mind :slight_smile:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.