Databases in DBM Host: SQLite, PostgreSQL, MySQL and more
Your bot can keep its data in a database: levels, warnings, settings for each server. DBM Host gives you two databases that live inside the phone, and lets you connect your own database hosted somewhere else.
Everything is in the app: open your bot, then Startup, then the Database section.
Databases inside the phone
They need no server and no account. The data stays in the bot files, so it is saved with the backup on disbmhost.com and follows the bot to another phone.
SQLite
Ready to use, nothing to install. It works with better-sqlite3 and sqlite3 in JavaScript, and with sqlite3 in Python.
const Database = require('better-sqlite3');
const db = new Database('data.db');
db.exec('CREATE TABLE IF NOT EXISTS users (id TEXT PRIMARY KEY, xp INTEGER)');PostgreSQL
A real PostgreSQL that runs inside your bot, with @electric-sql/pglite. Nothing to install, it is already in the app. It is made for JavaScript and TypeScript bots.
const { PGlite } = require('@electric-sql/pglite');
const db = new PGlite('./data/postgres');
const { rows } = await db.query('SELECT $1::text AS hello', ['world']);Good to know: an empty PostgreSQL database uses about 40 MB of storage, and the first start takes a few seconds. SQLite is lighter if you only store a little data.
Your own database
You can connect a MySQL, PostgreSQL, MongoDB or Redis database hosted elsewhere, for example on a free plan from a database host.
In Database, pick the type of your database
Paste its connection URL. Your host gives it to you, it looks like
mysql://user:password@host:3306/databasePress Test the connection. The app checks that the phone can reach the server
Press Save. The URL is kept as a secret variable, like your token
Press Add to install the package your code needs, then restart the bot
Your code reads the URL from process.env, or os.environ in Python. Never write the password in your files.
Variable and package for each database
Database | Variable | JavaScript | Python |
|---|---|---|---|
MySQL |
|
|
|
PostgreSQL |
|
|
|
MongoDB |
|
|
|
Redis |
|
|
|
Examples
MySQL
const mysql = require('mysql2/promise');
const db = mysql.createPool(process.env.DATABASE_URL);
const [rows] = await db.query('SELECT 1 + 1 AS two');PostgreSQL
const { Pool } = require('pg');
const db = new Pool({ connectionString: process.env.DATABASE_URL });
const { rows } = await db.query('SELECT NOW()');MongoDB
const { MongoClient } = require('mongodb');
const client = new MongoClient(process.env.MONGODB_URI);
await client.connect();
const users = client.db('bot').collection('users');Redis
const Redis = require('ioredis');
const redis = new Redis(process.env.REDIS_URL);
await redis.set('hello', 'world');If the test fails
timeout: the server does not answer. Many hosts only accept known addresses: allow all addresses (0.0.0.0/0) in your database settings, because a phone changes address often
ECONNREFUSED: the address is right but the port is wrong, or the database is stopped
ENOTFOUND: the host name in the URL has a typo
This URL is not valid: the URL must start with mysql://, postgresql://, mongodb://, mongodb+srv://, redis:// or rediss://
The test only checks that the server answers. If it works but your bot still cannot log in, check the user name and the password in the URL.
