Registry / database / mongodb-dynamic-api

mongodb-dynamic-api

JSON →
library4.15.0jsnpmunverified

A production-ready NestJS 11 module that instantly generates fully typed REST APIs and WebSockets for any MongoDB collection with zero boilerplate. Version 4.15.0 provides dual-token JWT authentication (access + refresh tokens), passwordless/OTP login, authorization with ability predicates, smart caching with auto-invalidation, WebSocket support, presence tracking, cascade delete, and typed lifecycle callbacks. Released under MIT license with weekly updates; ships TypeScript types. Alternative to @nestjsx/crud or building CRUD manually, with built-in authentication and real-time features.

npm install mongodb-dynamic-api
INSTALL
IMPORT
SIG · MONGODB-DYNAMIC-AP
M
mongodb-dynamic-api
databasejavascriptv4.15.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

DynamicApiModule
import { DynamicApiModule } from 'mongodb-dynamic-api'
const DynamicApiModule = require('mongodb-dynamic-api')
ESM-only; use named import. DynamicApiModule is the main module for registering CRUD controllers.
useAuth
import { useAuth } from 'mongodb-dynamic-api'
useAuth is a generator function that returns a configured DynamicApiModule with JWT auth. Requires a user entity.
DynamicApiModule.forRoot
DynamicApiModule.forRoot({ ... })
DynamicApiModule.forRootAsync({ ... })
forRoot takes a configuration object; for async config, use DynamicApiModule.forRootAsync with imports and useFactory.

Shows minimal setup: import DynamicApiModule, configure with entities, MongoDB URI, and JWT secret, then start NestJS app.

import { NestFactory } from '@nestjs/core'; import { DynamicApiModule } from 'mongodb-dynamic-api'; import { Module } from '@nestjs/common'; @Module({ imports: [ DynamicApiModule.forRoot({ entities: ['users', 'posts'], mongodb: { uri: process.env.MONGODB_URI ?? 'mongodb://localhost:27017/mydb' }, jwt: { secret: process.env.JWT_SECRET ?? 'secretkey' }, }), ], }) class AppModule {} async function bootstrap() { const app = await NestFactory.create(AppModule); await app.listen(3000); } bootstrap();
Debug
Known issues
breakingv4 returns { accessToken, refreshToken } instead of a single token. Old code destructuring { token } will break.
fix
Destructure { accessToken, refreshToken } from login/register responses. Update any code expecting key 'token'.
affects: >=4.0.0 <5.0.0
breakingv4 changes default access token expiry from '1d' to '15m' and refresh token default to '7d'.
fix
If relying on previous 1d expiry, set explicitly: jwt: { expiresIn: '1d' }. Refresh token must be stored and used with /auth/refresh-token.
affects: >=4.0.0 <5.0.0
breakingv4 adds two new endpoints: POST /auth/refresh-token and POST /auth/logout. Existing custom routes may conflict.
fix
Ensure no duplicate route definitions for /auth/refresh-token or /auth/logout. Add guards as needed.
affects: >=4.0.0 <5.0.0
deprecatedv3-style single-token authentication is deprecated in v4 and will be removed in v5.
fix
Migrate to dual-token auth: use refresh token for token renewal and logout invalidation.
affects: >=4.0.0 <5.0.0
gotchaRefresh token field option refreshTokenField defaults to 'refreshToken' which is a bcrypt hash stored server-side. If entity schema does not have this field, startup will fail.
fix
Add a string field named 'refreshToken' to the user entity (or configure a different field name via refreshTokenField).
affects: >=4.0.0
Errors
Common errors & fixes
Cannot find module 'class-validator' or its corresponding type declarations.
class-validator is a required peer dependency not installed or not in devDependencies.
fix
Run `npm install class-validator class-transformer` at project root.
DynamicApiModule.forRoot is not a function
Wrong import: using default import instead of named import, or using CommonJS require.
fix
Use named import: import { DynamicApiModule } from 'mongodb-dynamic-api'.
TypeError: entities must be an array of collection names
Missing or invalid entities configuration in forRoot options.
fix
Provide an array of entity/collection names: entities: ['users', 'posts'].
Cannot resolve dependency DynamicApiModule. Make sure it is part of @Module(...) imports.
DynamicApiModule.forRoot() not called inside @Module imports array.
fix
Add DynamicApiModule.forRoot({...}) to the imports of your root or feature module.
Upgrade
Version history
4.15.0latest on npm
Audit
Dependencies
class-validatorrequiredUsed for DTO validation; required by @nestjs/common ValidationPipe integration
Agent activity
11 hits · last 30 days
node
10
Resources
mongodb-dynamic-api — npm install mongodb-dynamic-api · libregistry