simple-light-mongo-orm
v1.0.5
Published
A lightweight JavaScript ORM for MongoDB
Maintainers
Readme
Simple-Light Mongo ORM
A minimal JavaScript ORM for MongoDB, designed to provide a clean and easy interface for working with Mongo collections as JavaScript objects — without the overhead of frameworks like Mongoose.
Features
- Schema-based validation (type, required, default, custom validators)
- Simple CRUD API:
create,find,findOne,updateOne,deleteOne - Lifecycle hooks:
preSave,postSave - Basic populate helper (resolve references between models)
- Works directly with official MongoDB Node.js driver
- Lightweight (no dependencies other than
mongodb)
Installation
npm install simple-light-mongo-ormOr add it manually to your package.json:
"dependencies": {
"simple-light-mongo-orm": "^1.0.0"
}Quick Start Example
const { connect, model, Schema, disconnect } = require('simple-light-mongo-orm')
async function main() {
// Connect to MongoDB
await connect('mongodb://127.0.0.1:27017', 'testdb')
// Define a schema
const User = await model('users', new Schema({
name: { type: 'String', required: true },
email: { type: 'String', required: true, validate: v => v.includes('@') },
age: { type: 'Number', default: 18 }
}))
// Create a new user
const user = await User.create({ name: 'Alice', email: '[email protected]' })
console.log('Created user:', user)
// Query users
const found = await User.findOne({ name: 'Alice' })
console.log('Found user:', found)
// Disconnect
await disconnect()
}
main().catch(console.error)Define Models
const Post = await model('posts', new Schema({
title: { type: 'String', required: true },
body: 'String',
author: { type: 'ObjectId', required: true }
}))Populate Example
You can populate references between models easily:
const posts = await Post.find({})
await Post.populate(posts, [{ path: 'author', model: User }])
console.log(posts)Hooks
Hooks allow you to execute functions before or after saving a document.
User.on('preSave', (doc) => {
doc.createdAt = new Date()
})
User.on('postSave', (doc) => {
console.log(`User saved: ${doc.name}`)
})Validation
Schemas automatically validate types, required fields, and custom rules.
const Product = await model('products', new Schema({
name: { type: 'String', required: true },
price: { type: 'Number', validate: v => v > 0 }
}))API Summary
connect(uri, dbName)
Connect to your MongoDB database.
model(name, schema)
Create or get a model (collection).
Schema(definition)
Define your schema with types, defaults, and validators.
Model.create(data)
Insert a new document.
Model.find(query)
Find multiple documents.
Model.findOne(query)
Find a single document.
Model.updateOne(query, update)
Update a document and return the updated result.
Model.deleteOne(query)
Delete a single document.
Document.save()
Save an existing or new document instance.
Document.remove()
Delete a document instance.
License
MIT License © 2025 — Created by izmroen
Contribute
Contributions are welcome! If you have ideas for improvements (relations, migrations, query builder, etc.), feel free to open an issue or pull request.
Keywords
mongo · mongodb · orm · odm · lightweight · database · javascript · nodejs
