npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mongodb-dynamic-api

v2.8.3

Published

Auto generated CRUD API for MongoDB using NestJS

Downloads

32

Readme

NPM version NPM npm

GitHub branch checks state CircleCI Sonar Quality Gate

Sonar Tests Sonar Coverage

Maintainability Rating Reliability Rating Security Rating Lines of Code Duplicated Lines (%)

GitHub top language GitHub code size in bytes GitHub commit activity GitHub last commit (branch)


mongodb-dynamic-api

npm install --save mongodb-dynamic-api

Dynamic 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 --strict option)
nest new --strict your-project-name
npm i -S mongodb-dynamic-api

Basic Configuration

  • Add DynamicApiModule.forRoot to your app.module.ts and 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 User with a name and an email field.

  • We use the @Schema and @Prop decorators from the @nestjs/mongoose package to define our MongoDB model.

  • You must extend the BaseEntity | SoftDeletableEntity class from the mongodb-dynamic-api package for all your collection models. See more details here.

  • You can also add the @DynamicAPISchemaOptions decorator 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.forFeature method to add the User API to our application.
  • We pass the User class to the entity property and specify the path users to the controllerOptions property.
  • Create a new file users.module.ts and 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 UsersModule to the imports in the app.module.ts after the DynamicApiModule.forRoot method.
// 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: