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 🙏

© 2024 – Pkg Stats / Ryan Hefner

postgres-baileys

v1.5.0

Published

A simple package to save your @whiskeysockets/baileys session to a PostgreSQL database.

Downloads

16

Readme

postgres-baileys

A robust and reliable Node.js package designed to seamlessly persist your @whiskeysockets/baileys WhatsApp session data within a PostgreSQL database.

Key Benefits

  • Persistent Sessions: Maintain uninterrupted WhatsApp bot connectivity, even across server restarts or crashes
  • Scalability: PostgreSQL's robust architecture supports handling large volumes of session data as your bot usage grows.
  • TypeScript Support: Leverages TypeScript for enhanced type safety and improved code maintainability.

Installation

Install the package using npm or yarn:

npm install postgres-baileys

Getting Started

import { makeWASocket } from "@whiskeysockets/baileys";
import { usePostgreSQLAuthState } from "postgres-baileys"; 

const postgreSQLConfig = {
  host: 'your-postgresql-host',
  port: 5432, 
  user: 'your-postgresql-user',
  password: 'your-postgresql-password',
  database: 'your-postgresql-database',
};

async function main() {
  try {
    const { state, saveCreds } = await usePostgreSQLAuthState(postgreSQLConfig, "your-unique-session-id");

    const sock = makeWASocket({
      printQRInTerminal: true,
      auth: state
    });

    sock.ev.on("creds.update", saveCreds); 

    console.log("WebSocket connected");
  } catch (error) {
    console.error("Error:", error);
  }
}

main();

OR

import { Pool } from 'pg';
import { usePostgreSQLAuthState } from './path-to-your-module'; // Adjust the path accordingly

// Assuming you have an already connected Pool instance
const pool = new Pool({
    host: 'your-host',
    port: 5432, // or your specific port
    user: 'your-username',
    password: 'your-password',
    database: 'your-database',
    ssl: true, // or your specific SSL configuration
});

// Define a session ID for this connection
const sessionId = 'unique-session-id';

async function main() {
    // Use the PostgreSQL authentication state
    const { state, saveCreds, deleteSession } = await usePostgreSQLAuthState(pool, sessionId);

    // Now, you can interact with the state, save credentials, or delete the session
    console.log('Initial Authentication State:', state);

    // Example: Save the current credentials
    await saveCreds();

    // Example: Delete the session when needed
    // await deleteSession();
}

main().catch(console.error);

Core Concepts

  1. PostgreSQL Configuration: Provide accurate connection details for your PostgreSQL database.
  2. Session ID: A unique identifier for your WhatsApp session. Use a consistent ID to resume the same session across restarts.
  3. usePostgreSQLAuthState: Fetches or initializes session data from the database, returning the current state and a saveCreds function.
  4. makeWASocket: Create your Baileys connection, passing in the retrieved state.
  5. creds.update Event: Listen for this event to automatically persist updated credentials to the database using the saveCreds function.

Advanced Usage

import { initAuthCreds } from "postgres-baileys";

// ...

// Manual credential initialization (optional)
const authCreds = initAuthCreds(); 

// ... (Use authCreds in your Baileys configuration if needed)

API Reference

  • usePostgreSQLAuthState(config, sessionId)

    • config: PostgreSQL connection configuration object
    • sessionId: Unique string identifier for your session
    • Returns:
      • state: The current authentication state or a newly initialized one.
      • saveCreds: A function to save updated credentials to the database
  • initAuthCreds()

    • Returns: A freshly generated set of Baileys authentication credentials.

Important Considerations

  • Database Setup: Ensure your PostgreSQL database is set up and accessible.
  • Error Handling: Implement robust error handling, especially for database connection issues.