Code Generation NodeJs and PostgreSQL
AutoCodeGen now supports REST API code generation for the PostgreSQL database.
If you know how to write SQL queries for the PostgreSQL database, then you should be able to build a backend web service without having to write a single line of code using AutoCodeGens code generation platform.
In this article, we will discuss how to generate NodeJS REST API code, which communicates with the PostgreSQL database and performs CRUD operations.
Go to AutoCodeGen and click on get started anywhere within the page, it will take us to the code generation platform. If you do not have an account, drop your email and signup. And once you are inside, you will see what is shown below.
You have access to a free subscription, which is shared by all users of autocodegen. Click on the View button, which will take us to the project page.
On the project page, click on create, and you can choose between MongoDB and PostgreSQL for the database. Choose PostgreSQL database. Node.JS is the only programming language to choice currently. Now we give it a project name.
We are going to build a school_students_info_manager project, which manages basic information about school students. When the project is created it will be available in the table below, now click on create to launch the project.
Once inside give it a project name again and save it.
Before we go on to define our data structure, let us see what are all the different data types that are available to us.
As we can see above, we have Text, Text array, Numeric, Numeric Array, Boolean, Varchar, Varchar Array, Date, and Time. Geeks for Geeks has good information on when to use what data types for the PostgreSQL database, and you can check it out here.
Now let us input the data structure for our student info table as shown below.
And once it is done follow below steps to generate code,
- click on generate button
- paste the project name in the same format
- and then click on “Generate”.
Once the code generation is done, which will take like a couple of seconds, you are good to download the code.
What we see above is the list of files generated. The package.json file is where all our dependencies are defined. index.js is our entry point for the web service, you can know more about each of the files by going through the code.
No modifications need to be made apart from updating our PostgreSQL DB connection information, which you can do from here. Once it is done, it is time to run the following command from your VS code terminal, which is npm i, which will install all the necessary dependencies, and node index.js, which will run our server at http://localhost:8080
As we can see above, our server is up and running on port 8080, Great success, we got a backend server up and running without writing a single line of code. Now it is time to test it using Postman, which we will discuss in the upcoming article.