grammyjs / storages

Storage adapters for grammY sessions.
51 stars 29 forks source link

Cloudflare D1 adapter is inefficient - calls "create table if not exists" on instantiation #230

Open treasureseth opened 3 months ago

treasureseth commented 3 months ago

The Cloudflare D1 Adapter has a private constructor and the only way to instantiate it is to call the static create method. That method internally calls init, which makes a "create table if not exists" query.

This means that simply instantiating the session storage driver would trigger this query - so effectively it pollutes the whole app. Instead, we should run a migration on setup and remove this superfluous query on instantiation.

https://github.com/grammyjs/storages/blob/216ee4f44900834d98b272b7a5c37d1c94f5b6bc/packages/cloudflare/src/d1.ts#L13-L30

Satont commented 3 months ago

Is this kind of expensive operation for cloudflare? I mean usually those sql things takes small ms.

But anyways we can drop this thing and instead move those queries into readme to setup section.

Are you wanna make PR?

treasureseth commented 3 months ago

I think the problem is more widespread. Many other adapters follow the same pattern of private constructor and force you to fire a create table if not exists query.

Here's the psql adapter that actually fires 2 extra queries: https://github.com/grammyjs/storages/blob/216ee4f44900834d98b272b7a5c37d1c94f5b6bc/packages/psql/src/mod.ts#L22-L41