@bricco/mongo-wrapper
v1.2.7
Published
A wrapper for mongodb with a unified interface against the data API / node driver.
Downloads
406
Readme
@bricco/mongo-wrapper
A simple wrapper to get a unified interface against the MongoDB Data API and MongoDB Node Driver
The wrapper is very useful if you want to use MongoDB in Vercel Edge Runtime or benifit from the Nextjs built-in data cache. But still have the possibilty do run tests against an In-Memory Storage Engine or use a local database with the native Node Driver.
Install
The usual ways:
yarn add @bricco/mongo-wrapper
Example
import createDb from '@bricco/mongo-wrapper'
const db = createDb({
apiKey: 'XXXXXXXX',
apiUrl: 'https://data.mongodb-api.com/app/data-abc123/endpoint/data/v1',
dataSource: 'myCluser',
database: 'myDatabase',
connectionString: 'mongodb+srv://user:<PWD>@myCluser.apc123.mongodb.net',
})
const car = await db('mycollection').findOne({ type: 'car' })
// => { _id: "61df...", type: "car", ...etc }
const useNodeDriver = true;
const bike = await db('mycollection', useNodeDriver).findOne({ type: 'bike' })
// => { _id: "61df...", type: "bike", ...etc }