astro-mongoose
is a plugin for integrating Mongoose with your Astro project, enabling seamless MongoDB connections and enhanced Developer Experience (DX) features.
- Easy MongoDB Connection: Quickly connect to MongoDB using Mongoose in astro project.
- Configurable Options: Customize connection parameters with ease.
- Automatic Connection Management: Handles reconnections and disconnections automatically.
- Detailed Logging: Get clear and descriptive logs for connection states.
- Graceful Shutdown: Cleans up connections properly on server shutdown or interruption.
- Error Handling: Provides meaningful error messages for connection issues.
You can install the plugin via npm:
npm install astro-mongoose mongoose
To get started, add astro-mongoose
in astro config file
import mongoose from "astro-mongoose";
export default {
integrations: [
mongoose(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
}),
],
};
You can customize the connection with additional options:
import mongoose from "astro-mongoose";
import type { ConnectOptions } from "mongoose";
const mongooseOptions: ConnectOptions = {
useNewUrlParser: true,
useUnifiedTopology: true,
};
export default {
integrations: [mongoose(process.env.MONGODB_URI, mongooseOptions)],
};
Option | Type | Description |
---|---|---|
uri |
string |
The MongoDB connection string (required). |
options |
ConnectOptions |
Additional Mongoose connection options (optional). |
The plugin automatically manages the MongoDB connection lifecycle:
- Connects when the Astro server starts.
- Closes the connection gracefully on server stop or interruption.
- Logs connection status and any errors.
The plugin throws an error if the MongoDB URI is not provided during initialization. Custom error messages are logged during connection attempts and when closing the connection.
This project is licensed under the MIT License - see the LICENSE file for details.