Registry / database / webiny-entity-mongodb

webiny-entity-mongodb

JSON →
library1.15.1jsnpmunverified

A MongoDB driver for the webiny-entity ORM, providing a persistent storage backend for entity objects. Current stable version is 1.15.1. It wraps the official MongoDB Node.js driver (peer dependency mongodb ^3.1.13) and integrates seamlessly with webiny-entity, offering features like automatic collection management, query building, and attribute persistence. Designed specifically for the Webiny framework but usable standalone. Low release cadence; primarily for internal Webiny JS usage.

npm install webiny-entity-mongodb
INSTALL
IMPORT
SIG · WEBINY-ENTITY-MONG
W
webiny-entity-mongodb
databasejavascriptv1.15.1
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.

default
import WebinyEntityMongoDb from 'webiny-entity-mongodb'
import { WebinyEntityMongoDb } from 'webiny-entity-mongodb'
The package exposes a default export only, not a named export.
MongoDbDriver
const MongoDbDriver = require('webiny-entity-mongodb')
const { MongoDbDriver } = require('webiny-entity-mongodb')
In CommonJS, the default export is assigned to the variable. Named destructuring will result in undefined.
TypeScript types
import type { MongoDbDriver } from 'webiny-entity-mongodb'
TypeScript types are included; you can use 'import type' to import the constructor type without bundling.

Connects to a local MongoDB instance, creates a driver instance, saves and retrieves a user entity.

import WebinyEntityMongoDb from 'webiny-entity-mongodb'; import { MongoClient } from 'mongodb'; async function main() { const client = new MongoClient(process.env.MONGO_URL ?? 'mongodb://localhost:27017'); await client.connect(); const db = client.db('myapp'); const driver = new WebinyEntityMongoDb({ db }); // Example entity (assuming a User entity defined elsewhere) const user = new User(); user.name = 'John'; await driver.save(user); const loaded = await driver.findOne({ name: 'John' }); console.log(loaded); await client.close(); } main().catch(console.error);
Debug
Known issues
gotchaThe driver's constructor expects an options object with a `db` property (MongoDb.Db instance), not a MongoClient or connection string.
fix
Ensure you pass `{ db: mongoClient.db(dbName) }`.
affects: *
gotchaAll entities must be instances of classes extending `webiny-entity` and have a defined `entityName` static property.
fix
Define `static entityName = 'EntityClassName'` in your entity class.
affects: *
deprecatedThe `onBeforeSave` and `onAfterSave` hooks have been deprecated in favor of passing callbacks directly to the driver methods.
fix
Use `driver.save(entity, { beforeSave: fn, afterSave: fn })` instead.
affects: <=1.14.0
Errors
Common errors & fixes
Error: Cannot read properties of undefined (reading 'collection')
The driver expects a `db` object with a `collection` method. Missing or incorrectly passed `db`.
fix
const db = client.db('mydb'); const driver = new WebinyEntityMongoDb({ db });
Error: entity.constructor.entityName is not defined
The entity class does not have a static `entityName` property.
fix
Add `static entityName = 'MyEntity';` to the entity class.
TypeError: driver.save is not a function
Importing the package as `import { WebinyEntityMongoDb }` instead of default import.
fix
Use `import WebinyEntityMongoDb from 'webiny-entity-mongodb'`.
Upgrade
Version history
1.15.1latest on npm
Audit
Dependencies
mongodbrequiredCore MongoDB driver, required as a peer dependency
Agent activity
28 hits · last 30 days
node
20
Meta
2
OpenAI (training)
1
Resources
webiny-entity-mongodb — npm install webiny-entity-mongodb · libregistry