Use TypeScript to create secure APIs with Node.js and Express – Create Data Models and Services

Tram Ho

Hi, in this series of Using TypeScript to write security APIs with Node.js and Express. , I would like to move on to the second lesson about creating Data Models and Services.

Overview of posts

  1. Introduction and initial installation
  2. Create Data Model and Services
  3. Create Endpoints
  4. API security
  5. Rights management

⚠️ You should read each article in turn to understand the contents of the series.

Before creating controllers and services, we will define the data structure in this application. A menu item will have several possible attribute types. I will take the following example:

  • id : (number) Unique identifier for each item
  • name : (string) item name
  • price : (number) the price of each item is assumed in VND
  • description : (string) describes item
  • image : (string) The URL points to the image of the item

Using TypeScript – an object-oriented language – to build the API, gives us various options to define and execute the structure of objects. We can use classes or interfaces to define them. For menu items, interfaces can be used because they are not part of the compiled JavaScript package; furthermore, for simplicity, we do not need instances of the menu item.

We will create a subdirectory named items in the src directory to save the files related to the menu items:

Model API Resources with Interfaces

To model the data, we will define an interface Item as follows:

File content:

Next, define an interface Items is a collection of Item :

File content:

Create Data Service to manipulate API Resource

Service allows packaging of related business logic (which we can share to many other projects). Thus, our application can use the service to access and manipulate records from the store.

Create module service definition file:

File content:

In the Data Model Interfaces , we will import interfaces:

For simplicity, I will not create a database just to save the records. Instead, I will create some representative objects and store them in RAM:

It should be noted that whenever you reset the server, the entire object represented above will be deleted from RAM. But thanks to HMR use of webpack does not need to be reset anymore, unless there is a change in the file service.

Next, we will create main CRUD methods with items. The first will be 2 methods of finding items:

Here I use asynchronous to simulate read and write operations in accordance with reality. Looking at the function name and its content, you can guess what the purpose of each function is.

CREATE

Next, we will create the CREATE method and add below the find function above:

The CREATE method simply takes an argument that is an object of type Item . This object will have properties as defined, except id will be created in the function. And to create an unique id , we use the value of the current date – DATE .

UPDATE

Next, we will add the UPDATE method right after CREATE :

The UPDATE method takes an object parameter of type Item . Here, the Object must include the id (obviously).

DELETE

Finally, we define the REMOVE method:

The REMOVE method takes a parameter as the id of the item to be deleted.

So we have just finished creating a service module. Any other project can use this service module’s code because it is not tied to any specific framework. In the next article, we will use them to create API Controllers

It is worth mentioning that you may have used a TypeScript class to define and encapsulate logical services. However, using functions makes testing module services easier.

The article may be confusing or erroneous, please comment. Thank you for reading my post. See you in the next article on Creating Endpoints

Share the news now

Source : Viblo