npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

baileys-prisma

v1.0.0

Published

Baileys WhatsApp + Prisma ORM: gerenciamento de sessões e multi-instâncias com persistência em PostgreSQL | Session management and multi-instance support

Downloads

69

Readme

Baileys + Prisma ORM 🚀🚀🚀

Gerencie sessões do WhatsApp com Baileys usando Prisma ORM e PostgreSQL.
Manage WhatsApp sessions with Baileys using Prisma ORM and PostgreSQL.

Maintained and developed by Marcelo BRBX.
This is my first official NPM package, designed to simplify WhatsApp Baileys integrations for the community.

A high-level, Clean Architecture–based wrapper for the Baileys library.
This project simplifies WhatsApp integration by handling session management, connection states, and automatic reconnections internally.


✨ Key Features

  • Clean Architecture & SOLID
    Logic separated into Domain, Application, and Infrastructure layers for high maintainability.

  • Persistent Sessions (Prisma ORM)
    Authentication state is safely stored in your database.

  • Automatic Reconnection
    Handles network issues and the Restart Required (515) error internally without crashing the process.

  • Easy Authentication
    Supports both QR Code and Pairing Code (Phone Number) methods.

  • Event-Driven
    Simple callbacks for success, failure, and authentication requirements.

  • Developer Friendly
    Complex Baileys socket logic is hidden behind a clean Facade.


License

MIT

🛠️ Tech Stack

  • Runtime: Node.js / TypeScript

  • WA Library: baileys

  • Database: Prisma ORM

  • Logging: Pino

--------------------------------------------------------

⚠️ Prisma Schema (REQUIRED)

This library will NOT work without the following Prisma model.

You MUST add this model to your schema.prisma:

model Session {
  pkId      BigInt @id @default(autoincrement())
  sessionId String
  id        String
  data      String @db.Text

  @@unique([sessionId, id], map: "unique_id_per_session_id_session")
  @@index([sessionId])
}

📋 Prerequisites (Pre-configuration) Before installing and using npm install baileys-prisma, you must ensure your environment is ready.

Database & Prisma Setup

--------------------------------------------------------

🚀 Getting Started Production

  1. Installation npm install baileys-prisma

  2. Basic Usage You don't need to manage sockets or handle complex reconnection logic. Simply instantiate the library and start.

Observation

  • A unique ID(sessionId) to associate this connection with a specific user in your system.
prisma-client.ts example, create your instance prisma, you need just add Prisma Schema (REQUIRED) in the schema.prisma file.

import "dotenv/config";
import { PrismaPg } from "@prisma/adapter-pg";
import { PrismaClient } from "./generated/prisma/client";

const connectionString = `${process.env.DATABASE_URL}`;

const adapter = new PrismaPg({ connectionString });
const prisma = new PrismaClient({ adapter });

export { prisma };
import { BaileysPrisma, interceptSessionLogs } from "baileys-prisma";
import type { WhatsappInterface } from "baileys-prisma";
import { prisma } from "./prisma-client";

const sessionId = "default";
const phoneNumber = "5521999999998";
const browserName = "Chrome";

const baileysPrisma = new BaileysPrisma(sessionId, browserName);

interceptSessionLogs({
  ClosingSession: () => console.log("🔐 Renovação de chaves de sessão 123"),
  OpeningSession: () => console.log("🟢 Sessão criptográfica aberta 123"),
  RemovingOldClosedSession: () =>
    console.log("🧹 Limpando sessões criptográficas antigas 123"),
  MigratingSessionTo: (code) =>
    console.log("🔄 Migrando estrutura de sessão123:", code),
  SessionAlreadyClosed: () => console.log("⚠️ Sessão já estava encerrada 123"),
  SessionAlreadyOpen: () => console.log("⚠️ Sessão já estava aberta 123"),
  SessionStorageMigrationError: () =>
    console.log("❌ Erro ao migrar armazenamento de sessão criptográfica 123"),
});

const config: WhatsappInterface = {
  basic: {
    sessionId,
    phoneNumber,
    isPairCode: true,
    timeReconnect: 3,
    log: "silent",
  },
  advanced: {
    onAuthRequired: async (code) => {
      console.log("onAuthRequired", code);
    },
    onFail: async (err) => {
      console.log("onFail", err);
    },
    onFatalFail: async (err) => {
      console.log("onFatalFail", err);
    },
    onSuccess: async () => {
      console.log("onSuccess");
    },
    onRestartRequired: async () => {
      console.log("onRestartRequired");
    },
  },
  prisma,
};

await baileysPrisma.start(config);

const phoneExample = "5521999999999";

/* to access native functions baileys
  const baileys = baileysPrisma.baileys;
*/
setInterval(() => {
  if (baileysPrisma.isConnected) {
    baileysPrisma.sendMessage(phoneExample, {
      text: "Hello, World!",
    });
  }
}, 30000);


🔒 Session Logs Interception (Optional)

This library provides a fine-grained log interception system that allows you to handle specific Baileys session events via dedicated callbacks.

If you don't provide these callbacks, the logs will still print normally to the console.

Example: Using Specific Session Callbacks

import { interceptSessionLogs } from "baileys-prisma";

interceptSessionLogs({
  ClosingSession: () => console.log("🔐 Renovação de chaves de sessão"),
  OpeningSession: () => console.log("🟢 Sessão criptográfica aberta"),
  RemovingOldClosedSession: () => console.log("🧹 Limpando sessões criptográficas antigas"),
  MigratingSessionTo: (code) => console.log("🔄 Migrando estrutura de sessão:", code),
  SessionAlreadyClosed: () => console.log("⚠️ Sessão já estava encerrada"),
  SessionAlreadyOpen: () => console.log("⚠️ Sessão já estava aberta"),
  SessionStorageMigrationError: () =>
    console.log("❌ Erro ao migrar armazenamento de sessão criptográfica"),
});

Available Callbacks

Event Description ClosingSession Triggered when the session is being closed. OpeningSession Triggered when the session is being opened. RemovingOldClosedSession Triggered when old closed sessions are being cleaned up. MigratingSessionTo Triggered when the session is being migrated to a new code. SessionAlreadyClosed Triggered when the session is already closed. SessionAlreadyOpen Triggered when the session is already open. SessionStorageMigrationError Triggered when an error occurs during session storage migration.

| Note: If no callback is provided for an event, the log will print normally to the console.

--------------------------------------------------------

🚀 Getting Started Development

  1. Installation Clone the repository and install dependencies:
git clone https://github.com/Developer-Marcelo/baileys-prisma
cd baileys-prisma
npm install

Simple Access: Use the main instance for common features.

Native Access: Access the .others property to interact directly with the native Baileys API.

🏗️ Architecture Overview The system follows Dependency Inversion from SOLID:

  1. BaileysFactory: Orquestrates the creation of the socket, repository, and service.

  2. WhatsappConnectionService: Contains the business logic for managing connection states (Application Layer).

  3. BaileysProvider: An infrastructure adapter that isolates the baileys library from the rest of the code.

  4. Prisma Repository: Handles the persistence of creds.json directly into the database.

Option,Type,Description

sessionId,string,Unique identifier for the WhatsApp instance.
isPairCode,boolean,"If true, uses the 8-digit pairing code. If false, generates a QR Code."
timeReconnect,number,Seconds to wait before attempting a restart after a 515 error.
browserName,string,"The browser agent name (e.g., ""Chrome"", ""Firefox"")."