lazySession
This is an advanced function of grammY.
Generally speaking, lazy sessions work just like normal sessions—just they are loaded on demand. Except for a few async
s and await
s here and there, their usage looks 100 % identical.
Instead of directly querying the storage every time an update arrives, lazy sessions quickly do this once you access ctx
. This can significantly reduce the database traffic (especially when your bot is added to group chats), because it skips a read and a wrote operation for all updates that the bot does not react to.
ts
// The options are identical
bot.use(lazySession({ storage: ... }))
bot.on('message', async ctx => {
// The session object is persisted across updates!
const session = await ctx.session
// ^
// |
// This plain property access (no function call) will trigger the database query!
})
Check out the documentation on the website to know more about how lazy sessions work in grammY.
Type Parameters
S
ts
S
C
ts
C extends Context
Parameters
options
ts
options: SessionOptions<S, C>
Optional configuration to pass to the session middleware
Return Type
ts
MiddlewareFn<C & LazySessionFlavor<S>>