discord-bot-database
is a package that advance version of mongoose package.
Install the package with npm:
npm install mongoose-bot-database
1 - Default and Custom Database Support: Connects to a default database and supports multiple custom database URLs dynamically.
const BotDatabase = require("bot-database");
const botDb = new BotDatabase("mongodb://localhost:27017/defaultdb");
botDb.connect();
botDb.on("connected", (dbUrl) => console.log(`Connected to: ${dbUrl}`));
botDb.on("error", (dbUrl, error) => console.error(`Error in: ${dbUrl}`, error));
(async () => {
await botDb.defineModel("User", {
name: String,
email: String,
});
})();
(async () => {
await botDb.defineModel("Session", {
userId: String,
}, {
ttl: 1000 * 60 * 60 * 24, // Documents expire after 1 day
});
})();
(async () => {
await botDb.defineModel(
"SensitiveData",
{ secret: String },
{ encryptFields: ["secret"], decryptFields: ["secret"] }
);
})();
(async () => {
await botDb.switchModelDatabase("User", "mongodb://localhost:27017/otherdb");
})();
(async () => {
await botDb.executeTransaction(null, async (session) => {
const User = botDb.models["User"];
await User.create([{ name: "Alice" }], { session });
await User.create([{ name: "Bob" }], { session });
});
})();
botDb.monitorConnections();
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();
})();
CLICK TO JOIN NOW > ARBOTIX DEVELOPMENT