86-Nodejs Course 2023: Resource: Data And Resource Setters And Getters
Tags: node, typescript, mongodb, fastify
Published: 2022-12-04 14:34:06

---
title: 86-Nodejs Course 2023: Resource: Data And Resource Setters And Getters
published: true
description: 
series: Nodejs Course 2023
tags: nodejs, typescript, mongodb, fastify
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w68gfnbo2xsxyxwimd7w.png
# Use a ratio of 100:42 for best results.
---

In our last talk about resources, we added two new methods `boot` and `extend`, these methods are meant to be called before transforming output and after transforming output respectively.

But we need some helpers to make it easier on our code to work with data and resources, so let's add some good helpers.

## Set data to final output

Before we start, a quick reminder, the `this.data` property refers to the final output of the resource and `this.resource` refers to the original resource that is going to be transformed.

Now let's add a new method to add a new output key that will be added to the final output.

```ts
// src/core/resources/resource.ts
import { set } from "@mongez/reinforcements";

// ...

export default class Resource {
  // ...
  /**
   * Set value to the final output
   */
  public set(key: string, value: any) {
    set(this.data, key, value);

    return this;
  }
  // ...
}
```

That's a simple `set` method but internally we used [Set utility](https://github.com/hassanzohdy/reinforcements#setting-value-in-object) to set the key in the final output even if it is nested.

Example of usage:

```ts
// src/app/users/resources/user-resource.ts

// ...
protected async boot() {
  this.set('address.location.city', 'Cairo');
}
```

This will add a new key `address.location.city` to the final output with the value `Cairo`, and any of the parent keys like `address` and `location` if they were not defined will be added automatically.

## Removing keys from final output

Let's implement the exact negate of the `set` method, we'll add a new method to remove a key or more from the final output.

```ts
// src/core/resources/resource.ts
import { set, unset } from "@mongez/reinforcements";

// ...

export default class Resource {
  // ...

  /**
   * Remove a key from the final output
   */
  public remove(...keys: string[]) {
    unset(this.data, keys);
  }
  // ...
}
```

This method will remove one or more keys from the final output, the good thing about [unset utility](https://github.com/hassanzohdy/reinforcements#unset-keys-from-object) that it allows removing nested keys.

Example of usage:

```ts
// src/app/users/resources/user-resource.ts

// ...
protected async boot() {
  this.remove('address.location.city', 'name');
}
```

This will remove the `address.location.city` and `name` keys from the final output.

## Getting value from resource

Now let's add our last utility in this article, we need a way to get a value from the resource, this helper method will allow us to get a value using dot notation, if that key does not exist, it will return the default value.

```ts
// src/core/resources/resource.ts
import { set, unset, get } from "@mongez/reinforcements";

// ...

export default class Resource {
  // ...

  /**
   * Get value from the resource
   */
  public get(key: string, defaultValue?: any) {
    return get(this.resource, key, defaultValue);
  }
  // ...
}
```

Simple and clean, this will allow us to fetch a value from the resource using dot notation, and if that key does not exist, it will return the default value.

Example of usage:

```ts
// src/app/users/resources/user-resource.ts

// ...

protected async boot() {
  const name = this.get('name', 'John Doe');

  this.set('name', name);
}
```

This will get the `name` key from the resource, if it does not exist, it will return `John Doe` as the default value.

## 🎨 Conclusion

In this article, we added three new methods to the resource class, these methods will help us to set data to the final output, remove keys from the final output, and get a value from the resource.

## ☕♨️ 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)