Creating admin-like web applications with NestJS and React Admin. Part 1.

Basic bootstrapping of API and UI.

Genadii Ganebnyi
May 30, 2019 · 5 min read

Introduction

In this series of articles, I will describe how to quickly bootstrap an API-based administration panel for your project using NestJS and React Admin.

In order to proceed further (in casу you are not reading just for fun), you may need to have NodeJS 10+, npm, yarn and MySQL installed on your computer. Also, you should have some basic knowledge of TypeScript and React.

Our project will consist of 2 part:

  • REST API, written in TypeScript
  • Admin panel, written in React

For demo purposes, we will create a simple application for managing guests list. This will include creating guests, showing list and updating guests info.

So let's start.

Creating API

For creating API we will use NestJS framework. I enjoy NestJS because it is TypeScript based and thus allows producing better readable, better structured and less error prone backend code.

We will use NestJS CLI tool to initialize our backend:

Now when the project skeleton is ready we will add other dependencies we need.

We will use TypeORM (again TypeScript) for working with MySQL:

NestJS CRUD library to simplify our endpoints creation:

And NestJS Config for managing our application configuration:

Once done with dependencies lets generate the skeleton of our Guests API endpoint

And create a model class for our guest entity (src/guests/guest.entity.ts)

Now we add some code to wire parts together

Update src/guests/guests.module.ts with TypeORM dependencies

Make src/guests/guests.service.ts extend RepositoryService from NestJS CRUD

and add it to src/guests/guests.controller.ts. Also, add Crud decorator to the controller in order to enable NestJS CRUD API-related features.

Finally, we configure TypeORM in the main module (src/app.module.ts):

And add the appropriate configuration files

src/config/database.ts:

.env:

And we are ready to start!

Not so fast :) Later on React Admin will require CORS to be enabled on API side. So we need to modify src/main.ts. And by the way lets make React’s life easier and free 3000 port!

Now it is really ready!

and continue to creating out administration panel UI.

Creating Admin UI

As mentioned before for this purpose we will use React Admin — a React based component library for creating admin-like interfaces.

Let’s start with initializing a React application

Then add React Admin to the project

and a small library developed by us (FusionWorks): @FusionWorks/ra-data-nest-crud, which integrates React Admin with our NestJS CRUD-based backend.

After this we are redy to initializing React Admin component and create guest editor. First, we update src/App.js with root Admin component and Resource component for guests:

Then add corresponding forms to src/Guests/index.js.

Please note that we are using React Admin’s ListGuesser and ShowGuesser for list and show veiws. If needed they could be replaced with custom implementation same way as create and edit forms below.

Once done with this, we are ready to rock!

Conclusion

So far everything looks great, but we have not yet touched such things as authentication, authorization, cases when database and API models should differ, etc. I will cover them in the next articles and we’ll if this stack survives nicely. So stay tuned to the FusionWorks:

Useful links

Source code for the article: https://github.com/FusionWorks/nestjs-crud-react-admin-boilerplate

If you want to dive dipper yourself here is the set of links to documentation that might be useful for you:

FusionWorks

Official company blog

FusionWorks

FusionWorks is a software development company that focuses on outsourcing services, full-cycle product development and IT community building. The company was founded in 2011 by Genadii Ganebnyi and Anton Perkin and has its headquarters in Moldova. More at https://fusion.works

Genadii Ganebnyi

Written by

СТО @ FusionWorks

FusionWorks

FusionWorks is a software development company that focuses on outsourcing services, full-cycle product development and IT community building. The company was founded in 2011 by Genadii Ganebnyi and Anton Perkin and has its headquarters in Moldova. More at https://fusion.works