Bot Database Module

3.4.5 · active · verified Wed Apr 22

The `bot-database` package, currently at stable version 3.4.5, provides a data persistence layer specifically designed for bot projects. It primarily integrates with MongoDB through the Mongoose ORM, abstracting common database operations to simplify data management within bot applications. This library likely offers pre-defined schemas or convenience methods tailored for typical bot data, such as user profiles, guild configurations, and command states, differentiating it from general-purpose Mongoose wrappers. Its focus on bot development aims to reduce boilerplate and streamline database interactions for bot creators. The release cadence is not explicitly stated but the version number suggests active development.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates connecting to MongoDB, initializing the `bot-database` module, defining a Mongoose schema, and performing basic CRUD operations (create/update and find) for a bot user.

import { DataBase } from 'bot-database';
import mongoose from 'mongoose';

// Define a simple Mongoose schema for a bot user
const userSchema = new mongoose.Schema({
  userId: { type: String, required: true, unique: true },
  username: { type: String, required: true },
  guildId: { type: String },
  points: { type: Number, default: 0 },
  lastSeen: { type: Date, default: Date.now },
});

const UserModel = mongoose.model('User', userSchema);

async function runBotDatabase() {
  const mongoUri = process.env.MONGO_URI ?? 'mongodb://localhost:27017/mybotdb';

  try {
    // Initialize the bot database module
    const db = new DataBase({
      mongoUri: mongoUri,
      models: { User: UserModel } // Pass in Mongoose models if the library expects them
    });

    await db.connect(); // Connect to MongoDB
    console.log('Successfully connected to MongoDB!');

    // Example: Create or update a user
    const userId = '123456789';
    const username = 'TestBotUser';

    let user = await UserModel.findOneAndUpdate(
      { userId },
      { username, $inc: { points: 1 } },
      { upsert: true, new: true }
    );

    console.log('User created/updated:', user);

    // Example: Find a user
    const foundUser = await UserModel.findOne({ userId });
    console.log('Found user:', foundUser);

  } catch (error) {
    console.error('Database operation failed:', error);
  } finally {
    await mongoose.disconnect(); // Disconnect from MongoDB
    console.log('Disconnected from MongoDB.');
  }
}

runBotDatabase();

view raw JSON →