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

mongoose-bot-database

v1.0.0

Published

advance version of mongoose database

Downloads

13

Readme

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