86-Nodejs Course 2023: Resource: Data And Resource Setters And Getters

hassanzohdy

Hasan Zohdy

Posted on December 4, 2022

86-Nodejs Course 2023: Resource: Data And Resource Setters And Getters

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.

// 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;
  }
  // ...
}
Enter fullscreen mode Exit fullscreen mode

That's a simple set method but internally we used Set utility to set the key in the final output even if it is nested.

Example of usage:

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

// ...
protected async boot() {
  this.set('address.location.city', 'Cairo');
}
Enter fullscreen mode Exit fullscreen mode

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.

// 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);
  }
  // ...
}
Enter fullscreen mode Exit fullscreen mode

This method will remove one or more keys from the final output, the good thing about unset utility that it allows removing nested keys.

Example of usage:

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

// ...
protected async boot() {
  this.remove('address.location.city', 'name');
}
Enter fullscreen mode Exit fullscreen mode

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.

// 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);
  }
  // ...
}
Enter fullscreen mode Exit fullscreen mode

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:

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

// ...

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

  this.set('name', name);
}
Enter fullscreen mode Exit fullscreen mode

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, 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)

💖 💪 🙅 🚩
hassanzohdy
Hasan Zohdy

Posted on December 4, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related