grammyjs / storages

Storage adapters for grammY sessions.
48 stars 23 forks source link

feat(denokv): custom prefix support #211

Closed ubertao closed 3 weeks ago

ubertao commented 5 months ago

Feature: allow custom prefix to store session data under specified key space. Currently all session data are stored under ["sessions"] key space. This feature would allow users to supply custom key prefix like this:

new DenoKVAdapter(kv, ["mybot", "server"]);
new DenoKVAdapter(kv, ["mybot", "instances"]);
new DenoKVAdapter(kv, ["mybot", "bot1", "chats"]);
new DenoKVAdapter(kv, ["mybot", "bot2", "chats"]);

Consider the use case where I have a bot server handling updates from multiple bot instances, each bot instance have their own users. This feature enables me to store server level, bot level and chat level data in different key space, so I can manage data more easily, e.g. statistics, storage management, etc.

Satont commented 5 months ago

getSessionKey not enought for you? https://grammy.dev/plugins/session#session-keys

You can prefix session key with whatever you want in this function.

ubertao commented 5 months ago

I know I can concatenate all key parts into one string in getSessionKey.

But I think using separte key parts is recommendated by DenoKV: https://docs.deno.com/kv/manual/key_space

Also, it can avoid some tricky key ordering issue, e.g. with single string key: bot123chat456 < bot45chat678 even though 123 > 45. This can give you unexpected result when you perform a list() on DenoKV keys.

When using key parts like ["bot", 123, "chat", 456], ["bot", 45, "chat", 678] you automatically get the correct order.

In addition, there could be performance impact since DenoKV is backed by FoundationDB. And key prefix is a core feature of FoundationDB, it might afftect the way underlying data is distributed and how it's indexed, for example: https://apple.github.io/foundationdb/layer-concept.html#the-foundationdb-way