<?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>Forem: Han Mai</title>
    <description>The latest articles on Forem by Han Mai (@maixuanhan).</description>
    <link>https://forem.com/maixuanhan</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%2F214636%2F1d78621b-63fe-41fe-8199-1ab4ad5c1048.png</url>
      <title>Forem: Han Mai</title>
      <link>https://forem.com/maixuanhan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/maixuanhan"/>
    <language>en</language>
    <item>
      <title>Need ideas about some SW/application for writing</title>
      <dc:creator>Han Mai</dc:creator>
      <pubDate>Sat, 19 Jun 2021 16:49:57 +0000</pubDate>
      <link>https://forem.com/maixuanhan/need-ideas-about-some-sw-application-for-writing-11ee</link>
      <guid>https://forem.com/maixuanhan/need-ideas-about-some-sw-application-for-writing-11ee</guid>
      <description>&lt;p&gt;Hi, I want to write about some technical discussion about system designs, architecture designs, programming techniques and how to deal with troubles in real-world but I've found that it is very difficult to just explain topics via multiple arbitrary and unconnected examples. I've got some topics in my mind but I cannot convey them into words. My writing is not good enough. So I'm thinking of implementing some real product while writing about it at a same time. I can post all the design considerations here and will receive feedbacks which will help me (and maybe other people) a lot.&lt;/p&gt;

&lt;p&gt;The problem is that I cannot think of any ideas about software/applications that are not only practical but also useful for real-life use cases. Simple case studies are good but I'm afraid of that the readers will lose the interest. So any suggestion would be welcome and appreciate.&lt;/p&gt;

&lt;p&gt;Thank you very much.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>angular</category>
      <category>reactnative</category>
      <category>node</category>
    </item>
    <item>
      <title>Stop using the mongoose's default connection</title>
      <dc:creator>Han Mai</dc:creator>
      <pubDate>Mon, 09 Sep 2019 02:29:55 +0000</pubDate>
      <link>https://forem.com/maixuanhan/stop-using-the-mongoose-s-default-connection-4nnj</link>
      <guid>https://forem.com/maixuanhan/stop-using-the-mongoose-s-default-connection-4nnj</guid>
      <description>&lt;p&gt;Look at the below typical example of mongoose use.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mongoose&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mongodb://localhost:27017/myapp&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;useNewUrlParser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;MyModel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Test&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;
&lt;span class="c1"&gt;// Works&lt;/span&gt;
&lt;span class="nx"&gt;MyModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;findOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* ... */&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What if we want to make another connections? Or if we want to connect to another database? We can't use &lt;code&gt;mongoose.connect()&lt;/code&gt; again, mongoose won't know which one we want to interact with. And don't ever think of creating different modules where separated mongoose objects are created and used because &lt;code&gt;require()&lt;/code&gt; doesn't work that way, mongoose object is cached for the first time it is imported.&lt;/p&gt;

&lt;p&gt;The connection object is used to create and retrieve models. Models are always scoped to a single connection. Please be aware that mongoose creates a default connection when we call &lt;code&gt;mongoose.connect()&lt;/code&gt;. We can access the default connection using &lt;code&gt;mongoose.connection&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Not everybody notices that. So my suggestion is that we avoid using &lt;code&gt;mongoose.connect()&lt;/code&gt;. Instead of that, we use &lt;code&gt;mongoose.createConnection()&lt;/code&gt;. By this way, we can save much time for other developers who will maintain and extend the project in the future. We implicitly ask them to aware the fact of mongoose's default connection.&lt;/p&gt;

&lt;p&gt;The above example may be re-written as the following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mongoose&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;connection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createConnection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mongodb://localhost:27017/myapp&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                           &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;useNewUrlParser&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;MyModel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Test&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;
&lt;span class="c1"&gt;// Works&lt;/span&gt;
&lt;span class="nx"&gt;MyModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;findOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* ... */&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reference API: &lt;a href="https://mongoosejs.com/docs/connections.html"&gt;https://mongoosejs.com/docs/connections.html&lt;/a&gt;&lt;br&gt;
** This post is copy from my personal blog &lt;a href="https://rainforest01.blogspot.com/2019/08/stop-using-mongooses-default-connection.html"&gt;https://rainforest01.blogspot.com/2019/08/stop-using-mongooses-default-connection.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>mongodb</category>
      <category>mongoosejs</category>
    </item>
  </channel>
</rss>
