@mongorm/orm
v0.1.7-beta.5
Published
A TypeScript-first MongoDB ORM for applications that want strong types without hiding MongoDB
Readme
Mongorm is a TypeScript-first MongoDB ORM for applications that want strong types and validation without hiding MongoDB behind a large abstraction.
It was created to make the common parts of MongoDB application development consistent: define data once, validate it at the boundary, query it with inferred types, and still keep access to MongoDB when the application needs it.
Features
Typed Schemas
Define the shape of your data once. Mongorm infers the input and output types from the schema and validates writes with Zod-backed fields.
const ROLES = ['admin', 'member'];
const userSchema = orm.schema({
name: orm.string(),
email: orm.string().email(),
role: orm.enum(ROLES),
});Create, Read, Update, and Delete and Queries
Create records and build readable queries without losing MongoDB filter semantics.
const user = await db.users.create({
name: 'Ada Lovelace',
email: '[email protected]',
role: 'admin',
});
const users = await db.users
.fields(['name', 'email'])
.find({ role: 'admin' })
.sort({ name: 'asc' })
.limit(20);Use .first() when a query should return one record or null:
const user = await db.users.find({ email: '[email protected]' }).first();Relations and Population
Connect related records and load them only when needed.
const user = await db.users
.find({ email: '[email protected]' })
.populate([
{
ref: 'company',
fields: ['name', 'address'],
},
])
.first();Bulk Operations
Use the same model for individual writes and larger batches.
await db.users.bulk.create([
{ name: 'Grace Hopper', email: '[email protected]', role: 'admin' },
{ name: 'Alan Turing', email: '[email protected]', role: 'member' },
]);Built-In Application Features
- Optional timestamps and soft deletes.
- Hidden fields for sensitive values.
- Reusable scopes for common read views.
- Explicit schema indexes with MongoDB options.
- Cursors, pagination, projections, sorting, and population.
- Access to the native collection for advanced MongoDB operations.
Getting Started
pnpm add @mongorm/ormimport { createDatabase, orm } from '@mongorm/orm';
const ROLES = ['admin', 'member'];
const userSchema = orm
.schema({
email: orm.email(),
role: orm.enum(ROLES),
password: orm.string().hidden(),
})
.options({
timestamps: true,
});
const schema = orm.defineSchemas({
users: userSchema,
});
const db = createDatabase({
uri: process.env.MONGODB_URI,
database: 'app',
schema,
});
await db.connect();
await db.users.bulk.create([
{ email: '[email protected]', role: 'member', password: 'password' },
{ email: '[email protected]', role: 'admin', password: 'password' },
]);
const user = await db.users
.find({ email: '[email protected]', role: 'admin' })
.fields(['$all', '+password'])
.first();
if (!user) throw new Error('User not found');
console.log(user);Mongorm keeps the MongoDB driver close at hand, so it simplifies everyday work without limiting access to MongoDB's full feature set.
License
GNU General Public License v3.0
