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

mysql-baileys

v1.5.1

Published

Implementation of MySQL in Baileys.

Downloads

435

Readme

Authentication with MySQL for Baileys

npm github baileys license CodeFactor Grade GitHub Issues or Pull Requests

Usage

1. Create table in MySQL (optional)

If you want with your specifications, if you don't create it, the code will automatically create

CREATE TABLE `auth` (
	`session` varchar(50) NOT NULL,
	`id` varchar(100) NOT NULL,
	`value` json DEFAULT NULL,
	UNIQUE KEY `idxunique` (`session`,`id`),
	KEY `idxsession` (`session`),
	KEY `idxid` (`id`)
) ENGINE=MyISAM

2. Install mysql-baileys

Edge Version:

npm i github:bobslavtriev/mysql-baileys

Stable Version:

npm i bobslavtriev/mysql-baileys

3. Import code

const { useMySQLAuthState } = require('mysql-baileys')

4. Implement code

const { state, saveCreds, removeCreds } = await useMySQLAuthState({
	session: sessionName, // required
	password: 'Password123#', // required
	database: 'baileys', // required
})

5. All parameters for useMySQLAuthState()

type MySQLConfig = {
	/* The hostname of the database you are connecting to. (Default: localhost) */
	host?: string,
	/* The port number to connect to. (Default: 3306) */
	port?: number,
	/* The MySQL user to authenticate as. (Default: root) */
	user?: string,
	/* The password of that MySQL user */
	password: string,
	/* Alias for the MySQL user password. Makes a bit more sense in a multifactor authentication setup (see "password2" and "password3") */
	password1?: string,
	/* 2nd factor authentication password. Mandatory when the authentication policy for the MySQL user account requires an additional authentication method that needs a password. */
	password2?: string,
	/* 3rd factor authentication password. Mandatory when the authentication policy for the MySQL user account requires two additional authentication methods and the last one needs a password. */
	password3?: string,
	/* Name of the database to use for this connection. (Default: base) */
	database: string,
	/* MySql table name. (Default: auth) */
	tableName?: string,
	/* Retry the query at each interval if it fails. (Default: 200ms) */
	retryRequestDelayMs: number,
	/* Maximum attempts if the query fails. (Default: 10) */
	maxtRetries?: number,
	/* Session name to identify the connection, allowing multisessions with mysql. */
	session: string,
	/* The source IP address to use for TCP connection. */
	localAddress?: string,
	/* The path to a unix domain socket to connect to. When used host and port are ignored. */
	socketPath?: string,
	/* Allow connecting to MySQL instances that ask for the old (insecure) authentication method. (Default: false) */
	insecureAuth?: boolean,
	/* If your connection is a server. (Default: false) */
	isServer?: boolean,
	/* Use the config SSL. (Default: disabled) */
	ssl?: string | SslOptions
}

Complete code for use

const { makeWASocket, makeCacheableSignalKeyStore, fetchLatestBaileysVersion } = require('@whiskeysockets/Baileys')
const { useMySQLAuthState } = require('mysql-baileys')

async function startSock(sessionName){
	const { error, version } = await fetchLatestBaileysVersion()

	if (error){
		console.log(`Session: ${sessionName} | No connection, check your internet.`)
		return startSock(sessionName)
	}

	const { state, saveCreds, removeCreds } = await useMySQLAuthState({
		session: sessionName,
		host: 'localhost',
		port: 3306,
		user: 'bob',
		password: 'Password123#',
		database: 'baileys',
		tableName: 'auth'
	})

	const sock = makeWASocket({
		auth: {
			creds: state.creds,
			keys: makeCacheableSignalKeyStore(state.keys, logger),
		},
		version: version,
		defaultQueryTimeoutMs: undefined
	})

	sock.ev.on('creds.update', saveCreds)

	sock.ev.on('connection.update', async({ connection, lastDisconnect }) => {
		// your code here
	})

	sock.ev.on('messages.upsert', async({ messages, type }) => {
		// your code here
	})
}

startSock('session1')

If you want to start other sessions in the same code, use this:

startSock('session1')
startSock('session2')
startSock('session3')
startSock('session4')