Go - Collection.Query.Fetch()

Retrieve a page of results for a query. This is an alternative to Collection.Query.Stream()

import (
  "context"
  "fmt"

  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  profiles, err := nitric.NewCollection("profiles").With(nitric.CollectionReading, nitric.CollectionWriting, nitric.CollectionDeleting)
  if err != nil {
    return
  }

  result, err := profiles.Query().Fetch(context.TODO())
  if err != nil {
    return
  }

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}

Parameters

  • Name
    ctx
    Required
    Required
    Type
    context
    Description

    The context of the call, used for tracing.

Examples

Paging through results from a query

import (
  "context"
  "fmt"

  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  profiles, err := nitric.NewCollection("profiles").With(nitric.CollectionReading, nitric.CollectionWriting, nitric.CollectionDeleting)
  if err != nil {
    return
  }

  result, err := profiles.Query().Fetch(context.TODO())
  if err != nil {
    return
  }

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}
import (
  "context"
  "fmt"

  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  profiles, err := nitric.NewCollection("profiles").With(nitric.CollectionReading)
  if err != nil {
    return
  }

  query := profiles.Query()

  ctx := context.TODO()

  results, err := query.Fetch(ctx)
  if err != nil {
    return
  }

  for len(results.Documents) > 0 || results.PagingToken != nil {
    for _, doc := range results.Documents {
      fmt.Println(doc)
    }

    if results.PagingToken != nil {
      results, err = query.FromPagingToken(results.PagingToken).Fetch(ctx)
      if err != nil {
        fmt.Println(err)
        return
      }
    }
  }

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}

See also