Skip to content

dcodemaxz/makeInMemoryStore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“¦ makeInMemoryStore V1.2

A fully featured in-memory state manager designed as a drop-in replacement for the deprecated makeInMemoryStore from the latest versions of Baileys (WhatsApp Web API library).

Built specifically for WhatsApp Multi-Device bots using the modern event-driven architecture.


πŸš€ Why This Exists

The original makeInMemoryStore was removed from newer versions of Baileys. Many bots still rely on it for:

  • Contact caching
  • Chat indexing
  • Message history tracking
  • Presence updates
  • Group metadata storage
  • Call state tracking
  • Auth state syncing

This module restores that functionality β€” with improvements and modern safeguards.


✨ Features

πŸ“‡ Contacts

  • setContacts()
  • upsertContact()
  • updateContact()
  • deleteContact()

πŸ’¬ Chats

  • setChats()
  • upsertChat()
  • updateChat()
  • deleteChat()

πŸ“¨ Messages

  • setMessages()
  • upsertMessage()
  • updateMessage()
  • deleteMessage()
  • loadMessage()

Supports:

  • Poll updates
  • Message merges (preserves old data)
  • Optional per-chat message limit (memory safeguard)
  • Event emission compatibility

πŸ‘€ Presence Tracking

  • setPresence()
  • updatePresence()

Tracks per-chat and per-participant presence state.

πŸ‘₯ Group Metadata

  • setGroupMetadata()
  • updateGroupMetadata()

πŸ“ž Call Offers

  • setCallOffer()
  • clearCallOffer()

🎨 Sticker Packs

  • setStickerPacks()
  • upsertStickerPack()

πŸ” Authentication State

  • setAuthState()
  • getAuthState()

πŸ”„ History Sync Tracking

  • markHistorySynced()
  • isHistorySynced()

πŸ’Ύ Persistence Helpers

  • load(state)
  • save()
  • clear()

πŸ”— Event Binding

  • bind(ev)
  • unbind(ev) (optional safety cleanup)

Fully compatible with Baileys socket event emitter.


🧠 Architecture Overview

This store:

  • Extends EventEmitter
  • Maintains internal indexed objects for fast lookup
  • Emits events similar to Baileys
  • Supports manual persistence
  • Includes safe guards & validation
  • Optionally limits message cache per chat
  • Works with modern Baileys socket events

⚠️ IMPORTANT:
This is an in-memory store.
All data is lost when the process restarts unless you manually persist it.


πŸ“₯ Installation

Install recommended logger:

npm install pino

Then copy store.js into your project.


βš™οΈ Basic Usage

// Require file
const makeInMemoryStore = require('./store')
const pino = require('pino')

// Create store
const store = makeInMemoryStore({
    logger: pino({ level: 'silent' }),
    maxMessagesPerChat: 500 // optional
})

πŸ”Œ Bind to Baileys Socket

const sock = makeWASocket({ ... })

store.bind(sock.ev)

Once bound, the store will automatically update itself when:

  • Contacts change
  • Messages arrive
  • Groups update
  • Presence updates
  • Call events fire
  • History sync completes

No manual syncing required.

If your socket reconnects or reloads, you may safely call:

store.unbind(sock.ev)

before re-binding to prevent duplicate listeners.


πŸ’Ύ Optional: Manual Persistence

Since this is memory-only, you can persist it manually.

Save to File

const fs = require('fs')

setInterval(() => {
    const state = store.save()
    fs.writeFileSync('./store.json', JSON.stringify(state))
}, 10000)

Load From File

if (fs.existsSync('./store.json')) {
    const data = JSON.parse(fs.readFileSync('./store.json'))
    store.load(data)
}

πŸ“š API Reference

Contacts

store.contacts
store.upsertContact(contact)
store.deleteContact([id])

Chats

store.chats
store.upsertChat(chat)
store.deleteChat([id])

Messages

store.messages
store.loadMessage(jid, messageId)
store.upsertMessage(message)

Presence

store.presences
store.setPresence(chatId, presence)

Groups

store.groupMetadata

Auth

store.getAuthState()

πŸ›  Production Recommendations

If you are running a large-scale bot:

  • Persist store regularly
  • Consider limiting stored messages per chat
  • Monitor memory usage
  • Use proper logging levels (pino supported)

For heavy production bots, you may consider migrating to:

  • Redis
  • MongoDB
  • PostgreSQL

For small–medium bots, this store is extremely fast and efficient.


πŸ§ͺ Recommended Use Cases

βœ… Development
βœ… Testing
βœ… Lightweight bots
βœ… Temporary session bots
⚠️ Not ideal for multi-instance clustering without external sync


πŸ“Š Performance Characteristics

  • O(1) object access
  • Fast message lookup
  • No disk I/O overhead
  • Fully event-driven
  • Minimal dependencies
  • Optional in-memory safeguards for long-running processes

🧩 Compatibility

Designed for:

  • Latest Multi-Device Baileys versions
  • Node.js 16+

πŸ‘¨β€πŸ’» Developer

Maxz
https://linktr.ee/dcodemaxz


πŸ“œ License

Custom implementation inspired by deprecated Baileys store system.
Free for personal and commercial use.

About

This code is to replace the store ( makeInMemoryStore ) which has been removed in the latest Baileys.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors