@bricco/mongo-wrapper
A powerful MongoDB wrapper for Next.js applications that simplifies database operations with built-in caching, BSON simplification, and comprehensive debugging capabilities.
Features
- Next.js Integration: Built-in support for Next.js caching with
unstable_cache
- BSON Simplification: Automatic conversion of ObjectIds to strings for easier handling
- Debug Logging: Comprehensive development logging with performance metrics
- TypeScript Support: Full TypeScript support with proper type inference
- Flexible Caching: Configurable caching strategies with tag-based invalidation
- Mutation Hooks: Customizable hooks for insert/update operations
- Performance Monitoring: Built-in timing and error tracking
Key Benefits
- Simplified Data Handling: ObjectIds are automatically converted to strings, eliminating the need for manual BSON handling
- Smart Caching: Leverage Next.js cache with automatic invalidation on mutations
- Development Friendly: Rich debug logging shows query performance and parameters
- Production Ready: Optimized for both development and production environments
- Test Compatible: Easy integration with in-memory MongoDB for testing
Install
yarn add @bricco/mongo-wrapper
Example
import { MongoClient } from 'mongodb'
import createDb from '@bricco/mongo-wrapper';
import { unstable_cache as cache, revalidateTag } from 'next/cache';
type MyCollections {
mycollection: MyCollection1,
mycollection2: MyCollection2
}
const client = new MongoClient('mongodb://localhost:27017', {
maxIdleTimeMS: 60000,
maxPoolSize: 10,
retryWrites: true,
serverSelectionTimeoutMS: 5000,
socketTimeoutMS: 45000,
w: 'majority',
});
const db = createDb<MyCollections>({
database: 'myDatabase',
client,
cache: cache,
onMutation: async ({ collection }) => {
try {
revalidateTag(collection);
} catch {
}
},
})
const car = await db('mycollection').findOne({ type: 'car' })
const car = await db('mycollection').findOne({ type: 'car' }, { cache: false })
revalidateTags('mycollection')