🚨 Shai-Hulud Strikes Again:834 Packages Compromised.Technical Analysis →
Socket
Book a DemoInstallSign in
Socket

@satont/grammy-typeorm-storage

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@satont/grammy-typeorm-storage

TypeORM storage for grammy library.

latest
Source
npmnpm
Version
1.0.2
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

TypeORM storage adapter for grammY

Storage adapter that can be used to store your session data with TypeORM when using sessions.

Installation

npm install @satont/grammy-typeorm-storage typeorm --save

Usage

You can check examples folder, or simple use followed code:

Implement the Session entity:

import { Column, createConnection, Entity, PrimaryGeneratedColumn } from 'typeorm';
import { ISession } from "@satont/grammy-typeorm-storage";

@Entity()
export class Session implements ISession {
  @PrimaryGeneratedColumn()
  id: string;

  @Column('varchar')
  key: string

  @Column('text')
  value: string
}

Create bot and pass adapter as storage:

import { Bot, Context, session, SessionFlavor } from "grammy";
import { TypeormAdapter } from "@satont/grammy-typeorm-storage";
import { createConnection } from 'typeorm';

// that import for example class before
import Session from './session'

// write session types
interface SessionData {
  counter: number;
}

// create context for grammy instance
type MyContext = Context & SessionFlavor<SessionData>;

// Create bot and register session middleware
async function bootstrap() {
  // create typeorm connection
  await createConnection({
    name: 'default',
    type: 'better-sqlite3',
    database: ':memory:',
    entities: [Session],
    synchronize: true,
  });

  const bot = new Bot<MyContext>("");
  bot.use(
    session({
      initial: () => ({ counter: 0 }),
      storage: new TypeormAdapter({ repository: getRepository(Session) }),
    })
  );
  
  // Register your usual middleware, and start the bot
  bot.command("stats", (ctx) =>
    ctx.reply(`Already got ${ctx.session.counter} photos!`)
  );
  bot.on(":photo", (ctx) => ctx.session.counter++);
  
  bot.start();

}

FAQs

Package last updated on 27 Dec 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts