예제 #1
0
// newDatastoreDB creates a new BookDatabase backed by Cloud Datastore.
// See the cloud and google packages for details on creating a suitable context:
// https://godoc.org/google.golang.org/cloud
// https://godoc.org/golang.org/x/oauth2/google
func newDatastoreDB(client *datastore.Client) (BookDatabase, error) {
	ctx := context.Background()
	// Verify that we can communicate and authenticate with the datastore service.
	t, err := client.NewTransaction(ctx)
	if err != nil {
		return nil, fmt.Errorf("datastoredb: could not connect: %v", err)
	}
	if err := t.Rollback(); err != nil {
		return nil, fmt.Errorf("datastoredb: could not connect: %v", err)
	}
	return &datastoreDB{
		client: client,
	}, nil
}