Databases
Just as providers can be easily exchanged between adapters, we can do the same with the database. Now the important thing to understand is how it works. The main purpose of the database inside the bot is to provide the bot with a record of the different events that have occurred between different conversations.
Many people use it as a chat history (in fact, it can also be used for that purpose), but you may find strange data in your logs because it stores not only messages, but also events.
Each database may need to adjust the access keys, configuration, among other properties that will be implemented as configuration of the implemented class.
import { MemoryDB } from "@builderbot/bot";
export type IDatabase = typeof MemoryDB
export const adapterDB = new MemoryDB();
Below you will find more information about each of these databases.
Memory
The Memory database, often called a Mock database, operates without storing bot-generated responses and needs no configuration. Primarily used for testing and development, it offers a lightweight and convenient solution. This database type is ideal for scenarios where data persistence isn't necessary, facilitating quick prototyping and efficient debugging without managing persistent data storage overhead.
import { MemoryDB } from "@builderbot/bot";
export type IDatabase = typeof MemoryDB
export const adapterDB = new MemoryDB();
Json
JSON database provides the benefit of securely storing bot-generated responses in a local file, ensuring durability across sessions. Utilizing this database type requires specifying a file path (filename
) where the JSON data will be stored. It proves particularly valuable when preserving conversation history or user interactions is imperative.
By enabling structured data storage, it simplifies data retrieval and analysis, rendering it appropriate for applications where data persistence and retrieval play a critical role.
import { JsonFileDB } from '@builderbot/database-json';
export type IDatabase = typeof JsonFileDB
export const adapterDB = new JsonFileDB({ filename: 'db.json' });
Mongo
MongoDB strength lies in its flexible document-oriented structure, which requires configuration parameters like the database URI and name (dbUri
, dbName
). Its scalable architecture provides robust storage capabilities, ideal for handling large data volumes.
By embracing a NoSQL approach, MongoDB offers flexibility in schema design, effortlessly accommodating evolving application needs. It proves particularly suitable for environments requiring high-performance data storage and retrieval, thanks to its efficient indexing and querying functionalities.
import { MongoDB } from '@builderbot/database-mongo'
export type IDatabase = typeof MongoDB
export const adapterDB = new MongoDB({
dbUri: MONGO_DB_URI,
dbName: MONGO_DB_NAME,
})
MySQL
MySQL database, a widely embraced relational database management system, provides strong data storage capabilities for storing bot-generated responses. When integrating with MySQL, essential parameters such as host
, user
, password
, and the database
name must be specified. This database variant assures data durability and scalability, rendering it well-suited for applications demanding high-performance data storage and retrieval.
With MySQL, developers can harness advanced querying features and transaction support, facilitating efficient management of bot-generated data within a structured and secure framework.
import { MysqlDB } from '@builderbot/database-mysql'
export type IDatabase = typeof MysqlDB
export const adapterDB = new MysqlDB({
host: MYSQL_DB_HOST,
user: MYSQL_DB_USER,
database: MYSQL_DB_NAME,
password: MYSQL_DB_PASSWORD,
})
Postgres
PostgreSQL database, celebrated for its reliability and cutting-edge features, presents formidable data storage solutions for bot-generated responses. Seamless integration with PostgreSQL entails specifying vital parameters such as host
, user
, password
, database
name, and port
. This database variant guarantees data integrity and scalability, rendering it optimal for applications requiring top-tier data storage and retrieval performance.
Uses Cases
If you want connect your bot to cloud database like Supabase or any CloudProvider you can use it.
PostgreSQL's flexible architecture and endorsement of advanced data types empower developers to craft sophisticated bots endowed with extensive functionality. Leveraging PostgreSQL equips developers with potent querying capabilities, transactional support, and comprehensive data management features, fostering the creation of resilient and efficient bot systems.
import { PostgreSQLDB } from '@builderbot/database-postgres'
export type IDatabase = typeof PostgreSQLDB
export const adapterDB = new PostgreSQLDB({
host: POSTGRES_DB_HOST,
user: POSTGRES_DB_USER,
database: POSTGRES_DB_NAME,
password: POSTGRES_DB_PASSWORD,
port: +POSTGRES_DB_PORT,
})
Custom Database
Custom database connector there is the possibility to build your own customized adapter, we know that there are many more database providers that can be very useful, an example can be Firebase Console which apart from giving us the possibility to interact via API Rest also offers a Dashboard to visualize your data.
import { createBot, createProvider, createFlow, addKeyword } from '@builderbot/bot'
import { BaileysProvider as Provider } from '@builderbot/provider-baileys'
import { FirebaseAdapter } from './database/firebase'
const PORT = process.env.PORT ?? 3008
const welcomeFlow = addKeyword<Provider, FirebaseAdapter>(['hi'])
.addAnswer('Ey! welcome')
const main = async () => {
const adapterFlow = createFlow([welcomeFlow])
const adapterProvider = createProvider(Provider)
const adapterDB = new FirebaseAdapter({
databaseURL: 'YOUR URL FIREBASE REALTIME DATABASE',
pathPrivateKeyJson: "YOUR PATH CREDENTIALS JSON FIREBASE"
})()
const { httpServer } = await createBot({
flow: adapterFlow,
provider: adapterProvider,
database: adapterDB,
})
httpServer(+PORT)
}
main()