kysely-org / kysely

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

Question: AWS Lambda and Connection Pooling (PostgresDialect) #96

Closed SethThomas closed 2 years ago

SethThomas commented 2 years ago

Thank you for the fantastic library!

I'm been experimenting with using kysely instead of Node PG directly. So far, I've found it to be an easy refactor.

I am using kysely in the context of AWS Lambda and RDS for Postgres (non-serverless option). When using node-pg directly, I avoid using the connection Pooling mechanism and opt for using the PG Client directly. This is due to the stateless execution environment of AWS Lambda and how Lambda can "freeze" the execution environment between invocations.

Having a look at the PostgresDialogConfig, I notice there is a required pool parameter. I'm not sure this is what I want.

Is there a way for me to use kysely without a connection pool? Alternatively, is there a recommended way to use kysely in AWS Lambda?

koskimas commented 2 years ago

Can't you simply set the pool minimum and maximum to 1 to only have a single connection? If that's not enough, the dialect interface is quite simple and you can implement your own dialect that only uses a single connection.

const db = new Kysely<Database>({
  dialect: {
    createAdapter() {
      return new PostgresAdapter()
    },
    createDriver() {
      // Here, instead of using the `PostgresDriver`, implement your own driver
      // that always uses the same connection.
    },
    createIntrospector(db: Kysely<unknown>) {
      return new PostgresIntrospector(db)
    },
    createQueryCompiler() {
      return new PostgresQueryCompiler()
    },
  },
})

Here's the driver interface you need to implement.

koskimas commented 2 years ago

This issue may also be of interest to you https://github.com/koskimas/kysely/issues/9

SethThomas commented 2 years ago

That works for me, thank you!

I'm actually using Kysely with Serverless Stack 🙂

Unfortunately, I'm dealing with a legacy RDS Database that prevents me from using Aurora Serverless.

Thanks again for the project, it's been fantastic to work with.

smith558 commented 8 months ago

That works for me, thank you!

I'm actually using Kysely with Serverless Stack 🙂

Unfortunately, I'm dealing with a legacy RDS Database that prevents me from using Aurora Serverless.

Thanks again for the project, it's been fantastic to work with.

How did you solve this? Did you end up staying with SST.dev? Considering using it in production as well for a db heavy application.