DEV Community

thesuperankes
thesuperankes

Posted on • Edited on

REST API CRUD NodeJS, typescript, mongo, express with 2 commands.

SuperApiBoost is a client who seeks to improve development times.

In this post I will show you how to make a complete api under NodeJs with Typescript, Mongo and Express with just two commands an a file.

First thing they need to do is install the client with npm.

npm i -g superapiboost
Enter fullscreen mode Exit fullscreen mode

To generate the project you must execute the "new" command with the -n flag to assign the name

sabo new -n=Awesome
Enter fullscreen mode Exit fullscreen mode

This will create a root folder with the name of the project and install the dependencies.

📦Awesome
 ┣ 📂node_modules
 ┣ 📂src
 ┃ ┣ 📂api
 ┃ ┃ ┣ 📂routes
 ┃ ┃ ┗ 📜index.ts
 ┃ ┣ 📂controllers
 ┃ ┃ ┗ 📜mongoBasic.ts #CRUD Methods
 ┃ ┣ 📂interfaces
 ┃ ┣ 📂tools #utils functions
 ┃ ┃ ┗ 📜validateType.ts
 ┃ ┣ 📜app.ts
 ┃ ┣ 📜config.ts
 ┃ ┗ 📜mongo.ts
Enter fullscreen mode Exit fullscreen mode

As an example we will create a crud for the following json.

{
  "name":"Andy",
  "cellphone":303030303,
  "isValid":false
}
Enter fullscreen mode Exit fullscreen mode

We will create a json file and use the following format for the generation of the routes and the controller.

{
  "name":{
    "type":"string",
    "required":true,
    "default":"'Andy'"
  },
  "cellphone":{
    "type":"number",
    "required":false
  },
  "isValid":{
    "type":"boolean",
    "required":true,
    "default":"true"
  }
}
Enter fullscreen mode Exit fullscreen mode

as a key we will assign the name of the property to create.

type: string, number, date, [].
required: false, true.
default(optional): 'true','"Name"'.

We will copy the complete path and execute the generate command.

sabo generate -n=user -p=c:/models/user.json
Enter fullscreen mode Exit fullscreen mode

This will create the interface, controller and routes.

Go to the config.ts file and assign the connection string of our mongo database.

Execute npm start and we will be able to consume the api with our preferred client.

Create

Create

Update

Update

Delete

Delete

GetAll

GetAll

GetById

GetById

Top comments (0)