DEV Community

Cover image for 48-Nodejs Course 2023: Database Models: Custom Data Casting: Encrypted Password
Hasan Zohdy
Hasan Zohdy

Posted on

48-Nodejs Course 2023: Database Models: Custom Data Casting: Encrypted Password

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 to achieve this easily.

Let's install it

yarn add @mongez/password
Enter fullscreen mode Exit fullscreen mode

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

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

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

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

Now let's try to create a new user

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

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

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

11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!