enzodb
v1.0.0
Published
⚡ EnzoDB — A powerful, elegant MongoDB ODM for Node.js. Define schemas, validate data, run queries with a beautiful chainable API.
Maintainers
Readme
⚡ EnzoDB — Official ODM for Node.js & TypeScript
🚀 Installation
npm install enzodb mongodbpnpm add enzodb mongodb
# or
yarn add enzodb mongodb⚡ Quick Start
import { EnzoDB, Schema } from 'enzodb';
// 1. Connect using your custom enzodb:// URI
const db = new EnzoDB('enzodb://enzo_user_abc:[email protected]:27017/enzo_abc');
await db.connect();
// 2. Define a Schema
const UserSchema = new Schema({
name: { type: 'string', required: true, minLength: 2, maxLength: 50 },
email: { type: 'string', required: true, unique: true, match: /^[^\s@]+@[^\s@]+$/ },
age: { type: 'number', min: 0, max: 120, default: 18 },
role: { type: 'string', enum: ['user', 'admin', 'moderator'], default: 'user' },
isActive: { type: 'boolean', default: true },
tags: { type: 'array', of: 'string', default: [] },
}, { timestamps: true });
// 3. Register Pre/Post Hooks
UserSchema.pre('save', (doc) => {
console.log(`Saving user: ${doc.name}`);
});
// 4. Create Model
const User = db.model('users', UserSchema);
// 5. CRUD Operations
// Create
const user = await User.create({
name: 'Omar ELSabbagh',
email: '[email protected]',
age: 22,
role: 'admin',
});
// Find with Chainable Query Builder
const users = await User.find({ isActive: true })
.sort({ createdAt: -1 })
.skip(0)
.limit(10)
.select(['name', 'email', 'role']);
console.log(users);📖 Features
- ⚡ Native
enzodb://URI Protocol Support (auto-translates to MongoDB driver connection string) - 💎 Strict & Flexible Schema Validation (Type checking, min/max, regex matching, enums, required, custom validators)
- 🔗 Chainable Query Builder (
.sort(),.skip(),.limit(),.select(),.lean(),.paginate()) - 🪝 Lifecycle Hooks (
pre&posthooks forsave,create,update,delete,findOne) - 🕒 Automatic Timestamps (
createdAt&updatedAt) - 📊 Aggregation & Bulk Operations (
aggregate(),bulkWrite()) - 🔒 Full TypeScript Generics Support
- 📦 Dual Output (CommonJS + ESM)
🛠️ Complete API Reference
1. CRUD Operations
// Create
await User.create({ name: 'Ali', email: '[email protected]' });
await User.createMany([{ name: 'Sara' }, { name: 'Zaid' }]);
// Read
const allUsers = await User.find();
const admin = await User.findOne({ role: 'admin' });
const userById = await User.findById('507f1f77bcf86cd799439011');
const total = await User.count({ isActive: true });
const exists = await User.exists({ email: '[email protected]' });
// Pagination Helper
const result = await User.find({ isActive: true }).paginate(1, 20);
console.log(result.docs, result.total, result.totalPages);
// Update
await User.updateOne({ email: '[email protected]' }, { $set: { age: 25 } });
await User.updateMany({ role: 'user' }, { $set: { isActive: true } });
const updated = await User.findOneAndUpdate(
{ email: '[email protected]' },
{ $set: { name: 'Ali Mohamed' } },
{ returnNew: true }
);
await User.findByIdAndUpdate('507f...', { $inc: { age: 1 } });
// Delete
await User.deleteOne({ email: '[email protected]' });
await User.deleteMany({ isActive: false });
await User.findByIdAndDelete('507f...');2. Aggregation Pipelines
const stats = await User.aggregate([
{ $match: { isActive: true } },
{
$group: {
_id: '$role',
totalCount: { $sum: 1 },
avgAge: { $avg: '$age' },
},
},
{ $sort: { totalCount: -1 } },
]);3. Database Utilities
const collections = await db.listCollections();
const pingInfo = await db.ping(); // { ok: true, latencyMs: 24 }
const stats = await db.stats();
await db.disconnect();🌐 EnzoDB Cloud Platform
Create free, isolated MongoDB cloud databases with real-time quota tracking on db.enzocord.site.
👤 Author
Developed by Omar ELSabbagh — EnzoCord
📄 License
MIT © 2026 Omar ELSabbagh
