<?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 Andy Nguyen on Medium]]></title>
        <description><![CDATA[Stories by Andy Nguyen on Medium]]></description>
        <link>https://medium.com/@andy200793?source=rss-9858843b357e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*7uiGQC4K7eDu9AS9qdu_gA.jpeg</url>
            <title>Stories by Andy Nguyen on Medium</title>
            <link>https://medium.com/@andy200793?source=rss-9858843b357e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 18:02:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@andy200793/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[How to build serverless cursor-based pagination service on the AWS Cloud]]></title>
            <link>https://medium.com/innomizetech/how-to-build-an-api-with-aws-lambda-to-paginate-data-using-serverless-41c4b6b676a4?source=rss-9858843b357e------2</link>
            <guid isPermaLink="false">https://medium.com/p/41c4b6b676a4</guid>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[lambda]]></category>
            <category><![CDATA[api-gateway]]></category>
            <category><![CDATA[pagination]]></category>
            <category><![CDATA[aws]]></category>
            <dc:creator><![CDATA[Andy Nguyen]]></dc:creator>
            <pubDate>Wed, 20 Nov 2019 04:24:19 GMT</pubDate>
            <atom:updated>2019-11-20T05:51:08.011Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Serverless Cursor-based Pagination on AWS" src="https://cdn-images-1.medium.com/max/1024/1*XoYwpXuDQvjvTdZZN4weew.png" /></figure><p>In this port, I’ll be guiding you on how to build an API with AWS Lambda and API Gateway to paginate data using the <a href="https://serverless.com/">Serverless</a> framework.</p><h3>Technologies stack</h3><ul><li><a href="https://aws.amazon.com/lambda/"><strong>AWS Lambda</strong></a> for running code without provisioning or managing servers. You can utilize free-tier to run your service with <strong>zero</strong> cost.</li><li><a href="https://aws.amazon.com/api-gateway/"><strong>AWS API Gateway</strong></a> to create, publish, and secure Rest APIs.</li><li><a href="https://serverless.com/framework/docs/providers/aws/guide/intro/"><strong>Serverless</strong></a><strong> </strong>framework to build and deploy your backend services on the cloud, you can build and deploy a serverless function in minutes.</li></ul><h3>Prerequisites</h3><ul><li>An AWS account:<a href="https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/"> </a><a href="https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/">https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/</a></li><li>Have some experiences on AWS Cloud computing and the Serverless framework.</li></ul><h3>Cursor-based Pagination</h3><p>Have you ever heard this concept, it just is simple a pagination technique but it does not work the same with the classic pagination. Yes, the classic pagination that I think you will be familiar with, it works based on the <strong>offset-limit</strong> concept. An offset is a number that specifies the number of rows to skip before starting to return rows from the query. For simplicity, you want to see the data on page 3 with page size is 10, just skip 20 rows and return the next 10 rows as the result.</p><p><strong>Offset-limit</strong> is one of the most commonly used techniques for data paging. However, there are several drawbacks of using this approach:</p><ul><li>With a database that has large datasets, the offset clause makes slower querying as the number of records increases because the database still read up all records before skipping with offset records and return it.</li><li>In datasets that change frequently, the paged data becomes <strong>unreliable</strong> and possibility returns <strong>duplicate</strong> results.</li></ul><p><strong>Cursor-based pagination</strong> (aka keyset pagination<strong>)</strong> works based on a <strong>cursor</strong> that is a unique identifier for a specific record in datasets. We will deal with the cursor as the pointer of the next record that we will receive on the next page. And on subsequent requests, the server returns the cursor until there is no record on the next page.</p><p>There are a couple of approaches to implement cursor-based pagination, below are some interesting articles that I think you should take a look to get more details:</p><ul><li><a href="https://www.sitepoint.com/paginating-real-time-data-cursor-based-pagination/">Evolving API Pagination at Slack</a></li><li><a href="https://jsonapi.org/profiles/ethanresnick/cursor-pagination/">“Cursor Pagination” Profile</a></li><li><a href="https://slack.engineering/evolving-api-pagination-at-slack-1c1f644f8e12">Evolving API Pagination at Slack</a></li></ul><p><strong>Advantages</strong>:</p><ul><li>Prevent many of the drawbacks of classic pagination that I have mentioned above.</li><li>Scale well with the large datasets.</li></ul><p><strong>Disadvantages</strong>:</p><ul><li>The end-user cannot see the total number of items in the list.</li><li>The end-user cannot jump to a specific page or back to the previous page.</li></ul><p>With the above benefits, I chose <strong>cursor-based pagination</strong> for this implementation.</p><p>In this demo, I will show you how to implement cursor-based pagination using a unique and sequential column of the table (i.e. auto-increment column). Yeah, it is pretty simple and easy to understand.</p><h3>Serverless</h3><blockquote>The Serverless Framework helps you develop and deploy your AWS Lambda functions, along with the AWS infrastructure resources they require. It’s a CLI that offers structure, automation and best practices out-of-the-box, allowing you to focus on building sophisticated, event-driven, serverless architectures, comprised of <a href="https://serverless.com/framework/docs/providers/aws/guide/intro/#functions">Functions</a> and <a href="https://serverless.com/framework/docs/providers/aws/guide/intro/#events">Events</a>.</blockquote><p>There are ways to create and deploy an AWS Lambda function by using AWS Cloudformation or AWS Console. But I chose serverless because of its convenience, automation and the ability to increase development time.</p><p>One most of my favorite Serverless feature is the <a href="https://serverless.com/plugins/">plugin</a>, some of the plugins I recommend to use in this <a href="https://blog.innomizetech.com/2019/11/06/top-serverless-plugins-we-are-using/">article</a>. In this guide, I will use <a href="https://github.com/prisma-labs/serverless-plugin-typescript">serverless-plugin-typescript</a> and <a href="https://github.com/dherault/serverless-offline">serverless-offline</a> for building Serverless applications.</p><ul><li><a href="https://github.com/prisma-labs/serverless-plugin-typescript"><strong>serverless-plugin-typescript</strong></a><strong>:</strong> this plugin helps us to compile typescript during development and deployment. I like this plugin in usage with zero-configuration but there is an issue that I can’t solve during using this plugin is <a href="https://www.typescriptlang.org/docs/handbook/module-resolution.html"><strong>path resolver</strong></a>. You have to use relative import and make your code becomes lacking in aesthetics. To use non-relatitve import you can use another option such as <a href="https://github.com/serverless-heaven/serverless-webpack">https://github.com/serverless-heaven/serverless-webpack</a>.</li><li><a href="https://github.com/dherault/serverless-offline"><strong>serverless-offline</strong></a><strong>:</strong> emulates <a href="https://aws.amazon.com/lambda/">AWS Lambda</a> and <a href="https://aws.amazon.com/api-gateway">API Gateway</a> on your local machine to speed up your development cycles. If you are using serverless this plugin be indispensable during testing on local.</li></ul><p>With the serverless application, we must define a template file for the definition of your AWS resources, configurations, list of plugins, etc.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d68b6546efd81ed476522aad06395d2c/href">https://medium.com/media/d68b6546efd81ed476522aad06395d2c/href</a></iframe><p>In the functions section, we will link to the file that contains code for reading data with pagination. Before implementing code for that function, we have to create a database and datasets for testing purposes (should have more 50 items in the datasets). I will use the MySQL database and you can choose others such as MSSQL or PostgreSQL, etc for creating the database and preparing datasets.</p><p>Continue, to connect to the database we can install this <a href="https://github.com/mysqljs/mysql">package</a>, it is an awesome Node.js module for communicating with the MySQL database. However, the AWS Lambda function scales by creating separate instances for each concurrent request (you can see <a href="https://docs.aws.amazon.com/lambda/latest/dg/scaling.html">here</a> for more details). And this is a serious problem for the RDBS database like MySQL when we can get the maximum connection error anytime with high concurrent requests.</p><p>Luckily, Jeremy Daly has built a <a href="https://github.com/jeremydaly/serverless-mysql">package</a> that is wrapped for <a href="https://github.com/mysqljs/mysql">MySQL</a> to solve the above problem. We implement code for database helper as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c44b5bce60e1e25a725d910187f65967/href">https://medium.com/media/c44b5bce60e1e25a725d910187f65967/href</a></iframe><p>We create a file to build a query for reading data support cursor-based pagination:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ee3dff88257d8f5e0ee7d91f3dd4f5d2/href">https://medium.com/media/ee3dff88257d8f5e0ee7d91f3dd4f5d2/href</a></iframe><p>As you see above code, I use the <a href="https://hiddentao.github.io/squel/index.html">squel</a> to build the query string, you also manual to build the raw query. But I recommend you to use the <strong>squel</strong> by its benefits like:</p><ul><li>Supports parameterized queries for safe value escaping.</li><li>Supports Non-standard SQL.</li><li>Uses method chaining for ease of use.</li></ul><p>Finally, implement code for the function as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d03dc0e0d0d18ef63feb186243627c60/href">https://medium.com/media/d03dc0e0d0d18ef63feb186243627c60/href</a></iframe><p>The API we develop accepts two params:</p><ul><li>limit: a number value to limit records that return from the server.</li><li>cursor: a base64 string that is returned from the previous request. It indicates the starting point to retrieve data for the next page.</li></ul><p>For testing, I will run the following command to start API on local:</p><pre>sls offline</pre><p>Let’s explore our work. Using the <strong>Postman</strong>, we can easier invoke API</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lSbm96ySX3d8HMwTAEQGQQ.png" /></figure><h3><strong>Deploy</strong></h3><p>Deployment is much easier with the <strong>Serverless</strong> framework and can be done with one single command. So let’s deploy and see results by running below command:</p><pre>sls deploy --stage [your-stage] --region [your-region]</pre><p>Feel free to check out your deployment and try to invoke API Gateway on the AWS and compare results. I hope it works for you. If not, please let me know.</p><p>The source code is available on our <a href="https://github.com/innomizetech/pagination-serverless">Github</a></p><h3>Conclusion</h3><p>I hope this guide will be helpful to you and bring to you some ideas to help you build your services with cost-optimized, secured, flexible, high availability.</p><p>In this guide, I only write a simple code to show how to build and deploy. You are free to extend this project with some additional features as below:</p><ul><li>Supports more param options such as <em>fields</em>, <em>sort by</em> …</li><li>Implementing classic pagination (offset-limit pagination).</li><li>Implementing the error handler for handling such as <strong>InvalidCursorError</strong> or <strong>UnexpectedError</strong>.</li><li>Uses other databases such as <a href="https://aws.amazon.com/dynamodb/">Amazon DynamoDB</a> is a great option.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=41c4b6b676a4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/innomizetech/how-to-build-an-api-with-aws-lambda-to-paginate-data-using-serverless-41c4b6b676a4">How to build serverless cursor-based pagination service on the AWS Cloud</a> was originally published in <a href="https://medium.com/innomizetech">INNOMIZE</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a CRUD application with Angular using Ngrx (Part 2)]]></title>
            <link>https://medium.com/innomizetech/building-a-crud-application-with-angular-using-ngrx-part-2-d13ad09c86f6?source=rss-9858843b357e------2</link>
            <guid isPermaLink="false">https://medium.com/p/d13ad09c86f6</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Andy Nguyen]]></dc:creator>
            <pubDate>Thu, 14 Nov 2019 02:58:17 GMT</pubDate>
            <atom:updated>2019-11-19T02:21:16.972Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/953/1*CtXgX7LzUH0EpxN84rntKw.png" /></figure><p>In the <a href="https://medium.com/innomizetech/building-a-crud-application-with-angular-using-ngrx-part-1-725af50f1548">previous part</a>, I have shown how to build a sample mockup API for CRUD application as well as build a sample list page. In this part, we will continue to build CRUD application on creating, updating and deleting an entity using Ngrx.</p><p>Before continuing, I want to introduce to you a special thing is “<strong>Optimistic UI</strong>”.</p><h3>What is O<strong>ptimistic UI?</strong></h3><blockquote>Optimistic UI is a pattern that you can use to simulate the results of a mutation and update the UI even before receiving a response from the server. Once the response is received from the server, the optimistic result is thrown away and replaced with the actual result ~ <a href="https://www.apollographql.com/docs/react/performance/optimistic-ui/">Apollo</a>.</blockquote><p>Let’s remember the previous approach for handling update operation (I call this approach is <strong>Synchronous UI</strong>):</p><ul><li>The end-user click update action.</li><li>The end-user click update action.</li><li>Make an HTTP request for the update operation.</li><li>Displays a spinner (or loading image) for waiting for the success response from the server.</li><li>After the server responses successfully, we continue to refresh data on UI to reflect with the updated result for consistency. Otherwise failed, display an error message.</li></ul><p>And if we apply the <strong>Optimistic UI</strong> approach:</p><ul><li>The end-user click update action.</li><li>Make an HTTP request for updating operation and the UI value is updated immediately assuming the response will be successful.</li><li>After the server responses successfully, we do not anything. Otherwise failed, revert the UI value as the previous state.</li></ul><p>As you see two above approaches, the <strong>Synchronous UI </strong>approach needs a step that showing a spinner for waiting for the server’s response and the UI just is updated after the server is responded. In most cases, there is no reason to wait for a successful response when we assume that all requests are expected to be successful. Like that, the <strong>Optimistic UI</strong> approach brings to the end-user a faster user experience, more smooth and stable.</p><p>Now, we will go dive to implement CRUD application with optimistic interaction.</p><p>As part 1, we have built the CRUD application to read Entity from the server and display it using Ngrx. Before we implement the update Entity feature, we need update code for some files as preparation.</p><p>Update the <em>entity.service.ts</em> file as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1a51e0eee7beb8c69cb6785853780d5c/href">https://medium.com/media/1a51e0eee7beb8c69cb6785853780d5c/href</a></iframe><p>Update <em>entities.page.html</em> to add some action links:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1abb44404a20481fa68d15164bc14a0/href">https://medium.com/media/a1abb44404a20481fa68d15164bc14a0/href</a></iframe><h3>Update Entity</h3><p>First, we update <em>entity.actions.ts</em> file to add some actions as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7d358ffcda4f4d247b797ccc5e03e706/href">https://medium.com/media/7d358ffcda4f4d247b797ccc5e03e706/href</a></iframe><p>Next, update the <em>entity.reducer.ts</em> file as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a2b614e51e8f4825d4b4e318a0b5685/href">https://medium.com/media/7a2b614e51e8f4825d4b4e318a0b5685/href</a></iframe><p>Update the<em> entity.effects.ts</em> file as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2a5481fa2d870da4875ea51099693a5e/href">https://medium.com/media/2a5481fa2d870da4875ea51099693a5e/href</a></iframe><p>And update the <em>entity.selectors.ts</em> file as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dacf39dc259b510a3abc30e0e13ba585/href">https://medium.com/media/dacf39dc259b510a3abc30e0e13ba585/href</a></iframe><p>As you can see above the action file, we define an additional action is “Load Entity” for reading Entity detail from the server or the available store and display it for the user to edit. If you click edit on List of Entities page, just load data from the store that is loaded before. In case the user pastes the editing Entity page URL direct to the browser’s address, an HTTP request will be called to retrieving Entity detail instead of.</p><p>In the reducer file, you will see the “<strong>updateEntity</strong>” action will update immediately the state reflects with the updated entity even the server does not respond yet. And we need an action to revert the store to the previous state for case unsuccessful response. I used <a href="https://github.com/brechtbilliet/ngrx-undo"><strong>ngrx-undo</strong></a> to handle this case, the <a href="https://github.com/brechtbilliet/ngrx-undo"><strong>ngrx-undo</strong></a> provides a wrapper function to revert a specified action with the simple configuration, you can see I have used this library in the entity.effects.ts file for error handling section. To install this library you can refer to this <a href="https://github.com/brechtbilliet/ngrx-undo">link</a>.</p><p>Ok, we will develop a page for Entity editing as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/720dd2dc464e8bec6d425202c6582e84/href">https://medium.com/media/720dd2dc464e8bec6d425202c6582e84/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/16cacd00be0a0716e0b9ff98a3e186a0/href">https://medium.com/media/16cacd00be0a0716e0b9ff98a3e186a0/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b89270d11737f6dd7b53718b0858b5c/href">https://medium.com/media/8b89270d11737f6dd7b53718b0858b5c/href</a></iframe><p>And don’t forget to define the route for Entity editing page:</p><pre>{</pre><pre>    path: &quot;edit/:id&quot;,</pre><pre>    component: EntityPage,</pre><pre>    resolve: {</pre><pre>        entity: EntityResolver</pre><pre>    }</pre><pre>}</pre><p>Gives the result as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/793/1*RFpOiO63rE0PklEwx7X99Q.png" /><figcaption>Entities page</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/793/1*DVVfHR3qa_e-bJp4t05gEg.png" /><figcaption>Entity editing page</figcaption></figure><h3>Create an Entity</h3><p>Now to create the Entity in the server, we need to perform HTTP POST operation and pass the data that needs to be inserted. We will take a “<strong>createEntity</strong>” action for that.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/44530a21645a7bf9e8e618d5ad7264eb/href">https://medium.com/media/44530a21645a7bf9e8e618d5ad7264eb/href</a></iframe><p>Update reducer and effect files to add some code as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1c2fa4e7147d994b0b7dd7a77414872a/href">https://medium.com/media/1c2fa4e7147d994b0b7dd7a77414872a/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ccaebebc46ba2b3596cfcc286507947/href">https://medium.com/media/0ccaebebc46ba2b3596cfcc286507947/href</a></iframe><p>And we can reuse the Entity editing page for creation operation, update code the entity.page.ts file as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/00f53852234480399d5aeede350480ac/href">https://medium.com/media/00f53852234480399d5aeede350480ac/href</a></iframe><h3>Delete Entity</h3><p>Similarly, we only need to define “<strong>deleteEntity</strong>” action and call the action on the delete method that we have added before:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/83bbcde82706da7b61dfe226d83a4b50/href">https://medium.com/media/83bbcde82706da7b61dfe226d83a4b50/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b3798f5826488f0fc94100386ff8b1a0/href">https://medium.com/media/b3798f5826488f0fc94100386ff8b1a0/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf5d14eb1a391eefa2453554aee4f356/href">https://medium.com/media/bf5d14eb1a391eefa2453554aee4f356/href</a></iframe><p>Implement the delete method as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1ed6d84a92bf4b92412221e5c4b36d5/href">https://medium.com/media/e1ed6d84a92bf4b92412221e5c4b36d5/href</a></iframe><p>Done, enjoy the result as below:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fcrud-angular-ngrx-part-2-iczk9%3Ffontsize%3D14&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fcrud-angular-ngrx-part-2-iczk9%3Ffontsize%3D14&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Ficzk9%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/9091d2acdc1dc46706affed8bdbfb149/href">https://medium.com/media/9091d2acdc1dc46706affed8bdbfb149/href</a></iframe><h3>Summary</h3><p>Coming to the end of this guide, let us have a quick recap of what we did.</p><ul><li>Use <strong>Optimistic UI</strong> approach to make the application faster and smoother.</li><li>Use the <strong>ngrx-undo</strong> library for reverting the store as the previous state on the unsuccessful response case.</li></ul><p>Thank for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d13ad09c86f6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/innomizetech/building-a-crud-application-with-angular-using-ngrx-part-2-d13ad09c86f6">Building a CRUD application with Angular using Ngrx (Part 2)</a> was originally published in <a href="https://medium.com/innomizetech">INNOMIZE</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a CRUD application with Angular using Ngrx (Part 1)]]></title>
            <link>https://medium.com/innomizetech/building-a-crud-application-with-angular-using-ngrx-part-1-725af50f1548?source=rss-9858843b357e------2</link>
            <guid isPermaLink="false">https://medium.com/p/725af50f1548</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Andy Nguyen]]></dc:creator>
            <pubDate>Tue, 12 Nov 2019 10:51:23 GMT</pubDate>
            <atom:updated>2019-11-19T02:23:38.203Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/953/1*CtXgX7LzUH0EpxN84rntKw.png" /></figure><p>In this guide, we just focus on how to build a CRUD application with Angular (version 8) that has already installed the <a href="https://ngrx.io/">Ngrx</a>, not on setting up an Angular application or installing the Ngrx library. You can follow the following guides to do those task:</p><ul><li>Set up an Angular application: <a href="https://codeburst.io/how-to-build-an-angular-app-with-angular-cli-in-a-couple-of-minutes-43089d3ab272">https://codeburst.io/how-to-build-an-angular-app-with-angular-cli-in-a-couple-of-minutes-43089d3ab272</a></li><li>Install the Ngrx library: <a href="https://ngrx.io/guide/store/install">https://ngrx.io/guide/store/install</a></li></ul><p>If you have no idea what is Ngrx, I recommend you to read <a href="https://ngrx.io/docs">this</a> to explore more about it. And if you have a basic knowledge of <a href="https://redux.js.org/">Redux</a>, I think you will be easy to approach this library.</p><p>In this part, we will learn how to define actions, reducer, selectors and build a page to display all items that are selected from the store using the <a href="https://ngrx.io/">Ngrx</a>.</p><p>In almost any application that we work with, we all encounter operations such as retrieving data, rendering it as a table view, create/update or delete a row on that view. And this data which is stored on the server is dealt with by the usage of HTTP requests (GET, POST, etc).</p><p>The first thing we need to do in here is where the data is stored that we will get through an HTTP API request. Because this article does not focus on this, I will use a fake backend server for mock. There are different ways to use a fake back-end server are:</p><ul><li>Use Angular In-Memory-Web-API</li><li>Create a local JSON file and use it</li><li>Or hard-code the data, and return this data.</li></ul><p>But I have another easy way, say big thanks to <a href="https://getsandbox.com/">https://getsandbox.com</a>. Just provide some rows of code, we already have a RESTful API for mock or testing. I have created a mock RESTful API on <a href="https://getsandbox.com/">https://getsandbox.com</a> for CRUD on the Entity object as below:</p><pre><strong>Fake RESTful APIs: (CRUD)</strong><br><strong>GET</strong>:    https://entity.getsandbox.com/entities/     (<strong>Get all</strong>)<br><strong>GET</strong>:    https://entity.getsandbox.com/entities/{id} (<strong>Get one</strong>)<br><strong>POST</strong>:   https://entity.getsandbox.com/entities/     (<strong>Create</strong>)<br><strong>PUT</strong>:    https://entity.getsandbox.com/entities/{id} (<strong>Update</strong>)<br><strong>DELETE</strong>: https://entity.getsandbox.com/entities/{id} (<strong>Delete</strong>)</pre><p>Ok, we have done on prerequisite items. Next, we’ll go together to define actions, reducer, and selectors to fetch Entity objects from the fake server I have created before and store it into the Ngrx store. If you have developed with a React Redux application before, all fundamental principles about Redux (action, reducer, selector, etc) will be similar to the Ngrx.</p><p>First, we will define a class that describes all properties of the Entity object we will deal with:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/46878d956ba8b1e3a8ca4fd7d84fb2ad/href">https://medium.com/media/46878d956ba8b1e3a8ca4fd7d84fb2ad/href</a></iframe><p>Let’s define some actions for loading entities as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f49fae10a32e8c4f31bdc53d1f993ea9/href">https://medium.com/media/f49fae10a32e8c4f31bdc53d1f993ea9/href</a></iframe><p>Next, we’ll define the Entity’s reducer. Here, we have to define an initial state and specify how the state is changed for each action is sent to the store.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4cfbc9eb419159b803781db3b805ac05/href">https://medium.com/media/4cfbc9eb419159b803781db3b805ac05/href</a></iframe><p>Continue define selectors:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c42cc7e793effb2de57c5c68dcbb7bf9/href">https://medium.com/media/c42cc7e793effb2de57c5c68dcbb7bf9/href</a></iframe><p>Create a service file to fetch data from the server:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8729b3dce32073ffe7985bf27db9ccae/href">https://medium.com/media/8729b3dce32073ffe7985bf27db9ccae/href</a></iframe><p>To read data from the server, we need to create a side effect to make an HTTP request to the server and store data into the store through Ngrx actions. With React, <a href="https://redux-saga.js.org/">Saga</a> can handle side effects for us. And the Ngrx also has provided the <strong>Effect </strong>concept that is the same as the Saga. As the Ngrx documentation: “Effects are an RxJS powered side effect model for <a href="https://ngrx.io/guide/store">Store</a>. Effects use streams to provide <a href="https://martinfowler.com/eaaDev/EventSourcing.html">new sources</a> of actions to reduce state based on external interactions such as <strong>network requests</strong>, web socket messages and time-based events.” So that we will use it for reading Entity data from the server.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7804b1de8a16095ec05f44f8dc48cce2/href">https://medium.com/media/7804b1de8a16095ec05f44f8dc48cce2/href</a></iframe><p>Finally, creating an Angular page (with route is “/entities”) to display a list of the entity that are fetched from the server. For easy to navigate to entities page, we create a link for quick navigation in app.component.html:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/63fa20b8aa19dfed3167ec9f30fe227c/href">https://medium.com/media/63fa20b8aa19dfed3167ec9f30fe227c/href</a></iframe><p>In entities.page.ts, the store will dispatch loadEntities action and the effect will be triggered to fetch data from server and store data into the store. Since the data is loaded to the store, we only get data from the store and display it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d77d82c1c9d1884e35a35001cafda1df/href">https://medium.com/media/d77d82c1c9d1884e35a35001cafda1df/href</a></iframe><p>The entity template HTML file:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5fe3515b4c682737df88cc8568546571/href">https://medium.com/media/5fe3515b4c682737df88cc8568546571/href</a></iframe><p>Note: Ensure that you have already imported CommonModule to use the async pipe.</p><p>Gives the result as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/798/1*RDuR2K-MBGNfDwI3dzKgxg.png" /><figcaption>Reading Entity data from the server</figcaption></figure><p>The operation for reading Entity data from the server has completed. But you will see the entities page display empty page before render list of Entity objects. This is cause of waiting from the server for reading data and it should be better for the user’s experience if a spinner display instead of. That is a good suggestion since I know Angular <a href="https://angular.io/api/router/Resolve">Resolve</a>. Resolve will wait for the data to be resolved before the page is finally navigated.</p><p>So I will define the resolver for entities page as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bdaf25a1517482f5d95c2b5edd400602/href">https://medium.com/media/bdaf25a1517482f5d95c2b5edd400602/href</a></iframe><p>As above code, the entities page only is activated if the loaded flag is true. Also, I have added a filter operator to dispatch loadEntities action if the loaded flag is false. So the application only makes an HTTP request only the loaded flag is false, this reduces significantly backend traffic during the application navigation.</p><p>Update the entities.page.ts to comment or remove the following code:</p><pre><em>// this.store.dispatch(fromEntityActions.loadEntities());</em></pre><p>Continue to add the resolve to the route configuration:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cf11f76d8eae92a5f90f6ea5513ed4b1/href">https://medium.com/media/cf11f76d8eae92a5f90f6ea5513ed4b1/href</a></iframe><p>Enjoy the result:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Fcrud-angular-ngrx-part-1-0k2ke%3Ffontsize%3D14&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Fcrud-angular-ngrx-part-1-0k2ke%3Ffontsize%3D14&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2F0k2ke%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/67169ff59788ce9f7db62a2046802546/href">https://medium.com/media/67169ff59788ce9f7db62a2046802546/href</a></iframe><h3>Summary</h3><p>Coming to the end of this guide, let us have a quick recap of what we did.</p><ul><li>Use <a href="https://getsandbox.com/">https://getsandbox.com/</a> to quick create a fake server for mocking and testing.</li><li>Use <strong>createAction</strong>, <strong>createReducer, </strong>and <strong>createFeatureSelector </strong>for creating actions, reducer, selectors.</li><li>Use <strong>Ngrx Effect</strong> for handle side effects to make an HTTP call for retrieving data from the server.</li><li>Use <strong>Angular Resolver</strong> to ensure that data has already loaded before the page is navigated.</li></ul><p>I hope this tutorial will be helpful to you :D</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=725af50f1548" width="1" height="1" alt=""><hr><p><a href="https://medium.com/innomizetech/building-a-crud-application-with-angular-using-ngrx-part-1-725af50f1548">Building a CRUD application with Angular using Ngrx (Part 1)</a> was originally published in <a href="https://medium.com/innomizetech">INNOMIZE</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>