Registry / database / functional-models-orm-mongo

functional-models-orm-mongo

JSON →
library3.6.1jsnpmunverified

An ORM adapter for functional-models that uses MongoDB as the backend datastore. Current stable version is 3.6.1, released periodically on npm. It provides an implementation of the functional-models datastore adapter interface, allowing developers to define functional models and persist them in MongoDB. Key differentiators include full TypeScript support, seamless integration with functional-models library, and a lightweight, functional approach to ORM compared to traditional OOP-based ORMs like Mongoose.

npm install functional-models-orm-mongo
INSTALL
IMPORT
SIG · FUNCTIONAL-MODELS-
F
functional-models-orm-mongo
databasejavascriptv3.6.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.

datastoreAdapter (named export)
import { datastoreAdapter } from 'functional-models-orm-mongo'
import mongoAdapter from 'functional-models-orm-mongo'
The package exports a named export `datastoreAdapter`, not a default export. Common mistake is to import it as default.
Full import for adapter initialization
const { datastoreAdapter } = require('functional-models-orm-mongo')
const functionalModelsOrmMongo = require('functional-models-orm-mongo')
For CommonJS, destructure the named export. Importing the whole module without destructuring will not work.
Type import for TypeScript
import type { DatastoreAdapter } from 'functional-models-orm-mongo'
TypeScript types are included. Use `import type` for type-only imports if needed.

Shows how to initialize the Mongo adapter with a MongoClient, create an ORM instance, define a model, and persist a record.

import { MongoClient } from 'mongodb'; import { createOrm } from 'functional-models'; import { datastoreAdapter } from 'functional-models-orm-mongo'; const client = new MongoClient(process.env.MONGO_URI ?? 'mongodb://localhost:27017'); await client.connect(); const adapter = datastoreAdapter({ mongoClient: client, databaseName: process.env.DB_NAME ?? 'test', }); const orm = createOrm({ datastoreAdapter: adapter }); // Define a model const UserModel = orm.createModel({ name: 'user', attributes: { name: { type: String, required: true }, email: { type: String, required: true }, }, }); // Create a user const user = UserModel.create({ name: 'Alice', email: 'alice@example.com' }); await user.save(); console.log('User saved:', user.toJSON()); await client.close();
Debug
Known issues
gotchaThe `datastoreAdapter` function must receive a connected MongoClient instance. Passing a disconnected client will result in runtime errors.
fix
Ensure you call `client.connect()` before calling `datastoreAdapter`.
affects: all
breakingVersion 3.0 changed the exported function name from `mongoAdapter` to `datastoreAdapter`.
fix
Update imports: import { mongoAdapter } from 'functional-models-orm-mongo' -> import { datastoreAdapter } from 'functional-models-orm-mongo'.
affects: <3.0
gotchaThe `functional-models` library uses a functional paradigm; models are immutable and operations return new instances. Do not mutate model instances directly.
fix
Use methods like `model.set(attr, value)` or `model.assign({...})` to get updated copies.
affects: all
gotchaMongoDB ObjectId fields are automatically converted to strings when serialized. If you need the raw ObjectId, access the internal `_id` property.
fix
Use `model.get('_id')` or inspect the raw document after save.
affects: all
Errors
Common errors & fixes
TypeError: Cannot destructure property 'datastoreAdapter' of 'require(...)' as it is undefined.
CommonJS import incorrectly tries to destructure from the module while it is not a default export.
fix
Make sure you are using `const { datastoreAdapter } = require('functional-models-orm-mongo');` and that the package is installed.
MongoError: db is undefined
The `databaseName` option was not provided or is invalid when calling `datastoreAdapter`.
fix
Pass a valid `databaseName` string in the options object: `datastoreAdapter({ mongoClient, databaseName: 'myDB' })`.
TypeError: client.connect is not a function
The MongoClient instance passed to the adapter is not a valid driver client, or is already destroyed.
fix
Create a proper MongoClient: `new MongoClient(uri)` and call `await client.connect()` before using.
Upgrade
Version history
3.6.1latest on npm
Audit
Dependencies
functional-modelsrequiredCore library that defines the model abstraction and ORM interface; this adapter is a plugin for it.
mongodbrequiredMongoDB driver for Node.js; used to connect to and interact with MongoDB databases.
Agent activity
7 hits · last 30 days
node
6
Resources
functional-models-orm-mongo — npm install functional-models-orm-mongo · libregistry