mongodb-dynamic-api v2.10.0
mongodb-dynamic-api
npm install --save mongodb-dynamic-apiDynamic API Modulewith WebSockets
In summary, DynamicApiModule is a flexible and configurable module using NestJS 10 that provides dynamic API functionality for your contents. It must be set up at the root level with global settings and then configured for individual features. It has several optional features such as Swagger UI, Versioning, Validation, Caching, Authentication, Authorization and WebSockets.
HOW TO ENJOY IT
- Start a new nest project with typescript (use the
--strictoption)
nest new --strict your-project-name- Go to your new project root and install the mongodb-dynamic-api package
npm i -S mongodb-dynamic-apiBasic Configuration
- Add
DynamicApiModule.forRootto yourapp.module.tsand pass your MongoDB connection string to the method.
// src/app.module.ts
import { DynamicApiModule } from 'mongodb-dynamic-api';
@Module({
imports: [
DynamicApiModule.forRoot(
'mongodb-uri', // <- replace by your own MongoDB connection string
),
// ...
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}Basic Usage
- Ok, now let's add our first content with just 2 files. It will be a simple
Userwith anameand anemailfield. We use the
@Schemaand@Propdecorators from the @nestjs/mongoose package to define our MongoDB model.You must extend the
BaseEntity|SoftDeletableEntityclass from themongodb-dynamic-apipackage for all your collection models. See more details here.You can also add the
@DynamicAPISchemaOptionsdecorator to pass schema options. See more details here.
Just create a new file user.ts and add the following code.
// src/users/user.ts
import { Prop, Schema } from '@nestjs/mongoose';
import { BaseEntity } from 'mongodb-dynamic-api';
@Schema({ collection: 'users' })
export class User extends BaseEntity { // <- extends BaseEntity
@Prop({ type: String, required: true })
name: string;
@Prop({ type: String, required: true })
email: string;
}- Then we will use the
DynamicApiModule.forFeaturemethod to add theUserAPI to our application. - We pass the
Userclass to theentityproperty and specify the pathusersto thecontrollerOptionsproperty. - Create a new file
users.module.tsand add the following code.
// src/users/users.module.ts
import { DynamicApiModule } from 'mongodb-dynamic-api';
import { User } from './user';
@Module({
imports: [
DynamicApiModule.forFeature({
entity: User,
controllerOptions: {
path: 'users',
},
}),
],
})
export class UsersModule {}- Last step, add the
UsersModuleto the imports in theapp.module.tsafter theDynamicApiModule.forRootmethod.
// src/app.module.ts
import { DynamicApiModule } from 'mongodb-dynamic-api';
import { UsersModule } from './users/users.module';
@Module({
imports: [
DynamicApiModule.forRoot(
'mongodb-uri', // <- replace by your own MongoDB connection string
),
UsersModule,
],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}And that's all ! You now have a fully functional CRUD API for the User content at the /users path.
| Endpoint | Body | Param | Query |
|---|---|---|---|
| GET /users Get many | x | x | x |
| GET /users/:id Get one | x | id: string | x |
| POST /users/many Create many | { list: [{ name: string; email: string; }] } | x | x |
| POST /users Create one | { name: string; email: string; } | x | x |
| PUT /users/:id Replace one | { name: string; email: string; } | id: string | x |
| PATCH /users Update many | { name?: string; email?: string; } | x | ids: string[] |
| PATCH /users/:id Update one | { name?: string; email?: string; } | id: string | x |
| DELETE /users Delete many | x | x | ids: string[] |
| DELETE /users/:id Delete one | x | id: string | x |
| POST /users/duplicate Duplicate many | { name?: string; email?: string; } | x | ids: string[] |
| POST /users/duplicate/:idDuplicate one | { name?: string; email?: string; } | id: string | x |
Go further with optional features like:
9 months ago
9 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago