Simplify your MongoDB connections with Mongoose using just one line of code, while maintaining full access to Mongoose functionality!
Install the package using npm:
npm install mongoose-light
Or using yarn:
yarn add mongoose-light
- Import the
initializeMongoDb
function andmongoose
from the package:
import { initializeMongoDb, mongoose } from 'mongoose-light';
- Call the
initializeMongoDb
function with your database configuration:
initializeMongoDb({
databaseUrl: process.env.DATABASEURL,
service: process.env.SERVICE,
});
- Use
mongoose
as you normally would for defining schemas, models, etc.
The initializeMongoDb
function accepts an object with the following properties:
-
databaseUrl
(string): The URL of your MongoDB instance. -
service
(string): The name of your database service.
import { initializeMongoDb, mongoose } from 'mongoose-light';
// Connect to MongoDB
initializeMongoDb({
databaseUrl: 'mongodb://localhost:27017',
service: 'myapp',
});
// Define a schema
const userSchema = new mongoose.Schema({
name: String,
email: String,
});
// Create a model
const User = mongoose.model('User', userSchema);
// Use the model
const newUser = new User({ name: 'John Doe', email: 'john@example.com' });
newUser.save();
- Simple one-line connection to MongoDB
- Exports the
mongoose
object, allowing you to use all Mongoose features - Automatic connection error handling
- Console logging for successful connections
- mongoose: ^5.x.x
mongoose-light simplifies the process of connecting to MongoDB using Mongoose, while still providing full access to the Mongoose library. This allows you to:
- Quickly set up your database connection with minimal code
- Continue using Mongoose exactly as you're used to
- Keep your codebase clean and maintainable
MIT
Contributions, issues, and feature requests are welcome! Feel free to check issues page.
Give a ⭐️ if this project helped you!
If you have any questions or need support, please open an issue on our GitHub repository.