kysely-org / kysely

A type-safe typescript SQL query builder
https://kysely.dev
MIT License
10.44k stars 266 forks source link

[QUESTION] Best way to use Kysely with cloudfare workers #369

Closed franfdezmorales closed 1 year ago

franfdezmorales commented 1 year ago

Hello!

I am doing a small side project, currently cloudfare workers has been migrated to an ES module system, with this change, the environment variables can only be accessed through the handler, is it correct that every time a route is called a Kysely instance is generated?

app.get('/', async (c) => {
    const kysely = new Kysely<Database>({
        dialect: new PlanetScaleDialect({
            url: c.env.DATABASE_URL
        })
    })
    //You do something with the kysely instance...
    return c.text('Hello!')
})
igalklebanov commented 1 year ago

Hey 👋

If environment variables are only accessible this way, you don't have other options.

Also, the docs do not recommend using/mutating global state, unlike other FaaS offerings where it is considered best practice to manage connections/pools in global scope (AWS lambdas I'm looking at you).

AFAIK, the underlying planetscale driver used is making calls over fetch API and is stateless, so no real harm in instantiating many instances.

franfdezmorales commented 1 year ago

Great, thank you very much for the quick response, very grateful for the work you are doing. Feel free to close the issue if you want to.