Registry / database / keyv-mongodb

keyv-mongodb

JSON →
library3.0.0jsnpmunverified

MongoDB storage adapter for Keyv with Mongoose support. v3.0.0 requires mongoose ^6 and mongodb ^4 as peer dependencies. Provides connection reuse for Mongoose connections, native MongoDB driver, or connection strings. Lightweight wrapper for Keyv's key-value cache interface. Regular releases, stable.

npm install keyv-mongodb
INSTALL
IMPORT
SIG · KEYV-MONGODB
K
keyv-mongodb
databasejavascriptv3.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

KeyvMongoDB
import KeyvMongoDB from 'keyv-mongodb'
const { KeyvMongoDB } = require('keyv-mongodb')
Default export class. In CJS, use require('keyv-mongodb').default
default (constructor)
import KeyvMongoDB from 'keyv-mongodb'
import { KeyvMongoDB } from 'keyv-mongodb'
Module uses default export, not named export

Create a Keyv instance with MongoDB adapter using a connection string, set and get a value

import Keyv from 'keyv'; import KeyvMongoDB from 'keyv-mongodb'; const keyv = new Keyv({ store: new KeyvMongoDB({ url: 'mongodb://localhost:27017/test' }) }); await keyv.set('foo', 'bar'); const value = await keyv.get('foo'); console.log(value); // 'bar'
Debug
Known issues
breakingPackage dropped support for Node.js < 12 and mongoose < 6, mongodb < 4 in v3.0.0
fix
Upgrade to Node.js >=12, mongoose ^6 or mongodb ^4
affects: >=3.0.0
deprecatedThe 'db' option expects a MongoDB Db instance (not MongoClient); 'dbPromise' option is deprecated since v2
fix
Use 'db' with a Db instance or 'url' with connection string
affects: >=2.0.0
gotchaMongoose connection must be already connected or connecting; adapter will wait for connection
fix
Call mongoose.connect() before creating KeyvMongoDB instance
affects: >=1.0.0
breakingImport style changed: v3 uses ESM default export; CJS users must use require('keyv-mongodb').default
fix
Use import or require with .default
affects: >=3.0.0
Errors
Common errors & fixes
TypeError: KeyvMongoDB is not a constructor
Using named import instead of default import in ESM or not accessing .default in CJS
fix
import KeyvMongoDB from 'keyv-mongodb' or const KeyvMongoDB = require('keyv-mongodb').default
MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
Missing or invalid MongoDB connection string provided to KeyvMongoDB
fix
Pass a valid url option or reuse an existing connection
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies
keyvrequiredCore caching library; this adapter is a store for Keyv
mongooseoptionalOptional peer dependency for Mongoose connection reuse
mongodboptionalOptional peer dependency for native MongoDB driver connection reuse
Agent activity
9 hits · last 30 days
node
6
Resources
keyv-mongodb — npm install keyv-mongodb · libregistry