softshipper
softshipper

Reputation: 34071

Where to get the Context for DB query?

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:[email protected]/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.

Upvotes: 2

Views: 1022

Answers (1)

damaredayo
damaredayo

Reputation: 1079

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.

Upvotes: 1

Related Questions