<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Raphael Häcker</title>
    <description>The latest articles on DEV Community by Raphael Häcker (@raphael_haecker).</description>
    <link>https://dev.to/raphael_haecker</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F573013%2F017f1ece-1fde-4a42-a494-9375f3e7fb6a.jpeg</url>
      <title>DEV Community: Raphael Häcker</title>
      <link>https://dev.to/raphael_haecker</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/raphael_haecker"/>
    <language>en</language>
    <item>
      <title>What I learned about external configuration of the database connection in NestJS</title>
      <dc:creator>Raphael Häcker</dc:creator>
      <pubDate>Fri, 07 May 2021 00:51:13 +0000</pubDate>
      <link>https://dev.to/raphael_haecker/what-i-learned-about-external-configuration-of-the-database-connection-in-nestjs-3jhh</link>
      <guid>https://dev.to/raphael_haecker/what-i-learned-about-external-configuration-of-the-database-connection-in-nestjs-3jhh</guid>
      <description>&lt;p&gt;As part of my 100 day of code challenge, I am building a &lt;a href="https://github.com/rhaecker/buzzz"&gt;web app&lt;/a&gt; using NestJS (and a not yet specified frontend framework). So one of the first things I tried setting up was the database connection. I plan on using a mongodb and because I might switch between different devices for development, I decided to sign up for a free account on a shared cluster at mongodb.com. &lt;/p&gt;

&lt;p&gt;This is a longer description of my journey with this. If you are only interested in the results, &lt;a href="https://dev.tolink:%20the%20results%20article"&gt;click here&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  First steps with the db
&lt;/h1&gt;

&lt;p&gt;Since I’m new to NestJS (and developing with Node as a backend in general) I started this, by following the frameworks &lt;a href="https://docs.nestjs.com/techniques/database"&gt;documentation&lt;/a&gt;. Although there is support for mongoose(link: &lt;a href="https://mongoosejs.com"&gt;https://mongoosejs.com&lt;/a&gt;), I decided to go with TypeORM. TypeORM is an Object Relational Mapper, that is written in TypeScript and since NestJS offers an integration, I felt like this would be a good choice. Another reason is, that I have no experience with mongodb, so if, for some reason, I decide, I won’t be using it for this project, it seems like it will be much easier changing databases with TypeORM, than if I would have used mongoose.&lt;/p&gt;

&lt;p&gt;So following the documentation, I started by adding the database configuration to the &lt;code&gt;app.module.ts&lt;/code&gt; like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'mongodb',
      url: 'mongodb+srv://&amp;lt;username&amp;gt;:&amp;lt;password&amp;gt;@some.subdomains.of.mongodb.net/&amp;lt;databasename&amp;gt;'
    })
  ],
  controllers: [AppController],
  providers: [AppService],
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When I then started the server, the connection worked. Nice, this was easy!&lt;br&gt;
Sadly at that point I stopped reading the documentation and started looking at the code. I realised, that I have to commit this to a public repository and I maybe shouldn’t commit login data to a publicly hosted database (actually, I think, no database connection data, publicly hosted or not, should go into a git repository).&lt;/p&gt;
&lt;h1&gt;
  
  
  Looking for options to configure
&lt;/h1&gt;

&lt;p&gt;So instead of reading the next few paragraphs in the database part of the &lt;a href="https://docs.nestjs.com/techniques/database#typeorm-integration"&gt;NestJS documentation&lt;/a&gt;, I started to search for options on how to do this configuration outside of the code, in a configuration- or environment-file, that I simply don’t have to commit.&lt;br&gt;
So instead of just reading about the ormconfig.json file, that is a way to configure TypeORM I looked for different options. And I found one, the &lt;a href="https://docs.nestjs.com/techniques/configuration"&gt;config module&lt;/a&gt; of NestJS.&lt;br&gt;
It internally uses &lt;a href="https://github.com/motdotla/dotenv"&gt;dotenv&lt;/a&gt;, which is used to load the content of a file called &lt;code&gt;.env&lt;/code&gt; into the Node process.env. This is a common approach in Node.js and is useful to quickly swap the applications environment. The &lt;code&gt;.env&lt;/code&gt;-file holds key-value pairs to represent the environment.&lt;br&gt;
The configuration module of NestJS offers different ways to access and to store environment variables, ranging from different locations for the &lt;code&gt;.env&lt;/code&gt;-file, to ignore the file and only use the runtime environment variables or even use a custom environment file, that allows for a more complex structure such as yaml or json. In this case, the values are parsed into key-value pairs for use in the process.env.&lt;br&gt;
There is a lot more advanced options for more complex situations, but these are the basic options, that the module offers for a simpler use case.&lt;/p&gt;

&lt;p&gt;So I again, opened the documentation of NestJS regarding the config module and ended up including it in the app.module.ts like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Module({
  imports: [
    ConfigModule.forRoot()
    })
  ],
  controllers: [AppController],
  providers: [AppService],
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For a first test, my &lt;code&gt;.env&lt;/code&gt; file only included one value: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;testvalue=testvalue&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;As I wanted to see this in action, I changed the bootstrapped &lt;code&gt;app.service.ts&lt;/code&gt; to look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Injectable()
export class AppService {

  constructor(private configService: ConfigService) {}

  getHello(): string {
    console.log(this.configService.get&amp;lt;string&amp;gt;('testvalue'));
    return 'Hello World!' + this.configService.get&amp;lt;string&amp;gt;('testvalue');
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This loads the value of the key ‘testvalue’ from the &lt;code&gt;.env&lt;/code&gt; file and it works as expected. Nice!&lt;/p&gt;

&lt;p&gt;Now one could be so naive to just think, well let’s use this in my &lt;code&gt;app.module.ts&lt;/code&gt;. That was exactly what I did, but I didn’t get it to work (and I found some examples on the internet, so I think this is more a me problem). So I look into alternatives and found the &lt;code&gt;ormconfig.json&lt;/code&gt; (yes, the one I could have found immediately …).&lt;/p&gt;

&lt;h1&gt;
  
  
  Configuration using the ormconfig.json
&lt;/h1&gt;

&lt;p&gt;So I actually found this in the documentation of TypeORM and because, when I tried to configure TypeORM in the &lt;code&gt;app.module.ts&lt;/code&gt; with the config module, I got this error:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Error: No connection options were found in any orm configuration files.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Searching for this pointed me in the right direction. Configuring the database connection with the &lt;code&gt;ormconfig.json&lt;/code&gt; file (or a lot of other file &lt;a href="https://typeorm.io/#/using-ormconfig"&gt;formats&lt;/a&gt;) is done by creating the file in the root of the project. In my case, I created an &lt;code&gt;ormconfig.json&lt;/code&gt; and it looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "type": "mongodb",
    "url": "mongodb+srv://&amp;lt;username&amp;gt;:&amp;lt;password&amp;gt;@some.subdomains.of.mongodb.net/&amp;lt;databasename&amp;gt;"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If this configuration file is used, it is important to not put any configuration in the &lt;code&gt;app.module.ts&lt;/code&gt;. One problem of this is, that it is an additional configuration file and since I don’t want too many different config files, I’d prefer not to create one only for the database. Another point is that I maybe want to run this in a container and then configure it from Environment variables for example. So this solution might not fit my needs.&lt;/p&gt;

&lt;p&gt;I also discovered the option to configure TypeORM from the environment variables directly, by using the keys &lt;a href="https://typeorm.io/#/using-ormconfig/using-environment-variables"&gt;TypeORM provides&lt;/a&gt;. This would work for me, but there is one thing, that is not possible with this configuration (and most likely I won’t need it for this project, but better having it, than needing it, right?). I can not have more than one database configured.&lt;/p&gt;

&lt;h1&gt;
  
  
  Configuration using a configuration service in NestJS
&lt;/h1&gt;

&lt;p&gt;So what I ended up doing was inspired &lt;a href="https://docs.nestjs.com/techniques/database#async-configuration"&gt;here&lt;/a&gt;. This describes an injectable service, that implements the &lt;code&gt;TypeOrmOptionsFactory&lt;/code&gt; interface, that can be used to asynchronously configure TypeORM. I combined this with the &lt;code&gt;ConfigModule&lt;/code&gt; of NestJS (and I guess, it would be possible to use other config options as well). The service that configures my mongodb looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Injectable()
export class MongoDBConfigService implements TypeOrmOptionsFactory {

    constructor(private configService: ConfigService) {}

    createTypeOrmOptions(): TypeOrmModuleOptions {
        return {
            type: 'mongodb',
            url: this.configService.get&amp;lt;string&amp;gt;('MONGODB_URL'),
            username: this.configService.get&amp;lt;string&amp;gt;('MONGODB_USER'),
            password: this.configService.get&amp;lt;string&amp;gt;('MONGODB_PASSWORD'),
    };
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this service I can add all the configuration parameters I need and I can just create one service per database.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;This was an unnecessary long journey. I think I could have avoided part of it, by just reading the next few instructions or doing a little more research.  On the other hand, I figured out a solution myself. From my point of view it works good and is really flexible, but I also don’t know much about NestJS and TypeORM so this is only a feeling I can’t prove.&lt;/p&gt;

</description>
      <category>100daysofcode</category>
      <category>nestjs</category>
      <category>typeorm</category>
    </item>
    <item>
      <title>NestJS and TypeORM database configuration</title>
      <dc:creator>Raphael Häcker</dc:creator>
      <pubDate>Tue, 04 May 2021 21:47:47 +0000</pubDate>
      <link>https://dev.to/raphael_haecker/nestjs-and-typeorm-database-configuration-15ob</link>
      <guid>https://dev.to/raphael_haecker/nestjs-and-typeorm-database-configuration-15ob</guid>
      <description>&lt;p&gt;After some initial problems I had with this topic, I decided to spend some time to look into different options. Here is an overview of the results:&lt;/p&gt;

&lt;h1&gt;
  
  
  Static configuration in the code
&lt;/h1&gt;

&lt;p&gt;This is the most obvious way to configure the database. Just write the parameters (url, username, …) in the code, where you import the &lt;a href="https://docs.nestjs.com/techniques/database#typeorm-integration"&gt;TypeORM&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: 'localhost',
      port: 3306,
      username: 'root',
      password: 'root',
      database: 'test',
      entities: [],
      synchronize: true,
    }),
  ],
})
export class AppModule {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are a lot of problems with this. You can’t easily change this in a deployed application, it clutters your &lt;code&gt;app.module.ts&lt;/code&gt; file and if you commit this to a public repository, you make your database access data public. Also if you always hard code configurations like this, you might end up having them all over the place in your code.&lt;br&gt;
So while this is fine for a quick test in your development environment, I wouldn’t recommend using this for much more than that.&lt;/p&gt;
&lt;h1&gt;
  
  
  Configuring TypeORM using ormconfig.json
&lt;/h1&gt;

&lt;p&gt;TypeORM offers the possibility to pass configuration parameters for one or more databases using an &lt;code&gt;ormconfig.json&lt;/code&gt; &lt;a href="https://typeorm.io/#/using-ormconfig/using-ormconfigjson"&gt;file&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This file includes all the relevant configuration options. In my case it was not much:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "type": "mongodb",
    "url": "mongodb+srv://&amp;lt;username&amp;gt;:&amp;lt;password&amp;gt;@some.subdomains.of.mongodb.net/&amp;lt;databasename&amp;gt;"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When using the &lt;code&gt;ormconfig.json&lt;/code&gt;, you should not put any options in the &lt;code&gt;app.module.ts&lt;/code&gt;, where you import TypeORM.&lt;/p&gt;

&lt;p&gt;Configuring TypeORM this way is a clean solution, you have one file for the database connection(s) and you can configure more than one database in this file. The downside is, that you have to have this additional file. So in some environments, where you may not be able to access all the files of the application, changing that database might be difficult.&lt;/p&gt;

&lt;h1&gt;
  
  
  Configuring TypeORM with environment variables
&lt;/h1&gt;

&lt;p&gt;As described &lt;a href="https://typeorm.io/#/using-ormconfig/using-environment-variables"&gt;here&lt;/a&gt; it is possible to configure TypeORM using environment variables. TypeORM offers a list of different variables to use in this case. This uses either a &lt;code&gt;.env&lt;/code&gt;-file or the actual environment variables.&lt;/p&gt;

&lt;p&gt;A limitation of this method is, that it is only possible to configure one database. But if you only need one database in your application, this is a good way to quickly configure TypeORM depending on the environment.&lt;/p&gt;

&lt;h1&gt;
  
  
  Configuring TypeORM with a configuration service
&lt;/h1&gt;

&lt;p&gt;This combines the asynchronous configuration that is described in the NestJS &lt;a href="https://docs.nestjs.com/techniques/database#async-configuration"&gt;documentation&lt;/a&gt; with the config module from &lt;a href="https://docs.nestjs.com/techniques/configuration"&gt;NestJS&lt;/a&gt;, that supports in getting values from the environment variables. &lt;/p&gt;

&lt;p&gt;Basically you have to create a service, that can be injected into the TypeORM import and that implements the &lt;code&gt;TypeOrmOptionsFactory&lt;/code&gt; interface. The values this service provides to TypeORM are retrieved from the NestJS config module.&lt;/p&gt;

&lt;p&gt;For my mongodb this looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Injectable } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { TypeOrmModuleOptions, TypeOrmOptionsFactory } from "@nestjs/typeorm";

@Injectable()
export class MongoDBConfigService implements TypeOrmOptionsFactory {

    constructor(private configService: ConfigService) {}

    createTypeOrmOptions(): TypeOrmModuleOptions {
        return {
            type: 'mongodb',
            url: this.configService.get&amp;lt;string&amp;gt;('MONGODB_URL'),
            username: this.configService.get&amp;lt;string&amp;gt;('MONGODB_USER'),
            password: this.configService.get&amp;lt;string&amp;gt;('MONGODB_PASSWORD'),
    };
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can create such a service per database you need. Using it to configure TypeORM in your &lt;code&gt;app.module.ts&lt;/code&gt; would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true
    }),
    TypeOrmModule.forRootAsync({
      useClass: MongoDBConfigService,
      inject: [MongoDBConfigService]
    })
  ],
  controllers: [AppController],
  providers: [AppService, MongoDBConfigService],
})

export class AppModule {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The advantage of this method, is that you can configure multiple databases from the environment of the app and keep your app.module.ts more or less clean. The downside is, it takes a few more steps to set up.&lt;/p&gt;

&lt;p&gt;Since I am new to NestJS and TypeORM, I’m writing down what I’m learning. So if I missed a method to do this or anything I wrote is incorrect, please let me know!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>nestjs</category>
      <category>typeorm</category>
    </item>
    <item>
      <title>My problem with starting out</title>
      <dc:creator>Raphael Häcker</dc:creator>
      <pubDate>Mon, 26 Apr 2021 14:55:29 +0000</pubDate>
      <link>https://dev.to/raphael_haecker/my-problem-with-starting-out-4a7b</link>
      <guid>https://dev.to/raphael_haecker/my-problem-with-starting-out-4a7b</guid>
      <description>&lt;p&gt;A lot of people know this struggle. You have something in mind, that you want to do, like starting to work out, build that cool side project or start a new challenge. But you can’t really force your self to commit to it. There is always either something else to do or you feel like you’re missing something, that you need to get started. So you just keep pushing the thing you actually want to do.&lt;/p&gt;

&lt;h1&gt;
  
  
  The excuses keep coming
&lt;/h1&gt;

&lt;blockquote&gt;
&lt;p&gt;“I can start, after this is done”&lt;br&gt;
“I promised to help with this other project, so I can’t really do this now”&lt;br&gt;
“Starting now means, I will be interrupted again soon”&lt;br&gt;
“My old laptop lacks the storage to do this in a comfortable way”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;These are just a few of the things that came to my mind, whenever I thought about starting something like a side project or the 100 days of code challenge. And the worst part? &lt;br&gt;
I got a new laptop, but I didn’t start. The old one was never really the reason I didn’t start. &lt;br&gt;
The other project I should have done? Not really worked on this either, just felt bad about it. &lt;/p&gt;

&lt;p&gt;It turns out, all the reasons my mind came up with were just excuses. Excuses that allowed me to keep not starting something and just keep procrastinating.&lt;/p&gt;

&lt;h1&gt;
  
  
  So what can I do?
&lt;/h1&gt;

&lt;p&gt;A few weeks ago, I scrolled through twitter and came across a &lt;a href="https://twitter.com/dabit3/status/1377400759642390535?s=20"&gt;tweet&lt;/a&gt; by &lt;a href="@https://twitter.com/dabit3"&gt;Nader Dabit&lt;/a&gt;. He was talking about how he creates technical content and learns something new. That somehow resonated with me. So I asked him for a tip, hoping for something like a hint on tooling or techniques. But he said, just do it and that he has the tendency to procrastinate. Oh? I absolutely have this tendency. I make plans and keep thinking about how to do the things I’d like to do, but don’t actually start to do them.&lt;/p&gt;

&lt;p&gt;From another source (and I sadly can’t remember where) I also got the advice, that if you have a problem motivating yourself, just starting the activity can help. And I know that feeling. Just opening the editor and writing a line to two of code or even just reading it, got me motivated enough to keep going. After a while there are also some results I can look on to keep me motivated.&lt;/p&gt;

&lt;p&gt;So for me it all comes down to just doing, just starting with something. &lt;/p&gt;

&lt;h1&gt;
  
  
  So I’m starting today
&lt;/h1&gt;

&lt;p&gt;With this post I’m starting with the 100 days of code challenge. &lt;br&gt;
What I want to do in the 100 days: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;I want to build an app, that works like a buzzer. You get a button you can press for example if you know the answer in a quizz. The host of the game then sees who pressed the button first. &lt;/li&gt;
&lt;li&gt;I want to use technologies or frameworks that I am not used to. The current planned stack is react for the frontend (I want to try react native!), NestJS for the Backend and MongoDB for storage.&lt;/li&gt;
&lt;li&gt;I want to write about what I do, maybe produce some videos or stream my coding sessions. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I haven’t everything ready. For example I didn’t set anything up to do the live streams or produce the videos. I also haven’t everything planned out for the app. So I will do this as I go with the challenge. I hope planning and set up sessions can also count towards the daily goal.&lt;/p&gt;

&lt;h1&gt;
  
  
  And now?
&lt;/h1&gt;

&lt;p&gt;Now I’m starting with my 100 days of code. Today I will &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Set up all the repositories &lt;/li&gt;
&lt;li&gt;Write a description of the app I want to build &lt;/li&gt;
&lt;li&gt;Tweet about it&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I know, none of that is coding, but if I keep planning for this, I will never start. In the next days I will definitely code. And if something interesting happens, I will try to write about it. &lt;br&gt;
So if you’re interested in my journey through this challenge follow me here or on &lt;a href="https://twitter.com/raphael_haecker"&gt;Twitter&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>motivation</category>
      <category>100daysofcode</category>
    </item>
  </channel>
</rss>
