<?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 Zaw Htet Naing on Medium]]></title>
        <description><![CDATA[Stories by Zaw Htet Naing on Medium]]></description>
        <link>https://medium.com/@zawhtetnaing?source=rss-c4b2bc565629------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*-75wOXChCotOP7jUWqauGw.jpeg</url>
            <title>Stories by Zaw Htet Naing on Medium</title>
            <link>https://medium.com/@zawhtetnaing?source=rss-c4b2bc565629------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 06:59:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@zawhtetnaing/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[Building a Self-Healing AI Agent: A Chatbot Design for Production-Grade Reliability]]></title>
            <link>https://medium.com/@zawhtetnaing/building-a-self-healing-ai-agent-a-chatbot-design-for-production-grade-reliability-47416f818e78?source=rss-c4b2bc565629------2</link>
            <guid isPermaLink="false">https://medium.com/p/47416f818e78</guid>
            <dc:creator><![CDATA[Zaw Htet Naing]]></dc:creator>
            <pubDate>Thu, 08 Jan 2026 10:40:50 GMT</pubDate>
            <atom:updated>2026-01-08T13:49:31.873Z</atom:updated>
            <content:encoded><![CDATA[<h4>The problem I am trying to solve:</h4><p>In many chat bot applications, AI chatbots follow a simple cycle: A user asks a question, the bot retrieves data and it generates an answer. But what happens when the answer is wrong? Usually, a human steps in -&gt; fixes the ticket and moves on.</p><p>This might solve the customer’s immediate problem, but it doesn’t fix the system. The bot can make the exact same mistake again and again.</p><p>To solve this, we can design a Self-Healing Architecture. The goal is “The bot should never do same mistake twice”.</p><h4>The Architecture</h4><p>Instead of very long prompt, we break the logics into specialized bots. We can think of it a customer support department where one staff answers customers, another checks quality, another diagnose failures and another update internal knowledge.</p><p>I used six bots. Let me explain the detailed logic of each bot later in this post. First, I will just leave the names of bots, so you can check diagram to understand how these bots work together at a system level. Here are the bots used in this architecture <strong>The Primary Agent, Confidence Scorer/Gatekeeper, Mistake Finder, Memory Management, Knowledge Builder, Instruction Builder.</strong></p><h4>Success Circle</h4><p>This scenario represents the workflow that the system is confident in its own answer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xQsu9LQ2Ben_AgdVNnJP1A.png" /></figure><h4>Mistake happened</h4><p>This scenario represents the workflow that the system is not confident in its own answer. So, it leaves the generated reply message as a pending message. Once a human edits or rejects the message, the Mistake Finder analyzes what went wrong and routes the failure into the appropriate self-healing path (updating memory or improving system instructions).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9SsEDls_DGBvr_YJLdGprg.png" /></figure><h4>The Internal Logic of the Bots</h4><ol><li><strong>The Primary Agent (Message Generator)</strong><br>It has access to the following tools: <br>1. Memory finder (Vector storage database. This one is important. Memory finder allows the bot to retrieve previous information where a similar customer intention was solved by a human)<br>2. Order Tracking<br>3. Product finder<br>4. Inventory APIs and any other tools that might need to get info to satisfy customer’s request ) and generates the initial response based on customer’s intent and conversation history.</li><li><strong>Confidence Scorer (Gatekeeper)</strong><br>This agent acts as Quality Control. It doesn’t talk to the customer. It only reviews the Primary Agent’s output.<br>The logic is that it assign marks based on strict rules.<br><strong>eg. if Priamry Agent used non-existent information, minus 10 mark<br></strong><em> </em><br>If the final result mark exceeds a specific threshold, send the message. Otherwise the message is held in “Pending” state and wait for human review.</li><li><strong>Mistake Finder<br></strong>This bot triggers when a human rejects or edits a “Pending” message.<br>It uses the following infos:<br>1. Prompt sent to primary agent <br>2. The tools results used by primary agent<br>3. The memory used by primary agent.<br> <br>Then, it identifies the mistake type (<strong>INCORRECT_PROMPT</strong>, <strong>INCORRECT_TOOL_RESULT</strong>, <strong>INCORRECT_MEMORY</strong> or <strong>OTHER</strong>) and provides a detailed explanation of why the failure happened.</li><li><strong>Memory Management</strong><br>This bot runs after Mistake Finder bot.<br>If the mistake is <strong>INCORRECT_MEMORY</strong>, it uses the detailed info from <strong>Mistake Finder Bot </strong>and the full context used by primary agent (prompt + tool results + memory) and then generate a new meory record.<br><strong>Human Fixes</strong> -&gt; <strong>Knowledge Builder</strong> extracts the <strong>Intention</strong> -&gt; <strong>Vector DB</strong> stores the <strong>Embedding</strong>.</li><li><strong>Knowledge Builder</strong><br>This bot is used by <strong>memory management bot </strong>to ensure the data going to insert into memory db is useful to future conversations and in high quality. <br>It operates on the <strong>full context used by the Primary Agent (</strong>prompt, tool results, etc..) and the details information from <strong>Mistake Finder. </strong><br>Example of memory record: <br> <br><strong>intention</strong>: “Cutomer asks for refund info” <br><strong>content</strong>: “As of 2026, customers can return products within 14 days of delivery. Items must be unused and in original packaging. Refunds are processed within 5 business days&quot; <br> <br>Note: The <strong>intention</strong> is used as the semantic key by both the <strong>Knowledge Builder</strong> and <strong>Primary Agent</strong> to identify correct memories.<br> <br>When inserting new information, the bot <strong>merges the new memory with existing related memories</strong> to produce a complete, up-to-date content. The Primary Agent then uses the customer’s intention to retrieve the most relevant memory during future conversations.</li><li><strong>Instruction Builder</strong><br>This bot runs if the mistake is INCORRECT_PROMPT. It uses current <strong>Primary Agent’s </strong>prompt<strong> </strong>+ details info from <strong>Mistake Finder</strong> bot to build new prompt. <br>Note: You can just add a new Gatekeeper bot to validate new prompt and update the prompt of <strong>Primary Agent</strong> directly. Or you can leave new prompt for human review.</li></ol><h4>Why Vector DB?</h4><p>We used customer intention as a key to find previous memory. A standard database is great for looking up exact matches, like an order number or a product name. But customer intention are often messy information which can be highly varied. <br>Vector DB supports a <strong>semantic search</strong>: even if a customer asks a question using different wording than the one we stored in db, the bot can find the correct memory record by doing similarity check.</p><h4>Example</h4><p>User asks about a current(2026) return policy. The Bot uses a tool to retrieve old memory and gives return policy used in 2010.</p><ol><li>The Confidence Scorer flags the date mismatch and leave the message in pending state.</li><li>A staff member will send the correct 2026 policy.</li><li>The <strong>Mistake Finder</strong> notes it was incorrect memory. <strong>The Memory Bot</strong> “upserts” the new 2026 policy into the Vector DB.</li></ol><p>The next time a user asks, the Primary Agent retrieves the 2026 data first. In this way the system has “healed” itself.</p><p>Thanks a million for reading. Have a good day :).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=47416f818e78" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What is CQRS pattern? How Can you simply implement CQRS in your project?]]></title>
            <link>https://medium.com/@zawhtetnaing/what-is-cqrs-pattern-how-can-you-simply-implement-cqrs-in-your-project-ee1c67f22826?source=rss-c4b2bc565629------2</link>
            <guid isPermaLink="false">https://medium.com/p/ee1c67f22826</guid>
            <category><![CDATA[php]]></category>
            <category><![CDATA[cqrs-pattern]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Zaw Htet Naing]]></dc:creator>
            <pubDate>Fri, 04 Oct 2024 08:20:06 GMT</pubDate>
            <atom:updated>2024-10-04T15:37:41.807Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*LBYizUpfl0YbbpdH" /><figcaption>Photo by <a href="https://unsplash.com/@emilep?utm_source=medium&amp;utm_medium=referral">Emile Perron</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>ကျနော်တို့ CQRS ကိုမပြောခင် ကျနော်တို့ရဲ့ traditional CRUD approach ကိုအရင်လေ့လာကြည့်ရအောင်။ ကျနော်တို့မှာ E-commerce application တစ်ခုရှိတယ်ဆိုပါစို့။ ကျနော်တို့ application ထဲက order အပိုင်းမှာ အောက်က actions တွေရှိတယ်ဆိုပါစို့။</p><ol><li>order တွေ ကို create မယ်။</li><li>order listing page မှာကျနော်တို့ create ထားတဲ့ order တွေကို list လိုက်ပြန်ပြမယ်။</li></ol><p>order listing page မှာပြန်ပြဖို့ ကျနော်တို့ order တွေ ပြန်ဆွဲထုတ်တဲ့အခါ information ပြည့်ပြည့်စုံစုံရအောင် product table, customer table, cart table, stock table အစရှိသဖြင့် table တွေ ကို join ပြီး query ရေးရတော့မယ်။ ကျနော်တို့ ရဲ့ application ပိုပြီး ရှုပ်ထွေးလာတာနဲ့အမျှ ကျနော်တို့ data ဆွဲထုတ်ဖို့သုံးရတဲ့ query တွေက complex ပိုဖြစ်လာမယ်။ ပို complex ရင် result ရဖို့ပိုကြာတယ်။ ဒါကပြသနာတစ်ခုဖြစ်ပါတယ်။</p><p>နောက်တစ်ခုက ကျနော်တို့ CRUD မှာ read action ကအလုပ်များလေ့ရှိတယ်။ အဲ့ဒီတော့ read ရဲ့ work load နဲ့ ကျန်တဲ့ actions တွေရဲ့ work load ကမညီမျှဘူး။</p><blockquote>ဥပမာ- facebook မှာ ကိုယ်က post တစ်ခုတင်လိုက်ရင် ထောင် နဲ့ချီတဲ့ လူတွေက အဲ့တာကိုမြင်ရတယ်။ create action count 1 | read action count 1000+</blockquote><p>ဒါပေမဲ့လည်းကျနော်တို့က user ဘယ်လောက်များလာလာ user တွေကို performance ကောင်းကောင်းနဲ့မြန်မြန်ဆန်ဆန် data တွေကိုပြန်ပြပေးဖို့လိုတယ်။ ဒါဆို ကျနော်တို့ ဘယ်လိုလုပ်ကြမလဲ။</p><ol><li>user တွေဆီပြန်ပြရမယ့် result ကိုသီခြား data model တစ်ခုအနေနဲ့ သိမ်းထားလိုက်မယ်။ ခုနက order listing ဥပမာမှာဆိုရင် ကျနော်တို့ product name, customer name, cart ID စတဲ့ user ဆီကိုပြန်ပြမယ့် field တွေပါတဲ့ table တစ်လုံးတည်ဆောက်လိုက်မယ်။ order create ရင်အဲ့ဒီ table ထဲကိုပါ data သွားထည့်မယ်။ order listing ပြန်ပြရင် table join တာတွေလုပ်မနေတော့ပဲ အဲ့ဒီ table အသစ်တစ်ခုထဲကိုပဲ query ဆွဲပေးလိုက်မယ်။ အိုကေဒါဆို complex query တွေကိုလျော့ချနိုင်မယ် Read action အတွက် workload ကျလာမယ်။</li><li>ကျနော်တို့ Read action အတွက်သီးသန့် performance ကောင်းတဲ့ db အသစ်တစ်ခုသာ ခွဲပြီးဆောက်လိုက်မယ်။ read replica တစ်ခုဆောက်လိုက်မယ်ပေါ့</li></ol><p>ကျနော်တို့ ဒီ solution 2ခုကို လေ့လာကြည့်ရင် သူတို့ရဲ့ အဓိက goal က CRUD ကနေပြီးတော့ R ကိုသီးသန့်ခွဲပြီး handle လိုက်တာတွေ့နိုင်ပါတယ်။ CUD အတွက် data model တစ်ခု R အတွက် data model တစ်ခုဖြစ်သွားတယ်။ CUD အတွက် database တစ်ခု R အတွက် database တစ်ခုဖြစ်သွားတယ်။ အစရှိသဖြင့်ပေါ့နော်။</p><p>ကျနော်တို့ အဲ့လို CUD နဲ့ R ကိုသီးသန့်ခွဲပြီး handle ချင်တဲ့ project တွေမှာ <strong>CQRS (command query responsibility segregation)</strong> ဆိုတဲ့ design pattern ကို apply လို့ရပါတယ်။ ဒီ design pattern ရဲ့အဓိကအားသာချက်ကတော့ CUD actions တွေနဲ့ R ကိုခွဲပြီး independently handle လို့ရသွားစေတာပါ။ အဲ့ဒီတော့ ကျနော်တို့ data model တွေပဲခွဲခွဲ၊ သီးသန့် read replica db ပဲသုံးသုံး၊ read နဲ့ write ကို သီးသန့် auth တွေပဲ ခံခံ လွယ်လွယ်ကူကူလုပ်ဆောင်လို့ရသွားစေပါတယ်။</p><p>Note: CQRS ကို applied တဲ့ project တွေမှာ project စရေးထဲက read နဲ့ write ကို data model/source တွေခွဲဖို့ကမဖြစ်မနေလိုအပ်ချက်မဟုတ်ပါဘူး။ ကျနော်တို့ application ကိုလူသုံးများလာတဲ့အခါ၊ ပုံမှန် crud နဲ့တော့ငါတို့ အဆင်မပြေတော့ဘူး ဖြစ်လာတဲ့အခါကျမှ ခွဲချပလိုက်လို့ရအောင် ကြိုရေးထားဖို့ပဲလိုပါတယ်။ CQRS ရဲ့ အဓိက goal က code ရေးတဲ့အခါ CUD နဲ့ R ကို ခွဲပြီး handle ဖို့ပါပဲ။</p><p>CQRS pattern မှာ CUD actions တွေကို Command လို့ယူဆပြီး R action ကို Query လို့ယူဆပါတယ်။ အဓိက ကတော့ Command နဲ့ Query တွေကို သီးသန့်ဆီဖြစ်နေအောင်ရေးဖို့လို့ပါတယ်။</p><h4>My thoughts on CQRS</h4><ol><li>CQRS pattern ကိုကျနော်ကြိုက်တဲ့ တစ်ချက်က ကျနော်တို့ လုပ်လက်စ project ထဲကို developer တစ်ယောက်၀င်လာတယ်ဆိုပါစို့။ အဲ့ဒီ developer က project တစ်ခုလုံးနားမလည်သေးပဲ သူလုပ်မယ့် task နဲ့ဆိုင်တဲ့ action ကိုနားလည်ရင်၀င်ရေးလို့အဆင်ပြေတယ်။ ဥပမာ — Product listing နဲ့ပတ်သက်တဲ့ improvement တစ်ခုရေးမယ်ဆိုပါစို့။ သူ task ကို business level ကနေနားလည်မယ်။ ProductListingQuery, ProductListingHandler ကိုနားလည်မယ်ဆိုရင် သူရေးလို့ရပြီပေါ့နော်။</li><li>နောက်တစ်ချက်က ကျနော်တို့ business level ကနေလုပ်လို့ရတဲ့ action တွေကို တစ်ခုနဲ့ တစ်ခု conflict မဖြစ်ပဲ သီးသန့်ခွဲပြီးသားဖြစ်သွားတယ်။ ဥပမာ — order အသစ် create မယ့်အလုပ်အတွက် OrderCreateCommand နဲ့ OrderCreateHandler ကသီးသန့်ရှိနေတယ်။ အဲ့ဒီတော့ order create တဲ့ အလုပ်က အချိန်ကြာလာတဲ့အခါ business requirement တွေ evolve ဖြစ်ပြီး complex ဖြစ်လာပါစေဦး ကျနော်တို့ code level ကနေ လိုက်ရေးရတဲ့အခါ အဆင်ပြေတယ်။</li><li>ကျနော် project မှာ ဘာ action တွေရှိတယ်ဆိုတာကို နောက်မှ project ထဲ၀င်လာတဲ့ developer တွေလည်းမြင်သာတယ်။ အသစ်ရောက်လာတဲ့ developer က project ကို၀င်ကြည့်တဲ့အခါ Command folder ထဲက order folder ထဲ၀င်ကြည့်လိုက်မယ်။ အဲ့ဒီကျ order နဲ့ပတ်သက်ပြီးလုပ်လို့ရတဲ့အလုပ်တွေကို ခြုံပြီး မြင်နိုင်တယ်။</li></ol><p>အားနည်းချက်ကတော့စရေးရတဲ့အခါ အလုပ်ရှုပ်တယ်။ order create မယ့် case မှာပဲ ပုံမှန်ဆို controller, service ၂ခုလောက်ထဲ နည်းနည်း၀င်ရေးပြီးရင်အဆင်ပြေသွားမယ့် ကိတ်စ မှာသူက OrderCreateCommand, OrderCreateHandler file အသစ်တွေဆောက်ရမယ်။ စဖြင့်အလုပ်ပိုရှုပ်တယ်ပေါ့နော်။</p><h4>Components in CQRS</h4><p>CQRS pattern ကို applied တော့မယ်ဆိုရင် အဓိကအားဖြင့် component 4 ခုပါလေ့ရှိပါတယ်။</p><ol><li><strong>Command</strong>: action တစ်ခုကိုဖော်ပြတယ်။ အဲ့ဒီ action လုပ်ဆောင်ဖို့အတွက်လိုအပ်တဲ့ information တွေပါတယ်။ ဒါပေမယ့်တကယ်မလုပ်ဆောင်ပေးဘူး။ <br>ဥပမာ: CreateOrderCommand ဆိုတဲ့ class တစ်ခုရှိတယ်ဆိုပါစို့။ ဒီ class မှာဆိုရင် getProducts(), setProducts(), getCustomer(), setCustomer() စတဲ့ method တွေပါလေ့ရှိတယ်။</li><li><strong>CommandHandler</strong>: command ကဖော်ပြတဲ့ action ကို လုပ်ဆောင်ပေးတယ်။ <br>ဥပမာ: CreateOrderHandler ဆိုရင် ခုနက command မှာပါလာတဲ့ information တွေကိုသုံးပြီး database ထဲတကယ်သွားထည့်ပေးတဲ့အလုပ်ကိုလုပ်လိမ့်မယ်။</li><li><strong>Query</strong>: command နဲ့ philosophy တူတူပဲ။ action တစ်ခုကိုဖော်ပြတယ်။ တကယ်မလုပ်ဆောင်ဘူး။</li><li><strong>QueryHandler</strong>: query ကဖော်ပြတဲ့ action ကိုတကယ်လုပ်ဆောင်ပေးတယ်။</li></ol><p>ဒီ component 4 ခုကတော့ necessary components တွေလို့ယူဆလို့ရပါတယ်။ (Note: ကျနော့်အမြင်ကိုဒီနေရာမှာ ကြားညှပ်ပြောချင်လို့ပါ။ ကျနော်တို့ code ရေးတဲ့အခါ Design pattern တွေကို applied လုပ်နိုင်တာကောင်းတယ်ပေါ့နော်။ ဒါပေမဲ့ အတိအကျ အမြဲလိုက်နာတာကို သဘောမကျဘူး။ project လိုအပ်ချက်၊ ကိုယ့်မှာရှိတဲ့ resource(လူ/အချိန်) ပေါ်မူတည်ပြီး design pattern တွေကို ကိုယ်နဲ့လိုက်ဖက်သလို flexible ဖြစ်အောင်သုံးလိုက်တာ ကိုပဲသဘောကျပါတယ်)</p><p>အပေါ်ကပြောသွားတဲ့ necessary component တွေအပြင် ထည့်လေ့ထည့်ထရှိတဲ့ component လေးတွေရှိပါတယ်။</p><ol><li>Command Bus: Command ကို သူနဲ့သက်ဆိုင်ရာ Handler ဆီ map ပေးဖို့အဓိကသုံးပါတယ်</li><li>Query Bus: Query ကိုသူနဲ့သက်ဆိုင်ရာ Handler ဆီ map ပေးဖို့အဓိကသုံးပါတယ်။</li></ol><h3>How Can you simply implement CQRS in your project?</h3><p>CQRS ကို implement လို့ရတဲ့နည်းတွေကတော့ အမျိုးမျိုးရှိတယ်ပေါ့နော်။ ကိုယ့်ဘာသာ Command class, CommandHandler class, Query class, QueryHandler class တွေဆောက်ပြီး handle လိုက်လည်းရပါတယ်။ ကျနော် ဒီ post မှာတော့ Attributes Driven CQRS PHP package ကိုသုံးပြီး implement တာကိုအဓိကပြောသွားမှာဖြစ်ပါတယ်။</p><p>Install လုပ်တာတွေ, usage တွေကို <a href="https://github.com/zawhtetnaing006/attributes-driven-cqrs">ဒီမှာ</a> ကြည့်လို့ရပါတယ်။</p><p>ကျနော်တစ်ခုချင်းဆီရှင်းနေရင်ကြာတယ်။ code ကိုပဲတန်းကြည့်လိုက်ကြရအောင်။</p><pre>use Zaw\AttributeDrivenCqrs\Attributes\HandleCommandWith;<br>use Zaw\AttributeDrivenCqrs\CommandBus;<br><br>#[HandleCommandWith(OrderCreateHandler::class)]<br>class OrderCreateCommand<br>{<br>    public function __construct(<br>        private string $customerName<br>    ) {<br>    }<br><br>    public function getCustomerName(): string<br>    {<br>        return $this-&gt;customerName;<br>    }<br>}<br><br>class OrderCreateHandler<br>{<br>    public function handle(OrderCreateCommand $command): void<br>    {<br>        echo &quot;Order for customer &#39;{$command-&gt;getCustomerName()}&#39; created&quot;;<br>    }<br>}<br><br>$command = new OrderCreateCommand(&#39;John Doe&#39;);<br>CommandBus::getInstance()-&gt;handle($command);</pre><p>OrderCreateCommand ဆိုတဲ့ class ကကျနော်တို့ order create ဖို့လိုတဲ့ information ကိုသိမ်းပေးမယ်။ ဒီ command ကိုဘယ် handler နဲ့သက်ဆိုင်တယ်ဆိုတာကို <strong>#[HandleCommandWith(Handler::class)] </strong>ဆိုတဲ့ attribute လေးနဲ့ map ပေးလိုက်တယ်ပေါ့နော်။ တကယ် order create ဖို့လိုလာတဲ့အခါ command ကိုတည်ဆောက် -&gt; command bus ထဲပစ်ထည့်ပေးလိုက်ရင်ရပြီ။</p><blockquote>Note: handler class တွေက handle ဆိုတဲ့ method ကို implement ပေးဖို့လိုပါတယ်။</blockquote><pre>$command = new OrderCreateCommand(&#39;John Doe&#39;);<br>CommandBus::getInstance()-&gt;handle($command);</pre><p>အဲ့ဒီမှာ ကျနော်တို့က order ကိုမ create ခင် validate လုပ်မယ်။ create ပြီးရင် customer ဆီ mail ပို့မယ်။ ဒီ၂ခုကိုလုပ်ချင်တယ်ဆိုပါစို့။ package မှာပါတဲ့ beforeHandle နဲ့ afterHandle ဆိုတဲ့ attribute ကိုသုံးပြီးလုပ်လို့ရပါတယ်။</p><blockquote>Note: ဒီ attriubute 2 ခုနဲ့ run မယ့်ကောင်တွေကို Middleware လို့ပဲခေါ်လိုက်ကြပါစို့။ middleware တွေက <strong>Zaw\AttributeDrivenCqrs\Middlewares\Interfaces\MiddlewareInterface </strong>ကို implement ပေးဖို့လိုပါတယ်။</blockquote><p>implement လိုက်ရင် process ဆိုတဲ့ method လေးကို implement ပေးဖို့လိုပါလိမ့်မယ်။ အဲ့ဒီ method မှာ parameter ၂ခုပါပါတယ်။ ပထမတစ်ခုက လက်ရှိ handle နေတဲ့ command or query ဖြစ်ပြီး၊ ဒုတိယတစ်ခုက handler ရဲ့ handle method က ပြန်လာမယ့် result ဖြစ်ပါတယ်။ တစ်ခုသတိထားရမှာက ဒုတိယ parameter က null ဖြစ်နေနိုင်ပါတယ်။ BeforeHandle အခြေနေတို့ handle method ကနေ return မပြန်ထားတာတို့မှာဆိုရင် သူက null ဖြစ်နေပါလိမ့်မယ်။</p><pre>#[BeforeHandle(ValidateOrderMiddleware::class)]<br>#[AfterHandle(SendMailToCustomerMiddleware::class)]<br>#[HandleCommandWith(OrderCreateHandler::class)]<br>class OrderCreateCommand<br>{<br>    public function __construct(<br>        private string $customerName<br>    ) {<br>    }<br><br>    public function getCustomerName(): string<br>    {<br>        return $this-&gt;customerName;<br>    }<br>}<br><br>class ValidateOrderMiddleware implements MiddlewareInterface{<br>    public function process($command, $result){<br>        echo &quot;Order for customer &#39;{$command-&gt;getCustomerName()}&#39; validated\n&quot;;<br>    }<br>}<br><br>class SendMailToCustomerMiddleware implements MiddlewareInterface{<br>    public function process($command, $result){<br>        echo &quot;Order for customer &#39;{$command-&gt;getCustomerName()}&#39; sent\n&quot;;<br>    }<br>}</pre><p>ကျနော်တို့ Command တွေကို database connection တစ်ခုနဲ့ handle ချင်တယ်။ query တွေကို database connection တစ်ခုနဲ့ handle ချင်တယ်ဆိုပါစို့။ အဲ့လို ကိတ်စတွေကို Global BeforeHandle နဲ့ AfterHandle ကိုသုံးပြီးရေးလို့ရပါတယ်။ Application bootstrap လုပ်တဲ့နေရာမှာ အောက်ကလိုရေးလိုက်လို့ရပါတယ်။</p><pre>CommandBus::getInstance()-&gt;registerBeforeHandle(SetDbConnectionToWrite::class);<br>CommandBus::getInstance()-&gt;registerAfterHandle(SetDefaultDbConnection::class);<br>QueryBus::getInstance()-&gt;registerBeforeHandle(SetDbConnectionToRead::class);<br>QueryBus::getInstance()-&gt;registerAfterHandle(SetDefaultDbConnection::class);</pre><p>Command/Query တစ်ခုချင်းဆီမှာတွဲထားတဲ့ BeforeHandle, AfterHandle တွေက သက်ဆိုင်ရာ command/query run တဲ့အချိန် အလုပ်လုပ်မှာဖြစ်ပြီး, CommandBus or QueryBus မှာ register ထားတဲ့ beforehandle, afterhandle တွေက ကျနော် run လိုက်တဲ့ command/query တိုင်းမှာ အလုပ်လုပ်မှာဖြစ်ပါတယ်။ (CommandBus မှာတွဲထားရင် command တိုင်းမှာ အလုပ်လုပ်ပြီး QueryBus မှာတွဲထားရင် Query တိုင်းမှာအလုပ်လုပ်ပါလိမ့်မယ်)</p><p>ကျနော်တို့ CommandBus/QueryBus တွေမှာ custom information တွေတွဲပေးလိုက်ချင်ရင် setContext, getContext method တွေကိုသုံးလို့ရပါတယ်။</p><pre>CommandBus::getInstance()-&gt;setContext(&#39;connection&#39;, &#39;write&#39;);<br>$connection = CommandBus::getInstance()-&gt;getContext(&#39;connection&#39;);<br><br>QueryBus::getInstance()-&gt;setContext(&#39;connection&#39;, &#39;read&#39;);<br>$connection = QueryBus::getInstance()-&gt;getContext(&#39;connection&#39;);  </pre><p>Dependency injection ကို Handler ရယ် Middleware မှာရယ် လောလောဆယ် support ပါတယ်။</p><pre>class OrderCreateHandler<br>{<br>    public function __construct(private OrderRepo $orderRepo) {}<br>    public function handle(OrderCreateCommand $command)<br>    {<br>       $this-&gt;orderRepo-&gt;createOrder($command-&gt;getCustomeName());<br>    }<br>}<br><br><br>class LoggerMiddleware implements MiddlewareInterface<br>{<br>    public function __construct(private Logger $logger)<br>    {<br>    }<br>    public function process($command, $result)<br>    {<br>        $this-&gt;logger-&gt;log(&quot;Process command &quot; . $command::class . &quot; with result &quot; . $result);<br>    }<br>}</pre><p>package ကတော့ကျနော်ပဲရေးထားတာပါ။ လိုအပ်တာရှိရင် zawhtetnaing006@gmail.com ကိုပို့ထားပေးလို့ရပါတယ်။ အဆုံးထိဖတ်ပေးတဲ့အတွက်ကျေးဇူးတင်ပါတယ်ခင်ဗျာ။</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ee1c67f22826" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript Promises In Burmese]]></title>
            <link>https://medium.com/@zawhtetnaing/javascript-promises-in-burmese-10d073db6b4c?source=rss-c4b2bc565629------2</link>
            <guid isPermaLink="false">https://medium.com/p/10d073db6b4c</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[asynchronous-programming]]></category>
            <dc:creator><![CDATA[Zaw Htet Naing]]></dc:creator>
            <pubDate>Fri, 28 Jun 2024 09:21:06 GMT</pubDate>
            <atom:updated>2024-06-28T09:21:06.132Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*muYSEs_5UeZnJ-tQ" /><figcaption>Photo by <a href="https://unsplash.com/@womanizer?utm_source=medium&amp;utm_medium=referral">Womanizer Toys</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Promises အကြောင်း စမလေ့လာခင် ကျနော်တို့ Async နဲ့ Sync အကြောင်းနည်းနည်းပြန်နွေးရအောင်။ ကျနော်တို့ waiter နဲ့ စားဖိုမှူး ဥပမာကိုသုံးပြီးလေ့လာကြည့်မယ်။</p><h4>In Sync</h4><p>- waiter က စားဖိုမှူး ကို customer ဘာမှာသွားလဲဆိုတာ inform ပေးတယ်<br>- waiter က စားဖိုမှူး ချက်ပြီးတဲ့အထိထိုင်စောင့်တယ်<br>- waiter က စားဖိုးမှူး ချက်ပြီးသွားတဲ့ order ကို customer ဆီပြန်ပို့ပေးတယ်</p><h4>In Async</h4><p>- waiter က စားဖိုမှူး ကို customer ဘာမှာသွားလဲဆိုတာ inform ပေးတယ်<br>- waiter က မစောင့်တော့ပဲ နောက် order information ကိုသွားယူတယ်<br>- နောက် order information ကိုနောက်ထပ်စားဖိုမှူးတစ်ယောက်ဆီပေးတယ်။ စားဖိုမှူးတွေက တပြိုင်နက်ထဲ ချက်ပြုတ်ကြတယ်<br>- စားဖိုမှူး ချက် ပြီးသွားတဲ့အခါကျရင် တစ်နည်းနည်းနဲ့ order ကို customer ဆီပြန်ပို့ပေးတယ်</p><h4>Note:</h4><p>ကျနော်တို့ ဒီမှာ သတိထားရမယ့်အချက်တွေရှိတယ်။<br>1. Async မှာ waiter က စားဖိုမှူးကို မစောင့်ဘူး<br>2. Async မှာ waiter က သူ order information စားဖိုမှူးကို ပေးပြီးတဲ့အခါဘာဆက်ဖြစ်သွားလဲမသိဘူး<br>3. Async မှာ စားဖိုမှူး က တစ်ယောက်ထဲမဟုတ်တော့ဘူး</p><h4>Promise ဆီပြန်သွားရအောင်</h4><p>ကျနော်တို့ ဘာမှ မလေ့လာသေးခင်အောက်က function လေး ၃ခုကိုအရင်ကြည့်ကြည့်မယ်။ ဒီ function 3 ခုက အပေါ်က ဥပမာကို Async နည်းနဲ့ implement ထားတာဖြစ်ပါတယ်။</p><pre>function takeOrders(orders) {<br>    for (let order of orders) {<br>        console.log(`Waiter sends ${order} to cook.`);<br>        cookOrder(order).then(deliverOrder);<br>    }<br>}<br><br>function cookOrder(order) {<br>    return new Promise((resolve, reject) =&gt; {<br>        console.log(`Cooking ${order}`);<br>        setTimeout(() =&gt; {<br>            console.log(`Done cooking ${order}`);<br>            resolve(order);<br>        }, 1000);<br>    });<br>}<br><br>function deliverOrder(order) {<br>    console.log(`Deliver ${order} to customer`);<br>}<br><br>takeOrders([&quot;မုန့်ဟင်းခါး&quot;, &quot;ခေါက်ဆွဲသုပ်&quot;, &quot;ပေါက်ဆီ&quot;]);</pre><p>ပထမ takeOrders function မှာ waiter က order တွေတစ်ခုပြီးတစ်ခုယူပြီး စားဖိုမှူးကိုသွားပေးပါတယ်။ စားဖိုမှူးက ချက်ပြုတ်ပြီးသွားတဲ့အခါမှ deliverOrder ဆိုတဲ့ function နဲ့ customer ဆီ order ကို deliver ပေးပါတယ်။ order 3 ခုရှိတဲ့အတွက်စားဖိုမှူး ၃ယောက်က တစ်ပြိုင်ထဲချက်ပြုတ်ပါလိမ့်မယ်။ တစ်နည်းအားဖြင့် process 3 ခုက တပြိုင်ထဲ run ပါလိမ့်မယ်။</p><p>အိုကေ Promise အကြောင်းလေ့လာဖို့ လက်နက်တပ်ဆင်လို့ပြီးပါပြီ။ တစ်ကယ်စလေ့လာကြရအောင်</p><p>Promise ဆိုတာက Async operation တွေ complete ဖြစ်သွားလား fail သွားလားဆိုတာကို represent လုပ်ပေးနိုင်တဲ့ Object တစ်ခုဖြစ်ပါတယ်။ promise အသစ်တစ်ခု create မယ်ဆိုကျနော်တို့ <strong>new Promise((resolve, reject) =&gt; {})</strong> ဆိုတဲ့ syntax နဲ့ create လို့ရပါတယ်။</p><p>ပထမ parameter က complete ဖြစ်တာကို ရည်ညွှန်းပါတယ်။<br>ဒုတိယ parameter က fail ဖြစ်တာကို ရည်ညွှန်းပါတယ်။<br>complete ဖြစ်ပြီးဆက်လုပ်မယ့်ကိတ်စတွေကို .then နဲ့ရေးရပါတယ်။<br>fail ပြီးဆက်လုပ်မယ့်ကိတ်စတွေကို .catch နဲ့ရေးရပါတယ်။</p><pre>function cookOrder(order) {<br>    return new Promise((ချက်လို့ပြီးစီး, ပျက်စီး) =&gt; {<br>        console.log(`Cooking ${order}`);<br>        setTimeout(() =&gt; {<br>            console.log(`Done cooking ${order}`);<br>            ချက်လို့ပြီးစီး(order);<br>        }, 1000);<br>    });<br>}<br><br>cookOrder(order).then(deliverOrder).catch(reportToCustomer)</pre><p>Promise ရဲ့ parameter နံမည်တွေက အသေသတ်မှတ်ထားတာမရှိပါဘူး။ ကိုယ်ကြိုက်တာပေးလို့ရပါတယ်။<br>ဒါပေမဲ့ အားလုံးဖတ်ရလွယ်အောင်နဲ့ တပြေးညီဖြစ်အောင် ပထမ parameter ကို resolve, ဒုတိယ parameter ကို reject လို့ standard ထားထားကြပါတယ်။</p><pre>new Promise((resolve,reject) =&gt; {})</pre><h4>Advanced Understanding In Promises</h4><p>ဒီအပိုင်းကို မလေ့လာခင်အပေါ်က အကြောင်းတွေ အရင်နားလည်ထားဖို့လိုပါတယ်ခင်ဗျ.</p><p>ကျနော်တို့ promise ပြီးရင်ဆက်လုပ်မယ့် ကိတ်စတွေကို .then or .catch နဲ့ရေးလို့ရတာသိပြီးကြပြီဖြစ်ပါတယ်။ တကယ်တော့ .then မှာကျနော်တို့ parameter ၂ ခုထည့်ပေးလို့ရပါတယ်။ ပထမ တစ်ခုက promise complete ဖြစ်သွားရင် ဆက်လုပ်မယ့် callback function ဖြစ်ပါတယ်။ ဒုတိယ တစ်ခုက promise fail သွားရင်ဆက်လုပ်မယ့် callback function ဖြစ်ပါတယ်။ ဒါဆိုရင် catch က ပထမ parameter မပါတဲ့ then နဲ့တူတူပါပဲ။</p><pre>cookOrder(order).then(deliverOrder).catch(reportToCustomer)<br>cookOrder(order).then(deliverOrder,reportToCustomer)<br>cookOrder(order).then(deliverOrder).then(,reportToCustomer)</pre><p>ဒီ ၃ခုက အဓိပါယ်တူတူပါပဲ</p><p>နောက်ထပ်မှတ်သားစရာတစ်ခုက .then ကနေပြန်ပေးလိုက်တာကလည်း promise ပဲဖြစ်ပါတယ်။ promise ပဲဖြစ်တဲ့အတွက်ကျနော်တို့ then နဲ့ထပ်ရေးလို့ရသွားပြန်ပါတယ်</p><pre>cookOrder(order).then(deliverOrder).then(simileToCustomer).catch(reportToCustomer)</pre><p>ဒီလိုအတွဲလိုက်ရေးလို့ရသွားပါတယ်</p><h4>Summary</h4><p>Javascript Promise က ရှုပ်တဲ့အတွက် ကိုယ်တိုင်ရေးကြည့်ဖို့လိုအပ်ပါတယ်။ အထူးသဖြင့် ကျနော့်လို PHP ကနေစခဲ့တဲ့ developer တွေအတွက် အထူးအဆန်းဖြစ်စေပါတယ်။ Node.js ဘက်လေ့လာတော့မယ်ဆိုရင်တော့ Promise ကိုနောကျေနေတာ အကောင်းဆုံးဖြစ်ပါလိမ့်မယ်။</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=10d073db6b4c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Glimpse Into JavaScript Magic (Event Loop)]]></title>
            <link>https://medium.com/@zawhtetnaing/a-glimpse-into-javascript-magic-event-loop-37babb34b12e?source=rss-c4b2bc565629------2</link>
            <guid isPermaLink="false">https://medium.com/p/37babb34b12e</guid>
            <category><![CDATA[event-loop-in-javascript]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Zaw Htet Naing]]></dc:creator>
            <pubDate>Tue, 11 Jun 2024 10:12:42 GMT</pubDate>
            <atom:updated>2024-06-11T10:12:42.189Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zxsOWREqUvfUF0Or" /><figcaption>Photo by <a href="https://unsplash.com/@grakozy?utm_source=medium&amp;utm_medium=referral">Greg Rakozy</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>The coolest feature in JavaScript</p><p>JavaScript က non-blocking language လို့ကျနော်တို့အများစုကြားဖူးကြပါတယ်။ ဒါဆို non-blocking ဆိုတာက ဘာလဲ? non-blocking ကိုခနထားဦးမယ်။ blocking ဆိုတာကဘာလဲ?</p><h3>Blocking</h3><p>Run ရတာကြာ တာကို blocking လို့ခေါ်ပါတယ်။<br><strong>eg 1</strong>: ကိုယ့် feature အလုပ်လုပ်ဖို့ 3rd party က api call ခေါ်ရတယ်။ api call ကြီးကကြာနေတယ်။ ဒါဆို api call မှာ blocking ဖြစ်တယ်။<br><strong>eg 2</strong>: ကိုယ့် feature အလုပ်လုပ်ဖို့ file အကြီးကြီးတစ်ခု read ရမယ်။ file read တာမှာကြာနေတယ်။ ဒါဆို file read မှာ blocking ဖြစ်တယ်။</p><h3>Non-blocking</h3><p>ကြာရင် မစောင့်ပဲဆက်အလုပ်လုပ် သွားတာကို Non-blocking လို့ခေါ်ပါတယ်။</p><p><strong>eg 1</strong>: api call ကြီးကကြာနေတယ်။ ဒါဆို api response ပေါ် <strong>မမှီခို </strong>တဲ့ ကိတ်စတွေဆက်လုပ်သွားတယ်။ api ကိုတနည်းနည်းနဲ့ တစ်နေရာရာမှာ သွား call နေတယ်။ api response ပေါ် <strong>မှီခို</strong> နေတာတွေကို api call က response ရရင်လုပ်လိုက်မယ်။</p><p><strong>eg 2</strong>: file read တာကြာနေတယ်။ ဒါဆို ထားခဲ့။ file ထဲက content ပေါ် <strong>မမှီခို</strong> တာတွေဆက်လုပ်သွားတယ်။</p><h3>Non-blocking Language vs Blocking Language</h3><p>Non-blocking ဖြစ်အောင်ရေးလို့ရရင် Non-blocking language. မရရင် blocking language ပါပဲ။ PHP ဆိုရေးမရပါဘူး ဒါကြောင့် blocking language ဖြစ်ပါတယ်။ workaround ways တွေတော့ရှိပါတယ်။ JavaScript (Node.js ရော, Browser မှာ run တဲ့ js ရော) ဆို non-blocking ဖြစ်အောင်ရေးလို့ရပါတယ်။</p><h3>Single Thread vs Multi Thread</h3><p>တစ်ကြိမ်မှာ အလုပ်တစ်ခုပဲလုပ်နိုင်ရင် single thread။<br>တစ်ကြိမ်မှာ အလုပ်အများကြီးလုပ်နိုင်ရင် multi thread ဖြစ်ပါတယ်။ JS ဆိုရင် single thread language ဖြစ်ပါတယ်။ တစ်ကြိမ်မှာတစ်ခုပဲအလုပ်လုပ်လို့ရပါတယ်။ ဒါဆို နေဦး Non-blocking က example ကြီးကလွဲနေပြီ။ ပြန်ကြည့်ကြည့်မယ်။</p><p><strong>eg 1</strong>: api call ကြီးကကြာနေတယ်။ ဒါဆို api response ပေါ် မမှီခို တဲ့ ကိတ်စတွေဆက်လုပ်သွားတယ်။ api ကိုတနည်းနည်းနဲ့ တစ်နေရာရာမှာ သွား call နေတယ်။ api response ပေါ် မှီခို နေတာတွေကို api call က response ရရင်လုပ်လိုက်မယ်။</p><p>ပိုမြင်သာအောင် code နည်းနည်းထည့်လိုက်မယ်</p><pre>console.log(&#39;Start&#39;);<br>fetch(&#39;https://jsonplaceholder.typicode.com/posts/1&#39;)<br>.then(response =&gt; response.json())<br>.then(data =&gt; {<br>  console.log(&#39;Data received:&#39;, data); // api response ပေါ်မှီခိုတဲ့ code <br>})<br>.catch(error =&gt; {<br>  console.error(&#39;Error:&#39;, error);<br>});<br>console.log(&#39;End&#39;); // api response ပေါ်မမှီခိုတဲ့ code</pre><p>ဒီ example မှာဆို api call ကို မမှီခိုတဲ့ code ကို non-blocking feature ကြောင့် api call ခေါ်နေတုန်း သွားပြီးအလုပ်လုပ်လိမ့်မယ်။ ဒါဆို အဲ့ဒီအချိန် api call ကိုဘယ်သူခေါ်ပေးနေတာလဲ။</p><blockquote>JS က single thread language ဖြစ်ပါတယ်။ <strong>console.log(‘End’)</strong> ကို run နေတုန်း api call ကိုမခေါ်နိုင်ပါဘူး။ ဒါဆို ဘယ်သူက api ကို call ပေးနေတာလဲ? အဲ့တာက js ရဲ့ magic ဖြစ်ပါတယ်။</blockquote><h3>Event Loop</h3><p>Event loop က JavaScript ကို single thread ဖြစ်နေရဲ့သားနဲ့ non-blocking ကို run နိုင်အောင်လုပ်ထားပေးတဲ့ နည်းပညာဖြစ်ပါတယ်။<br>Event loop ကိုနားလည်ဖို့ အဓိက အပိုင်း ၃ပိုင်းကို နားလည်ရင် အိုကေပါပြီ</p><ol><li>The Call Stack</li><li>Web APIs or Node.js core APIs</li><li>The Task Queue</li></ol><h4>The Call Stack</h4><p>ကျနော်တို့ Call Stack ကို to-do list တစ်ခုလိုစဥ်းစားကြည့်ရအောင်။ ကျနော် function တစ်ခု ခေါ်လိုက်ရင် to-do list ထဲသွားထည့်လိုက်တယ်။ run ပြီးသွားရင် to-do list ထဲကထုတ်ပလိုက်မယ်။</p><pre>function tenth() { }<br><br>function ninth() { tenth() }<br><br>function eigth() { ninth() }<br><br>function seventh() { eigth() }<br><br>function sixth() { seventh() }<br><br>function fifth() { sixth() }<br><br>function fourth() { fifth() }<br><br>function third() { fourth() }<br><br>function second() { third() }<br><br>function first() { second() }<br><br>first();</pre><p>ဒီမှာဆိုရင် ကျနော်တို့ ပထမ စစချင်း ခေါ်လိုက်တာ first() ဆိုတဲ့ function ဖြစ်ပါတယ်။ ဒါဆို သူက Call Stack ထဲကိုအရင်ဆုံး၀င်သွားလိမ့်မယ်။<br>first() ကို run လိုက်တဲ့အခါ second() ဆိုတဲ့ function ကိုထပ်ခေါ်ပါတယ်။ ဒါဆို Call Stack ထဲ second() ဆိုတဲ့ function ၀င်သွားပါပြီ။</p><p><em>**ဒီနေရာမှာသတိထားရမှာက first ဆိုတဲ့ function က run လို့မပြီးသေးပါဘူး**</em></p><p>second ထဲမှာ thrid() ကိုခေါ်ထားပါတယ်။<br>ဒါဆို Call Stack ထဲကို thrid() ထပ်၀င်သွားပါလိမ့်မယ်။<br>အဲ့လိုနဲ့ဆက်တိုက်၀င်သွားပြီး Call Stack က အောက်ကပုံလိုဖြစ်သွားပါလိမ့်မယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/233/1*Ysvo9FSUEvdnlSwVr2Z-bg.png" /></figure><p>ပြီးတော့ အဲ့ဒီမှာ ပထမ စစချင်း run လို့ပြီးသွားမှာက နောက်ဆုံးမှ ၀င်သွားတဲ့ tenth() function ဖြစ်ပါတယ်။ အဲ့တော့ နောက်ဆုံးမှ ၀င်လာတဲ့ function က Call Stack ကနေအရင်ဆုံးထွက်သွားလိမ့်မယ်။ last in first out ဖြစ်ပါတယ်။</p><h4>Web APIs or Node.js core APIs</h4><p>ခုနက ကျနော်တို့ဆွေးနွေးနေတဲ့ example မှာ api call ကိုဘယ်သူခေါ်ပေးမှာလဲဆိုတော့ Web API or Node.js API ကခေါ်ပေးမှာဖြစ်ပါတယ်။ Web API ဆိုတာကျနော်တို့ browser ကိုအလုပ်လှမ်းခိုင်းလို့ရတဲ့နည်းပညာတစ်ခုဖြစ်ပါတယ်။ JavaScript က အဲ့ဒီနည်းပညာကိုသုံးပြီးတော့ blocking ဖြစ်မယ့်အလုပ်တွေဆို browser ကိုလှမ်းခိုင်းလိုက်ပါတယ်။ ဒါပေမယ့် Node.js environment မှာ run နေတဲ့ JavaScript အတွက်ကကျတော့လှမ်းခိုင်းစရာ browser မရှိပါဘူး။ အဲ့ဒီအတွက် Node.js မှာ C++ နဲ့ရေးထားတဲ့ api လေးတွေရှိပါတယ်။ အဲ့တာကိုလှမ်းခေါ်ပြီး JavaScript က Blocking ဖြစ်မယ့်ကိတ်စတွေ C++ ကိုသွား run ခိုင်းပါတယ်။</p><h4>The Task Queue (Callback Queue)</h4><p>ခုနက ကျနော်တို့ဆွေးနွေးနေတဲ့ example မှာ api call ခေါ်ပြီး ရင် api response ရမှဆက်လုပ်ရမယ့်အလုပ်တွေရှိပါတယ်။ အဲ့အလုပ်တွေကိုကျ Web APIs or Node.js APIs ကလုပ်ပေးမှာလား? JavaScript ကလုပ်ပေးမှာလား? JavaScript ကလုပ်ပေးမှာပါ။ Web Api ကို data fetch ခိုင်းလိုက်တယ်။ data fetch လို့ပြီးသွားရင် .then ထဲမှာထည့်ထားတဲ့ callback function ကို Task Queue ထဲပို့ပေးလိုက်တယ်။ Web API or Node.js APIs က လုပ်နေတဲ့ task ပြီးသွားရင် ဆက်လုပ်ရမယ့် tasks တွေ(eg. promise.then(functionToDo), setTimeout(functionToDo, 5000)) တွေကို The Task Queue ထဲ push လိုက်ပါတယ်။</p><p>Call Stack ထဲမှာ run စရာကုန်သွားရင် Task Queue ထဲကဟာကို Call Stack ထဲထည့်လိုက်တယ်။ အဲ့ဒီအတိုင်း run စရာဘာမှမကျန်တော့တဲ့အထိ JavaScript က loop ပတ်ပြီး run နေတယ်။</p><h3>How Event Loop works?</h3><ol><li>Execute code: JavaScript file ရဲ့ထိပ်ဆုံးကနေ code ကို စ execute လုပ်တယ်။ functions တွေကို Call Stack ထဲထည့်တယ်။</li><li>Call stack: function က synchronous ဆိုရင်ချက်ချင်း execute လုပ်တယ်။ ပြီးတော့ Call Stack ထဲကထုတ်လိုက်တယ်။</li><li>Asynchronous Tasks: call လိုက်တဲ့ function က Asynchronous ဖြစ်နေတယ်ဆိုရင် Web Api ကိုလွှဲပေးလိုက်တယ်. main thread ကကျန်တဲ့ JavaScript code တွေကို ထပ် execute လုပ်နေတယ်။</li><li>Task Queue: Asynchronous operation တစ်ခုပြီးသွားပြီဆိုရင် ဆက်လုပ်ရမယ့်အလုပ်တွေ (callbacks) ကို Call Stack ထဲမထည့်သေးပဲ Task Queue ထဲထည့်လိုက်တယ်။</li><li>Event Loop: event loop က Call Stack နဲ့ Task Queue ကိုအမြဲ check နေတယ်။ Call Stack ထဲမှာရှိတာအကုန် run တယ်။ run စရာမရှိတော့ရင် Task Queue ထဲကဟာကို Call Stack ထဲထည့်ပြီး ထပ် run တယ်။</li></ol><h3>Summary</h3><p>JavaScript ကတစ်ကြိမ်မှာ task တစ်ခုပဲလုပ်နိုင်တယ်ဆိုပေမယ့် Web APIs or Node.js core APIs တွေရဲ့အကူအညီနဲ့ non-blocking ဖြစ်အောင်လုပ်ပေးတယ်။ ဒီ <a href="https://www.jsv9000.app/">https://www.jsv9000.app/</a> မှာ event loop အလုပ်လုပ်သွားပုံကို Visualized ကြည့်နိုင်ပါတယ်.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=37babb34b12e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Node.js Error Handling in Burmese]]></title>
            <link>https://medium.com/@zawhtetnaing/node-js-error-handling-in-burmese-74a328e665c1?source=rss-c4b2bc565629------2</link>
            <guid isPermaLink="false">https://medium.com/p/74a328e665c1</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[error-handling]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Zaw Htet Naing]]></dc:creator>
            <pubDate>Fri, 07 Jun 2024 10:50:37 GMT</pubDate>
            <atom:updated>2024-06-07T10:50:37.510Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*dn_Wtz7dqO2ajhtR" /><figcaption>Photo by <a href="https://unsplash.com/@cookiethepom?utm_source=medium&amp;utm_medium=referral">Cookie the Pom</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Error ဆိုတာဘာလဲ? Bug ဆိုတာဘာလဲ?</p><h3>Errors အမျိုးအစားများ</h3><h4><strong>Operational Errors</strong></h4><p>ဒါက ကျနော်တို့ရေးထားတဲ့ code ကြောင့်ဖြစ်တာမဟုတ်ဘူး။ ကျနော်တို့ code ကို run နေတဲ့ environment ကြောင့်ဖြစ်တဲ့ error တွေလို့ယူဆလို့ရပါတယ်။</p><p><strong>ဥပမာ</strong>:</p><p>1. network connections တွေ failed သွားတာ</p><p>2. out-of-memory ဖြစ်သွားတာ</p><p>3. database server ကိုချိတ်မရတာ</p><p>Operational error အမျိုးအစားက ကျနော်တို့ error ဘယ်ချိန်မှာတက်မလဲမသိနိုင်ပါဘူး။ ပြီးတော့ ရှောင်လွဲလို့မရနိုင်ပါဘူး။</p><p>ဒါကြောင့် ဒီလို error တက်ခဲ့ရင် ဘာဆက်လုပ်မယ်ဆိုတာကို code ရေးထဲကနေကြိုပြီးထည့်စဥ်းစားသင့်ပါတယ်။</p><p>ဥပမာ: user ကို network failed နေလို့ service မပေးနိုင်သေးပါဘူးဆိုတဲ့ notification ပို့မယ်။ retry ပြန်လုပ်မယ်။</p><h4>Programmer Errors</h4><p>ဒါက ကျနော်တို့ရေးထားတဲ့ code ကြောင့်ဖြစ်တဲ့ error တွေပါ။ ဒီ error အမျိုးအစားကို “<strong>Bug</strong>” လို့လည်းခေါ်ပါတယ်။</p><p>ဥပမာ:</p><p>1. define မလုပ်ထားတဲ့ variable ကိုယူသုံးထားတာ</p><p>2. စဥ်းစားထားတဲ့ logic မှားနေတာ</p><p>3. Syntax တွေမှားနေတာ</p><p>4. တကယ်မရှိတဲ့ array အခန်းတွေကိုယူသုံးထားတာ</p><p>Programmer error အမျိုးအစားက ကျနော်တို့ ကြိုသိနိုင်ပါတယ်။ ဒီ error အမျိုးအစားကို development လုပ်တဲ့ အချိန်နဲ့ testing လုပ်တဲ့အချိန်မှာ အကုန်ရှာပြီး fix ထားသင့်ပါတယ်။ ဒီ error တွေကို ရှာဖို့အတွက် tests တွေရေးတာမျိုး, code review လုပ်တာမျိုးတွေလိုအပ်ပါတယ်။</p><h3>Error Object and “throw” keyword in Node.js</h3><h4>Error Object</h4><p>Node.js မှာ built-in ပါတဲ့ object တစ်ခုဖြစ်ပါတယ်။ အဲ့ဒီ object မှာ stack trace ဆိုတာပါပါတယ်။</p><p>stack trace ဆိုတာကျနော်တို့ error တက်သွားတဲ့နေရာကို ဘယ် function က စပြီ: ဘယ် function တွေ အဆင့်ဆင့်ခေါ်လိုက်လို့ ဒီရောက်လာပါတယ်ဆိုတဲ့ information လို့အလွယ်ခနမှတ်ထားပေးပါ။</p><p>stack trace ကိုကြည့်ပြီး ကျနော်တို့ error ကို ဘယ်လို fix ရမလဲဆိုတာသိနိုင်ပါတယ်။</p><blockquote>ပြန်အကျဥ်းချုံးလိုက်ရင် **Error Object ဆိုတာ error ရဲ့ details information ပါတဲ့ Object ပါ**</blockquote><h4>“throw” keyword</h4><p>ကျနော်တို့ throw ကိုသုံးလိုက်ရင် ပုံမှန် program flow အတိုင်းအလုပ်မလုပ်တော့ပဲ။ အနီးဆုံး catch block ကိုသွားပြီး run ပါတယ်။ catch block မရှိရင် uncaught exception ဖြစ်သွားပါတယ်။</p><p>ဥပမာ:</p><pre>try{<br>  throw &#39;error&#39;<br>  console.log(&#39;After throw keyword&#39;)<br>} catch(error) {<br>  console.log(error);<br>}</pre><p>ဒီ ဥပမာမှာဆိုရင် <strong>console.log(‘After throw keyword’)</strong> က ဘယ်တော့မှ အလုပ်လုပ်မှာမဟုတ်ပါဘူး။</p><p>throw ‘error’ ရောက်သွားတဲ့အခါ အနီးဆုံး catch block ကိုလိုက်ရှာပြီး console.log(error) ဆိုတာကို အလုပ်လုပ်သွားမှာဖြစ်ပါတယ်။</p><h4>ဘာတွေ throw လို့ရလဲ။</h4><p>ကျနော်တို့ ရှိသမျှ datatype အကုန် throw လို့ရပါတယ်။ integer, string, boolean, object အစရှိသဖြင့်အကုန် throw လို့ရပါတယ်။</p><p>Best practice ကတော့ throw keyword ကို error object နဲ့တွဲသုံးတာဖြစ်ပါတယ်။ ဒါဆိုကျနော်တို့ catch ထဲကိုရောက်လာမှာက error object ဆိုတာကြိုမျှော်လင့်ထားလို့ရပါတယ်။</p><h3>Error Handling in Node.js</h3><p>Node.js မှာဆို ကျနော်တို့ error handling အတွက် 2 မျိုးစဥ်စားရပါတယ်။</p><p>1. synchronous code တွေကို ဘယ်လို error handle မလဲ</p><p>2. asynchronous code တွေကို ဘယ်လို error handle မလဲ</p><h4>Synchronous Error Handling</h4><p>synchronous အတွက်က try..catch နဲ့လုံလောက်ပါတယ်။ ကျနော်တို့ error တက်နိုင်တဲ့ code တွေကို try ထဲမှာထည့်မယ်။ catch ထဲကနေ error ကိုဘယ်လို handle မလဲ implement မယ်။</p><pre>try {<br>     throw new Error(&#39;Something went wrong&#39;);<br>} catch (error) {<br>     console.error(&#39;Caught an error:&#39;, error.message);<br>}</pre><h4>Asynchronous Error Handling</h4><p>Asynchronous အတွက်က ရွေးစရာများပါတယ်။</p><p>1. callback</p><pre>const fs = require(&#39;fs&#39;);<br>fs.readFile(&#39;nonexistentfile.txt&#39;, &#39;utf8&#39;, (err, data) =&gt; {<br>  if (err) {<br>      return console.error(&#39;Error reading file:&#39;, err.message);<br>  }<br>    console.log(data);<br>});</pre><p>2. promises</p><pre>const fs = require(&#39;fs&#39;).promises;<br>fs.readFile(&#39;nonexistentfile.txt&#39;, &#39;utf8&#39;)<br>.then(data =&gt; {<br>   console.log(data);<br>})<br>.catch(err =&gt; {<br>   console.error(&#39;Error reading file:&#39;, err.message);<br>});</pre><p>3. async/await<br>async/await ကကျတော့ synchronous လိုပြန်ဖြစ်သွားလို့ try..catch ကိုပဲသုံးလိုက်လို့ရပါတယ်</p><pre>const fs = require(&#39;fs&#39;).promises;<br>async function readFile() {<br>  try {<br>    const data = await fs.readFile(&#39;nonexistentfile.txt&#39;, &#39;utf8&#39;);<br>    console.log(data);<br>  } catch (err) {<br>    console.error(&#39;Error reading file:&#39;, err.message);<br>  }<br>}<br>readFile();</pre><blockquote>Note: အရမ်း complex ဖြစ်တဲ့ system တွေမှာဆို Event emitters တွေကိုလည်း error reporting လုပ်ဖို့အတွက်သုံးကြလေ့ရှိပါတယ်။ အရမ်းရှည်တဲ့ async operation တွေမှာဆို error တွေက တစ်ခုထက်ပိုပြီး အများကြီးဖြစ်နိုင်ပါတယ်။ error တွေ့တိုင်း Event တွေလွတ်ပြီး listeners တွေနဲ့ပြန် listen လို့ရပါတယ်။</blockquote><h3>Error handle တယ်ဆိုတာဘာလုပ်ရမှာလဲ</h3><h4>Retry</h4><p>api တစ်ခုခုကို သွား fetch လိုက်တယ်။ 503 ပြန်လာတယ်။ အဲ့တော့ catch block ထဲရောက်လာပြီဆိုပါစို့။ 503 ဆိုတာက overload ဖြစ်နေတာ network fail နေတာ တခုခုဖြစ်နိုင်ပါတယ်။ ဒါဆို ၃ ခါ ၄ခါလောက် RETRY ကြည့်သင့်ပါတယ်။ ပြန်ရရင်ရနေမှာပါ။</p><h4>Report</h4><p>user က format မကျတဲ့ email ကြီးထည့်ပေးလိုက်တယ်။ အဲ့တာကိုကျနော်တို့ က error throw ထားတယ်။ အဲ့လို error ဆိုရင်တော့ user ဆီပြန်ပြီး ပြောပြလိုက်သင့်ပါတယ်။</p><h4>Crash</h4><p>ကျနော်တို့ website run ဖို့မဖြစ်မနေလိုနေတဲ့ file တစ် file ရှိတယ်။ အဲ့ဒီ file ကို webserver က access မရှိဘူးဖြစ်နေတယ်။ အဲ့တာဆို ကျနော်တို့ ဘာလုပ်လုပ်မထူးပါဘူး။ အဲ့ဒီ webpage အတွက် maintenance လုပ်နေပါတယ်လို့ user ကိုပြောပြီး ဘာအလုပ်မှဆက်မလုပ်ပေးတော့တာက အကောင်းဆုံးပါ။ ပြီးမှ server admin ကို access ပေးပေးဖို့ပြောပေါ့။</p><h3>Summary</h3><p>ကျနော်တို့ reliable ဖြစ်တဲ့ system တစ်ခုရေးဖို့အတွက် error handling ကမဖြစ်မနေလိုပါတယ်။ ကျနော်တို့ ဒီ post မှာဆို error အမျိုးအစားတွေကို ဆွေးနွေးပြီးပါပြီ။ အဲ့လို error တွေဘယ်လို handle မလဲဆိုတာလည်း ဆွေးနွေးခဲ့ပြီးပါပြီ။ အားလုံးပဲ အဆင်ပြေကြပါစေခင်ဗျာ</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=74a328e665c1" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JWT (JSON Web Token)အရိုးရှင်းဆုံး ရှင်းလင်းချက်]]></title>
            <link>https://medium.com/@zawhtetnaing/jwt-json-web-token-%E1%80%A1%E1%80%9B%E1%80%AD%E1%80%AF%E1%80%B8%E1%80%9B%E1%80%BE%E1%80%84%E1%80%BA%E1%80%B8%E1%80%86%E1%80%AF%E1%80%B6%E1%80%B8-%E1%80%9B%E1%80%BE%E1%80%84%E1%80%BA%E1%80%B8%E1%80%9C%E1%80%84%E1%80%BA%E1%80%B8%E1%80%81%E1%80%BB%E1%80%80%E1%80%BA-206050aea6a9?source=rss-c4b2bc565629------2</link>
            <guid isPermaLink="false">https://medium.com/p/206050aea6a9</guid>
            <category><![CDATA[jwt]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Zaw Htet Naing]]></dc:creator>
            <pubDate>Mon, 06 May 2024 10:35:41 GMT</pubDate>
            <atom:updated>2024-05-06T10:51:55.699Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*CbW4llt0WeuSbTtY" /><figcaption>Photo by <a href="https://unsplash.com/@emilep?utm_source=medium&amp;utm_medium=referral">Emile Perron</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>jwt က stateless protcol တွေရဲ့ authentication အတွက် အသုံး၀င်တဲ့နည်းပညာတစ်ခုဖြစ်ပါတယ်။</p><p>ကျနော် jwt အကြောင်းသိချင်လို့ရှာဖတ်တုန်းက ကိုယ်သိချင်တဲ့ point ကိုတော်တော်နဲ့မရောက်ပါဘူး။ ရှင်းပြထားတဲ့ post တွေမှာ technical terms တွေအသုံးများတာရယ်၊ ကျနော့် background ကလည်း အဲ့ဒီ techinical terms တွေကို နားမလည်ခဲ့တာရယ်ကြောင့် အခက်တွေ့ခဲ့တယ်ပေါ့နော်။ တခြားသူတွေအဲ့လိုပြသနာမကြုံရအောင် jwt အကြောင်းလေး share ပေးချင်ပါတယ်။</p><h4>Stateless Protocol</h4><p>jwt က ဘာပြသနာတွေဖြေရှင်းပေးလဲနားလည်ဖို့ ကျနော်တို့ stateless အကြောင်းလေးအရင်လေ့လာကြည့်ရအောင်။ stateless ဆိုတာက ဥပမာ ကျနော်က post create တဲ့ api တစ်ခု call လိုက်တယ်(request တစ်ခုပို့လိုက်တယ်)။ ပြီးသွားရင် username change တဲ့ api တစ်ခု ထပ် call လိုက်တယ်(request နောက်တစ်ခုပို့လိုက်တယ်)။ အဲ့ဒီမှာ ဒုတိယပို့လိုက်တဲ့ username change တဲ့ api က ပထမ ပို့လိုက်တဲ့ post create api call ရဲ့ information တစ်ခုမှမသိဘူး။ အဲ့လို request တွေက independent ဖြစ်ပြီး ရှေ့က request တွေရဲ့ information တစ်ခုမှမသိတာကို stateless လို့ခေါ်ပါတယ်။</p><h4>Stateless ရဲ့ပြသနာ</h4><p>ကျနော်တို့ login ၀င်တဲ့ api call တစ်ခုရှိတယ်ဆိုပါစို့။ login api call ထဲမှာ username နဲ့ password ထည့်ပေးလိုက်ပြီ။ အဲ့တာကို ကျနော်တို့ server ဘက်ကတိုက်စစ်တယ်။ မှန်တယ်ဆိုပြီး user ရဲ့ information တွေပြန်ပေးလိုက်တယ်။ အိုကေဟုတ်ပြီ။ အဲ့တာပြီးတော့ ကျနော် post တစ်ခု create တဲ့ api call ခေါ်မယ်။ အဲ့ဒီမှာ ကျနော်တို့ က login ၀င်ပြီးသားလူကိုပဲ ပေးပြီးတော့ post create မယ်။ အဲ့ဒီမှာပြသနာတက်ရော http က stateless ဖြစ်တဲ့အတွက် post create တဲ့ api က အရှေ့မှာ user က login ၀င်ပြီးသားဆိုတာကိုမသိဘူး။ အဲ့တော့ post create တဲ့ api မှာလည်း username, password ထည့်ပေးရတော့မယ်။ post create တဲ့ api မှမဟုတ်ဘူး, user authentication လိုတဲ့ api တိုင်းမှာ username နဲ့ password ထည့်ပေးရတော့မယ်။ အဲ့တာကြီးကမဟုတ်သေးဘူး။ ဘယ်လိုဖြေရှင်းကြမလဲ?</p><h4>Session based solution</h4><p>stateless ရဲ့ပြသနာကိုဖြေရှင်းဖို့ နည်းတစ်ခုက session လို့ခေါ်ပါတယ်။ client က login ၀င်လိုက်တယ်။ success ဖြစ်သွားပြီဆိုရင် server က session ဆိုတဲ့ object လေးတစ်ခု create လိုက်တယ်။ အဲ့ဒီမှာ session id ဆိုတာလေးပါမယ်။ အဲ့ဒီ session id ကို server မှာလည်းသိမ်းလိုက်တယ်။ client ကိုလည်း အဲ့ဒီ session id လေး response ပြန်ပေးလိုက်တယ်။ client က နောက်ထပ်ခေါ်မယ့် request တွေမှာ အဲ့ဒီ session id လေးပြန်ထည့်ပေးလိုက်တယ်။ client ထည့်ပေးလိုက်တဲ့ session id က valid ဖြစ်ရင် server ဘက်က ဒီ client က authenticated ဖြစ်တယ်လို့သတ်မှတ်လိုက်တယ်။</p><h4>Session based solution ရဲ့ ပြသနာ</h4><p>သူရဲ့ပြသနာက server ဘက်မှာ session နဲ့ပတ်သက်တဲ့ information ကိုသိမ်းထားပေးစရာလိုလာတယ်။ သိမ်းတဲ့အချိန်မှာ server ရဲ့ memory, file stroage, databse အဆင်ပြေသလိုသုံးပြီးသိမ်းကြတယ်။</p><p>Memory မှာသိမ်းမယ်ဆိုပါစို့။ server 3 လုံးမှာကိုယ့် project ကို host ထားရင် user က တစ်ခါ login ၀င်ပြီးတိုင်း server 3 ခုလုံးရဲ့ memory မှာ user session ကိုသွား create ပေးရမယ်။ server 3 ခုက user session နဲ့ပတ်သက်ပြီး synchronous ဖြစ်နေရမယ်။ ဒါကလည်း အလုပ်အတော်ရှုပ်စေမယ့်ကိတ်စ</p><p>database/file နဲ့သိမ်းမယ်ဆိုပါစို့။ project A မှာ login ၀င်ထားတဲ့ user က project B မှာ authenticated ဖြစ်ဖို့ဆိုရင် project A ဘက်ကသိမ်းထားတဲ့ session နဲ့ပတ်သက်တဲ့ information တွေကို project B ဘက်ကို share ပေးရတော့မယ်။ project A နဲ့ project B က session နဲ့ပတ်သက်ပြီး synchronous ဖြစ်နေရတော့မယ်။ ဒါကလည်း အလုပ်အတော်ရှုပ်စေမယ့်ကိတ်စ။ ဒီကြားထဲ user များလာရင် session နဲ့ပတ်သက်တာတွေကို သိမ်းရတဲ့ storage ကလည်းလိုက်ပြီးကြီးလာဦးမယ်။</p><h4>JWT solution</h4><p>jwt ကကျတော့ user နဲ့ ပတ်သက်တဲ့ information တွေကို token တစ်ခုအနေနဲ့ ထုတ်ပြီး user ကိုပြန်ပေးလိုက်တယ်။ အဲ့ဒီ token က information တွေကို hide မလုပ်ထားဘူး။ ဒါပေမဲ့ ပြုပြင်ပြောင်းလဲလို့မရအောင်လုပ်ထားတယ်။</p><p>ဥပမာ — ကျနော်က ရွာ “A” က ရွာလူကြီးဆိုပါစို့။ ကျနော့်ဆီကို မောင်မောင်က ကျနော့်တို့ရွာက ဖြစ်ကြောင်းထောက်ခံစာလာတောင်းတယ်။ ကျနော်က သူကျနော်တို့ရွာကဟုတ်မဟုတ် စစ်ဆေးလိုက်တယ်။ မှန်တယ်ဆိုရင် ကျနော်က “မောင်မောင်သည် ရွာ A မှာနေတဲ့သူဖြစ်ပါသည်” လို့စာတစ်စောင်ရေးလိုက်တယ်။ ပြီးရင်စာအိတ်အကြည်ရောင်ထဲကို အဲ့ဒီစာကိုထည့်လိုက်တယ်။ ပြီးရင်စာအိတ်ကို လက် ဗွေနှိပ်ထားတဲ့ ဖယောင်း seal လေးနဲပိတ်လိုက်တယ်။ နောက်ရွာတွေကို ကျနော်က နှိပ်လိုက်တဲ့ လက် ဗွေလေးကို share ပေးထားလိုက်တယ်။ နောက်ရွာရောက်လို့ အဲ့ဒီရွာသားက စာအိတ်ကိုပြလိုက်ရင် သူတို့ကကျနော် share ပေးထားတဲ့ လက်ဗွေနဲ့ seal ထဲက လက်ဗွေကိုတိုက်စစ်လိုက်တယ်။ မှန်တယ်ဆိုရင် စာအိတ်ထဲမှာပါတဲ့ information ကမှန်တယ်လို့ယူဆလိုက်တယ်။ အဲ့ဒီရွာသား ကျနော့်ရွာပြန်ရောက်လာရင်လည်း ကျနော်မှာရှိတဲ့ လက်ဗွေနဲ့ စာအိတ်မှာရှိတဲ့ လက် ဗွေတိုက်စစ်လိုက်တယ်။ မှန်တယ်ဆိုရင် စာအိတ်ထဲက information မှန်တယ်လို့ယူဆလိုက်တယ်။</p><p>အဲ့တာကို session approach နဲ့သွားမယ်ဆိုရင် ကျနော်တို့က မောင်မောင် ကျနော့်ရွာကဆိုတာ စစ်ဆေးပြီးတာ မောင်မောင် ကို ကျနော့်စာရင်းထဲမှတ်(session create) ပြီးရင် ကျန်တဲ့ ရွာတွေကို မောင်မောင်က ငါတို့ရွာကပါဆိုပြီး ဖုန်းဆက်ပြီးလိုက်ပြောရမယ်။ အဲ့ဒီရွာက လူတွေက “မောင်မောင်သည် ရွာ A မှဖြစ်ပါသည်” ဆိုတာကို သူတို့စာရင်းထဲ မှတ်(session create) ရတော့မယ်။ မောင်မောင်ကျနော့်ရွာပြန်လာရင်လည်း မောင်မောင် ကိုစာရင်းနဲ့တိုက်စစ် ရမယ်။ မောင်မောင်တစ်ယောက်ထဲမဟုတ်ပဲ ရွာသားအများကြီးဆိုကျနော်စာရင်းကို ထိန်းသိမ်းဖို့ခက်လာပြီ။ အဓိကအားဖြင့်ရွာတွေ အကုန်လုံးရဲ့ စာရင်းကို တစ်ပြေးညီဖြစ်အောင်ထိန်းသိမ်းရခက်သွားပြီ။</p><h4>Structure of JWT</h4><p>JWT မှာဆို အပိုင်း ၃ ပိုင်းပါတယ်။</p><ol><li>header</li><li>payload</li><li>signature</li></ol><p>ပြီးရင်အဲ့ဒီ ၃ပိုင်းကို “.” လေးနဲ့ဆက်ထားတယ်။ eg: base64UrlEncode(header).base64UrlEncode(payload).signature</p><p>အဲ့ဒီမှာ header နဲ့ payload က ခုနက ဥပမာအရ စာအိတ်အကြည်ထဲမှာ မြင်နေရတဲ့ စာသားတွေလို့ယူဆလို့ရတယ်။ သူတို့က ဖုံးကွယ်မထားဘူး။ token ရှိတဲ့သူတိုင်းမြင်နိုင်တယ်။ signature က ကျတော့ လက် ဗွေ seal လေးလို့သတ်မှတ်နိုင်တယ်။ အဲ့ဒီ signature က header နဲ့ ထည့်ပေးလိုက်တဲ့ payload ပေါ်မူတည်ပြီးအမြဲပြောင်းနေမှာဖြစ်တယ်။ ဒါကြောင့်မို့ ထုတ်ပေးလိုက်တဲ့ signature က payload မတူရင်မတူနိုင်ဘူး။</p><h4>Header</h4><p>header က token type နဲ့ signature ကိုထုတ်ပေးဖို့သုံးလိုက်တဲ့ algorithm ပါတဲ့ json obj လေးဖြစ်ပါတယ်။ ဥပမာ —</p><pre>{<br>  &quot;alg&quot;: &quot;HS256&quot;,<br>  &quot;typ&quot;: &quot;JWT&quot;<br>}</pre><h4>Payload</h4><p>payload ကကျတော့ ကျနော်တို့ တကယ်မှတ်ချင်တဲ့ information တွေထည့်ပေးရမယ့်နေရာလေးဖြစ်ပါတယ်။ ဥပမာ- “name”:”Mg Mg” ။ အဲ့လို information တွေအပြင် token ကိုဘယ်အချိန်မှာထုတ်ပေးလိုက်တယ်။ token က ဘယ်အချိန် expired ဖြစ်မယ်။ အဲ့လို information တွေလည်းထည့်ပေးလေ့ရှိပါတယ်။ အဲ့ဒီ information တွေအားလုံးကို json obj လေးအနေနဲ့ ထည့်ပေးလိုက်ပါတယ်။</p><h4>Signature</h4><p>header ကို based64 encode လုပ်မယ်။ payload ကို based64 encode လုပ်မယ်။ ပြီးရင် အဲ့ဒီ encode လုပ်ထားတဲ့ ဟာ၂ခုကို secret ဆိုတဲ့ server ပဲသိတဲ့ key တစ်ခုသုံးပြီး hash လုပ်ပလိုက်မယ်။ အဲ့ဒီတော့ header or payload or secret key တစ်ခုခုပြောင်းသွားရင် hash value ကပြောင်းသွားလိုက်မယ်။ token ထဲမှာ header နဲ့ payload ပါပေမဲ့ secret key က မပါဘူး။</p><h4>JWT verification</h4><p>jwt token ကို request ကနေယူ (request ရဲ့ Header ထဲမှာ <strong>Authorization: Bearer &lt;token&gt; </strong>အဲ့လိုလေး token ကိုထည့်ပေးလေ့ရှိပါတယ်)။ header နဲ့ payload ကိုယူပြီး အပေါ်က step တွေအတိုင်း signature ကိုပြန်ထုတ်။ ရလာတဲ့ signature နဲ့ token မှာပါလာတဲ့ signature တူရင် payload ထဲက infomation ကိုမှန်တယ်လို့ယူဆပြီး request ကို ဆက်လက် process လုပ်ပေးလို့ရပါပြီ။ ကျနော်တို့ တခြား project တွေကို auth share ပေးချင်ရင်လည်း secret key လေး share ပေးလိုက်ရင်ရပါပြီ။</p><h4>Summary</h4><p>တစ်ခုသတိထားရမှာ payload ထဲမှာထည့်ပေးမယ့် information တွေကို jwt မှာ encrypt မလုပ်ထားပါဘူး။ အဲ့တာကြောင့် ကိုယ့်ဘာသာ encrypt လုပ်ရင်လုပ်, မလုပ်ရင် sensitive ဖြစ်တဲ့ information တွေမထည့်ပေးသင့်ပါဘူး။</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=206050aea6a9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Asynchronous Programming (in Burmese)]]></title>
            <link>https://medium.com/@zawhtetnaing/understanding-asynchronous-programming-in-burmese-752622442abe?source=rss-c4b2bc565629------2</link>
            <guid isPermaLink="false">https://medium.com/p/752622442abe</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[asynchronous-programming]]></category>
            <dc:creator><![CDATA[Zaw Htet Naing]]></dc:creator>
            <pubDate>Thu, 11 Apr 2024 09:08:16 GMT</pubDate>
            <atom:updated>2024-04-11T09:08:16.468Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/790/1*g0nUuSZdww-NEuJOKYw9BA.png" /></figure><p>Synchronous programming ကဘာလဲ? <br>Asynchronous programming ကရောဘာလဲ? <br>Asynchronous programming ကိုဘာလို့လိုတာလဲ? <br>ဒါတွေကို ဒီပို့စ်မှာ ဆွေးနွေးသွားပါမယ်ခင်ဗျာ။</p><h4>Synchronous Programming ကဘာလဲ?</h4><p>ဟိုး file ထိပ်ဆုံးကနေစမယ်။ file ဆုံးတဲ့အထိ တစ်ကြောင်းချင်း execute လုပ်သွားမယ်။ <strong><em>instruction တစ်ခုပြီးမှ နောက်တစ်ခုကို execute လုပ်မယ်</em> </strong>။</p><h4>Asynchronous Programming ကဘာလဲ?</h4><p>Synchronous နဲ့တူတူပဲ ဟိုး file ထိပ်ဆုံးနေ စမယ်။ file ဆုံးတဲ့အထိတစ်ကြောင်းချင်း execute လုပ်မယ်။ async နဲ့ run ဖို့ ပြောထားတဲ့ instruction ကိုတွေ့ရင် <strong><em>ကျန်တဲ့ code တွေကနေခွဲပြီး အဲ့ဒီ instruction ကိုသပ်သပ် execute လုပ်မယ်</em></strong>။</p><p>Async နဲ့ Sync က နားလည်ရခက်ပါတယ်။ မြန်မာလို အစဥ်လိုက် programming နဲ့ အပြိုင် programming လို့သာမှတ်လိုက်ပါ။ <br>Synchronous က instruction တွေကို အစဥ်လိုက်တစ်ခုပြီးမှ တစ်ခုအလုပ်လုပ်တယ်။ Asynchronous က instructions ၁ ခုထက်ပိုပြီးအပြိုင်အလုပ်လုပ်မယ်။ အဲ့တာဆို အရမ်းရှင်းသွားပါပြီ။</p><blockquote>Synchronous programming = အစဥ်လိုက် programming<br>Asynchronous programming = အပြိုင် programming</blockquote><h4>Synchronous ရဲ့ပြသနာကဘာလဲ?</h4><p>တကယ်က sync ကအများကြီးပိုရိုးရှင်းပါတယ်။ အများကြီးလဲ ပိုပြီး လုပ်ကိုင်ရလွယ်ပါတယ်။ ကျနော်တို့ run စေချင်တဲ့ အစဥ်အတိုင်း ထိပ်ဆုံးကနေဖြည်းဖြည်းချင်းအောက်ကို တစ်ခုပြီးတစ်ခု run သွားတယ်။ ကျနော်တို့ run စေချင်တဲ့ အစီစဥ်အတိုင်းပဲအမြဲ run တာဖြစ်တဲ့အတွက် code ရေးရင် စဥ်းစားရခေါင်းမခြောက်တော့ပါဘူး။ အောက်က ဥပမာ ကိုတချက်ကြည့်ကြည့်ပါ။</p><pre>console.log(1)<br>console.log(2)<br>console.log(3)</pre><p>1 ထုတ်မယ်, ပြီးရင် 2, ပြီးရင် 3 ဒီမှာဆို sync ကပြသနာလုံး၀မရှိပါဘူး။ အောက်က ဥပမာ ထပ်ကြည့်ကြည့်ပါ။</p><pre>console.log(1);<br>function sumUpTo(n) {<br>    let sum = 0;<br>    for (let i = 1; i &lt;= n; i++) {<br>      sum += i;<br>    }<br>    return sum;<br>}<br>console.log(sumUpTo(5000000000));<br>console.log(3);</pre><p>ဒီမှာ sumUpTo ဆိုတဲ့ function ကအကြာကြီး run ရမယ့် function ဆိုပါစို့။ console.log(3) ဆိုတဲ့ instruction က sumUpTo ကရလာမယ့် result အပေါ်လည်းမမှီခိုဘူး။ ဒါပေမဲ့ sumUpTo run လို့ပြီးတဲ့အထိစောင့်ရမယ်ဖြစ်နေတယ်။ အဲ့တာ Sync ရဲ့ ပြသနာပါပဲ။ အရမ်းအချိန်ယူရမယ့် အလုပ်တစ်ခုက ကြားညှပ်နေရင် နောက်အလုပ်တွေ ဆက်မလုပ်တော့ဘူး။ အဲ့ဒီနားမှာ တစ် နေရော။ အဲ့ဒီတော့ ကိုယ်ရေးနေတဲ့ application ရဲ့ performance ကျလာရော။</p><h4>Asynchronous ကအဲ့ဒီပြသနာကို ဘယ်လိုဖြေရှင်းလဲ?</h4><p>ခုနက code လေးကိုနည်းနည်း update လိုက်မယ်။</p><pre>console.log(1);<br>function sumUpTo(n) {<br>    let sum = 0;<br>    for (let i = 1; i &lt;= n; i++) {<br>      sum += i;<br>    }<br>    return sum;<br>}<br>setImmediate(()=&gt;console.log(sumUpTo(5000000000)))<br>console.log(3);</pre><p>အဲ့တာလေးကို run လိုက်ရင် 1 ရယ် 3 ရယ် အရင် log ထွက်ပြီး အချိန်နည်းနည်းကြာမှ sumUpTo(5000000000) ရဲ့ result log ထွက်လာပါလိမ့်မယ်။ ဒါဆို console.log(3) က sumUpTo function ပြီးတဲ့အထိစောင့်စရာမလိုတော့ပါဘူး အရင် run နှင့်တာဖြစ်သွားပါပြီ။ (<em>setImmediate function က ကျနော်တို့ async နဲ့ run ချင်တယ်။ ဒါပေမဲ့ တတ်နိုင်သမျအမြန်ဆုံး run ချင်တယ်ဆိုရင် အဲ့တာကို သုံးလို့ရပါတယ်</em>)</p><p>ဒါပေမဲ့ Async ကရေးရတာပိုလက်၀င်ပါတယ်။ ခုနက code လေးကိုနည်းနည်းထပ် update ကြည့်ကြမယ်။</p><pre>console.log(1);<br>function sumUpTo(n) {<br>    let sum = 0;<br>    for (let i = 1; i &lt;= n; i++) {<br>      sum += i;<br>    }<br>    return sum;<br>}<br><br>let a = &#39;before sumUp&#39;;<br>setImmediate(()=&gt;{<br>  console.log(sumUpTo(5000000000));<br>  console.log(a);<br>})<br>a = &#39;after sumUp&#39;;<br>console.log(3);</pre><p>ဒီမှာဆို sumUpTo ကိုခေါ်ပြီးသွားတဲ့အခါ a ဆိုတာလေးကို log ထုတ်ထားပါတယ်။ a ကို “before sumUp” ဆိုတာ assign တယ်။ ပြီးရင် log ထုတ်တယ်။ ကျနော်တို့ ရေးနေကျ sync မှာဆို “before sumUp” ဆိုတဲ့ log ထွက်လာသင့်ပါတယ်။ ဒါပေမဲ့ အခု run ရင် “after sumUp” ဆိုပြီးထွက်လာပါလိမ့်မယ်။ ကျနော်တို့ log ထုတ်တဲ့ instruction ကိုရေးပြီးမှ assign ထားတဲ့ value ကြီးထွက်လာပါတယ်။</p><p>Asynchronous programming က powerful တော့ဖြစ်တယ်။ ဒါပေမဲ့ sync ထက်စာရင် ရေးရတာပိုပြီးစဥ်းစားဖို့လိုပါတယ်။ ကျနော်တို့ sync မှာ code က run သွားစရာအစဥ်လိုက်လမ်းကြောင်းတစ်ခုပဲရှိတယ်။ Async မှာက code run သွားမယ့်လမ်းကြောင်းတွေက ကိုယ့် app က ရှုပ်ထွေးလေလေ ပိုများလေလေဖြစ်လာမှာဖြစ်ပါတယ်။ ဒါကြောင့် Async ရေးရင် ဘယ် လမ်းကြောင်းက run သွားပါစေ ကိုယ်လိုချင်တဲ့ result ကနေသွေဖယ်မသွားဖို့ သတိထားရပါမယ်။</p><h4>Summary</h4><p>ကျနော်တို့ အခုဆို Async programming ကဘာလဲ။ ဘာပြသနာဖြေရှင်းပေးလဲ ဆိုတာကို အကြမ်းဖြင်းနားလည်ပါပြီ။ နောက်ပို့စ်တွေမှာ Js မှာ Async ကိုဘယ်လိုနည်းတွေနဲ့ ရေးလို့ရတယ်ဆိုတာ ထပ်တင်သွားပါမယ်။ အားလုံးကိုနောက်ပို့စ်မှာပြန်တွေ့ကြမယ်။</p><h4>လက်ဆောင်</h4><p>အပိုလက်ဆောင်အနေနဲ့ mind-blowing ဖြစ်တာလေးတစ်ခု ချန်ခဲ့မယ် :P။ js က single threaded ဖြစ်ပါတယ်။ single thread ဆိုတာက ဘာနဲ့တူလဲဆိုတော့ တစ်လမ်းသွား ကားလမ်းနဲ့တူပါတယ်။ ကားတွေကို instrutions တွေလို့ယူဆကြည့်ပါ။ ကားတွေက တစ်လမ်းသွား ဖြစ်တဲ့အတွက် တစ်စီးနဲ့တစ်စီး ကျော်သွားလို့လဲမရဘူး။ အပြိုင်သွားလို့လဲမရပါဘူး။</p><h3>IF JS IS SINGLE-THREADED, HOW THE HEL* IT HANDLES ASYNC?</h3><p>ဒီမေးခွန်းအပေါ်မှာ ကိုယ်ထင်တာတွေ ပြောသွားပါဦး 😀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=752622442abe" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[npm Package တစ်ခု ကိုယ်တိုင်ရေးပြီး publish လုပ်မယ်]]></title>
            <link>https://medium.com/@zawhtetnaing/npm-package-%E1%80%90%E1%80%85%E1%80%BA%E1%80%81%E1%80%AF-%E1%80%80%E1%80%AD%E1%80%AF%E1%80%9A%E1%80%BA%E1%80%90%E1%80%AD%E1%80%AF%E1%80%84%E1%80%BA%E1%80%9B%E1%80%B1%E1%80%B8%E1%80%95%E1%80%BC%E1%80%AE%E1%80%B8-publish-%E1%80%9C%E1%80%AF%E1%80%95%E1%80%BA%E1%80%99%E1%80%9A%E1%80%BA-9f42e6c14682?source=rss-c4b2bc565629------2</link>
            <guid isPermaLink="false">https://medium.com/p/9f42e6c14682</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[npm-package]]></category>
            <category><![CDATA[npm]]></category>
            <dc:creator><![CDATA[Zaw Htet Naing]]></dc:creator>
            <pubDate>Wed, 03 Apr 2024 08:50:01 GMT</pubDate>
            <atom:updated>2024-04-03T08:53:19.580Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qj1OTPHk-djj2C1Nnkn4VQ.png" /></figure><h3>npm package တစ်ခု ကိုယ်တိုင်ရေးပြီး publish လုပ်မယ်</h3><p>ဒီ post အဆုံးထိဖတ်ပြီးသွားရင် ကိုယ်ရေးထားတဲ့ package လေးကို ကြိုက်တဲ့ project မှာ npm install mypackage လေး run ရုံနဲ့ ယူသုံးလို့ရသွားပါလိမ့်မယ်။</p><p>ကျနော်တစ်ခါတစ်လေ အရမ်းမိုက်တဲ့ idea တွေ သွားရင်းလာရင်းရတတ်ပါတယ်။ အများအားဖြင့်အဲ့လို idea တွေက လေထဲမှာပဲ ပျောက်သွားတယ်။ အဲ့လိုလေထဲပျောက်စေတဲ့ အဓိက ပြသနာကတော့ idea တွေကို ဘယ်လို contribute လုပ်ရမလဲမသိတာပါ။ လွယ်လွယ်ကူကူ contribute လုပ်တဲ့နည်းတွေရှိမှန်း သတိမထားမိခဲ့ဘူးပေါ့။ ဒီနေ့တော့ ကျနော် အရမ်းမိုက်တဲ့ idea တစ်ခုကို Node.js community ကြီးအတွက်မျှ၀ေသွားပါမယ်။ idea ကတော့ console.log ကို အပြာရောင်လေးနဲ့ထုတ်တာပါ 😐။ အဲ့တာကို npm package လေးတစ်ခုအဖြစ် create သွားပါမယ်။ မိတ်ဆွေတို့လည်း တခြား idea လေးတွေ contribute လုပ်လိုရပါတယ်။ အနီရောင်ပြောင်းပေးတာတို့ အ၀ါရောင်ပြောင်းပေးတာတို့ပေါ့ 😂။</p><h4>npm package တစ်ခုရဲ့ မဖြစ်မနေလိုအပ်ချက်ကဘာလဲ?</h4><p>အရမ်း simple ဖြစ်ပါတယ်။ ငှက်ပျောသီးခွာစားတာတောင်ပိုရှုပ်ပါဦးမယ်။ npm package တစ်ခု create တော့မယ်ဆိုရင် 2 ခုပဲမဖြစ်မနေလိုပါတယ်။</p><ol><li>npm account</li><li>package.json file</li></ol><p>အဲ့ဒီ 2 ခုထဲနဲ့ npm package တစ်ခု publish လုပ်လို့ရပါတယ်။</p><h4>npm package တကယ် create ကြည့်မယ်</h4><p>ကဲ စမ်း create ကြည့်ရအောင် ပထမ အဆင့် npm အကောင့်လေးဖွင့်ပါမယ်။ <a href="https://www.npmjs.com/">https://www.npmjs.com/</a> ကိုသွားပြီး sign up လုပ်လိုက်ပါ။ facebook အကောင့်ဖွင့်တာထက်ပိုလွယ်ပါတယ်။</p><p>…</p><p>ဆက်မဖတ်ပါနဲ့ အကောင့်သွားဖွင့်ပါ။ ပြီးမှပြန်လာခဲ့နော်</p><p>…</p><p>အိုကေ အခုကျနော်တို့မှာ npm အကောင့်တစ်ခုရလာပါပြီ။ အဲ့တာကို ကိုယ့်စက်မှာ login ၀င်ပါမယ်။ ဘယ်လို၀င်မလဲ?</p><pre>npm login</pre><p>လို့ရိုက်လိုက်ပါ 😂။ အဲ့လောက်ထိလွယ်တာပါ။ အဲ့လိုရိုက်လိုက်ရင် အောက်ကပုံလို link လေးကျလာပါလိမ့်မယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U4e9wx3DSMSVDYF-PU25eA.png" /></figure><p>အဲ့ဒီ link လေးကို copy ကူးပြီး browser မှာဖွင့်လိုက်ပါ။ ပြီးရင် username, password ရိုက်ထည့်လိုက်ပါ။ အဲ့တာဆို login ၀င်လို့ပြီးပါပြီ။</p><p>အခု package အတွက် folder တစ်ခု ဆောက်ပါမယ်။ folder name ကို blue လို့ပေးလိုက်ကြစို့။ blue folder ထဲ၀င်ပြီး package.json ဆိုတဲ့ file လေးတစ်ခု create လိုက်ပါ။ ပြီးရင် အောက်က json လေးကို package.json ထဲကူးထည့်လိုက်ပါ။</p><pre>{<br>  &quot;name&quot;: &quot;@kris2856/blue&quot;,<br>  &quot;version&quot;: &quot;1.0.0&quot;<br>}</pre><p>အပေါ်က code လေးကို ကြည့်ကြည့်ပါ။ name ရယ်, version ရယ် ၂ခုပဲပါပါတယ်။ တကယ်လည်း အဲ့ဒီ ၂ ခုထဲပဲလိုတာပါ။</p><h4>name နဲ့ version ကို လေ့လာမယ်</h4><p><strong>name</strong> အပိုင်းလေးကို တချက်ကြည့်ကြည့်ပါ။ @kris2856 ဆိုတာက scope လို့ခေါ်ပါတယ်။ အနောက်က blue က package name ပါ။ blue ဆိုတဲ့ package က တခြားလူတွေ pulish ပြီးသားရှိနိုင်တယ်ဗျာ။ အဲ့တာကြောင့်မို့ kris2856 ရဲ့ blue ပါဆိုပြီး @kris2856/ လေးအရှေ့မှာထည့်ပေးလိုက်တာပါ။ မိတ်ဆွေတို့လည်း ကိုယ့် @username/blue အဲ့လိုလေးပေးလိုက်ပါ။</p><p><strong>version </strong>အပိုင်းလေးကို တချက်ကြည့်ကြည့်ပါ။ 1.0.0 ဆိုပြီး အပိုင်း ၃ ပိုင်းပါပါတယ်။</p><p>ထိပ်ဆုံးအပိုင်း “1” က major version ကို ကိုယ်စားပြုပါတယ်။ ကိုယ် package ကို update လိုက်တဲ့အခါ အရင် version အဟောင်း ကပုံစံအတိုင်း သုံးထားတဲ့ နေရာတွေ ပြသနာ တက်လောက်တဲ့အထိ package က code တွေပြောင်းလဲလိုက်ရင် major version ကို တစ်ခုတိုးပေးသင့်ပါတယ်။</p><p>အလယ်က “0” လေးက minor version ကိုကိုယ်စားပြုပါတယ်။ feature အသစ်တွေထည့်လိုက်တယ်။ ဒါပေမဲ့ update တင်လိုက်လို့ ကိုယ့် package ကိုယူသုံးထားတဲ့ နေရာတွေ ပြသနာ မတတ်နိုင်ဘူး။ တနည်းအားဖြင့် feature အသစ်တွေတော့ပါလာတယ် ဒါပေမဲ့ backwards-compatible ဖြစ်နေသေးတယ်ဆိုရင် minor version ကို တစ်ခုတိုးပေးလိုက်ရင်ရပါတယ်။ major version တိုးစရာမလိုသေးပါဘူး။</p><p>နောက်ဆုံးက “0” လေးက patch version လို့ခေါ်ပါတယ်။ သူက ရှိပြီးသား feature တွေကို bugfix လုပ်တယ်။ backwards-compatible လည်းဖြစ်နေသေးတယ်ဆိုရင် patch version တိုးပေးရင်ရပါပြီ။ major ရော minor ရောတိုးပေးစရာမလိုပါဘူး။</p><h4>publish လုပ်ကြစို့</h4><p>အခု ကျနော်တို့ npm package တစ်ခု create ပြီးပါပြီ။ publish ဘယ်လိုလုပ်မလဲ?</p><pre>npm publish --access=public</pre><p>လို့ရိုက်လိုက်ပါ။ အိုကေဒါဆို publish လုပ်ပြီးပါပြီ။ အနောက်က “ — access=public” ဆိုတာလေးကို သတိထားကြည့်ပါ။ အဲ့တာက ဒီ package ကို public access ပေးထားတယ်။ ကြိုက်တဲ့သူ install လို့ရတယ်လို့ ပြောချင်တာပါ။ ကျနော်တို့ က ကိုယ့် organization ထဲမှာပဲ သုံးမယ့် private package တွေလည်း create လို့ရပါတယ်။ အခု package ကိုက Node.js community တစ်ခုလုံးပေးသုံးစေချင်လို့ public access ပေးလိုက်ပါမယ်။</p><h4>publish လိုက်တဲ့ package ကိုယူသုံးကြစို့</h4><p>ပထမ စစချင်း package publish လိုက်တာ ဘယ်ရောက်သွားလဲသွားကြည့်ပါမယ်။ အိုကေ သွားကြည့်ရမယ့် url ကိုပြင်ဆင်မယ်။ url ရဲ့ ပထမ ဆုံးအပိုင်းက npm ရဲ့ default registry ပါ (<a href="https://registry.npmjs.org/@kris2856/blue">https://registry.npmjs.org</a>)၊ default registry ရဲ့အနောက်မှာ package.json ထဲက name ဆိုတဲ့အပိုင်းလေးကိုထည့်ပေးလိုက်ပါ။ ကျနော့်အတွက်ဆို url က <a href="https://registry.npmjs.org/@kris2856/blue">https://registry.npmjs.org/@kris2856/blue</a> ဖြစ်သွားပါမယ်။ မိတ်ဆွေတို့က ကိုယ်သုံးထားတဲ့ username ထည့်လိုက်ကြပါ(<a href="https://registry.npmjs.org/@kris2856/blue">https://registry.npmjs.org/&lt;username&gt;/blue</a>)။ အိုကေပြီးရင် အဲ့တာကို browser မှာဖွင့်ကြည့်လိုက်ပါ။ ကိုယ့် package ရဲ့ metadata ကို json format လေးနဲ့မြင်ရပါလိမ့်မယ်။</p><p>အိုကေ နောက်တစ်ဆင့် သွားကြမယ်။ ကိုယ့် package ကို ကိုယ့်ဘာသာအရင်စမ်းသုံးကြည့်မယ်။</p><p>folder အသစ်တစ်ခုဆောက်လိုက်ပါ။ folder name ကို package-test လို့ပေးလိုက်ပါ။ ပြီးရင် အောက်က command လေးတွေရိုက်လိုက်ပါ။ @kris2856/blue နေရာမှာ ကိုယ် package.json မှာ ထည့်ပေးလိုက်တဲ့ name နဲ့အစားထိုးပြီးရိုက်ထည့်လိုက်ပါ။</p><pre>cd package-test<br>npm install @kris2856/blue</pre><p>အိုကေအဲ့တာဆို အဲ့ဒီ package-test folder ထဲမှာ node_modules ဆိုတဲ့ folder လေးပေါ်လာလိမ့်မယ်။ အဲ့ဒီထဲမှာ ကိုယ့် package လေးရောက်နေပါလိမ့်မယ်။ ပြီးတော့ package-test ရဲ့ package.json ထဲမှာ</p><pre>{<br>  &quot;dependencies&quot;: {<br>    &quot;@kris2856/blue&quot;: &quot;^1.0.0&quot;<br>  }<br>}</pre><p>ဆိုပြီး၀င်သွားပါလိမ့်မယ်။ အရမ်း ပျော်စရာကောင်းပါတယ်။ Node.js community ထဲမှာ ကိုယ့် လက်ရာ package လေးတစ်ခုရောက်သွားပါပြီ။ ရှက်စရာတော့နည်းနည်းကောင်းတယ်။ ကျနော်တို့ package ထဲမှာ code တစ်လုံးမှ မပါသေးဘူး 😂 😂။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*-bstAVXbE8JUmsDvGRokiw.gif" /></figure><h4>Package ကို version အသစ် release မယ်</h4><p>ကျနော်တို့ အခု blue 1.1.0 ကိုရေးပါမယ်။ package ကို version အသစ်ထုတ်မယ်ပေါ့။ blue folder ထဲသွားလိုက်ပါ။ ပြီးရင်အောက်က code လေးကို copy ကူးပြီး index.js ဆိုတဲ့နံမည်လေးနဲ့ save လိုက်ပါ။</p><pre>module.exports = function blue(text) {<br>    const colorReset = &quot;\x1b[0m&quot;;<br>    const colorBlue = &quot;\x1b[34m&quot;;<br>    console.log(colorBlue + text + colorReset);<br>};</pre><p>ပြီးရင် package.json မှာ version ကို 1.1.0 ဆိုပြီး edit လိုက်ပါ။ ပြီးရင် <strong>npm publish</strong> ဆိုပြီးရိုက်လိုက်ပါ။ အိုကေ ဒါဆို version အသစ်ကို release လုပ်ပြီးပါပြီ။</p><p><strong>Package ကို ယူသုံးထားတဲ့ဘက်မှာ update လုပ်မယ်</strong></p><p>ကျနော်တို့ ရဲ့ package-test ဆိုတဲ့ folder ထဲသွားလိုက်ပါ။ <strong>npm update @kris2856/blue</strong> ဆိုပြီးရိုက်လိုက်ပါ။ အဲ့တာဆို ကျနော်တို့ရဲ့ blue package ရဲ့ version 1.1.0 ကို update ဖြစ်သွားပါပြီ။ package-test folder ထဲမှာ test.js ဆိုတဲ့ file တစ်ခုဆောက်ပြီးအောက်က code ကို copy ကူးထည့်လိုက်ပါ။</p><pre>const blue = require(&quot;@kris2856/blue&quot;);<br>blue(&quot;Hello, I am blue&quot;);</pre><p>ပြီးရင် အောက်ကလိုလေးရိုက်ပြီး test.js file လေးကို run ကြည့်လိုက်ပါ။</p><pre>cd package-test<br>node test.js</pre><p>အဲ့တာဆို Hello, I am blue လေးကို အပြာရောင်နဲ့မြင်ရပါပြီ။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*kn55sSbRabX-miwVEZi2YA.gif" /></figure><p>အိုကေ ဒါဆို package ကို publish လုပ်တာ version အသစ် release တာ update လုပ်တာပြီးသွားပါပြီ။ ပို့စ် အစမှာပြောခဲ့တဲ့အတိုင်း ကိုယ့်နံမည်နဲ့ package တစ်ခု npm မှာရှိနေပြီ။ အရမ်းရင်ခုန်စရာကောင်းပါတယ်။ ဒါပေမဲ့ ကျနော်တို့ professional ဆန်ချင်သေးတယ်။ နည်းနည်းထပ်ကွန့်မယ်။</p><h4>စတိုင်ကျကျ professional ဆန်ကြမယ်</h4><p>blue package ကို github မှာသွားတင်ပါမယ်။ ပြီးရင် package.json ကိုအောက်ကလိုလေး update လုပ်ပါမယ်။</p><pre>{<br>  &quot;name&quot;: &quot;@kris2856/blue&quot;,<br>  &quot;version&quot;: &quot;1.1.1&quot;,<br>  &quot;description&quot;: &quot;Write log with blue color.&quot;,<br>  &quot;repository&quot;: &quot;zawhtetnaing006/blue&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;keywords&quot;: [<br>    &quot;blue&quot;,<br>    &quot;npm&quot;,<br>    &quot;package&quot;,<br>    &quot;kris2856&quot;<br>  ]<br>}</pre><p><strong>repository</strong>: ကျနော်တို့ သွားတင်လိုက်တဲ့ github repo ကိုထည့်ပေးရပါမယ်။<br><strong>main</strong>: package ရဲ့ entry point ကိုထည့်ပေးရပါမယ်။ ကျနော်တို့အတွက်ဆို index.js ပါ။ <br><strong>keywords</strong>: npm website မှာကျနော်တို့ package ကိုရှာဖို့အတွက်သုံးမယ့် keyword တွေရဲ့ list ပါ။</p><p>version နေရာမှာ 1.1.1 လို့ပြောင်းသွားတာကို သတိထားပါ။ package.json ပြောင်းတာကို patch version တစ်ခုထုတ်ပေးလိုက်တယ်လို့ ယူဆထားတာပါ။ <strong>npm publish</strong> လို့ရိုက်လိုက်ပါ။ အဲ့တာဆို version 1.1.1 ကို release ပေးလိုက်ပါပြီ။ git repo လည်းထည့်ပေးလိုက်ပါပြီ။</p><p>ပြီးရင် npm website ကိုသွားပြီး @kris2856/blue လို့ရှာကြည့်ရင် ကျနော်တို့ package လေးထွက်လာပါလိမ့်မယ်။ ကျနော်တို့ package ကို license မထည့်ရသေးပါဘူး။ ထည့်ကြည့်ကြရအောင်။</p><h4>Package ကို license ထည့်မယ်</h4><p>github မှာ insight ဆိုတဲ့ page လေးရှိပါတယ်။ အဲ့ဒီမှာကျနော်တို့ license ထည့်ပေးလို့ရပါတယ်။ github မှာကိုယ့် repo ထဲ၀င် -&gt; insights ကိုသွား -&gt; community standards ထဲ၀င် -&gt; License ရဲ့ဘေးဘက်က add လေးကို နှိပ် -&gt; MIT license ကိုရွေး -&gt; reivew and submit ကိုနှိပ် -&gt; commit ကိုနှိပ် အဲ့တာဆို license လေးကိုယ့် project မှာထည့်ပြီးပါပြီ။ ပြီးသွားရင် local ကနေ git pull လိုက်ပါ။ အဲ့တာဆို LICENSE ဆိုတဲ့ file လေး ကိုယ့် package ထဲရောက်လာပါလိမ့်မယ်။ package.json မှာလည်း version ကို patch နေရာမှာတစ်တိုး <strong>“license”: “MIT&quot; </strong>ဆိုတာကို ထပ်ထည့်ပေး လိုက်ရင် ကျနော်တို့ package မှာ license ထည့်ပြီးသားဖြစ်သွားပါပြီ။ final package.json က အောက်ကပုံလေးနဲ့ တူပါလိမ့်မယ်။</p><pre>{<br>  &quot;name&quot;: &quot;@kris2856/blue&quot;,<br>  &quot;version&quot;: &quot;1.1.2&quot;,<br>  &quot;description&quot;: &quot;write log with blue color.&quot;,<br>  &quot;license&quot;: &quot;MIT&quot;,<br>  &quot;repository&quot;: &quot;zawhtetnaing006/blue&quot;,<br>  &quot;main&quot;: &quot;index.js&quot;,<br>  &quot;keywords&quot;: [<br>    &quot;tiny&quot;,<br>    &quot;npm&quot;,<br>    &quot;package&quot;,<br>    &quot;kris2856&quot;<br>  ]<br>}</pre><p>ပြီးရင် npm publish ကို ထပ်ရိုက်လိုက်ပါ။ license လည်းထည့်ပြီးသား အလုပ်လည်းလုပ်နေတဲ့ package လေးတစ်ခု create ပြီးသွားပါပြီ။</p><h4>Summary</h4><p>အခုဆို ကျနော်တို့ package တစ်ခုကို ဘယ်လို စ create ရမလဲ လေ့လာပြီးပါပြီ။ README ကို စနစ်တကျ ထည့်ဖို့တွေ၊ package create ရင် ဆောင်ရန် ရှောင်ရန် တွေကိုတော့ ဆက်လေ့လာဖို့လိုပါမယ်။ ကျနော့် post အများစုက ဆက်လေ့လာဖို့ လမ်းစပေးသလောက်ပဲပါပါတယ်။ ကျန်တာကို တော့ ကျနော်တို့ ကိုယ့်ဘာသာဆက်လေ့လာဖို့လိုပါလိမ့်မယ်။ ကျနော်တို့ မြန်မာ community မှာလည်းလိုနေသေးတဲ့ package တွေအများကြီးရှိပါတယ်။ input ထည့်လိုက်တဲ့ အချိန်တွေကို myanmar time zone နဲ့တန်းတွက်ပေးတာ, မြန်မာ nrc validate လုပ်ပေးတာ, တိုင်းရင်းသားဘာသားစကား localization တွေ စသဖြင့်ပေါ့။ တချို့ package တွေလည်းရှိပြီးသားဖြစ်ကောင်းဖြစ်ပါလိမ့်မယ်။ လေ့လာရင်း လူတွေအများကြီးသုံးတဲ့ package တွေရေးနိုင်ကြပါစေခင်ဗျာ။</p><h4>Reference:</h4><p><a href="https://www.freecodecamp.org/news/how-to-make-a-beautiful-tiny-npm-package-and-publish-it-2881d4307f78/">https://www.freecodecamp.org/news/how-to-make-a-beautiful-tiny-npm-package-and-publish-it-2881d4307f78/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9f42e6c14682" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[npm(Node Package Manager) in Burmese]]></title>
            <link>https://medium.com/@zawhtetnaing/npm-node-package-manager-in-burmese-8b4048450f1e?source=rss-c4b2bc565629------2</link>
            <guid isPermaLink="false">https://medium.com/p/8b4048450f1e</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[npm]]></category>
            <category><![CDATA[burmese]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Zaw Htet Naing]]></dc:creator>
            <pubDate>Tue, 02 Apr 2024 09:45:16 GMT</pubDate>
            <atom:updated>2024-04-02T10:57:18.480Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*BAF7fM9UQ0xN8Vsv" /><figcaption>Photo by <a href="https://unsplash.com/@pinjasaur?utm_source=medium&amp;utm_medium=referral">Paul Esch-Laurent</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>modules တွေ packages တွေ install ဖို့ Node.js ရဲ့ standard package manager က npm ဖြစ်ပါတယ်။</p><h4>npm က ကျနော်သိခဲ့တာထက်ပိုတယ်</h4><p>အစက npm ကို ကျနော် command-line tool တစ်ခုလို့ပဲသိခဲ့တာပါ။ ကျနော်တို့ npm နဲ့ package တွေ install မယ်, update မယ်ပေါ့။ တကယ်တော့ အဲ့ဒီထက်အများကြီးပိုမိုက်ပါတယ်။ သူက js project တွေစုထားတဲ့ repo ကြီးတစ်ခုလည်းဖြစ်ပါတယ်။ ကျနော်တို့ npm လို့ပြောလိုက်ရင် ၃ ပိုင်းပါ၀င်ပါမယ်။</p><ol><li>website</li><li>Command Line Interface (CLI)</li><li>registry</li></ol><h4>npm website</h4><p>Npm website ကိုက ကျနော်တို့ package တွေရှာမယ်။ package တွေကိုယ်တိုင်ရေးပြီး pulic ဖို့အတွက် profile setup လုပ်မယ်။ ကိုယ့် company ထဲမှာ private package တွေ handle ဖို့ organization တွေ manage လုပ်မယ်။ စတာတွေအတွက်သုံးလို့ရပါတယ်။</p><h4>npm command line interface (CLI)</h4><p>terminal မှာ npm command လေးသုံးပြီးတော့ package တွေ install မယ်။ update မယ်။ publish လုပ်မယ်။ စတဲ့ npm နဲ့ ပတ်သက်တဲ့ operations တွေကို cli လေးသုံးပြီးလုပ်လို့ရပါတယ်။</p><h4>npm registry</h4><p>js package တွေရဲ့ metadata (package name, version, license), code တွေပါသိမ်းထားပေးတဲ့ အရမ်းကြီးတဲ့ public database လို့မှတ်ယူလို့ရပါတယ်။ npm ရဲ့ default package registry က <a href="https://registry.npmjs.org/">https://registry.npmjs.org/</a> ဖြစ်ပါတယ်။ <a href="https://registry.npmjs.org/">https://registry.npmjs.org/</a> အနောက်မှာ ကိုယ်သိချင်တဲ့ package လေးကိုရိုက်ထည့်လိုက်ရင် အဲ့ဒီ package ရဲ့ metadata တွေကို response ပြန်ပေးပါတယ်။ ဥပမာ — <a href="https://registry.npmjs.org/@nestjs/common">https://registry.npmjs.org/@nestjs/common</a> ကို browser မှာဖွင့်ကြည့်လိုက်ရင် nestjs ရဲ့ common package metadataတွေ response ပြန်ပေးပါလိမ့်မယ်။</p><h4>Installation</h4><p>Node.js ကို install လိုက်တာနဲ့ npm ပါတခါထဲ install ပြီးသားဖြစ်သွားပါတယ်။ သီးသန့်ကြီး ထွေထွေထူးထူး install ပေးဖို့မလိုပါဘူး။</p><h3>npm command line အသုံးပြုပုံများ</h3><h4>Installing a single package</h4><p><strong>npm install &lt;package-name&gt;</strong> လို့ရိုက်လိုက်ရင် သူကအလုပ် ၂ ခုလုပ်ပါတယ်။</p><ol><li>package ကို download လုပ်ပြီး node_mouldes ဆိုတဲ့ folder ထဲမှာသွားသိမ်းပေးပါတယ်။ node_modules ဆိုတဲ့ folder create ထားတာမရှိရင် create ပြီးသွားသိမ်းပေးပါတယ်။</li><li>package.json file ထဲမှာ ဒီ package က ဒီ project အတွက်လိုပါတယ်ဆိုတဲ့ information ကိုသွားမှတ်ထားပေးပါတယ်။</li></ol><p><strong>npm install &lt;package-name&gt;@&lt;version&gt;</strong> ဒီလိုရိုက်ပြီး ကိုယ် install ချင်တဲ့ version ကို သတ်မှတ်ပေးလိုက်လို့လည်းရပါတယ်။</p><p>npm က package တွေကို install တဲ့အခါ နည်း ၂ မျိုးနဲ့ install လို့ရပါတယ်။ local install နဲ့ global install ပါ။ local install က ကိုယ့် project အတွက် လိုအပ်တဲ့ package ကို install ဖို့သုံးလေ့ရှိပါတယ်။ global install နည်းကိုက အဓိက အားဖြင့် project တွေမှာ ခနခန သုံးရမယ့် command line tool တွေ install ဖို့သုံးကြပါတယ်။ ဥပမာ — @nestjs/cli ဆို global install လေ့ရှိကြပါတယ်။ global install က ကိုယ့် computer ထဲက system path ထဲမှာ သွား install ပါတယ်။ ဒါကြောင့်မို့လို့ အဲ့ဒီ computer သုံးနေသမျ gobal install ထားတဲ့ package တွေကို access ရနေပါလိမ့်မယ်။ global install အတွက်က command မှာ -g လေးထည့်ပေးလိုက်ရင်ရပါပြီ။ ဥပမာ — <strong>npm install -g @nestjs/cli</strong></p><h4>Installing all dependencies</h4><p><strong>npm install</strong> လို့ရိုက်လိုက်ရင် package.json file ကို ဖတ်ပြီး ကိုယ့် project အတွက်လိုအပ်သမျ package တွေကို သွားဒေါင်း ပြီး node_modules folder ထဲသွားသိမ်းပေးပါတယ်။</p><h4>Updating Packages</h4><p><strong>npm update</strong> လို့ရိုက်ပြီးတော့ ရှိသမျ package တွေအကုန်တစ်ခါထဲ update လို့ရပါတယ်။ <strong>npm update &lt;package-name&gt;</strong> လို့ရိုက်ပြီးတော့ ကိုယ် update ချင်တဲ့ package တစ်ခုထဲ update လို့လည်းရပါတယ်။</p><h4>Running Tasks</h4><p>package.json file ထဲမှာ scripts ဆိုတဲ့ အပိုင်းလေးတစ်ခုထည့်လို့ရပါတယ်။ ကျနော်တို့ <strong>npm run &lt;task-name</strong>&gt; လို့ရိုက်လိုက်ရင် သူက package.json ထဲမှာရေးထားတဲ့ script အပိုင်းလေးမှာ &lt;task-name&gt; နဲ့သွားတိုက်စစ်ပြီး run ပေးပါတယ်။ ဥပမာ-</p><pre>{<br>  ...<br>  &quot;scripts&quot;: {<br>    &quot;info&quot;: &quot;node -v&quot;<br>  },<br>  ...<br>}</pre><p>package.json file ထဲမှာ ဒီလိုလေးရှိတယ်ဆိုပါစို့။ ကျနော်တို့ <strong>npm run info</strong> လို့ရိုက်လိုက်ရင် သူက <strong>node -v</strong> ဆိုတဲ့ command လေးကို run ပေးပါလိမ့်မယ်။ info ဆိုတာ ကို scripts ထဲမှာ သွားစစ်ပါတယ်။ ပြီးတော့”info”: “node -v&quot; ဆိုတာလေးကို တွေ့တော့ run ပေးလိုက်ပါတယ်။ ဒီ feature ကို အောက်ကပုံလို webpack command အရှည်ကြီးတွေရိုက်ဖို့သုံးကြလေ့ရှိပါတယ်။</p><pre>{<br>  &quot;scripts&quot;: {<br>    &quot;watch&quot;: &quot;webpack --watch --progress --colors --config webpack.conf.js&quot;,<br>    &quot;dev&quot;: &quot;webpack --progress --colors --config webpack.conf.js&quot;,<br>    &quot;prod&quot;: &quot;NODE_ENV=production webpack -p --config webpack.conf.js&quot;<br>  }<br>}</pre><h3>npx (the package runner)</h3><p>npm version 5.2 ထဲကနေပြီး npx က preinstall အနေနဲ့ပါလာခဲ့ပါတယ်။ install မလုပ်ရသေးဘူးဆိုရင်လည်း <strong>npm install -g npx</strong> ဆိုပြီး အလွယ်တကူ install လို့ရပါတယ်။ သူက အတော်အသုံး၀င်တဲ့ cli tool လေးပါ။ ကျနော်တို့ install မလုပ်ထားတဲ့ package က command တွေကို npx နဲ့ run လို့ရပါတယ်။ ဥပမာ- <strong>npx cowsay hi</strong> လို့ရိုက်ရင် result လေးထွက်လာပါလိမ့်မယ်။ ဒါပေမယ့်ကျနော်တို့ cowsay package ကို install ထားတာမရှိပါဘူး။</p><p>တစ်ခုသတိပြုရမှာက npm နဲ့ npx လုံးလုံးမတူပါဘူး။ npx က install မထားတဲ့ package က command တွေကို run ဖို့ create ထားတဲ့ tool လေးပါ။</p><h4>Summary</h4><p>ကျနော်တို့ npm အကြောင်းအခြေခံတွေလေ့လာလို့ပြီးပါပြီ။ Node.js ကို အခြေခံကနေ တဖြည်းဖြည်း ကိုယ်တိုင် project တွေ ရေးနိုင်တဲ့အထိ လေ့လာလို့ရမယ့် stories တွေကို series အလိုက်လေးတင်သွားဖို့စဥ်းစားထားပါတယ်။ စာရေးရတာအားရှိရင် claps လေးတွေပေးသွားကြပါဦး :)</p><h4>Reference:</h4><p><a href="https://nodejs.org/en/learn/getting-started/an-introduction-to-the-npm-package-manager">https://nodejs.org/en/learn/getting-started/an-introduction-to-the-npm-package-manager</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8b4048450f1e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Node.js Modules: A Guide for Beginners in Burmese]]></title>
            <link>https://medium.com/@zawhtetnaing/node-js-modules-a-guide-for-beginners-in-burmese-3d6b518efed8?source=rss-c4b2bc565629------2</link>
            <guid isPermaLink="false">https://medium.com/p/3d6b518efed8</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Zaw Htet Naing]]></dc:creator>
            <pubDate>Fri, 29 Mar 2024 15:45:56 GMT</pubDate>
            <atom:updated>2024-03-29T15:45:56.933Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IWoP7XBDBM0RbpxZ" /><figcaption>Photo by James Harrison on Unsplash</figcaption></figure><p>Node.js အကြောင်းလေ့လာကြတဲ့အခါ ကျနော်တို့ modules တွေအကြောင်းနားလည်ဖို့လည်းအရေးကြီးပါတယ်။ ဒီ post မှာ Node.js module တွေဘယ်လိုအလုပ်လုပ်လဲ, ဘာကြောင့်အရေးပါတာလဲ, ဘာအမျိုးအစားတွေရှိလဲ စတာတွေကိုလေ့လာသွားကြပါမယ်။</p><h4>Modules ဆိုတာဘာလဲ?</h4><p>Functionality တစ်ခုကို support ပေးနိုင်တဲ့ mini application လေးလို့ မှတ်ယူလို့ရပါတယ်။ ဥပမာ Node.js မှာ ပါတဲ့ filesystem module ဆိုရင် file တွေနဲ့ပတ်သက်ပြီး အလုပ်လုပ်တာတွေကို support ပေးနိုင်တယ်။ ကျနော်တို့က အဲ့လို module လေးတွေကိုပေါင်းပြီး ပိုပြီးကြီးတဲ့ application တွေတည်ဆောက်နိုင်ပါတယ်။</p><h4>Types of Node.js Modules</h4><ol><li><strong>Core Modules</strong>: Nodejs နဲ့တစ်ခါထဲတွဲပြီးပါလာတဲ့ module တွေဖြစ်ပါတယ်။ ဥပမာ: filesystem တွေကို manage ဖို့ fs module, http server တွေ create ဖို့အတွက် http module အစရှိတာတွေပါပါတယ်။</li><li><strong>Local Modules</strong>: ကိုယ်တိုင်ကိုယ့် application ထဲမှာ ထည့်သုံးဖို့ရေးသားထားတဲ့ module တွေဖြစ်ပါတယ်။ ဥပမာ blog crud application တစ်ခုရေးမှာဆို user module, post module အစရှိတဲ့ module လေးတွေ create ပြီးပေါင်းစပ်ရေးနိုင်ပါတယ်။</li><li><strong>Third-Party Modules</strong>: ကျနော်တို့ ခနခန လိုတတ်တဲ့ functionality တွေအတွက် developers တွေကပဲ create ထားပေးတဲ့ module တွေဖြစ်ပါတယ်။ npm, yarn စတဲ့ package manager တွေကနေ install လို့ရနိုင်ပါတယ်။ ဥပမာ: express-validator, socket.io</li></ol><h4>Types of module systems in Node.js</h4><ol><li><strong>CommonJs Module System</strong>: Nodejs စပေါ်လာထဲက ရှိခဲ့တဲ့ default module system ပါ။ Node.js ရဲ့ default configuration မှာ .js နဲ့ဆုံးတဲ့ file တိုင်းကို CommonJs module တစ်ခုအနေနဲ့ handle ပါတယ်။ (ကိုယ့် project မှာ .js နဲ့ဆုံးတဲ့ဖိုင်တွေကို ES Module အဖြစ် handle စေချင်ရင် package.json မှာ “type”: “module” လို့ထည့်ပေးလိုက်ရင်ရပါတယ်)</li><li><strong>ES Module System</strong>: 2015 မှာ javascript language တစ်ခုလုံးအတွက် standard module system အဖြစ်မိတ်ဆက်ခဲ့တာပါ။ Node.js version 14 က နေစပြီးတော့ ES module system ကို stable support ပေးခဲ့ပါတယ်။</li></ol><h4>Difference between CommonJs and ES module systems</h4><ol><li><strong>File Extension</strong><br>Node.js က .cjs files တွေကို CommonJs အဖြစ်ယူဆပြီး .mjs files တွေကို ES modules အဖြစ်ယူဆပါမယ်။ .js files တွေကိုတော့ package.json ထဲမှာ “type”:”module” ဆိုပြီးထည့်ထားရင် ES module အဖြစ်ယူဆပြီး မထည့်ထားရင် CommonJs module အဖြစ်ယူဆမှာဖြစ်ပါတယ်။</li><li><strong>Syntax</strong><br>CommonJs က require နဲ့ module.exports ကိုသုံးပါတယ်။ ES module က import နဲ့ export ကိုသုံးပါတယ်။ syntax အကြောင်းကိုအောက် က link မှာ အသေးစိတ်ဖတ်နိုင်ပါတယ်။<br><a href="https://reflectoring.io/nodejs-modules-imports/">https://reflectoring.io/nodejs-modules-imports/</a></li><li><strong>Nature</strong><br>ES Module ထဲကနေ CommonJs module တွေကို import လို့ရပြီး၊ CommonJs module ထဲကနေ ES Module တွေကို import လို့မရပါဘူး။ (.cjs/.js file ထဲကနေပြီးတော့ .mjs file ကို import လုပ်မရပါ၊ .mjs file ထဲကနေပြီးတော့ .cjs/.js file ကို import လုပ်လို့ရပါတယ်)</li><li><strong>Import လုပ်တဲ့အချိန်</strong> <br>Js code တွေကို အောက်ကပုံလေးအတိုင်း execute လုပ်ပါတယ်။ <br>CommonJs က execution အဆင့်မှာ import လုပ်ပြီးတော့ ES module က parsing အဆင့်မှာ import လုပ်ပါတယ်။ အဲ့လို import လုပ်တဲ့အချိန်ကွာသွားတာကြောင့်ဘာတွေဖြစ်သွားလဲအသေးစိတ်သိချင်ရင် <a href="https://reflectoring.io/nodejs-modules-imports/">ဒီ post</a> က Dynamic vs. Static ဆိုတဲ့အပိုင်းလေးကို ဖတ်ကြည့်လို့ရပါတယ်။</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*0p1_fhM8zwl3gD-vJIOSGg.jpeg" /></figure><h4>Conclusion</h4><p>အခုဆို ကျနော်တို့ Node.js ရဲ့ modules တွေကို အခြေခံနားလည်သွားပါပြီ။ ဘယ်ချိန်မှာ CommonJs သုံးပြီး ဘယ်ချိန်မှာ ES module သုံးရမယ်ဆိုပြီးတော့ တိတိကျကျသတ်မှတ်ထားတာမရှိပါဘူး။ project အသစ်စမယ်ဆိုရင်တော့ ES module ကိုသုံးသင့်ပါတယ်။ ဘာလို့လဲဆိုတော့ ES module က js language တစ်ခုလုံးရဲ့ standard module system ဖြစ်ပါတယ်။ Node.js ကလည်း stable support ပေးထားပါတယ်။</p><p>Reference:<br><a href="https://nodejs.org/en/learn/command-line/run-nodejs-scripts-from-the-command-line/">https://nodejs.org/en/learn/command-line/run-nodejs-scripts-from-the-command-line/</a><br><a href="https://stackoverflow.com/questions/57492546/what-is-the-difference-between-js-and-mjs-files">https://stackoverflow.com/questions/57492546/what-is-the-difference-between-js-and-mjs-files</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3d6b518efed8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>