{"componentChunkName":"component---src-templates-post-template-js","path":"/posts/mongoose-to-typeorm-equivalent-of-pre-save-in-typeorm","result":{"data":{"markdownRemark":{"id":"0b329b77-760c-5dec-ad15-79db01e506ec","html":"<p>I have a code where I create a user and hash the password before saving.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">userSchema.pre(&#39;save&#39;, async function(next) {\n  const user = this;\n  if (user.isModified(&#39;password&#39;)) {\n    user.password = await brcrypt.hash(user.password, BCRYPT_HASH_ROUND);\n  }\n  next();\n});</code></pre></div>\n<p>The <code class=\"language-text\">userSchema</code> could be any data model but the interesting part here is the <code class=\"language-text\">pre(&#39;save&#39;), ...</code></p>\n<p>Lately, I’ve been playing around with <a href=\"https://nestjs.com/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Nestjs</a> to use for my next project and I was trying to implement a user management with it.</p>\n<p>Here’s my code when adding a new user</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">import { Repository, EntityRepository } from &#39;typeorm&#39;;\nimport { User } from &#39;./user.entity&#39;;\nimport { UserCredentialsDto } from &#39;./dto/user-credentials.dto&#39;;\nimport * as bcrypt from &#39;bcryptjs&#39;;\nconst BCRYPT_HASH_ROUND = 8;\n\n@EntityRepository(User)\nexport class UserRepository extends Repository&lt;User&gt; {\n  async add(userCredentialsDTO: UserCredentialsDto): Promise&lt;any&gt; {\n    const { email, password } = userCredentialsDTO;\n    const user = new User();\n    user.email = email;\n    user.password = await bcrypt.hash(this.password, BCRYPT_HASH_ROUND);\n    try {\n       return await user.save();\n    } catch (e) {\n      if (e.code === &#39;23505&#39;) {\n        throw new Error(&#39;Email already exists&#39;);\n      }\n    }\n  }\n}</code></pre></div>\n<p>In my code, I’m hashing the password explicity before executing the <code class=\"language-text\">user.save()</code> method. I want what I have in <code class=\"language-text\">mongoose</code> where I tapped in the <code class=\"language-text\">pre-save</code> event and hash the password from there.</p>\n<p>To do this, I just need to use <code class=\"language-text\">@BeforeInsert</code> listener on my user entity like this</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">import {\n  BaseEntity,\n  Entity,\n  PrimaryGeneratedColumn,\n  Column,\n  BeforeInsert,\n} from &#39;typeorm&#39;;\nimport * as bcrypt from &#39;bcryptjs&#39;;\nconst BCRYPT_HASH_ROUND = 8;\n\n@Entity()\nexport class User extends BaseEntity {\n  @PrimaryGeneratedColumn()\n  id: number;\n\n  @Column({ unique: true, length: 256 })\n  email: string;\n\n  @Column()\n  password: string;\n\n  @BeforeInsert()\n  async beforeInsert() {\n    this.password = await bcrypt.hash(this.password, BCRYPT_HASH_ROUND);\n  }\n  // imagine more code below\n  ....</code></pre></div>\n<p>Another benefit with this approach would be that the <code class=\"language-text\">UserRepository</code> doesn’t need to know anything about <code class=\"language-text\">bcryptjs</code>!</p>\n<h3 id=\"todayilearned\" style=\"position:relative;\"><a href=\"#todayilearned\" aria-label=\"todayilearned permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>#TodayILearned</h3>","fields":{"slug":"posts/mongoose-to-typeorm-equivalent-of-pre-save-in-typeorm","tagSlugs":["/tag/mongoose/","/tag/typeorm/","/tag/nestjs/","/tag/today-i-learned/"]},"frontmatter":{"date":"2019-07-08T01:32:16.000Z","description":"What is the equivalent of pre-save in TypeORM?","tags":["mongoose","typeorm","nestjs","TodayILearned"],"title":"Mongoose to TypeORM - What is the equivalent of pre-save in TypeORM?"}}},"pageContext":{"slug":"posts/mongoose-to-typeorm-equivalent-of-pre-save-in-typeorm"}},"staticQueryHashes":["251939775","401334301","4072520377"]}