DEV Community

Cover image for 92-Nodejs Course 2023: Restful Routes: Listing Records
Hasan Zohdy
Hasan Zohdy

Posted on

 

92-Nodejs Course 2023: Restful Routes: Listing Records

We saw in our previous article how we implemented our Restful class, now we'll implement the list method.

πŸ“ Workflow

The point here is the child restful class i.e RestfulUser class we need to define inside it the base model that will perform our database operations, so firstly we need to define the base model in the Restful class.

OK what next? next we'll use that model in the base class to list all the records.

Also, we can pass a query object to the list method to filter the records from the request using request.all() method.

As we already have implemented our pagination, we can also return the pagination data in the response.

πŸš€ Implementation

Navigate to our Restful class and add the following code:

// src/core/router/restful.ts
import { Model } from "core/database";
import { Request, Response } from "core/http";
import { RouteResource } from "core/router/types";

export default class Restful implements RouteResource {
  /**
   * Base model
   */
  protected model?: Model;

  /**
   * List records
   */
  public async list(request: Request, response: Response) {
    const records = await this.model?.list(request.all()) || [];

    return response.success({
      records,
    });
  }
// ...
Enter fullscreen mode Exit fullscreen mode

Once you write down this and you'll find that Typescript Compiler is screaming at you, you can not use list method directly from the Model class, thus we need to use ChildModel type instead.

But we didn't export the model types file, so let's do this first

// core/database/model/index.ts
export { default as Model } from "./model";
export * from "./types";
Enter fullscreen mode Exit fullscreen mode

Now we can import the ChildModel type from the database directly.

// src/core/router/restful.ts
import { Model, ChildModel } from "core/database";
import { Request, Response } from "core/http";
import { RouteResource } from "core/router/types";

export default class Restful implements RouteResource {
  /**
   * Base model
   */
  protected model?: ChildModel;

  /**
   * List records
   */
  public async list(request: Request, response: Response) {
    const records = await this.model?.list(request.all()) || [];

    return response.success({
      records,
    });
  }
// ...
Enter fullscreen mode Exit fullscreen mode

This is good, but if you remember that the ChildModel is a generic type, so we need to pass the model type to it, we can pass the type using our Restful class declaration.

// src/core/router/restful.ts
import { Model, ChildModel } from "core/database";
import { Request, Response } from "core/http";
import { RouteResource } from "core/router/types";

export default class Restful<T> implements RouteResource {
  /**
   * Base model
   */
  protected model?: ChildModel<T>;

  /**
   * List records
   */
  public async list(request: Request, response: Response) {
    const records = await this.model?.list(request.all()) || [];

    return response.success({
      records,
    });
  }
// ...
Enter fullscreen mode Exit fullscreen mode

Now we're good, do you want to make it more fancy? let's make the T type extends the Model class, so we can use the list method directly from the T type.

// src/core/router/restful.ts
import { Model, ChildModel } from "core/database";
import { Request, Response } from "core/http";
import { RouteResource } from "core/router/types";

export default class Restful<T extends Model> implements RouteResource {
  /**
   * Base model
   */
  protected model?: ChildModel<T>;

  /**
   * List records
   */
  public async list(request: Request, response: Response) {
    const records = await this.model?.list(request.all()) || [];

    return response.success({
      records,
    });
  }
// ...
Enter fullscreen mode Exit fullscreen mode

Now that's an elegant code

The final question remains here is why i added || [] at the end? well, because maybe you would forget to pass the model to the Restful class, so we need to make sure that the records variable is an array.

πŸ“ Testing

Now let's put this code into test, let's try it in our RestfulUser class

// src/app/user/controller/restful-user.ts
import { Request, Response } from "core/http";
import Restful from "core/router/restful";
import { RouteResource } from "core/router/types";
import User from "../models/user";

// Note that we passed the model πŸ‘‡πŸ» type as a generic type to the Restful class
class RestfulUser extends Restful<User> implements RouteResource {
  /**
   * Base model
   */
  protected model = User;

  /**
   * Get single record
   */
  public async get(request: Request, response: Response) {
    //
  }

  /**
   * Create record
   */
  public async create(request: Request) {
    //
  }

  /**
   * Update record
   */
  public async update(request: Request, response: Response) {
    //
  }

  /**
   * Delete record
   */
  public async delete(request: Request, response: Response) {
    //
  }

  /**
   * Patch record
   */
  public async patch(request: Request, response: Response) {
    //
  }
}

const restfulUser = new RestfulUser();

export default restfulUser;
Enter fullscreen mode Exit fullscreen mode

Now we can call our beloved route /users directly, no more code to be added, we defined the model and also defined the model type, so we're good to go now.

The other good part here, is we can use also the filter directly, just try /users?name=hasan or whatever value you add, and you'll see that the filter is working!

🎨 Conclusion

We just did some pretty good things here, learnt how to use generics with good practical example, also learnt how to implement the list method so we don't have to write it to every controller.

β˜•β™¨οΈ Buy me a Coffee β™¨οΈβ˜•

If you enjoy my articles and see it useful to you, you may buy me a coffee, it will help me to keep going and keep creating more content.

πŸš€ Project Repository

You can find the latest updates of this project on Github

😍 Join our community

Join our community on Discord to get help and support (Node Js 2023 Channel).

🎞️ Video Course (Arabic Voice)

If you want to learn this course in video format, you can find it on Youtube, the course is in Arabic language.

πŸ“š Bonus Content πŸ“š

You may have a look at these articles, it will definitely boost your knowledge and productivity.

General Topics

Packages & Libraries

React Js Packages

Courses (Articles)

Top comments (0)

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.