48-Nodejs Course 2023: Database Models: Custom Data Casting: Encrypted Password
Tags: node, typescript, mongodb, fastify
Published: 2022-11-07 10:46:36

---
title: 48-Nodejs Course 2023: Database Models: Custom Data Casting: Encrypted Password
published: true
description: 
series: Nodejs Course 2023
tags: nodejs, typescript, mongodb, fastify
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1t7u2329gvy8f74whrak.png
# Use a ratio of 100:42 for best results.
---

## Custom Casts

Now let's talk about custom casts, what if we want to cast a value to a custom type, for example, we want to cast the password value.

Let's make a folder called `casts` in the `src/core/database/casts` folder and create `cast-password.ts` file.

We need to generate a make a password generated and to be solid, we will use [Password Hash Generator And Verifier](https://github.com/hassanzohdy/mongez-password) to achieve this easily.

Let's install it

```bash
yarn add @mongez/password
```

Now let's create the `cast-password.ts` file

```ts
// src/core/database/casts/cast-password.ts
import { hash } from '@mongez/password';

/**
 * Cast password
 */
export default function castPassword(
  columnName: string,
  value: any
) {
  return hash(value, 15); // second argument is the salt
}
```

Now let's update the `User` model to use this cast

```ts
// src/app/users/models/user.ts
import { Model } from "core/database";
import castPassword from "core/database/casts/cast-password";
import { Casts, Document } from "core/database/model/types";

export default class User extends Model {
  /**
   * Collection name
   */
  public static collectionName = "users";

  /**
   * {@inheritDoc}
   */
  public defaultValue: Document = {
    isActive: true,
    isEmailVerified: false,
    isPhoneVerified: false,
  };

  protected casts: Casts = {
    isActive: "boolean",
    isPhoneVerified: "boolean",
    joinDate: "date",
    password: castPassword,
  };
}
```

Now let's try to create a new user

```ts
// src/app/users/routes.ts
import User from './models/user';

const user = new User({
  name: 'Hassan',
  password: '123456',
});

await user.save();

consol.log(user.data); // { name: 'Hassan', password: '$2b$23$...' }
```

And that's it!

## 🎨 Conclusion

In this lesson, we learned how to create custom casts, and we created a custom cast for the password value.

## 🚀 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)

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