Home > Back-end >  GetAll() in a cloud.google.com/go/datastore Transaction{}?
GetAll() in a cloud.google.com/go/datastore Transaction{}?

Time:09-17

I need to run an equivalent of this, but in a transaction: db.GetAll(ctx, datastore.NewQuery("Items").Ancestor(pkey), &itemContainers)

But the Transaction{} type does not seem to have a GetAll() method. How can I get this done?

CodePudding user response:

To use queries in transactions you should attach your transaction to the query. E.g.

q = datastore.NewQuery("Items").Ancestor(pkey).Transaction(tx)
db.Getall(ctx, q, &itemContainers)

CodePudding user response:

You don't need transactions for GetAll(), you can simply call it on your *Client variable.

Transactions are for sets of atomic operations that need to be run or rolled back all at once. If you have a set of keys you need to fetch, for example, you'd call GetMulti() on the Transaction type: https://pkg.go.dev/cloud.google.com/go/datastore#Transaction.GetMulti

GetAll can be done with a single operation, it doesn't need to be batched in a transaction: https://pkg.go.dev/cloud.google.com/go/datastore#Client.GetAll

  • Related