
Security News
Risky Biz Podcast: Making Reachability Analysis Work in Real-World Codebases
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Leaf ODM is an abstraction layer for MongoDB on NodeJS. It aims to provide a better class support for MongoDB while keeping it's schema-less feature.
Nowadays the official MongoDB provides really good support and I think we don't really need any ODM/ORM for MongoDB on NodeJS, at least when using JavaScript. When using TypeScript, it's a bit different, most TS users want to use classes to have strong typing and prevent errors, but you can't pass a class instance to MongoDB.
Actual libraries such as TypeORM and Mongoose still have some issues:
I was at this time trying to develop an API and I knew that some fields are dynamic and cannot be defined in a schema. As a TypeScript user, I tried TypeORM but lost the schema-less feature, and Mongoose was not adapted for the project.
So, I just decided to create my own abstraction layer on top of MongoDB's official driver.
Start by installing the dependency:
# Using NPM:
npm install leafodm
# Using Yarn:
yarn add leafodm
Then, connect your database in your app entry-point:
import { DatabaseManager } from 'leafodm'
await DatabaseManager.init('mongodb://localhost:27017/database')
// With dotenv:
await DatabaseManager.init(process.env.DATABASE_URL)
Note: I highly recommend you to use dotenv
here, never let your credentials hardcoded.
Now, you can define a model such as:
import { BaseEntity } from 'leafodm'
class User extends BaseEntity {
name: string
email: string
password: string
}
That's done! You are now able run queries on your model, here are some examples:
const user = new User()
user.name = 'John Doe'
user.email = 'jdoe@example.org'
user.password = 'abcd1234'
await user.create()
await User.findOne({ email: 'jdoe@example.org' }) // -> Return first document that contains 'jdoe@example.org' as email
await User.findOne('60bbedee3310443e74b495da') // -> Return document by its ID
await User.find({ name: 'John Doe' }) // -> Every users with 'John Doe' as name
await User.find() // -> Return the whole collection
// It's also possible to set multiple conditions
await User.find({
_id: '60bbedee3310443e74b495da',
admin: true
}) // -> Return a user that correspond to this ID and that is an admin
Note: _id are automatically converted to ObjectId, even nested
const user = await User.findOne('60bbedee3310443e74b495da')
user.name = 'John Doe Jr'
await user.update()
const user = await User.findOne('60bbedee3310443e74b495da')
await user.delete()
const users = await User.sortBy({
name: 'DESC'
}).find()
const users = await User.take(5).find() // Will only return 5 documents
const users = await User.skip(5).find() // Will skip 5 first documents
This library is very useful when combined with class-transformer
and class-validator
,
no need to instance your model and write data to every fields.
Here is an example with the web framework FoalTS:
@Post('/register')
@ValidateBody(User)
async createUser(ctx: Context) {
const user: User = ctx.request.body
const exists = User.findOne({
email: user.email
})
if (exists) {
throw new HttpResponseBadRequest('this email is already taken')
}
return new HttpResponseOK({
status: true,
message: 'account created',
data: await user.create(),
});
}
You can follow and track the work on project.
My goals for future releases are:
sortBy
should suggest only properties)User.delete(id)
to prevent creating an instance before.new User({ name: 'John Doe' })
(inspired from Laravel Eloquent)sortBy
, take
and skip
, this works but the code behind is not well maintainable)Contributing is welcome, you can clone this project and make PR if you want! Just make sure you use same code style and follow ESLint.
If you find any bugs or suggestion, feel free to open an issue. For questions, please uses discussions.
FAQs
MongoDB abstraction layer in TypeScript
The npm package leafodm receives a total of 0 weekly downloads. As such, leafodm popularity was classified as not popular.
We found that leafodm demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.