···
3
-
A lightweight, fully type-safe MongoDB ODM in TypeScript, inspired by Drizzle ORM.
3
+
A lightweight, type-safe ODM for MongoDB in TypeScript — inspired by [Drizzle ORM](https://orm.drizzle.team/) and built for developers who value simplicity, transparency, and strong typings.
7
-
* **Schema-first:** Define and validate document schemas using Zod.
8
-
* **Type-safe queries:** Auto-complete and type-safe insert/find/update/delete operations.
9
-
* **Lightweight & modular:** No decorators, no runtime magic – everything is composable and transparent.
10
-
* **Developer-first DX:** Simple, minimal API with great IDE support.
11
-
* Works directly on top of MongoDB's native driver.
9
+
* **Schema-first:** Define and validate collections using [Zod](https://zod.dev/).
10
+
* **Type-safe operations:** Auto-complete and strict typings for `insert`, `find`, `update`, and `delete`.
11
+
* **Minimal & modular:** No decorators or magic. Just clean, composable APIs.
12
+
* **Developer-friendly DX:** Great TypeScript support and IDE integration.
13
+
* **Built on MongoDB native driver:** Zero overhead with full control.
npm install mizzleorm mongodb zod
···
yarn add mizzleorm mongodb zod
23
-
### 1. Define your schema
29
+
### 1. Define a schema
import { defineModel } from 'mizzleorm';
···
export type User = z.infer<typeof userSchema>;
40
-
### 2. Connect to MongoDB and create a model
43
-
// src/index.ts or your main application file
44
-
import { connect, MongoModel, InferModel, InsertType } from 'mizzleorm';
45
-
import { userSchema } from './schemas/user'; // Assuming you saved the schema above
48
+
### 2. Initialize connection and model
52
+
import { connect, disconnect, MongoModel, InferModel, InsertType } from 'mizzleorm';
53
+
import { userSchema } from './schemas/user';
import { ObjectId } from 'mongodb';
type User = InferModel<typeof userSchema>;
type UserInsert = InsertType<typeof userSchema>;
···
await connect('mongodb://localhost:27017', 'your_database_name');
const UserModel = new MongoModel('users', userSchema);
56
-
// ... perform operations
63
+
// Your operations go here
···
main().catch(console.error);
### 3. Perform operations
67
-
// Insert a document
const newUser: UserInsert = {
email: 'john.doe@example.com',
const insertResult = await UserModel.insertOne(newUser);
74
-
console.log('Inserted user:', insertResult.insertedId);
const users = await UserModel.find({ name: 'John Doe' });
78
-
console.log('Found users:', users);
80
-
// Find one document
81
-
const foundUser = await UserModel.findOne({ _id: new ObjectId(insertResult.insertedId) });
82
-
console.log('Found one user:', foundUser);
88
+
const found = await UserModel.findOne({ _id: new ObjectId(insertResult.insertedId) });
84
-
// Update a document
85
-
const updateResult = await UserModel.update(
86
-
{ _id: new ObjectId(insertResult.insertedId) },
89
-
console.log('Updated user count:', updateResult.modifiedCount);
91
+
await UserModel.update({ name: 'John Doe' }, { age: 31 });
92
-
const deleteResult = await UserModel.delete({ name: 'John Doe' });
93
-
console.log('Deleted user count:', deleteResult.deletedCount);
94
+
await UserModel.delete({ name: 'John Doe' });
97
+
await UserModel.insertMany([
98
+
{ name: 'Alice', email: 'alice@example.com', age: 25 },
99
+
{ name: 'Bob', email: 'bob@example.com' },
103
+
await UserModel.findById(insertResult.insertedId);
106
+
await UserModel.updateOne({ name: 'Alice' }, { age: 26 });
109
+
await UserModel.replaceOne({ name: 'Bob' }, {
111
+
email: 'bob@newmail.com',
116
+
await UserModel.deleteOne({ name: 'Alice' });
119
+
const count = await UserModel.count({ age: { $gte: 18 } });
122
+
const aggregation = await UserModel.aggregate([
123
+
{ $match: { age: { $gte: 18 } } },
124
+
{ $group: { _id: null, avgAge: { $avg: '$age' } } },
128
+
const paginated = await UserModel.findPaginated(
129
+
{ age: { $gte: 18 } },
130
+
{ skip: 0, limit: 10, sort: { age: -1 } }
96
-
## Project Structure
136
+
## 🧠 Project Structure
101
-
│ ├── schema.ts # schema definition utility
141
+
│ ├── schema.ts # Schema definition utility
│ ├── model.ts # MongoModel wrapper
103
-
│ ├── client.ts # MongoDB connection
104
-
│ ├── index.ts # public API export
106
-
│ └── user.ts # usage example
143
+
│ ├── client.ts # MongoDB client connection
144
+
│ └── index.ts # Public API exports
145
+
├── examples/ # Example usage files
147
+
├── tests/ # Unit and integration tests
115
-
To build the project:
157
+
### Build the library:
121
-
To run the example:
163
+
### Run the example:
173
+
MIT — use it freely and contribute back if you'd like!