<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nayden Gochev on Medium]]></title>
        <description><![CDATA[Stories by Nayden Gochev on Medium]]></description>
        <link>https://medium.com/@gochev?source=rss-786405a68949------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*-eHJx_qKULhPl61u3uvckA.jpeg</url>
            <title>Stories by Nayden Gochev on Medium</title>
            <link>https://medium.com/@gochev?source=rss-786405a68949------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 05:30:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gochev/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Creating UX with MARKDOWN]]></title>
            <link>https://medium.com/@gochev/creating-ux-with-markdown-bf5bcafd8daf?source=rss-786405a68949------2</link>
            <guid isPermaLink="false">https://medium.com/p/bf5bcafd8daf</guid>
            <category><![CDATA[sideprojectlove]]></category>
            <category><![CDATA[ux-design]]></category>
            <category><![CDATA[ux-writing]]></category>
            <category><![CDATA[ux-strategy]]></category>
            <category><![CDATA[ux]]></category>
            <dc:creator><![CDATA[Nayden Gochev]]></dc:creator>
            <pubDate>Fri, 01 Apr 2022 08:16:40 GMT</pubDate>
            <atom:updated>2022-04-01T08:16:40.190Z</atom:updated>
            <content:encoded><![CDATA[<p>Hello, everyone, I would like to present an idea or a side-project ;) I play with for a while now.</p><h3>Who am I</h3><p>I am a coder/programmer, not a UX person but very often I have to create some UX screens. Also, I know many of you are creating UX screens quite fast in tools like Figma and etc but it still takes time to create the wireframes, align everything to export it to share it and so on.</p><p>I am a HUGE fan of <em>Markdown</em> so I was thinking can we use Markdown to create a mockup ? or wireframes?<br>So at the end, I created something I call utext spelt as “you text” so you text and get UX.</p><h3>The POC</h3><p>Currently, I have only POC the grammar and language and parser are created using <a href="https://pegjs.org/">https://pegjs.org/</a> as parser/translator of the code.</p><p>You can play with the project see here <a href="https://utext.github.io/">https://utext.github.io/</a></p><p>if you click on the “?” and wait a bit you will see some examples.<br>But just to show if you want to display a login form for example it will look like this :</p><pre>===Login===<br>username : ___<br>password: *___<br>===</pre><p>Which generates this</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/0*kiMUCoDQvhurrHM4.png" /></figure><p>if you want to customize the buttons you can for example:</p><pre>===Login===<br>username : ___<br>password: *___<br>===[login][dont login]===</pre><p>Will give you this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/0*vO6gpxud-3ODr8EH.png" /></figure><p>The idea is to VISUALLY explain what and how it should look and voala you have the wireframe.</p><p>I even went a step further you can export this as HTML code even and even change the theme if you want to see how it looks with different UI.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/0*SMH5uUKc9hJru-ou.png" /></figure><p>All the elements supported are basically everything from HTML + some bootsrap elements. You can check them in the help at the “?” link in the website. So it is not just simple HTML a bootstrap card example will be something like this:</p><pre>---Bootstrap Cards works too---<br>I am the content [random btn]<br><br>you can otuput a table too : <br><br>|header1|header2|<br>|col1   |col2   |<br>|col1   |col2|<br>---<br>*Note should start and end with at least 3 dashes*</pre><p>which turns into</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/737/0*R95pm3urRkFCxzqk.png" /></figure><p>Any feedback is welcome :) I just made it for fun :) and just want to hear how this looks to you :) any comments ?</p><p>A yes most markdown stuff like image or heading and etc just work, go play with it :) bugs are expected ! :)</p><p>P.S. Since it is a plain JavaScript (no server side) you can save only in gist in your github account or load a gist… or copy paste :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bf5bcafd8daf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Use Client Certificate Authentication with Java and RestTemplate]]></title>
            <link>https://medium.com/@gochev/use-client-certificate-authentication-with-java-and-resttemplate-19530e34cde7?source=rss-786405a68949------2</link>
            <guid isPermaLink="false">https://medium.com/p/19530e34cde7</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[java8]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[authentication]]></category>
            <category><![CDATA[certificate]]></category>
            <dc:creator><![CDATA[Nayden Gochev]]></dc:creator>
            <pubDate>Mon, 04 Nov 2019 18:14:28 GMT</pubDate>
            <atom:updated>2019-11-04T18:14:28.708Z</atom:updated>
            <content:encoded><![CDATA[<p>As a follow up of the <a href="https://medium.com/@gochev/convert-pfx-certificate-to-jks-p12-crt-97ab8350af07">https://medium.com/@gochev/convert-pfx-certificate-to-jks-p12-crt-97ab8350af07 </a>we now have a keystore and a truststore (if anyone needs) and we will use this keystore to send client side authentication using Spring’s RestTemplate.</p><p>First copy your keystore.jks and truststore.jks in your classpath, no one wants absolute paths right ?:)</p><p>Again a reminder The difference between truststore and keystore if you are not aware is(quote from the <a href="http://download.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html#RelsTM_KM">JSSE ref guide</a>):</p><blockquote><em>TrustManager: Determines whether the remote authentication credentials (and thus the connection) should be trusted.</em></blockquote><blockquote><em>KeyManager: Determines which authentication credentials to send to the remote host.</em></blockquote><p>The magic happens in the creation of SSLContext. Keep in mind the Spring Boot have a nice RestTemplateBuilder but I will not gonna use it, because someone of you might have an older version or like me, might just use a plain old amazing Spring.</p><blockquote>If you <strong>just</strong> want to use the keystore:</blockquote><blockquote>final String allPassword = “123456”;<br>SSLContext sslContext = SSLContextBuilder<br> .create()<br> .loadKeyMaterial(ResourceUtils.getFile(“classpath:keystore.jks”),<br> allPassword.toCharArray(), allPassword.toCharArray())<br> .build();</blockquote><p>if you just want to use the truststore</p><blockquote>final String allPassword = “123456”;<br>SSLContext sslContext = SSLContextBuilder<br> .create()<br> .loadTrustMaterial(ResourceUtils.getFile(“classpath:truststore.jks”), allPassword.toCharArray())<br> .build();</blockquote><p>I guess you know how to use both ;), if you want to IGNORE the truststore certificate checking and trust ALL certificates (might be handy for testing purposes and localhost)</p><blockquote>final String allPassword = “123456”;<br>TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -&gt; true;<br>SSLContext sslContext = SSLContextBuilder<br> .create()<br> .loadTrustMaterial(ResourceUtils.getFile(“classpath:truststore.jks”), allPassword.toCharArray())<br> .loadTrustMaterial(null, acceptingTrustStrategy) //accept all<br> .build();</blockquote><p>Ones you have the sslContext you simply do :</p><pre>HttpClient client = HttpClients.custom()<br> .setSSLContext(sslContext)<br> .build();</pre><pre>HttpComponentsClientHttpRequestFactory requestFactory =<br> new HttpComponentsClientHttpRequestFactory();</pre><pre>requestFactory.setHttpClient(client);</pre><pre>RestTemplate restTemplate = new RestTemplate(requestFactory);</pre><pre>return restTemplate;</pre><p>And Voala, now each time you make a get/post or exchange with your restTemplate you will send the client side certificate.</p><p>Full example (the “tests” version) that sends client side certificate and ignores the SSL certificate</p><pre>private RestTemplate getRestTemplateClientAuthentication()<br> throws IOException, UnrecoverableKeyException, CertificateException, NoSuchAlgorithmException,<br> KeyStoreException, KeyManagementException {</pre><pre>final String allPassword = “123456”;<br> TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -&gt; true;<br> SSLContext sslContext = SSLContextBuilder<br> .create()<br> .loadKeyMaterial(ResourceUtils.getFile(“classpath:keystore.jks”),<br> allPassword.toCharArray(), allPassword.toCharArray())<br>//.loadTrustMaterial(ResourceUtils.getFile(“classpath:truststore.jks”), allPassword.toCharArray())<br> .loadTrustMaterial(null, acceptingTrustStrategy)<br> .build();</pre><pre>HttpClient client = HttpClients.custom()<br> .setSSLContext(sslContext)<br> .build();</pre><pre>HttpComponentsClientHttpRequestFactory requestFactory =<br> new HttpComponentsClientHttpRequestFactory();</pre><pre>requestFactory.setHttpClient(client);</pre><pre>RestTemplate restTemplate = new RestTemplate(requestFactory);</pre><pre>return restTemplate;<br>}</pre><p>Hope this is handy for you !</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=19530e34cde7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Convert PFX certificate to JKS, P12, CRT]]></title>
            <link>https://medium.com/@gochev/convert-pfx-certificate-to-jks-p12-crt-97ab8350af07?source=rss-786405a68949------2</link>
            <guid isPermaLink="false">https://medium.com/p/97ab8350af07</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[authentication]]></category>
            <category><![CDATA[java8]]></category>
            <category><![CDATA[certificate]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Nayden Gochev]]></dc:creator>
            <pubDate>Mon, 04 Nov 2019 18:09:38 GMT</pubDate>
            <atom:updated>2019-11-04T18:15:03.851Z</atom:updated>
            <content:encoded><![CDATA[<p>When you need to use a client Certificate Authentication from Java the issues starts even from the beginning with the certificate, since usually it is not in the way you want it.</p><p>I recently had to use a PFX certificate for client authentication (maybe another post will be coming) and for that reason I had to convert it to a Java keystore (JKS).</p><p>We will create BOTH a truststore and a keystore, because based on your needs you might need one or the other. <br>The difference between truststore and keystore if you are not aware is(quote from the <a href="http://download.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html#RelsTM_KM">JSSE ref guide</a>:</p><blockquote><em>TrustManager: Determines whether the remote authentication credentials (and thus the connection) should be trusted.</em></blockquote><blockquote><em>KeyManager: Determines which authentication credentials to send to the remote host.</em></blockquote><blockquote>Ok that’s enough what you will need is openssl and Java 7 or newer !</blockquote><p>First let’s generate a key from the pfx file, this key is later used for p12 keystore.</p><blockquote><strong>openssl pkcs12 -in example.pfx -nocerts -out example.key</strong></blockquote><blockquote>Enter Import Password:</blockquote><blockquote>MAC verified OK</blockquote><blockquote>Enter PEM pass phrase:</blockquote><blockquote>Verifying — Enter PEM pass phrase:</blockquote><p>As shown here you will be asked for the password of the pfx file, later you will be asked to enter a PEM passphase lets for example use 123456 for everything here.</p><p>The second commands is almost the same but it is about nokey and a crt this time</p><blockquote><strong>openssl pkcs12 -in example.pfx -clcerts -nokeys -out example.crt</strong></blockquote><blockquote>Enter Import Password:</blockquote><blockquote>MAC verified OK</blockquote><blockquote>Now we have a key and and a crt file</blockquote><p>Next step is to create a truststore.</p><blockquote><strong>keytool -import -file example.crt -alias exampleCA -keystore truststore.jks</strong></blockquote><blockquote>Enter keystore password:</blockquote><blockquote>Re-enter new password:</blockquote><blockquote>Owner: CN=…..</blockquote><blockquote>…….</blockquote><blockquote>Trust this certificate? [no]: yes</blockquote><blockquote>Certificate was added to keystore</blockquote><p>As you can see here you just import this crt file into a jks truststore and set some password. For the question do you trust this certificate you say yes, so it is added in the truststore.</p><blockquote>We are done if you only need a truststore !</blockquote><p>The last step(s) is to create a keystore</p><blockquote><strong>openssl pkcs12 -export -in example.crt -inkey example.key -certfile example.crt -name “examplecert” -out keystore.p12</strong></blockquote><blockquote>Enter pass phrase for example.key:</blockquote><blockquote>Enter Export Password:</blockquote><blockquote>Verifying — Enter Export Password:</blockquote><p>This p12 keystore is enough in many cases, still if you need a JKS keystore you need one additional command</p><blockquote><strong>keytool -importkeystore -srckeystore keystore.p12 -srcstoretype pkcs12 -destkeystore keystore.jks -deststoretype JKS</strong></blockquote><blockquote>Importing keystore keystore.p12 to keystore.jks…</blockquote><blockquote>Enter destination keystore password:</blockquote><blockquote>Re-enter new password:</blockquote><blockquote>Enter source keystore password:</blockquote><blockquote>Entry for alias examplecert successfully imported.</blockquote><blockquote>Import command completed: 1 entries successfully imported, 0 entries failed or cancelled</blockquote><blockquote>Warning:</blockquote><blockquote>The JKS keystore uses a proprietary format. It is recommended to migrate to PKCS12 which is an industry standard format using “keytool -importkeystore -srckeystore keystore.jks -destkeystore keystore.jks -deststoretype pkcs12”.</blockquote><blockquote>That is all folks ! I hope this helps someone :)</blockquote><blockquote>Lets verify what we have at the end !</blockquote><blockquote><strong>ls</strong></blockquote><blockquote>example.pfx example.key keystore.p12</blockquote><blockquote>example.crt keystore.jks truststore.jks</blockquote><p>See you in <a href="https://medium.com/@gochev/use-client-certificate-authentication-with-java-and-resttemplate-19530e34cde7">post 2 how to use this keystore for client side authentication.</a> Also how to use the truststore if you need to use it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=97ab8350af07" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[NestJS + Mongo + Typegoose]]></title>
            <link>https://medium.com/@gochev/nestjs-mongo-typegoose-2672e65cf7f2?source=rss-786405a68949------2</link>
            <guid isPermaLink="false">https://medium.com/p/2672e65cf7f2</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[mongoose]]></category>
            <category><![CDATA[nestjs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[typegoose]]></category>
            <dc:creator><![CDATA[Nayden Gochev]]></dc:creator>
            <pubDate>Mon, 04 Nov 2019 11:35:00 GMT</pubDate>
            <atom:updated>2019-11-04T12:40:19.715Z</atom:updated>
            <content:encoded><![CDATA[<h3>Currently there are 3 options to use Mongo with Node (and NestJS).</h3><p>We will look into each and every one of them and I will provide an example how you can use MongoDB in your NestJS application in a headache free way.</p><blockquote>1) <strong>NestJS + Mongoose</strong> where maybe the best tutorial I have found is here <a href="https://scotch.io/tutorials/building-a-modern-app-using-nestjs-mongodb-and-vuejs">https://scotch.io/tutorials/building-a-modern-app-using-nestjs-mongodb-and-vuejs</a> <strong>the issue</strong> is that I hate the fact I had to write the schema definitions and the typescript interfaces. If you are fine with writing everything 2 times ones the Schema and ones the Document as Typescript Interface maybe this is the best way to go !</blockquote><blockquote>2) <strong>NestJS + TypeORM</strong> where you can actually use TypeORM with MongoDB, however I do not recomment this if you want to learn more I would ask you to write this blog post <a href="https://medium.com/articode/typeorm-mongodb-review-8855903228b1">https://medium.com/articode/typeorm-mongodb-review-8855903228b1</a></blockquote><blockquote>3) <strong>NestJS + Typegoose</strong> — basically what it does is it uses your domain objects to get the schema from them. And this is what this post is all about. There is a lot of documentation how you can achieve that, however I didn’t like most of it, it just looked like too much code. On top of that ALL tutorials ALWAYS include using of a DTO class and I don’t see a reason to use DTO classes at all in 99% of the cases. DTOs are great, don’t get me wrong, there are many pros of using DTOs, but none of the tutorials on the internet actually explains why they want DTOs and in fact none of them need DTOs so I would like to write the most easy straightforward way with NestJS + MongoDB + and TypeGoose.</blockquote><p>So first of all we will install NestJS CLI, NestJS is very, very similar to Angular and even if you don&#39;t like angular, trust me you will like NestJS, since I am actually the same! Btw great beginners tutorial for NestJS you can read here find <a href="https://scotch.io/tutorials/getting-started-with-nestjs">https://scotch.io/tutorials/getting-started-with-nestjs</a></p><h3>So lets start by creating the NestJS app</h3><blockquote>npm i -g <a href="http://twitter.com/nestjs/cli">@nestjs/cli</a></blockquote><p>Then create a NestJS project.</p><blockquote>nest new nestjspoc-nest</blockquote><blockquote>cd nestjspoc-nest</blockquote><blockquote>// start the application using nodemon<br> npm run start:dev</blockquote><p>open browser to localhost:3000 to verify hello world is displayed.</p><p>Ok we will create a simple Service and Controller in a Module, lets say our applications will do something with Users and we will want UserModule which will hold the User domain objects, User services and user controllers.</p><blockquote>nest generate module user</blockquote><blockquote>nest generate service user<br> nest generate controller user</blockquote><p>Now you should have a folder which has UserModule, UserService and UserController.</p><p>Which are almost empty.</p><p>Nest we will use <strong>nestjs-typegoose</strong> because it just makes everything even easier.</p><blockquote>npm install — save nestjs-typegoose</blockquote><p>the typegoose has several peer dependencies so we need to install them as well. The two <strong>nestjs</strong> dependencies we already have but we need the other two.</p><pre><strong>&quot;@typegoose/typegoose&quot;: &quot;^6.0.0&quot;,<br></strong>&quot;@nestjs/common&quot;<strong>: </strong>&quot;^6.3.1&quot;<strong>,<br></strong>&quot;@nestjs/core&quot;<strong>: </strong>&quot;^6.3.1&quot;<strong>,<br>&quot;mongoose&quot;: &quot;^5.5.13&quot;</strong></pre><p>Ones done your package.json should look like this:</p><pre><strong>{<br>  </strong>&quot;name&quot;<strong>: </strong>&quot;nestjspoc&quot;<strong>,<br>  </strong>&quot;version&quot;<strong>: </strong>&quot;0.0.1&quot;<strong>,<br>  </strong>&quot;description&quot;<strong>: </strong>&quot;&quot;<strong>,<br>  </strong>&quot;author&quot;<strong>: </strong>&quot;&quot;<strong>,<br>  </strong>&quot;license&quot;<strong>: </strong>&quot;MIT&quot;<strong>,<br>  </strong>&quot;scripts&quot;<strong>: {<br>    </strong>&quot;prebuild&quot;<strong>: </strong>&quot;rimraf dist&quot;<strong>,<br>    </strong>&quot;build&quot;<strong>: </strong>&quot;nest build&quot;<strong>,<br>    </strong>&quot;format&quot;<strong>: </strong>&quot;prettier --write <strong>\&quot;</strong>src/**/*.ts<strong>\&quot; \&quot;</strong>test/**/*.ts<strong>\&quot;</strong>&quot;<strong>,<br>    </strong>&quot;start&quot;<strong>: </strong>&quot;nest start&quot;<strong>,<br>    </strong>&quot;start:dev&quot;<strong>: </strong>&quot;nest start --watch&quot;<strong>,<br>    </strong>&quot;start:debug&quot;<strong>: </strong>&quot;nest start --debug --watch&quot;<strong>,<br>    </strong>&quot;start:prod&quot;<strong>: </strong>&quot;node dist/main&quot;<strong>,<br>    </strong>&quot;lint&quot;<strong>: </strong>&quot;tslint -p tsconfig.json -c tslint.json&quot;<strong>,<br>    </strong>&quot;test&quot;<strong>: </strong>&quot;jest&quot;<strong>,<br>    </strong>&quot;test:watch&quot;<strong>: </strong>&quot;jest --watch&quot;<strong>,<br>    </strong>&quot;test:cov&quot;<strong>: </strong>&quot;jest --coverage&quot;<strong>,<br>    </strong>&quot;test:debug&quot;<strong>: </strong>&quot;node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand&quot;<strong>,<br>    </strong>&quot;test:e2e&quot;<strong>: </strong>&quot;jest --config ./test/jest-e2e.json&quot;<br>  <strong>},<br>  </strong>&quot;dependencies&quot;<strong>: {<br>    </strong>&quot;@nestjs/common&quot;<strong>: </strong>&quot;^6.7.2&quot;<strong>,<br>    </strong>&quot;@nestjs/core&quot;<strong>: </strong>&quot;^6.7.2&quot;<strong>,<br>    </strong>&quot;@nestjs/platform-express&quot;<strong>: </strong>&quot;^6.7.2&quot;<strong>,<br>    &quot;nestjs-typegoose&quot;: &quot;^7.0.0&quot;,<br>    </strong>&quot;rimraf&quot;<strong>: </strong>&quot;^3.0.0&quot;<strong>,<br>    </strong>&quot;rxjs&quot;<strong>: </strong>&quot;^6.5.3&quot;<strong>,<br>    &quot;@typegoose/typegoose&quot;: &quot;^6.0.0&quot;,<br>    &quot;mongoose&quot;: &quot;^5.5.13&quot;</strong><br>  <strong>},<br>  </strong>&quot;devDependencies&quot;<strong>: {<br>    </strong>&quot;@nestjs/cli&quot;<strong>: </strong>&quot;^6.9.0&quot;<strong>,<br>    </strong>&quot;@nestjs/schematics&quot;<strong>: </strong>&quot;^6.7.0&quot;<strong>,<br>    </strong>&quot;@nestjs/testing&quot;<strong>: </strong>&quot;^6.7.1&quot;<strong>,<br>    </strong>&quot;@types/express&quot;<strong>: </strong>&quot;^4.17.1&quot;<strong>,<br>    </strong>&quot;@types/jest&quot;<strong>: </strong>&quot;^24.0.18&quot;<strong>,<br>    </strong>&quot;@types/node&quot;<strong>: </strong>&quot;^12.7.5&quot;<strong>,<br>    </strong>&quot;@types/supertest&quot;<strong>: </strong>&quot;^2.0.8&quot;<strong>,<br>    </strong>&quot;jest&quot;<strong>: </strong>&quot;^24.9.0&quot;<strong>,<br>    </strong>&quot;prettier&quot;<strong>: </strong>&quot;^1.18.2&quot;<strong>,<br>    </strong>&quot;supertest&quot;<strong>: </strong>&quot;^4.0.2&quot;<strong>,<br>    </strong>&quot;ts-jest&quot;<strong>: </strong>&quot;^24.1.0&quot;<strong>,<br>    </strong>&quot;ts-loader&quot;<strong>: </strong>&quot;^6.1.1&quot;<strong>,<br>    </strong>&quot;ts-node&quot;<strong>: </strong>&quot;^8.4.1&quot;<strong>,<br>    </strong>&quot;tsconfig-paths&quot;<strong>: </strong>&quot;^3.9.0&quot;<strong>,<br>    </strong>&quot;tslint&quot;<strong>: </strong>&quot;^5.20.0&quot;<strong>,<br>    </strong>&quot;typescript&quot;<strong>: </strong>&quot;^3.6.3&quot;<br>  <strong>},<br>  </strong>&quot;jest&quot;<strong>: {<br>    </strong>&quot;moduleFileExtensions&quot;<strong>: <em>[<br>      </em></strong>&quot;js&quot;<strong>,<br>      </strong>&quot;json&quot;<strong>,<br>      </strong>&quot;ts&quot;<br>    <strong><em>]</em>,<br>    </strong>&quot;rootDir&quot;<strong>: </strong>&quot;src&quot;<strong>,<br>    </strong>&quot;testRegex&quot;<strong>: </strong>&quot;.spec.ts$&quot;<strong>,<br>    </strong>&quot;transform&quot;<strong>: {<br>      </strong>&quot;^.+<strong>\\</strong>.(t|j)s$&quot;<strong>: </strong>&quot;ts-jest&quot;<br>    <strong>},<br>    </strong>&quot;coverageDirectory&quot;<strong>: </strong>&quot;./coverage&quot;<strong>,<br>    </strong>&quot;testEnvironment&quot;<strong>: </strong>&quot;node&quot;<br>  <strong>}<br>}</strong></pre><p>The ones we added manually are in bold.</p><blockquote>Well this is the setup, let’s write some code.</blockquote><p>Create your domain object user in a file user.ts for example :</p><pre><strong>import {</strong><em>prop</em><strong>, </strong>Typegoose<strong>} from </strong>&#39;@typegoose/typegoose&#39;<strong>;<br><br>export class User extends </strong>Typegoose <strong>{<br>    @prop<em>()<br>    </em></strong>name<strong>?: string;<br>}</strong></pre><p>You see the <a href="http://twitter.com/prop">@prop</a>() yup you need this. You can learn more about validations and what you can do in the typegoose documentation.<br>Then lets create or update our UserService class.</p><pre><strong>import {</strong><em>Injectable</em><strong>} from </strong>&#39;@nestjs/common&#39;<strong>;<br>import {User} from </strong>&#39;./domain/user&#39;<strong>;<br>import {</strong>InjectModel<strong>} from </strong>&#39;nestjs-typegoose&#39;<strong>;<br>import {ReturnModelType} from </strong>&#39;@typegoose/typegoose&#39;<strong>;<br><br>@Injectable<em>()<br></em>export class UserService {<br>    constructor<em>(</em>@InjectModel<em>(</em>User<em>) </em>private readonly </strong>userModel<strong>: ReturnModelType&lt;typeof User&gt;<em>) </em>{<br>    }<br><br>    async </strong>createCustomUser<strong><em>(</em></strong>user<strong>: User<em>) </em>{<br>        const </strong>createdUser <strong>= new this.</strong>userModel<strong><em>(</em></strong>user<strong><em>)</em>;<br>        return await </strong>createdUser<strong>.</strong>save<strong><em>()</em>;<br>    }<br><br>    async </strong>listUsers<strong><em>()</em>: Promise&lt;User<em>[] </em>| null&gt; {<br>        return await this.</strong>userModel<strong>.</strong>find<strong><em>()</em>.</strong>exec<strong><em>()</em>;<br>    }<br>}</strong></pre><blockquote>Ok the first magic is actually here !</blockquote><p>You may notice the line @InjectModel(User) private readonly userModel: ReturnModelType, this will give us a userModel that we can use for our User type.</p><p>The createCustomUser and listUsers use this userModel and I believe it is all clear HOW :)</p><p>Next update our UserController.</p><pre><strong>import {</strong><em>Body</em><strong>, </strong><em>Controller</em><strong>, </strong>Get<strong>, </strong>Post<strong>} from </strong>&#39;@nestjs/common&#39;<strong>;<br>import {UserService} from </strong>&#39;./user.service&#39;<strong>;<br>import {User} from </strong>&#39;./domain/user&#39;<strong>;<br><br>@Controller<em>(</em></strong>&#39;user&#39;<strong><em>)<br></em>export class UserController {<br>    constructor<em>(</em>private readonly </strong>userService<strong>: UserService<em>) </em>{ }<br><br>    @Get<em>(</em></strong>&#39;listusers&#39;<strong><em>)<br>    </em>async </strong>listUsers<strong><em>()</em>: Promise&lt;User<em>[] </em>| null&gt; {<br>        return await this.</strong>userService<strong>.</strong>listUsers<strong><em>()</em>;<br>    }<br><br>    @Post<em>(</em></strong>&#39;createuser&#39;<strong><em>)<br>    </em>async </strong>createUser<strong><em>(</em>@Body<em>() </em></strong>cat<strong>: User<em>)</em>: Promise&lt;User&gt; {<br>        return await this.</strong>userService<strong>.</strong>createCustomUser<strong><em>(</em></strong>cat<strong><em>)</em>;<br>    }<br>}</strong></pre><p>Nothing fancy here, we just inject our Service and we call our two methods.</p><blockquote>Few more magic !</blockquote><p>There are two magic lines more, that you need to add to your UserModule and AppModule</p><p>On the UserModule definition we need this line <strong>imports: <em>[</em>TypegooseModule.<em>forFeature([</em>User<em>])]</em></strong>:</p><pre><strong>import { </strong><em>Module </em><strong>} from </strong>&#39;@nestjs/common&#39;<strong>;<br>import { UserController } from </strong>&#39;./user.controller&#39;<strong>;<br>import { UserService } from </strong>&#39;./user.service&#39;<strong>;<br>import {User} from </strong>&#39;./domain/user&#39;<strong>;<br>import {TypegooseModule} from </strong>&#39;nestjs-typegoose&#39;<strong>;<br><br>@Module<em>(</em>{<br>  imports: <em>[</em>TypegooseModule.<em>forFeature([</em>User<em>])]</em>,<br>  </strong>controllers<strong>: <em>[</em>UserController<em>]</em>,<br>  </strong>providers<strong>: <em>[</em>UserService<em>]</em>,<br>}<em>)<br></em>export class UserModule {}</strong></pre><p>And on the AppModule we need the Mongoose configuraiton of the MongoDB connection string imports<strong>:</strong></p><p><strong>imports: <em>[</em>TypegooseModule.</strong><em>forRoot</em><strong><em>(</em></strong>‘mongodb://localhost:27017/nest’<strong><em>)</em>,<br> UserModule<em>]</em>,</strong></p><pre><strong>import {</strong><em>Module</em><strong>} from </strong>&#39;@nestjs/common&#39;<strong>;<br>import {AppController} from </strong>&#39;./app.controller&#39;<strong>;<br>import {AppService} from </strong>&#39;./app.service&#39;<strong>;<br>import {UserModule} from </strong>&#39;./user/user.module&#39;<strong>;<br>import {TypegooseModule} from </strong>&#39;nestjs-typegoose&#39;<strong>;<br><br>@Module<em>(</em>{<br>    </strong>imports<strong>: <em>[</em>TypegooseModule.</strong><em>forRoot</em><strong><em>(</em></strong>&#39;mongodb://localhost:27017/nest&#39;<strong><em>)</em>,<br>        UserModule<em>]</em>,<br>    </strong>controllers<strong>: <em>[</em>AppController<em>]</em>,<br>    </strong>providers<strong>: <em>[</em>AppService<em>]</em>,<br>}<em>)<br></em>export class AppModule {<br>}</strong></pre><p>And yes you need MongoDB to be running ;)</p><p>Well that’s it!</p><blockquote>Testing it - the monkey way !</blockquote><h4>Create a user:</h4><blockquote><em>curl -X POST </em><a href="http://localhost:3000/user/createuser"><em>http://localhost:3000/user/createuser</em></a><em> -H ‘Content-Type: application/json’ -d ‘{ “name”: “Nayden Gochev” }’</em></blockquote><p>And you will receive</p><blockquote>{“_id”:”5dc00795d9a25df587a1e5f9&quot;,”name”:”Nayden Gochev”,”__v”:0}</blockquote><h4>List all users:</h4><blockquote>curl -X GET <a href="http://localhost:3000/user/listusers">http://localhost:3000/user/listusers</a></blockquote><h3>Missing bits !</h3><p>What is missing ? Validation, Security and Testing of course, all of this — next time ;) now was the nest time.</p><h3>Source code</h3><p>the <strong>full source code</strong> can be download here:<br><a href="https://github.com/gochev/nest-js-poc-mongodb">https://github.com/gochev/nest-js-poc-mongodb</a></p><h3>About me:</h3><p>I am a Java developer with a lot of Spring knowledge, but recently I had to write some JavaScript even if I don’t want to, so maybe that explains why I like NestJS, and yes this done in Java and Spring is a lot easier <a href="https://github.com/gochev/spring-mvc-poc-mongodb">https://github.com/gochev/spring-mvc-poc-mongodb</a> but it is fun with NestJS right ? :)</p><p><em>Originally published at </em><a href="http://gochev.blogspot.com/2019/11/nestjs-mongo-typegoose.html"><em>http://gochev.blogspot.com</em></a><em> on November 4, 2019.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2672e65cf7f2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>