2.4.2 • Published 2 months ago

@grammyjs/storage-prisma v2.4.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

Prisma storage adapter for grammY

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

Installation

npm install @grammyjs/storage-prisma --save

Usage

You can check the examples folder, or simply use followed code:

Implement the Session model in your Prisma schema:

model Session {
  id    Int    @id @default(autoincrement())
  key   String @unique
  value String
}

The id field is not needed for this adapter to work.

The only restriction is that key must be a String index, either by adding the @unique keyword or the @id keyword, and value must be a String.

Generate Prisma client using the terminal:

npx prisma generate

Migrate the schema changes to your database:

npx prisma db push
# or
npx prisma migrate dev

Create bot and pass adapter as storage:

import { Bot, Context, session, SessionFlavor } from "grammy";
import { PrismaAdapter } from "@grammyjs/storage-prisma";
import { PrismaClient } from "@prisma/client";

// Create Prisma client
const prisma = new PrismaClient();

// 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() {
  const bot = new Bot<MyContext>("");

  bot.use(
    session({
      initial: () => ({ counter: 0 }),
      storage: new PrismaAdapter(prisma.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();
}
2.4.2

2 months ago

2.4.1

5 months ago

2.3.2

8 months ago

2.4.0

7 months ago

2.3.1

8 months ago

2.3.0

11 months ago

2.2.0

1 year ago

2.1.4

1 year ago

2.1.3

1 year ago

2.1.1

1 year ago

2.1.0

1 year ago

2.0.2

1 year ago

2.0.0

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago