Example implementation of Mongoloquent in a NestJS application, demonstrating all supported relationship types.
| Relationship | Example |
|---|---|
hasMany |
User → Posts |
hasOne |
User → Phone |
belongsTo |
Post → User |
belongsToMany |
User ↔ Roles (pivot: role_user) |
hasManyThrough |
Application → Deployments through Environments |
morphOne |
User/Post → Image (imageable) |
morphMany |
Post/Video → Comments (polymorphic) |
morphTo |
Comment/Image → commentable/imageable |
morphToMany |
Post/Video → Tags (polymorphic pivot: taggable) |
morphedByMany |
Tag → Posts/Videos (inverse of morphToMany) |
- Node.js
- MongoDB instance (local or remote)
$ npm installCopy .env.example to .env and configure your MongoDB connection:
MONGODB_URI=mongodb://localhost:27017
MONGODB_DB_NAME=sample-nestjs
MONGODB_TIMEZONE=Asia/Jakarta# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prodSwagger UI is available at http://localhost:3000/docs after starting the app.
# unit tests
$ npm run test
# test coverage
$ npm run test:cov
# e2e tests
$ npm run test:e2eUnit tests cover all controller endpoints and service methods using mocked models and services. No database connection is required.
- Mongoloquent — MongoDB ORM with Eloquent-style relationships
- @mongoloquent/nestjs — NestJS integration module
- NestJS Documentation