<?xml version="1.0" encoding="utf-8" ?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:tt="http://teletype.in/" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/"><title>Mkots</title><subtitle>This blog is my English homework if you notice any mistake, please  write a comment where is it and I'll correct it</subtitle><author><name>Mkots</name></author><id>https://teletype.in/atom/mkots</id><link rel="self" type="application/atom+xml" href="https://teletype.in/atom/mkots?offset=0"></link><link rel="alternate" type="text/html" href="https://teletype.in/@mkots?utm_source=teletype&amp;utm_medium=feed_atom&amp;utm_campaign=mkots"></link><link rel="next" type="application/rss+xml" href="https://teletype.in/atom/mkots?offset=10"></link><link rel="search" type="application/opensearchdescription+xml" title="Teletype" href="https://teletype.in/opensearch.xml"></link><updated>2026-04-30T18:00:20.940Z</updated><entry><id>mkots:setting-up-server</id><link rel="alternate" type="text/html" href="https://teletype.in/@mkots/setting-up-server?utm_source=teletype&amp;utm_medium=feed_atom&amp;utm_campaign=mkots"></link><title>Configuring a new server</title><published>2020-08-04T20:11:57.974Z</published><updated>2020-08-04T20:11:57.974Z</updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://teletype.in/files/bc/cd/bccd5c4b-e23d-4336-be4b-76bd12b2e090.png"></media:thumbnail><category term="misc" label="Misc"></category><summary type="html">1. Add a new user:</summary><content type="html">
  &lt;p&gt;1. Add a new user:&lt;/p&gt;
  &lt;pre&gt;adduser username &lt;/pre&gt;
  &lt;p&gt;2. Add this user to sudoers group:&lt;/p&gt;
  &lt;pre&gt;usermod -aG sudo username&lt;/pre&gt;
  &lt;p&gt;3. Switch to this user:&lt;/p&gt;
  &lt;pre&gt;su - username&lt;/pre&gt;
  &lt;p&gt;4. Open the ssh configuration file:&lt;/p&gt;
  &lt;pre&gt;vim /etc/ssh/sshd_config&lt;/pre&gt;
  &lt;p&gt;5. In the line &lt;strong&gt;PermitRootLogin yes&lt;/strong&gt; replace the word &lt;strong&gt;Yes &lt;/strong&gt;with the word &lt;strong&gt;No:&lt;/strong&gt;&lt;/p&gt;
  &lt;pre&gt;PermitRootLogin no&lt;/pre&gt;
  &lt;p&gt;6. In the line &lt;strong&gt;#PasswordAuthentication yes&lt;/strong&gt; replace the word &lt;strong&gt;Yes &lt;/strong&gt;with the word &lt;strong&gt;No &lt;/strong&gt;and uncomment this line:&lt;/p&gt;
  &lt;pre&gt;PasswordAuthentication no&lt;/pre&gt;
  &lt;p&gt;7. Add your&lt;strong&gt; id_rsa.pub&lt;/strong&gt; to &lt;strong&gt;autorized_keys&lt;/strong&gt; (&lt;u&gt;run it from the client&lt;/u&gt;):&lt;/p&gt;
  &lt;pre&gt;ssh-copy-id username@remote_host&lt;/pre&gt;
  &lt;p&gt;8. Save the file and restart the service:&lt;/p&gt;
  &lt;pre&gt;sudo service ssh restart&lt;/pre&gt;
  &lt;p&gt;9. Install curl:&lt;/p&gt;
  &lt;pre&gt;sudo apt update &amp;amp;&amp;amp; sudo apt install curl -y &lt;/pre&gt;
  &lt;p&gt;10. Download openvpn-installer:&lt;/p&gt;
  &lt;pre&gt;curl -O https://raw.githubusercontent.com/angristan/openvpn-install/master/openvpn-install.sh&lt;/pre&gt;
  &lt;p&gt;11. Add execution permission:&lt;/p&gt;
  &lt;pre&gt;chmod +x openvpn-install.sh&lt;/pre&gt;
  &lt;p&gt;12. Then run it:&lt;/p&gt;
  &lt;pre&gt;sudo ./openvpn-install.sh&lt;/pre&gt;
  &lt;p&gt;13. Create ssh &amp;quot;bookmark&amp;quot; (&lt;u&gt;run it from the client&lt;/u&gt;):&lt;/p&gt;
  &lt;pre&gt;echo &amp;quot;Host dev
     HostName 0.0.0.0
     User username
     Port 22
 &amp;quot; &amp;gt;&amp;gt; ~/.ssh/config
&lt;/pre&gt;
  &lt;p&gt;14. Copy *.ovpn files to your PC (&lt;u&gt;run it from the client&lt;/u&gt;):&lt;/p&gt;
  &lt;p&gt;&lt;code&gt;scp dev:~/*.ovpn ~/Downloads/&lt;/code&gt;&lt;br /&gt;&lt;/p&gt;

</content></entry><entry><id>mkots:SkPUllEAB</id><link rel="alternate" type="text/html" href="https://teletype.in/@mkots/SkPUllEAB?utm_source=teletype&amp;utm_medium=feed_atom&amp;utm_campaign=mkots"></link><title>Create an API via NestJS Part V</title><published>2019-12-15T17:41:35.006Z</published><updated>2019-12-22T09:49:42.595Z</updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://teletype.in/files/6e/4f/6e4f7482-a68c-46e4-8b88-b63baaa0e829.png"></media:thumbnail><summary type="html">&lt;img src=&quot;https://teletype.in/files/6e/4f/6e4f7482-a68c-46e4-8b88-b63baaa0e829.png&quot;&gt;nest g module user — generate module</summary><content type="html">
  &lt;h2&gt;In this part, we&amp;#x27;ll create a User module. &lt;/h2&gt;
  &lt;h3&gt;Firstly we have to set up the scaffold&lt;/h3&gt;
  &lt;p&gt;&lt;code&gt;nest g module user&lt;/code&gt; — generate module&lt;/p&gt;
  &lt;p&gt;&lt;code&gt;nest g controller user&lt;/code&gt; — generate controller&lt;/p&gt;
  &lt;p&gt;If everything is alright you should see the new folder in your src/. It must contain  files:&lt;/p&gt;
  &lt;ol&gt;
    &lt;li&gt;user.controller.spec.ts — tests for controller&lt;/li&gt;
    &lt;li&gt;user.controller.ts ­— the controller file (for more information follow the &lt;a href=&quot;https://docs.nestjs.com/controllers&quot; target=&quot;_blank&quot;&gt;link)&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;user.module.ts — the module file (for more information &lt;a href=&quot;https://docs.nestjs.com/modules&quot; target=&quot;_blank&quot;&gt;link)&lt;/a&gt;&lt;/li&gt;
  &lt;/ol&gt;
  &lt;p&gt;After that, we can create the service file &lt;/p&gt;
  &lt;p&gt;&lt;code&gt;nest g service user/user&lt;/code&gt;&lt;/p&gt;
  &lt;blockquote&gt;This command will create a subfolder in your user folder named user. It quite comfortable for me but if you used to flat project structure you can run this command just: nest g service user/ &lt;/blockquote&gt;
  &lt;p&gt;Now let&amp;#x27;s create DTO and Entity files for user module&lt;/p&gt;
  &lt;p&gt;&lt;code&gt;touch src/user/user.dto.ts&lt;br /&gt;touch src/user/user.entity.ts&lt;/code&gt;&lt;br /&gt;Finally, install necessary packages &lt;/p&gt;
  &lt;p&gt;&lt;code&gt;yarn add jsonwebtoken @types/jsonwebtoken&lt;/code&gt;&lt;/p&gt;
  &lt;p&gt;&lt;code&gt;yarn add bcrypt @types/bcrypt&lt;/code&gt;&lt;/p&gt;
  &lt;h3&gt;Let&amp;#x27;s describe the user data model&lt;/h3&gt;
  &lt;p&gt;Open user.entity.ts and write something like this:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/6e/4f/6e4f7482-a68c-46e4-8b88-b63baaa0e829.png&quot; width=&quot;2604&quot; /&gt;
    &lt;figcaption&gt;user.entity.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;blockquote&gt;Line 12 — declare new entity named &amp;#x27;user&amp;#x27;&lt;br /&gt;Line 14-15 — create autogenerated column for user id&lt;br /&gt;Line 17-18 — create column to store date user had register &lt;br /&gt;Line 20-24 —  the unique column for user id, it needs for decline registration with the same username&lt;br /&gt;Line 27-27 — column for password&lt;br /&gt;Line 29-32 — function witch called before password could store. This function hashes the user&amp;#x27;s password before we stored it in the database&lt;br /&gt;Line 34-41 ­— this function will return UserObject after registration &lt;br /&gt;Line 43-53 — getter function for &lt;a href=&quot;https://jwt.io/introduction/&quot; target=&quot;_blank&quot;&gt;JWT token&lt;br /&gt;&lt;/a&gt;Line 55-57 — password comparator&lt;/blockquote&gt;
  &lt;p&gt;&lt;u&gt;Not to forget add your SECRET string into .env file, it should be like:&lt;/u&gt;&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/59/36/5936921f-ae98-4f42-8830-2333668fd176.png&quot; width=&quot;1560&quot; /&gt;
    &lt;figcaption&gt;.env&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;p&gt;And import it into user.module:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/84/9b/849b96c1-a8b0-4199-821f-0ab7e0069461.png&quot; width=&quot;2468&quot; /&gt;
    &lt;figcaption&gt;user.module.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;h3&gt;Describe UserDTO&lt;/h3&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/f1/4b/f14bddca-0383-4b14-ba95-a4b7fb48cef3.png&quot; width=&quot;2236&quot; /&gt;
    &lt;figcaption&gt;user.dto.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;h3&gt;Create some methods for user module&lt;/h3&gt;
  &lt;p&gt;Open the user cntroller and create register, login and get all users command:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/2d/2e/2d2e99cf-5dc4-45b3-91d8-17ecaf5c6b2b.png&quot; width=&quot;3108&quot; /&gt;
    &lt;figcaption&gt;user.controller.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;h3&gt;let&amp;#x27;s implement our service methods&lt;/h3&gt;
  &lt;p&gt;Firstly you could create dummy functions&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/7f/13/7f1333fb-236d-4738-9c20-5d41efb6b771.png&quot; width=&quot;2436&quot; /&gt;
    &lt;figcaption&gt;user.service.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;p&gt;Now we can implement getAll() function:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/1f/14/1f14b3ae-c285-4ffb-8bd9-9685e6e535bf.png&quot; width=&quot;2640&quot; /&gt;
    &lt;figcaption&gt;user.service.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;blockquote&gt;We use responseObject(false) because we don&amp;#x27;t need to get JWT token in this case&lt;/blockquote&gt;
  &lt;p&gt;Next we must implement login() function:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/79/87/798792d7-30ad-4ba3-9ba3-cf5d1f90bb74.png&quot; width=&quot;3280&quot; /&gt;
    &lt;figcaption&gt;user.service.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;p&gt;And finnaly implement register() function:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/a4/72/a4728c9a-4ecf-41c3-93fd-a98bd31f329d.png&quot; width=&quot;3312&quot; /&gt;
    &lt;figcaption&gt;user.service.ts&lt;/figcaption&gt;
  &lt;/figure&gt;

</content></entry><entry><id>mkots:H1x0HJPaB</id><link rel="alternate" type="text/html" href="https://teletype.in/@mkots/H1x0HJPaB?utm_source=teletype&amp;utm_medium=feed_atom&amp;utm_campaign=mkots"></link><title>Create an API via NestJS Part IV</title><published>2019-12-05T20:17:12.081Z</published><updated>2019-12-06T13:17:15.592Z</updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://teletype.in/files/8f/8f85e323-3c95-471a-addf-da0d34c46b9f.png"></media:thumbnail><category term="create-an-api-via-nestjs" label="Create an API via NestJS"></category><summary type="html">&lt;img src=&quot;https://teletype.in/files/8f/8f85e323-3c95-471a-addf-da0d34c46b9f.png&quot;&gt;Go to quiz.servise.ts and edit the read() function such as:</summary><content type="html">
  &lt;h2&gt;Validation error for CRUD&lt;/h2&gt;
  &lt;h3&gt;Create validation function for the Read method&lt;/h3&gt;
  &lt;p&gt;Go to &lt;code&gt;quiz.servise.ts&lt;/code&gt; and edit the &lt;em&gt;read()&lt;/em&gt; function such as:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/8f/8f85e323-3c95-471a-addf-da0d34c46b9f.png&quot; width=&quot;3212&quot; /&gt;
    &lt;figcaption&gt;quiz.servise.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;p&gt;Line by line explanation:&lt;/p&gt;
  &lt;ol&gt;
    &lt;li&gt;Declaration of the read() function which takes id with string type.&lt;/li&gt;
    &lt;li&gt;Writing a response from a database to the quiz constant.&lt;/li&gt;
    &lt;li&gt;Checking whether the quiz constant&amp;#x27;s value is &amp;#x27;null&amp;#x27;. The condition will be true if the quiz is undefined or equals to &amp;#x27;null&amp;#x27;.&lt;/li&gt;
    &lt;li&gt;Throwing a new exception. It will be handled by our logging interceptor from the previous part. HttpException is the function of two arguments: the first one — a message which will be shown when error threw, second — just server response code like a 404 for Not Found or 200 for OK.&lt;/li&gt;
    &lt;li&gt; &amp;quot;Else&amp;quot; statement.&lt;/li&gt;
    &lt;li&gt;Returning data that we received from the database.&lt;/li&gt;
  &lt;/ol&gt;
  &lt;h3&gt;Do the same for Update and Delete methods&lt;/h3&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/c9/c9220bb5-0c80-4295-ab35-7ee6fae91d99.png&quot; width=&quot;3212&quot; /&gt;
    &lt;figcaption&gt;quiz.servise.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;h3&gt;Create a class validator&lt;/h3&gt;
  &lt;p&gt;Install the necessary packages:&lt;/p&gt;
  &lt;p&gt;&lt;code&gt;yarn add class-transformer class-validator&lt;/code&gt;&lt;/p&gt;
  &lt;p&gt;And create file&lt;/p&gt;
  &lt;p&gt;&lt;code&gt;touch src/shared/validation.pipe.ts&lt;/code&gt;&lt;/p&gt;
  &lt;p&gt;Now just copy code from the &lt;a href=&quot;https://docs.nestjs.com/pipes#class-validator&quot; target=&quot;_blank&quot;&gt;official documentation,&lt;/a&gt; and write some fixes:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/11/11f57634-f3da-47cc-8314-7692ea86f24f.png&quot; width=&quot;3112&quot; /&gt;
    &lt;figcaption&gt;validation.pipe.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;blockquote&gt;Rows 14-19 — validate did we receive an empty object.&lt;br /&gt;Rows 28-31 ­— pass the errors to logging interceptor.&lt;br /&gt;Rows 41-49 — aggregate all errors into one string.&lt;br /&gt;Rows 50-56 — a function that checks if an object is empty.&lt;/blockquote&gt;
  &lt;p&gt;Then we have to change our DTO file to be able to validate fields:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/09/0957917d-7c35-4269-a5df-935bb862b49b.png&quot; width=&quot;2164&quot; /&gt;
    &lt;figcaption&gt;quiz.dto.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;p&gt;Finally, connect your Validation Pipe to the controller:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/28/28ae63bf-bfd5-44b4-b9d5-842e13dac6db.png&quot; width=&quot;2976&quot; /&gt;
    &lt;figcaption&gt;quiz.controller.ts&lt;/figcaption&gt;
  &lt;/figure&gt;

</content></entry><entry><id>mkots:rkDjfOd2B</id><link rel="alternate" type="text/html" href="https://teletype.in/@mkots/rkDjfOd2B?utm_source=teletype&amp;utm_medium=feed_atom&amp;utm_campaign=mkots"></link><title>Create an API via NestJS Part III</title><published>2019-11-24T21:14:07.201Z</published><updated>2019-11-25T18:53:33.103Z</updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://teletype.in/files/e5/e5fc522f-26e5-4b17-a950-fa31b9d85b63.png"></media:thumbnail><category term="create-an-api-via-nestjs" label="Create an API via NestJS"></category><summary type="html">&lt;img src=&quot;https://teletype.in/files/e5/e5fc522f-26e5-4b17-a950-fa31b9d85b63.png&quot;&gt;The next thing that we should do is creating the Logger for our ErrorHandler:</summary><content type="html">
  &lt;h2&gt;Create Extended Error handler&lt;/h2&gt;
  &lt;ol&gt;
    &lt;li&gt;Create folder:&lt;br /&gt;&lt;code&gt;mkdir src/shared &amp;amp;&amp;amp; cd src/shared&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;Create the filter:&lt;br /&gt;&lt;code&gt;touch http-error.filter.ts&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;Write simple error handler&lt;/li&gt;
  &lt;/ol&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/e5/e5fc522f-26e5-4b17-a950-fa31b9d85b63.png&quot; width=&quot;3012&quot; /&gt;
    &lt;figcaption&gt;src/shared/http-error.filter.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;h2&gt;Include custom error handler as a provider to the app module &lt;/h2&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/77/77e4d85b-e34b-443d-b9c5-577adae6101f.png&quot; width=&quot;2772&quot; /&gt;
    &lt;figcaption&gt;src/app.module.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;h2&gt;Add a logger to this error handler&lt;/h2&gt;
  &lt;p&gt;The next thing that we should do is creating the Logger for our ErrorHandler:&lt;/p&gt;
  &lt;p&gt;Open your http-error.filter.ts file and type simple logger:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/1b/1bd3163a-8027-4711-931f-7b419c32a099.png&quot; width=&quot;2268&quot; /&gt;
    &lt;figcaption&gt;src/shared/http-error.filter.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;blockquote&gt;Pay attention, this code must be member of catch() function&lt;/blockquote&gt;
  &lt;p&gt;Now if we send some incorrect request we should see:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/d7/d7268544-2463-46cb-852d-88017df67ac8.png&quot; width=&quot;4096&quot; /&gt;
    &lt;figcaption&gt;console output&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;p&gt;Looks pretty good, but we still don&amp;#x27;t logging crrect requests. &lt;/p&gt;
  &lt;h2&gt;Let&amp;#x27;s make &lt;a href=&quot;https://docs.nestjs.com/interceptors&quot; target=&quot;_blank&quot;&gt;interceptor&lt;/a&gt;&lt;/h2&gt;
  &lt;p&gt;Create a new file: &lt;code&gt;touch src/shared/logging.interceptor.ts&lt;/code&gt;&lt;/p&gt;
  &lt;p&gt;And write simple logger:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/69/69a3f91b-e3ac-4575-93c7-ab445008466d.png&quot; width=&quot;3348&quot; /&gt;
    &lt;figcaption&gt;src/shared/logging.interceptor.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;p&gt;Now we can connect it as a provider to app.module&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/c2/c2a8e18f-8c30-4884-9604-8960743669c3.png&quot; width=&quot;2876&quot; /&gt;
    &lt;figcaption&gt;src/app.module.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;p&gt;Check how it works. Start the server and send some correct request, you should get some like this:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/e8/e8bdcaf7-f0e6-4251-a0b9-dfb9c343d116.png&quot; width=&quot;4096&quot; /&gt;
  &lt;/figure&gt;

</content></entry><entry><id>mkots:BJ6Tg1NsB</id><link rel="alternate" type="text/html" href="https://teletype.in/@mkots/BJ6Tg1NsB?utm_source=teletype&amp;utm_medium=feed_atom&amp;utm_campaign=mkots"></link><title>Create an API via NestJS Part II</title><published>2019-11-09T06:46:29.073Z</published><updated>2019-11-27T16:14:59.687Z</updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://teletype.in/files/bb/bb1cfd00-df78-4b49-9270-e72340a1ff10.png"></media:thumbnail><category term="create-an-api-via-nestjs" label="Create an API via NestJS"></category><summary type="html">&lt;img src=&quot;https://teletype.in/files/bb/bb1cfd00-df78-4b49-9270-e72340a1ff10.png&quot;&gt;Via NestCLI generate:</summary><content type="html">
  &lt;h2&gt;Create CRUD&lt;/h2&gt;
  &lt;h3&gt;Create infrastructure files:&lt;/h3&gt;
  &lt;p&gt;Via NestCLI generate:&lt;/p&gt;
  &lt;ol&gt;
    &lt;li&gt;Module: &lt;code&gt;nest g mo quiz&lt;/code&gt; &lt;/li&gt;
    &lt;li&gt;Controller: &lt;code&gt;nest g controller quiz&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;Service: &lt;code&gt;nest g service quiz/quiz&lt;/code&gt;&lt;/li&gt;
  &lt;/ol&gt;
  &lt;blockquote&gt;Where &amp;#x60;quiz&amp;#x60; is the name of your module.&lt;/blockquote&gt;
  &lt;h3&gt;Setup API controller&lt;/h3&gt;
  &lt;p&gt;1. Open quiz.&lt;strong&gt;controller.ts &lt;/strong&gt;and write some CRUD methods:&lt;/p&gt;
  &lt;p&gt;You should have something like this&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/bb/bb1cfd00-df78-4b49-9270-e72340a1ff10.png&quot; width=&quot;1504&quot; /&gt;
    &lt;figcaption&gt;quiz.controller.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;p&gt;2. Open quiz.&lt;strong&gt;module.ts and &lt;/strong&gt;import our TypeORM Model:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/8f/8f01e65c-dc54-42c6-92e6-7ce56e099b73.png&quot; width=&quot;1234&quot; /&gt;
    &lt;figcaption&gt;quiz.module.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;p&gt;3. Open quiz&lt;strong&gt;.service.ts&lt;/strong&gt; and add our Entity Repository:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/3d/3dcde500-26b0-4d8b-8ee2-e3e95820fd5c.png&quot; width=&quot;1234&quot; /&gt;
    &lt;figcaption&gt;quiz.service.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;h3&gt;Implement CRUD methods&lt;/h3&gt;
  &lt;p&gt;Now we can implement our CRUD methods, that we had been initialized before. Let&amp;#x27;s open quiz.&lt;strong&gt;service.ts&lt;/strong&gt; file and write implementation:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/89/89b508f0-9574-4f2b-963b-e064363f5abd.png&quot; width=&quot;2468&quot; /&gt;
    &lt;figcaption&gt;quiz.service.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;p&gt;As we can see in lines 16 and 20 we use the  &lt;code&gt;data &lt;/code&gt;param with type &lt;code&gt;any&lt;/code&gt;. That is not accorded to TypeScript guidelines, we must write DTO (Data Transfer Object) to tipizate this param:&lt;/p&gt;
  &lt;h3&gt;Create DTO&lt;/h3&gt;
  &lt;p&gt;Firstly we have to create this file via command: &lt;code&gt;touch src/quiz/quiz.&lt;strong&gt;dto.ts&lt;/strong&gt;&lt;/code&gt;&lt;/p&gt;
  &lt;p&gt;Then open this file and describe our data:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/4f/4ffd34df-88c2-46b9-a521-c81628df99b7.png&quot; width=&quot;796&quot; /&gt;
    &lt;figcaption&gt;quiz.dto.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;p&gt;Now we can type param data in quiz&lt;strong&gt;.service.ts:&lt;/strong&gt;&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/86/86ddf77b-75a4-464e-95d4-62de61d1f0ea.png&quot; width=&quot;2504&quot; /&gt;
    &lt;figcaption&gt;quiz.service.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;h3&gt;Connect the service to the controller&lt;/h3&gt;
  &lt;p&gt;Now, finally, we can connect our service to the controller. Open quiz.&lt;strong&gt;controller.ts&lt;/strong&gt; and determinate service functions:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/35/35d5daee-6bf0-4296-bede-a5f2cef7997e.png&quot; width=&quot;2876&quot; /&gt;
    &lt;figcaption&gt;quiz.controller.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;p&gt;Finally, you can start your server and test its functionality &lt;/p&gt;
  &lt;h3&gt;Test &lt;/h3&gt;
  &lt;p&gt;Actually, we must write many tests and mutch documentation for something like Swagger to our API, but not today. In this part, Postman is enough for us. Let&amp;#x27;s install it:&lt;/p&gt;
  &lt;pre&gt;sudo snap install postman &lt;/pre&gt;
  &lt;p&gt;In the Postman press &amp;quot;New&amp;quot; button: &lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/4f/4f4bf8a5-a766-4d21-9bb6-619b215a70b7.png&quot; width=&quot;751&quot; /&gt;
  &lt;/figure&gt;
  &lt;p&gt;On the next step choose &amp;quot;Request&amp;quot;:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/a6/a6117398-1b26-44a3-8bba-c48c6bd23d7e.png&quot; width=&quot;1098&quot; /&gt;
  &lt;/figure&gt;
  &lt;p&gt;Next, setup Name for request and Name for collection, and press Save to Collection:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/37/379af54b-a506-4cc5-9e14-21af738d5317.png&quot; width=&quot;491&quot; /&gt;
  &lt;/figure&gt;
  &lt;figure class=&quot;m_original&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/ad/adc727d3-81dc-4708-9a82-e6384c20aaa4.png&quot; width=&quot;497&quot; /&gt;
  &lt;/figure&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/08/0803cfa8-5ad2-4221-87b4-496f24b8c550.png&quot; width=&quot;483&quot; /&gt;
  &lt;/figure&gt;
  &lt;h3&gt;GET readAll()&lt;/h3&gt;
  &lt;p&gt;Finally, we can create our first request:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/16/160163da-087a-4f63-a9c0-2c64f0e09262.png&quot; width=&quot;1065&quot; /&gt;
  &lt;/figure&gt;
  &lt;ol&gt;
    &lt;li&gt;Choose type GET&lt;/li&gt;
    &lt;li&gt;Write address for entry point with format IP : PORT / service&lt;/li&gt;
    &lt;li&gt;Press Send&lt;/li&gt;
  &lt;/ol&gt;
  &lt;p&gt;If everything is alright, you should see the empty array in the response&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/7e/7e10615f-9428-417a-a327-7f5aa7751549.png&quot; width=&quot;425&quot; /&gt;
  &lt;/figure&gt;
  &lt;h3&gt;POST create()&lt;/h3&gt;
  &lt;p&gt;Change Type to POST and create some entry:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/e5/e5cce03d-a11c-479e-a093-af39e6800c88.png&quot; width=&quot;1063&quot; /&gt;
  &lt;/figure&gt;
  &lt;ol&gt;
    &lt;li&gt;Change Type of Request&lt;/li&gt;
    &lt;li&gt;Go to tab &amp;quot;Body&amp;quot;&lt;/li&gt;
    &lt;li&gt;Choose &amp;quot;raw&amp;quot; type&lt;/li&gt;
    &lt;li&gt;Set &amp;quot;JSON&amp;quot; format&lt;/li&gt;
    &lt;li&gt;Write your request data. Fields for body you can find in DTO or Entry files&lt;/li&gt;
  &lt;/ol&gt;
  &lt;p&gt;Press Send. And you should see a response like this:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/04/04584115-25d9-4499-a91b-71899355ab8a.png&quot; width=&quot;448&quot; /&gt;
  &lt;/figure&gt;
  &lt;p&gt;Copy id value, it will be necessary for the next request&lt;/p&gt;
  &lt;h3&gt;PUT update()&lt;/h3&gt;
  &lt;p&gt;Change Type to PUT and edit our entry:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/47/47837127-7650-44bf-bc36-76fe3b595d05.png&quot; width=&quot;1058&quot; /&gt;
  &lt;/figure&gt;
  &lt;ol&gt;
    &lt;li&gt;Change Type to PUT&lt;/li&gt;
    &lt;li&gt;Change URL according to format URL : PORT / SERVICE / UUID &lt;/li&gt;
    &lt;li&gt;Edit Body data &lt;/li&gt;
  &lt;/ol&gt;
  &lt;p&gt;Press Send. Now response will be different:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/c9/c979b1e5-70a6-45fa-a982-78336902c04a.png&quot; width=&quot;452&quot; /&gt;
  &lt;/figure&gt;
  &lt;h3&gt;DELETE delete()&lt;/h3&gt;
  &lt;p&gt;Change type to DELETE&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/6f/6f805e7c-c69b-41bd-9261-839687686879.png&quot; width=&quot;1063&quot; /&gt;
  &lt;/figure&gt;
  &lt;ol&gt;
    &lt;li&gt;Change Type to DELETE and press Send&lt;/li&gt;
  &lt;/ol&gt;
  &lt;p&gt;The response should be:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/ea/ea24a48d-0120-4e81-9684-cf8aa776374d.png&quot; width=&quot;451&quot; /&gt;
  &lt;/figure&gt;

</content></entry><entry><id>mkots:SJ1wGU2qr</id><link rel="alternate" type="text/html" href="https://teletype.in/@mkots/SJ1wGU2qr?utm_source=teletype&amp;utm_medium=feed_atom&amp;utm_campaign=mkots"></link><title>Create an API via NestJS Part I</title><published>2019-11-03T14:17:10.252Z</published><updated>2019-11-09T06:48:16.051Z</updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://teletype.in/files/ef/efbc26dc-2a43-4e6e-b2f7-08bb19553e1a.png"></media:thumbnail><category term="create-an-api-via-nestjs" label="Create an API via NestJS"></category><summary type="html">&lt;img src=&quot;https://teletype.in/files/ef/efbc26dc-2a43-4e6e-b2f7-08bb19553e1a.png&quot;&gt;Firstly we need to install CLI tools for creating the scaffold.</summary><content type="html">
  &lt;h2&gt;1. Setup the project&lt;/h2&gt;
  &lt;p&gt;Firstly we need to install CLI tools for creating the scaffold.&lt;/p&gt;
  &lt;p&gt;I use the npm for this because the yarn has a bug with node engine version:&lt;/p&gt;
  &lt;pre&gt;npm i -g @nestjs/cli&lt;/pre&gt;
  &lt;p&gt;After that, we must create a new boilerplate project:&lt;/p&gt;
  &lt;p&gt;&lt;code&gt;nest new {type your project name here}&lt;/code&gt;&lt;/p&gt;
  &lt;p&gt;This wizard will ask you about which package manager is you prefer:&lt;/p&gt;
  &lt;pre&gt;Which package manager would you ❤️ to use?
&amp;gt;npm
&amp;gt;yarn&lt;/pre&gt;
  &lt;p&gt;I prefer yarn and in this article, I had chosen it.&lt;/p&gt;
  &lt;p&gt;When installation will be done you will see:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/ef/efbc26dc-2a43-4e6e-b2f7-08bb19553e1a.png&quot; width=&quot;1582&quot; /&gt;
    &lt;figcaption&gt;bash output&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;p&gt;Now we can setting up Prettier:&lt;/p&gt;
  &lt;p&gt;&lt;code&gt;cd {YourProjectName}&lt;br /&gt;vim .prettierrc&lt;/code&gt; &lt;/p&gt;
  &lt;p&gt;In this file we must wrote something like this:&lt;/p&gt;
  &lt;p&gt;&lt;code&gt;{&lt;br /&gt;  &amp;quot;useTabs&amp;quot;: false,&lt;br /&gt;  &amp;quot;tabWidth&amp;quot;: 2,&lt;br /&gt;  &amp;quot;singleQuote&amp;quot;: true,&lt;br /&gt;  &amp;quot;trailingComma&amp;quot;: &amp;quot;all&amp;quot;,&lt;br /&gt;  &amp;quot;semi&amp;quot;: true,&lt;br /&gt;  &amp;quot;jsxBracketSameLine&amp;quot;: true,&lt;br /&gt;  &amp;quot;bracketSpacing&amp;quot;: true,&lt;br /&gt;  &amp;quot;arrowParens&amp;quot;: &amp;quot;avoid&amp;quot;&lt;br /&gt;}&lt;/code&gt;&lt;/p&gt;
  &lt;p&gt;Finally we can start project and have see Hello World message, let&amp;#x27;s try:&lt;/p&gt;
  &lt;p&gt;&lt;code&gt;cd {YourProjectName}&lt;/code&gt;&lt;/p&gt;
  &lt;pre&gt;yarn run start &lt;/pre&gt;
  &lt;p&gt;When you will see:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/9a/9ae9a12a-3fd7-4e70-a42a-06d01be44402.png&quot; width=&quot;2020&quot; /&gt;
    &lt;figcaption&gt;bash output&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;p&gt;You can check how it&amp;#x27;s work into your browser on page &lt;a href=&quot;http://localhost:3000&quot; target=&quot;_blank&quot;&gt;http://localhost:3000&lt;/a&gt;&lt;/p&gt;
  &lt;p&gt;And now you probably get stuck with :3000, what does it mean? Yeah, it is port of your server, it can be change in &lt;em&gt;src/main.ts&lt;/em&gt; file line 6:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/6c/6c8a6665-bfa3-480b-b8e7-afbcfa715ee5.png&quot; width=&quot;1202&quot; /&gt;
    &lt;figcaption&gt;src/main.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;p&gt;It&amp;#x27;s not beautiful enough, what we have to do if we&amp;#x27;ll want change it dynamically? Yes, we must pull it into enviroment variable. But how? &lt;/p&gt;
  &lt;p&gt;The &lt;em&gt;dotenv &lt;/em&gt;packagecomes to our aid, let&amp;#x27;s add it:&lt;/p&gt;
  &lt;p&gt;&lt;code&gt;yarn add dotenv&lt;/code&gt;&lt;/p&gt;
  &lt;p&gt;and setup it:&lt;/p&gt;
  &lt;p&gt;&lt;code&gt;touch .env &amp;amp;&amp;amp; echo &amp;quot;PORT=4000&amp;quot; &amp;gt; .env&lt;/code&gt;&lt;/p&gt;
  &lt;p&gt;Now we can use environment variables in our project just like this:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/f9/f989cbbd-e943-4b51-a84e-d4fe3654ec9f.png&quot; width=&quot;1202&quot; /&gt;
    &lt;figcaption&gt;src/main.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;p&gt;What is we have done?&lt;/p&gt;
  &lt;ol&gt;
    &lt;li&gt;Imported package dotenv/config into main.ts&lt;/li&gt;
    &lt;li&gt;Created constant with the value from .env file or, if it&amp;#x27;s empty,  8080&lt;/li&gt;
    &lt;li&gt;Used it in the function app.listen() &lt;/li&gt;
  &lt;/ol&gt;
  &lt;p&gt;Now we can check this, go to &lt;a href=&quot;http://localhost:4000&quot; target=&quot;_blank&quot;&gt;http://localhost:4000&lt;/a&gt; if we have done everything right, we&amp;#x27;ll see the &amp;quot;Hello World&amp;quot; message.&lt;/p&gt;
  &lt;blockquote&gt;Not forget to reload your server, if you want use hotreload you have to use &lt;u&gt;yarn run start:dev &lt;/u&gt;instead of &lt;u&gt;yarn run start &lt;/u&gt;&lt;/blockquote&gt;
  &lt;p&gt;Looks good, but how we can know witch PORT used now? We have to write a Logger for our server! Open &lt;em&gt;main.ts&lt;/em&gt; file and add this string after &lt;em&gt;app.listen()&lt;/em&gt; function:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/75/757cd981-d0c5-4665-b0a2-355fd2da348e.png&quot; width=&quot;1286&quot; /&gt;
    &lt;figcaption&gt;src/main.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;p&gt;Now, after you start your server you must see message like this:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/99/996a06fe-4432-4a95-8bca-2a114b1d9ec6.png&quot; width=&quot;2048&quot; /&gt;
    &lt;figcaption&gt;bash output&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;h2&gt;2. Install packages to using DataBase&lt;/h2&gt;
  &lt;h3&gt;JS packages&lt;/h3&gt;
  &lt;p&gt;In order to work with the database, we need to install some packages:&lt;/p&gt;
  &lt;pre&gt;yarn add pg typeorm @nestjs/typeorm&lt;/pre&gt;
  &lt;h3&gt;OS packages&lt;/h3&gt;
  &lt;p&gt;For development we also need Postgres Server and PgAdmin tool, if you are use Ubuntu 18.04 you have to install it this way:&lt;/p&gt;
  &lt;ol&gt;
    &lt;li&gt;&lt;code&gt;cd /etc/apt/sources.list.d/&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;&lt;code&gt;sudo touch pgdg.list&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;&lt;code&gt;echo &amp;quot;deb &lt;a href=&quot;http://apt.postgresql.org/pub/repos/apt/&quot; target=&quot;_blank&quot;&gt;http://apt.postgresql.org/pub/repos/apt/&lt;/a&gt; bionic-pgdg main&amp;quot; &amp;gt; pgdg.list&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;&lt;code&gt;wget --quiet -O - &lt;a href=&quot;https://www.postgresql.org/media/keys/ACCC4CF8.asc&quot; target=&quot;_blank&quot;&gt;https://www.postgresql.org/media/keys/ACCC4CF8.asc&lt;/a&gt; | sudo apt-key add -&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;&lt;code&gt;sudo apt-get update&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;&lt;code&gt;sudo apt install postgresql-11 postgresql-client-11 pgadmin4 &lt;/code&gt;&lt;/li&gt;
  &lt;/ol&gt;
  &lt;p&gt;For another OS check &lt;a href=&quot;https://www.postgresql.org/download/&quot; target=&quot;_blank&quot;&gt;this page&lt;/a&gt;.&lt;/p&gt;
  &lt;h3&gt;After installation instruction:&lt;/h3&gt;
  &lt;ol&gt;
    &lt;li&gt;Edit the &lt;code&gt;pg_hba.conf &lt;/code&gt;file: &lt;code&gt;sudo vim /etc/postgresql/11/main/pg_hba.conf&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;Change the line &lt;code&gt;local all postgres peer &lt;/code&gt;to&lt;code&gt; local all postgres trust&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;Restart &lt;code&gt;postgresql&lt;/code&gt; service: &lt;code&gt;sudo service postgresql restart&lt;/code&gt;&lt;/li&gt;
    &lt;li&gt;Log in into DB and change password: &lt;/li&gt;
    &lt;ol&gt;
      &lt;li&gt;&lt;code&gt;psql -U postgres&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;In the postgres=# prompt: &lt;code&gt;ALTER USER postgres with password ‘new-password’;&lt;/code&gt;&lt;/li&gt;
    &lt;/ol&gt;
    &lt;li&gt;Revert the changes:&lt;/li&gt;
    &lt;ol&gt;
      &lt;li&gt; &lt;code&gt;sudo vim /etc/postgresql/11/main/pg_hba.conf &lt;/code&gt;change line from &lt;code&gt;trust &lt;/code&gt;to &lt;code&gt;md5&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;Restart &lt;code&gt;postgresql&lt;/code&gt; service: &lt;code&gt;sudo service postgresql restart&lt;/code&gt;&lt;/li&gt;
    &lt;/ol&gt;
  &lt;/ol&gt;
  &lt;h2&gt;3. Setup DataBase&lt;/h2&gt;
  &lt;h3&gt;Create a new DataBase &lt;/h3&gt;
  &lt;ol&gt;
    &lt;ul&gt;&lt;/ul&gt;
    &lt;li&gt;Login to Postgres&lt;/li&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;code&gt;psql -U postgres&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;li&gt;Create a new DB&lt;/li&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;code&gt;create database quiz;&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code&gt;\q&lt;/code&gt;  - to quit from psql&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/ol&gt;
  &lt;h3&gt;Connect the DataBase to your project&lt;/h3&gt;
  &lt;ol&gt;
    &lt;ol&gt;&lt;/ol&gt;
  &lt;/ol&gt;
  &lt;p&gt;Create ORM config file&lt;/p&gt;
  &lt;ul&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;code&gt;touch ormconfig.js&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;Edit this file as follows &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/ul&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/bc/bcbaee86-ce89-49b7-9912-bc8f65330b90.png&quot; width=&quot;1572&quot; /&gt;
    &lt;figcaption&gt;&lt;code&gt;ormconfig.js&lt;/code&gt;&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;blockquote&gt;Where fields &amp;quot;host&amp;quot;, &amp;quot;username&amp;quot;, &amp;quot;password&amp;quot;, &amp;quot;database&amp;quot; is your credentials to database which can set in .env file  &lt;/blockquote&gt;
  &lt;ul&gt;
    &lt;ul&gt;
      &lt;li&gt;Set credentials in .env file:&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/ul&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/d6/d6f8813f-e2ee-41e0-95f1-c43b2b48652e.png&quot; width=&quot;796&quot; /&gt;
    &lt;figcaption&gt;.en&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;p&gt;Import ORM module:&lt;/p&gt;
  &lt;ul&gt;
    &lt;ul&gt;
      &lt;li&gt;Go to file &lt;code&gt;src/app.module.ts&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;Add the following lines:&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/ul&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/78/7825a894-1649-4031-9034-74793d69d9bd.png&quot; width=&quot;1184&quot; /&gt;
    &lt;figcaption&gt;src/app.module.ts&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;h2&gt;Start working with DB&lt;/h2&gt;
  &lt;h3&gt;Create entities&lt;/h3&gt;
  &lt;ol&gt;
    &lt;li&gt;Create entnities structure&lt;/li&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;code&gt;mkdir src/quiz&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code&gt;cd src/quiz/ &amp;amp;&amp;amp; touch quiz.entity.ts&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
    &lt;li&gt;Edit &lt;code&gt;quiz.entity.ts &lt;/code&gt;as follow:&lt;/li&gt;
    &lt;ol&gt;&lt;/ol&gt;
  &lt;/ol&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/4c/4cad6e3f-5e87-47f7-9086-9997a36d92c4.png&quot; width=&quot;1118&quot; /&gt;
  &lt;/figure&gt;
  &lt;h3&gt;Check if it work&lt;/h3&gt;
  &lt;p&gt;Star your server via &lt;code&gt;yarn run start &lt;/code&gt;. Then you must see output like that:&lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/5a/5a058c7d-3d6f-4189-8751-f9b3766e91f6.png&quot; width=&quot;2048&quot; /&gt;
    &lt;figcaption&gt;bash output&lt;/figcaption&gt;
  &lt;/figure&gt;
  &lt;p&gt;Now browse the DB:&lt;/p&gt;
  &lt;ul&gt;
    &lt;ul&gt;
      &lt;li&gt;&lt;code&gt;psql -U postgres - connect to db server&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code&gt;\c quiz - open db called quiz&lt;/code&gt;&lt;/li&gt;
      &lt;li&gt;&lt;code&gt;\dt - describe tables within&lt;/code&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/ul&gt;
  &lt;p&gt;If everything is ok, you should see: &lt;/p&gt;
  &lt;figure class=&quot;m_column&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/d4/d49fdc29-aa3e-4dd0-ae3c-7e44e4ea6d1f.png&quot; width=&quot;932&quot; /&gt;
    &lt;figcaption&gt;bash output&lt;/figcaption&gt;
  &lt;/figure&gt;

</content></entry><entry><id>mkots:wordpress-in-docker</id><link rel="alternate" type="text/html" href="https://teletype.in/@mkots/wordpress-in-docker?utm_source=teletype&amp;utm_medium=feed_atom&amp;utm_campaign=mkots"></link><title>Wordpress in Docker</title><published>2018-07-13T15:07:56.264Z</published><updated>2020-08-04T20:26:44.735Z</updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://teletype.in/files/b4/64/b4646739-c405-4a24-b17b-746e8d0a6fc4.jpeg"></media:thumbnail><category term="misc" label="Misc"></category><summary type="html">&lt;img src=&quot;https://teletype.in/files/ff/ff418c77-9349-4286-b1fe-1b073d052db5.png&quot;&gt;Возможно, потребуется установка curl</summary><content type="html">
  &lt;ol&gt;
    &lt;li&gt;Скачать Docker-compose&lt;/li&gt;
  &lt;/ol&gt;
  &lt;pre&gt;sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose&lt;/pre&gt;
  &lt;p&gt;Возможно, потребуется установка &lt;code&gt;curl&lt;/code&gt;&lt;/p&gt;
  &lt;pre&gt;sudo apt install curl
&lt;/pre&gt;
  &lt;p&gt;2. Дать права на исполнение&lt;/p&gt;
  &lt;pre&gt;sudo chmod +x /usr/local/bin/docker-compose
&lt;/pre&gt;
  &lt;p&gt;3. Протестировать&lt;/p&gt;
  &lt;pre&gt;docker-compose --version
&lt;/pre&gt;
  &lt;p&gt;4. Создать папку под проект и перейти в нее&lt;/p&gt;
  &lt;pre&gt;sudo mkdir ~/wordpress &amp;amp;&amp;amp; cd ~/wordpress
&lt;/pre&gt;
  &lt;p&gt;5. Создать файл &lt;code&gt;docker-compose.yml&lt;/code&gt; и добавить в него конфиг&lt;/p&gt;
  &lt;pre&gt;sudo nano docker-compose.yml


version: &amp;#x27;3.3&amp;#x27;

services:
   db:
     image: mysql:5.7
     volumes:
       - db_data:/var/lib/mysql
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: somewordpress
       MYSQL_DATABASE: wordpress
       MYSQL_USER: wordpress
       MYSQL_PASSWORD: wordpress

   wordpress:
     depends_on:
       - db
     image: wordpress:latest
     ports:
       - &amp;quot;8000:80&amp;quot;
     restart: always
     environment:
       WORDPRESS_DB_HOST: db:3306
       WORDPRESS_DB_USER: wordpress
       WORDPRESS_DB_PASSWORD: wordpress
volumes:
    db_data:
&lt;/pre&gt;
  &lt;p&gt;6. Собрать и поднять контейнер&lt;/p&gt;
  &lt;pre&gt;sudo docker-compose up -d
&lt;/pre&gt;
  &lt;p&gt;7. Установить Wordpress&lt;/p&gt;
  &lt;p&gt;Перейти на http://127.0.0.1:8000&lt;/p&gt;
  &lt;figure class=&quot;m_custom&quot;&gt;
    &lt;img src=&quot;https://teletype.in/files/ff/ff418c77-9349-4286-b1fe-1b073d052db5.png&quot; width=&quot;504&quot; /&gt;
  &lt;/figure&gt;

</content></entry></feed>