82-Nodejs Course 2023: Resources: Resource Collection
Tags: node, typescript, mongodb, fastify
Published: 2022-12-03 10:43:03

---
title: 82-Nodejs Course 2023: Resources: Resource Collection
published: true
description: 
series: Nodejs Course 2023
tags: nodejs, typescript, mongodb, fastify
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rnwzw52ouc6jk7pbwbgl.png
# Use a ratio of 100:42 for best results.
---

Let's go with a new feature here, Getting collection (`array`) of resources from single resource.

## Use Case

We're going to list users, so we want to get users models then loop over these models and inject each model to `UserResource` then return the array of resources.

We have here two cases, firstly, we need to parse the given resource, wether it's a plain object, model or a resource, thus we can collect data properly from it.

Secondly, to get the collection of resources.

## Parsing Resource

We already receives the resource in the constructor, but for now we're just treating it as if its a plain object or just an object and we're trying to access a value from it.

Now let's add two new cases, if the resource is a model, then we'll get the data from it, and if its a resource, then we'll get the resource from it.

```ts
// src/core/resources/resource.ts
// ...

import { Model } from "core/database";

// ...


export default class Resource {
  // ...
  /**
   * Constructor
   */
  public constructor(protected resource: any = {}) {
    //
    if (this.resource instanceof Model) {
      this.resource = this.resource.data;
    } else if (this.resource instanceof Resource) {
      this.resource = this.resource.resource;
    }
  }
  // ...
}
```

In the constructor, we'll check if the resource is a model, then we'll get the data from it, and if its a resource, then we'll get the resource from it.

Now let's add a new static method, we'll call it `collect` this method will receive bunch of data regardless of its type, and it will return an array of resources.

```ts
// src/core/resources/resource.ts


import { Model } from "core/database";

// ...


export default class Resource {
  // ...
  /**
   * Constructor
   */
  public constructor(protected resource: any = {}) {
    //
    if (this.resource instanceof Model) {
      this.resource = this.resource.data;
    } else if (this.resource instanceof Resource) {
      this.resource = this.resource.resource;
    }
  }
  
  /**
   * return list of resources for the given array ouf data
   */
  public static collect(data: any[]) {
    return data.map(item => {
      return new this(item);
    });
  }
  // ...
}
```

Now we can use it like this:

```ts
// src/app/users/controllers/users-list.ts
import User from "../models/user";
import UserResource from "../resources/user-resource";

export default async function usersList() {
  const users = await User.list();

  return {
    users: UserResource.collect(users),
  };
}
```

Now the `collect` method will loop over all users and return a new instance of `UserResource` for each user.

The question here, why it is a static method? well because we don't have yet an instance, we need to make an instance for each collection item therefore it doesn't make sense to make it anything else but a static method.

## 🎨 Conclusion

We added two simple features here, first one is to parse the resource, wether it's a plain object, model or a resource, thus we can collect data properly from it.

Secondly, to get the collection of resources using our static method `collect`.

We did two things here in this article, we created a new method `transformValue` which will transform a single value, and we also updated our `toJSON` method to use this new method, and also to check if the value is an array, if it is then we'll loop over it and apply the transformation on each item.

## ☕♨️ Buy me a Coffee ♨️☕

If you enjoy my articles and see it useful to you, you may [buy me a coffee](https://www.paypal.com/paypalme/hassanzohdy), 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](https://github.com/hassanzohdy/nodejs-2023)

## 😍 Join our community

Join our community on [Discord](https://discord.gg/pb2vmdfhGf) 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](https://www.youtube.com/playlist?list=PLGO8ntvxgiZMJc7RN2lIq9WmMOlWZGzmz), 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

* [Event Driven Architecture: A Practical Guide in Javascript](https://dev.to/hassanzohdy/event-driven-architecture-the-best-paradigm-that-i-love-to-work-with-in-javascript-and-node-js-1gnk)
* [Best Practices For Case Styles: Camel, Pascal, Snake, and Kebab Case In Node And Javascript](https://dev.to/hassanzohdy/best-practices-for-case-styles-camel-pascal-snake-and-kebab-case-in-node-and-javascript-55oi)
* [After 6 years of practicing MongoDB, Here are my thoughts on MongoDB vs MySQL
](https://dev.to/hassanzohdy/after-6-years-of-practicing-mongodb-here-are-my-thoughts-on-mongodb-vs-mysql-574b)

Packages & Libraries

* [Collections: Your ultimate Javascript Arrays Manager](https://dev.to/hassanzohdy/collections-your-ultimate-javascript-array-handler-3o15)
* [Supportive Is: an elegant utility to check types of values in JavaScript](https://dev.to/hassanzohdy/supportive-is-an-elegant-utility-to-check-types-of-values-in-javascript-1b3e)
* [Localization: An agnostic i18n package to manage localization in your project](https://dev.to/hassanzohdy/mongez-localization-the-simplest-way-to-translate-your-website-regardless-your-favorite-framework-4gi3)

 React Js Packages

* [useFetcher: easiest way to fetch data in React Js](https://dev.to/hassanzohdy/usefetcher-the-easiest-way-to-fetch-data-in-react-45o9)

Courses (Articles)

* [OOP In JS And TS From The Very Beginning](https://dev.to/hassanzohdy/jsts-oop-course-1-introduction-2ld)
* [ES6++: The Ultimate Guide to ES6 and Beyond](https://dev.to/hassanzohdy/es6-1-introduction-h3e)
* [React Js: Let"s Create File Manager With React Js and Node Js](https://dev.to/hassanzohdy/lets-create-a-file-manager-from-scratch-with-react-and-typescript-chapter-i-a-good-way-to-expand-your-experience-5g4k)