Where to get the Context for DB query?

Viewed 188

I am using https://entgo.io/ as entity framework and https://echo.labstack.com/ as web framework.

Consider the following code snippet:


func main() {

    client, err := Open("postgresql://pgadmin:pgpw@127.0.0.1/nfeed")
    if err != nil {
        log.Fatal(err.Error())
        return
    }
    ctx := context.Background()

    if err := client.Schema.Create(ctx); err != nil {
        log.Fatal(err)
        return
    }


    e := echo.New()


    e.GET("/hashtags", func(c echo.Context) error {
        pattern := c.QueryParam("pattern")
        if len(pattern) == 0 {
            return c.JSON(http.StatusNotFound, err)
        }

        hs, err := client.Hashtag.Query().All(????)


        return c.JSON(http.StatusOK, "Hello")
    })
    e.Logger.Fatal(e.Start(":3000"))
}

The function call client.Hashtag.Query().All(context) expects https://pkg.go.dev/context as parameter.

The question is, how or where can I get the context? In my opinion, the Echo framework should provide me a Context. Unfortunately, I could not found within the Echo module.

1 Answers

You define a context variable on the line ctx := context.Background(), which is a valid context type, there's good documentation on this in your own link https://pkg.go.dev/context.

There's no reason that wouldn't work, if you need more then you can check the documentation on how to make a more in depth context variable.

Related