Database storage adapter that can be used to store your session data in Supabase database when using sessions.
npm install @grammyjs/storage-supabase --save
To get started, you first need to
-
Have both
@supabase/supabase-js
andgrammy
installed -
Have a defined table for sessions in supabase will the following informations:
-
id
as a primary key of typevarchar
, cannot be null -
session
astext
. Make it nullable.
You could also add
created_at
andupdated_at
to keep track of changes. -
You can check examples folder for full blown usage, or see a simple use case below:
import { Bot, Context, session, SessionFlavor } from 'grammy';
import { supabaseAdapter } from '@grammyjs/storage-supabase';
import { createClient } from '@supabase/supabase-js';
interface SessionData {
counter: number;
}
type MyContext = Context & SessionFlavor<SessionData>;
const URL = 'http://localhost:3000';
const KEY = 'some.fake.key';
// supabase instance
const supabase = createClient(URL, KEY);
//create storage
const storage = supabaseAdapter({
supabase,
table: 'session', // the defined table name you want to use to store your session
});
// Create bot and register session middleware
const bot = new Bot<MyContext>(''); // <-- put your bot token here
bot.use(
session({
initial: () => ({ counter: 0 }),
storage,
}),
);
// Display total stats of images uploaded so far
bot.command('stats', (ctx) => ctx.reply
(`Already got ${ctx.session.counter} images!`));
// Collect statistics of photos uploaded
bot.on(':photo', (ctx) => ctx.session.counter++);
bot.start();