mongoose-bot-database

1.0.0 • Public • Published

Discord Bot Database

discord-bot-database is a package that advance version of mongoose package.

Installation

Install the package with npm:

npm install mongoose-bot-database

[Key Features Included]

1 - Default and Custom Database Support: Connects to a default database and supports multiple custom database URLs dynamically.
2 - Model Management: Define models dynamically with optional custom database connections.
3 - TTL (Time-To-Live) Indexing: Automatic document expiration after a specified duration.
4 - Encryption/Decryption: AES-based field encryption and decryption.
5 - Connection Monitoring: Automatically checks database connection health periodically.
6 - Transaction Support: Run operations safely within MongoDB transactions.
7 - Dynamic Database Switching: Change a model's database connection at runtime.
8 - Event-Driven: Emits events like connected, error, and connection_warning.

[How To Use]

1. Import the Package

const BotDatabase = require("bot-database");

2. Basic Usage > Initialize

const botDb = new BotDatabase("mongodb://localhost:27017/defaultdb");
botDb.connect();

2. Basic Usage > Event Listeners

botDb.on("connected", (dbUrl) => console.log(`Connected to: ${dbUrl}`));

botDb.on("error", (dbUrl, error) => console.error(`Error in: ${dbUrl}`, error));

3. Define a Model

(async () => {
    await botDb.defineModel("User", {
        name: String,
        email: String,
    });
})();

4. TTL (Time-To-Live) Feature

(async () => {
    await botDb.defineModel("Session", {
        userId: String,
    }, {
        ttl: 1000 * 60 * 60 * 24, // Documents expire after 1 day
    });
})();

5. Encryption and Decryption

(async () => {
    await botDb.defineModel(
        "SensitiveData",
        { secret: String },
        { encryptFields: ["secret"], decryptFields: ["secret"] }
    );
})();

6. Switch a Model's Database

(async () => {
    await botDb.switchModelDatabase("User", "mongodb://localhost:27017/otherdb");
})();

7. Transactions

(async () => {
    await botDb.executeTransaction(null, async (session) => {
        const User = botDb.models["User"];
        await User.create([{ name: "Alice" }], { session });
        await User.create([{ name: "Bob" }], { session });
    });
})();

8. Monitor Connections

botDb.monitorConnections();

Example Full Code Usage

const BotDatabase = require("bot-database");

(async () => {
    const botDb = new BotDatabase("mongodb://localhost:27017/defaultdb");

    // Connect to the default database
    await botDb.connect();

    // Define a model with encryption and TTL
    await botDb.defineModel(
        "SensitiveData",
        {
            secret: String,
            userId: String,
        },
        {
            ttl: 1000 * 60 * 60 * 24, // TTL of 1 day
            encryptFields: ["secret"],
            decryptFields: ["secret"],
        }
    );

    // Insert encrypted data
    const SensitiveData = botDb.models["SensitiveData"];
    await SensitiveData.create({ secret: "MySecretKey123", userId: "user1" });

    // Retrieve and decrypt data
    const data = await SensitiveData.find();
    console.log("Decrypted Data:", data);

    // Switch database dynamically
    await botDb.switchModelDatabase("SensitiveData", "mongodb://localhost:27017/newdb");

    // Execute a transaction
    await botDb.executeTransaction(null, async (session) => {
        const User = botDb.models["SensitiveData"];
        await User.create([{ secret: "AnotherSecret" }], { session });
    });

    // Monitor database connections
    botDb.monitorConnections();
})();

JOIN DISCORD FOR SUPPORT

CLICK TO JOIN NOW > ARBOTIX DEVELOPMENT

Package Sidebar

Install

npm i mongoose-bot-database

Weekly Downloads

4

Version

1.0.0

License

ISC

Unpacked Size

12.6 kB

Total Files

3

Last publish

Collaborators

  • hjgaming