Skip to content
Roman edited this page Feb 15, 2024 · 7 revisions

RateLimiterPrisma

Tested with Prisma version ^5.8.0. Note, model table should be already created.

const http = require('http');
const { RateLimiterPrisma } = require('rate-limiter-flexible');
const { PrismaClient } = require('@prisma/client');

const prisma = new PrismaClient();

const rateLimiter = new RateLimiterPrisma({
  storeClient: prisma,
  points: 3, // Number of points
  duration: 5, // Per second(s)
})

const rlRes = await rateLimiter.consume('user_id', 1) // consume 1 point

The full documentation is on Wiki.

Schema example

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = "postgres://root:secret@localhost:5432"
}

model RateLimiterFlexible {
  key     String   @id
  points  Int
  expire  DateTime?
}

Commands to run before the first launch

  1. Generate Prisma client with

    prisma generate --schema=./schema.prisma

  2. Create table or collections with

    prisma db push --schema=./src/schema.prisma