<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Flavien Normand</title>
    <description>The latest articles on DEV Community by Flavien Normand (@supamiu).</description>
    <link>https://dev.to/supamiu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F281728%2F58ae767a-ac53-473e-b3f3-8773f112178e.jpg</url>
      <title>DEV Community: Flavien Normand</title>
      <link>https://dev.to/supamiu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/supamiu"/>
    <language>en</language>
    <item>
      <title>Realtime Database or Cloud Firestore, when and why?</title>
      <dc:creator>Flavien Normand</dc:creator>
      <pubDate>Thu, 05 Jan 2023 15:18:12 +0000</pubDate>
      <link>https://dev.to/zenika/realtime-database-or-cloud-firestore-when-and-why-2f3l</link>
      <guid>https://dev.to/zenika/realtime-database-or-cloud-firestore-when-and-why-2f3l</guid>
      <description>&lt;p&gt;When using Firebase, wether it's for the web or mobile, you'll eventually come to this question: Should I use realtime Database (rtdb) or Cloud Firestore to store this?&lt;/p&gt;

&lt;p&gt;In this article we're not going to find the answer, we'll find the right questions for you to get the answer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Realtime Database
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZW327nkO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gc4qbo26m0z96re6447g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZW327nkO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gc4qbo26m0z96re6447g.png" alt="Realtime Database" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Firebase Realtime Database, also known as "rtdb", is a realtime key:value database. It's very performant for small data operations and has good transactions capabilities to integrate with Cloud Functions easily. To simplify to the extreme, you can see rtdb as a big JSON where you can observe any property at any depth.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pricing
&lt;/h3&gt;

&lt;p&gt;Realtime database's pricing is based on traffic, the cost being $1/GB transferred after the first 10GB (the free tier). Storage cost is also $5/GB after the first GB that's in free tier.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cloud Firestore
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kI02aCf9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kd0cc7glvlacrqy4tor6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kI02aCf9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kd0cc7glvlacrqy4tor6.png" alt="Cloud Firestore" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cloud Firestore is a realtime document database, it scales very well with big amounts of data and documents, allowing stuff like indexes and querying with an easier approach than rtdb. To simplify to the extreme, it's realtime mongodb with less features.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pricing
&lt;/h3&gt;

&lt;p&gt;Cloud Firestore's pricing is based on the # of operations you'll do on the database. These prices are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;$0.06 per 100K read (50K free)&lt;/li&gt;
&lt;li&gt;$0.18 per 100k write (20K free)&lt;/li&gt;
&lt;li&gt;$0.02 per 100k delete (20k free)&lt;/li&gt;
&lt;li&gt;$0.18 per GB stored (1GB free)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Ok, cool, but which one to pick?
&lt;/h2&gt;

&lt;p&gt;Now that we have elements about these databases, it's easier to pick one, mainly because these are designed to be cheap if properly used for that they're designed for.&lt;/p&gt;

&lt;p&gt;For instance, store big documents that you won't update often on realtime database and your pricing will explode.&lt;/p&gt;

&lt;p&gt;My general rules are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Counters, realtime gauges and other things that need to be updated a lot should go on realtime database.&lt;/li&gt;
&lt;li&gt;Larger documents should go on Firestore.&lt;/li&gt;
&lt;li&gt;If it's a large document and it should be updated often, consider splitting it into what will be updated often and what won't.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Which gives us these questions when picking which database to use to store something in Firebase:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is it going to be updated more than once per second? 
Yes: RTDB, No: Firestore&lt;/li&gt;
&lt;li&gt;Is it larger than a couple of primitive fields?
Yes: Firestore, No: RTDB&lt;/li&gt;
&lt;li&gt;Do you need to search through your data?
If yes, well Firestore but you should definitely use the algolia extension for that !&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In conclusion, make sure to pick the right database for the right thing if you don't want to be one of these nightmare stories people tell you when you're talking about firebase. Big things go to Firestore, things that need frequent updates go to Realtime Database.&lt;/p&gt;

</description>
      <category>cloud</category>
      <category>firebase</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Handle concurrency in Cloud Firestore with a single boolean</title>
      <dc:creator>Flavien Normand</dc:creator>
      <pubDate>Mon, 02 Jan 2023 09:19:41 +0000</pubDate>
      <link>https://dev.to/zenika/handle-concurrency-in-cloud-firestore-with-a-single-boolean-h3f</link>
      <guid>https://dev.to/zenika/handle-concurrency-in-cloud-firestore-with-a-single-boolean-h3f</guid>
      <description>&lt;p&gt;Working with Cloud Firestore really feels amazing, realtime, scalability, easy to use, nice SDKs !&lt;/p&gt;

&lt;p&gt;Then you stumble on concurrency, and it hurts. Cuncurrency has always been a big issue in many online apps, and there's a lot of ways to handle it, today i'm going to present to you one of the easiest ways I've ever seen, using a boolean that's quite hard to find when you just google "firestore concurrency" stuff.&lt;/p&gt;

&lt;h2&gt;
  
  
  First of all, the problem with concurrency
&lt;/h2&gt;

&lt;p&gt;Let's imagine this flow of actions, with two users: A and B, both observing the same document (&lt;code&gt;{ "name": "bar", "size": 10}&lt;/code&gt;) and editing it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A edits field "name" to set it to "foo".&lt;/li&gt;
&lt;li&gt;B edits field "size" to set it to 25.
These actions happen with less than a second between each other.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because of the Firestore restriction that limits write ops to 1/s/doc (a given document can only be edited once per second), this is what B will see, in sequence:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; &lt;code&gt;{ "name": "bar", "size": 10}&lt;/code&gt; // First read&lt;/li&gt;
&lt;li&gt; &lt;code&gt;{ "name": "bar", "size": 25}&lt;/code&gt; // Optimistic local write&lt;/li&gt;
&lt;li&gt; &lt;code&gt;{ "name": "foo", "size": 10}&lt;/code&gt; // Result from A's edit&lt;/li&gt;
&lt;li&gt; &lt;code&gt;{ "name": "foo", "size": 25}&lt;/code&gt; // Server result from B's edit&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As you can see here, the user will see the data flicker from previous to new state, and this is because we don't filter on "wait until all operations are done".&lt;/p&gt;

&lt;h2&gt;
  
  
  The hasPendingWrites boolean
&lt;/h2&gt;

&lt;p&gt;Firebase Web JS SDK has a nice tool to handle this kind of issues: &lt;code&gt;hasPendingWrites&lt;/code&gt; (&lt;a href="https://firebase.google.com/docs/reference/js/firestore_.snapshotmetadata.md#snapshotmetadatahaspendingwrites"&gt;see more details in docs&lt;/a&gt;). What this does it tell us if the snapshot we're getting can be considered as stable or not for current client, as it won't be stable if it has pending writes.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This is even available in other Firebase SDKs but we're not going to cover them here.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How to use it
&lt;/h3&gt;

&lt;p&gt;Here I'm going to give an example with &lt;code&gt;@angular/fire&lt;/code&gt;'s approach, using rxjs, but you can use it from any JS SDK library, or the SDK itself if you want to, as it's a SDK-level property.&lt;/p&gt;

&lt;p&gt;With RxJS, you can simply use the &lt;code&gt;filter&lt;/code&gt; operator on your snapshot listener:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;docSnapshots&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;firestore&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;exampleCollection&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;exampleKey&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;snap&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;snap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;metadata&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hasPendingWrites&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;snap&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;snap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Et voilà ! You now have a document data observable that will only emit data once all your write operations have been resolved 🎉&lt;/p&gt;

</description>
      <category>firebase</category>
      <category>angular</category>
      <category>serverless</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Optimisations Firebase, moins cher et plus performant</title>
      <dc:creator>Flavien Normand</dc:creator>
      <pubDate>Mon, 18 Jan 2021 16:24:40 +0000</pubDate>
      <link>https://dev.to/zenika/optimisations-firebase-moins-cher-et-plus-performant-1am1</link>
      <guid>https://dev.to/zenika/optimisations-firebase-moins-cher-et-plus-performant-1am1</guid>
      <description>&lt;p&gt;Créé en 2011 et racheté par Alphabet (Google) en 2014, &lt;a href="http://firebase.google.com"&gt;Firebase&lt;/a&gt; se présente aujourd'hui comme une solution Backend as a Service très complète et performante. Son approche temps réel permet des applications très réactives et vivantes sans investissement sur le développement d'une solution backend.&lt;/p&gt;

&lt;p&gt;Malgré cela, vous avez déjà peut-être hésité à l'utiliser, parce que le pricing n'était pas des plus clairs et peut être aussi parce que vous avez pu voir les fameuses firebase horror stories, comme celle-ci: &lt;a href="https://hackernoon.com/how-we-spent-30k-usd-in-firebase-in-less-than-72-hours-307490bd24d"&gt;https://hackernoon.com/how-we-spent-30k-usd-in-firebase-in-less-than-72-hours-307490bd24d&lt;/a&gt; (TL;DR: une entreprise a payé 30k€ à cause d’une mauvaise utilisation de firebase qui lui a créé une situation où la facturation grossissait de manière exponentielle)&lt;/p&gt;

&lt;p&gt;Comme toutes les solutions cloud, une mauvaise utilisation peut conduire à de mauvaises surprises côté facturation, comme le démontre l'article ci-dessus. Nous allons voir ici comment éviter ces pièges en comprenant comment Firebase fonctionne, et comment il vous facture.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TKY20dwz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7yuu18hbrmrlcwz935s8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TKY20dwz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7yuu18hbrmrlcwz935s8.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Realtime Database
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://firebase.google.com/docs/database"&gt;Realtime Database&lt;/a&gt;, c'est &lt;em&gt;la&lt;/em&gt; fonctionnalité de base de Firebase : une base de données sous forme d'un JSON géant dans lequel vous pouvez piocher et observer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Le pricing
&lt;/h2&gt;

&lt;p&gt;Dans Realtime Database, vous êtes facturés au Go transféré, ce qui en fait une base idéale pour stocker des petites données qui changent beaucoup, des indicateurs, des compteurs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ce qui peut mal tourner
&lt;/h2&gt;

&lt;p&gt;Du fait que Realtime Database facture au Go transféré et qu'il propose du temps réel, si vous avez une donnée qui grossi de façon linéaire avec le nombre de vos utilisateurs, alors vous avez un pricing exponentiel. En effet, chaque nouvel utilisateur engendre un listener de plus, et fait grossir la donnée, ce qui envoie la modification aux autres utilisateurs.&lt;/p&gt;

&lt;p&gt;C'est comme ça que #UnaVacaPorDeLaCalle a vu son pricing exploser.&lt;/p&gt;

&lt;h2&gt;
  
  
  Comment éviter ça?
&lt;/h2&gt;

&lt;p&gt;Si vous avez de gros documents, préférez Firestore, et ne faites jamais une récupération d'un tableau complet pour avoir sa longueur, préférez une &lt;a href="https://firebase.google.com/docs/database/extend-with-functions"&gt;Cloud Function&lt;/a&gt; qui ira mettre à jour un compteur à la place, que vous pourrez récupérer facilement.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mL_u9jdm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6joqc1to9r4h68g2k72r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mL_u9jdm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/6joqc1to9r4h68g2k72r.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Firestore
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://firebase.google.com/docs/firestore"&gt;Cloud Firestore&lt;/a&gt;, c'est une base de données NoSQL documents en temps réel. Sortie de beta il y a maintenant un an environ, elle est actuellement disponible dans la suite firebase.&lt;/p&gt;

&lt;h2&gt;
  
  
  Le pricing
&lt;/h2&gt;

&lt;p&gt;Firestore vous fait payer &lt;a href="https://firebase.google.com/docs/firestore/pricing"&gt;par opération sur un document&lt;/a&gt;, et non sur le volume transféré, ce qui ouvre les possibilités pour des volumes de données plus importants.&lt;/p&gt;

&lt;h3&gt;
  
  
  Les limitations
&lt;/h3&gt;

&lt;p&gt;Même si Firestore est plus intéressant pour les gros volumes de données, il faut garder à l'esprit qu'il a des limites que vous ne pouvez pas modifier:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Max. 1 opération par seconde par document.&lt;/li&gt;
&lt;li&gt;Max. 1MB par document.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ce sont les deux plus importantes pour le pricing, &lt;/p&gt;

&lt;h2&gt;
  
  
  Ce qui peut mal tourner, ou pas
&lt;/h2&gt;

&lt;p&gt;Firestore est beaucoup moins sujet à des explosions de prix, de par son pricing par opération et le fait que récupérer une collection ne vous coûte qu'une opération au total et non une opération par document.&lt;/p&gt;

&lt;p&gt;Cependant, il est important de savoir ce qui coûte des opérations, surtout dans les règles, qui peuvent récupérer un document au prix d'une opération.&lt;/p&gt;

&lt;h2&gt;
  
  
  Comment optimiser?
&lt;/h2&gt;

&lt;p&gt;Dans Firestore, l'idée derrière l'optimisation est de réduire le nombre d'opérations de lecture que vous allez faire. Un design idéal veut donc que vous ayez un document qui contient tout ce dont un utilisateur a besoin. Cependant, cela reste à la merci de la limite de l'opération par seconde par document, et donc rend chaque "document utilisateur" modifiable uniquement une fois par seconde.&lt;/p&gt;

&lt;p&gt;Même si vous ne pouvez pas atteindre cet idéal, avoir moins d'opérations de lecture obligatoires (au lancement de votre application, sans interaction utilisateur) est la priorité et peut fortement impacter votre pricing Firestore.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;En conclusion, pensez toujours à l'utilisation de vous allez faire d'une des bases de Firebase et pensez toujours aux anti patterns qui peuvent vous coûter cher afin de les éviter, utilisez les cloud fonctions avec les events de base de données afin de faire des opérations de type count.&lt;/p&gt;

&lt;p&gt;Vous êtes intéressé•e par Firebase et vous voudriez en apprendre plus ? N'hésitez pas à &lt;a href="//mailto:training@zenika.com"&gt;nous contacter&lt;/a&gt; pour une &lt;a href="https://training.zenika.com/fr-fr/training/firebase/description"&gt;formation&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>firebase</category>
      <category>finops</category>
    </item>
    <item>
      <title>Enhance serialization with @kaiu/serializer</title>
      <dc:creator>Flavien Normand</dc:creator>
      <pubDate>Mon, 16 Dec 2019 17:04:39 +0000</pubDate>
      <link>https://dev.to/supamiu/enhance-serialization-with-kaiu-serializer-112d</link>
      <guid>https://dev.to/supamiu/enhance-serialization-with-kaiu-serializer-112d</guid>
      <description>&lt;p&gt;In today's frontend applications, there's one thing we almost all have in common:  we want to serialize JSON strings into objects.&lt;/p&gt;

&lt;p&gt;Thanks to the amazing tool that &lt;code&gt;JSON&lt;/code&gt; is in Javascript, this became an easy and safe way to handle serialization inside our applications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But what if your objects need to be an instance of a class?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/US0y4X7FONHaM/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/US0y4X7FONHaM/giphy.gif" alt="What?"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In a project, we had an issue that I think many developpers are facing: having methods inside objects that you're getting from an API.&lt;/p&gt;

&lt;p&gt;Let's say you have a &lt;code&gt;Foo&lt;/code&gt; class inside your typescript application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Foo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;baz&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nx"&gt;getBarBaz&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;}${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;baz&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you get an object that comes from an endpoint in which you're storing foos, you want the models to have the &lt;code&gt;barBarBaz()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Let's say you have this JSON coming from an API:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"bar"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"example"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"baz"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You will want it to end up as an instance of &lt;code&gt;Foo&lt;/code&gt;, so you'll end up having to create manual mapping for each class you want to persist inside the API, something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Foo&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;APIModel&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;baz&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nx"&gt;getBarBaz&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;}${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;baz&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;hydrateFromJSONObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Partial&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Foo&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="nx"&gt;Foo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;baz&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;baz&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But then you start to work with more complex structures, with complex field that are class instances themselves, or even arrays of class instances. And things start to become quite difficult to handle without having to write a lot of repetitive code.&lt;/p&gt;

&lt;h2&gt;
  
  
  But there's a solution !
&lt;/h2&gt;

&lt;p&gt;Because we found this hard to maintain, we started thinking about a solution that uses the power of decorators to make all this easier for everyone. &lt;/p&gt;

&lt;p&gt;That's why we created &lt;a href="https://www.npmjs.com/package/@kaiu/serializer"&gt;&lt;strong&gt;@kaiu/serializer&lt;/strong&gt;&lt;/a&gt;, a simple library to deal with this easily, using simple decorators.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Serializer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@kaiu/serializer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;serializer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Serializer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deserialize&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Foo&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jsonString&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getBarBaz&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// Will properly print "example0" if given the example json.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There is some super cool decorators for specific use cases, like &lt;code&gt;@FieldName&lt;/code&gt; (&lt;a href="https://kaiu-lab.github.io/serializer/globals.html#fieldname"&gt;docs&lt;/a&gt;) which allows you to map a field to a different name in your application, or &lt;code&gt;@DeserializeAs&lt;/code&gt; (&lt;a href="https://kaiu-lab.github.io/serializer/globals.html#deserializeas"&gt;docs&lt;/a&gt;) which allows you to map a field to a class instance.&lt;/p&gt;

&lt;p&gt;Some advanced use cases are also implemented, like inheritance handling, so you can have a parent class with multiple children classes, the serializer will be able to use the matching child class based on a given field value (&lt;a href="https://kaiu-lab.github.io/serializer/#discriminant-field"&gt;docs&lt;/a&gt;)&lt;/p&gt;

&lt;h4&gt;
  
  
  For the angular devs
&lt;/h4&gt;

&lt;p&gt;As our initial use case was for an Angular application, we created a simple wrapper that provides the serializer as a service in a module, with easy configuration injection. You can find it on npmjs: &lt;a href="https://www.npmjs.com/package/@kaiu/ng-serializer"&gt;@kaiu/ng-serializer&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Feel free to star &lt;a href="https://github.com/kaiu-lab/serializer"&gt;the project on github&lt;/a&gt; or create issues and PRs, they are always welcome 😄&lt;/p&gt;

&lt;p&gt;If you have any question, please don't hesitate to ask them in the comments or ping me on twitter: &lt;a href="https://twitter.org/Supamiu_"&gt;@Supamiu_&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>serialization</category>
      <category>angular</category>
    </item>
    <item>
      <title>Apollo on Angular, almost a love story</title>
      <dc:creator>Flavien Normand</dc:creator>
      <pubDate>Wed, 11 Dec 2019 13:04:45 +0000</pubDate>
      <link>https://dev.to/supamiu/apollo-on-angular-almost-a-love-story-5ao</link>
      <guid>https://dev.to/supamiu/apollo-on-angular-almost-a-love-story-5ao</guid>
      <description>&lt;h1&gt;
  
  
  Apollo
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://www.apollographql.com/"&gt;Apollo&lt;/a&gt; is a (if not the) graphQL client for frontend applications.&lt;/p&gt;

&lt;p&gt;It is available for React, Angular, Vue and many more, with great docs available in order to get started easily and understand how to use its power.&lt;/p&gt;

&lt;h2&gt;
  
  
  Apollo-angular
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.apollographql.com/docs/angular/"&gt;Apollo-angular&lt;/a&gt; is the Angular wrapper for the apollo client. It fits particularly well with Angular because all its API was designed to use Observable and reactive paradigm. If you use Angular daily you should know how much it uses Observables and how it can be annoying when an external API doesn't. Apollo-angular will help you to develop efficiently with Apollo and Angular, by providing everything you need the way you need it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;p&gt;When getting started on apollo-angular, you can simply follow the example shown in the &lt;a href="https://www.apollographql.com/docs/angular/basics/setup/"&gt;docs&lt;/a&gt;, which will allow you to install the library, then configure its module and finally provide a factory for Apollo.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;h2&gt;
  
  
  But then, it's done?
&lt;/h2&gt;

&lt;p&gt;Even if this example is perfect in order to get you started with the library, it is far from being good for production use, because of how you have to &lt;a href="https://www.apollographql.com/docs/react/networking/authentication/#header"&gt;setup authorization header&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The main reason being that if your application needs to change something on the client's configuration, you won't be able to create a new Apollo instance, instead you will get an error saying &lt;code&gt;apollo has been already created&lt;/code&gt;. This example is only good if your token has no expiration date, or if it's shorter than your application's life (if it's not meant to be opened for this long).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--t8vpe2Vd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://www.reactiongifs.com/r/but-why.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--t8vpe2Vd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://www.reactiongifs.com/r/but-why.gif" alt="But why?"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The reason for this is that Apollo client has to be single instance, it is made to be singleton and once you created one, especially in an Angular context, where everything is a service, it's hard to delete it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The solution
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Context
&lt;/h4&gt;

&lt;p&gt;In my case, I'm using &lt;a href="https://firebase.google.com/"&gt;Firebase&lt;/a&gt; for the authentication part, in order to query my graphQL API, created using &lt;a href="https://hasura.io/"&gt;Hasura&lt;/a&gt;. To secure the application, it requires a classic JWT authentication to match a role in the IAM, unauthenticated access is forbidden, in order to avoid unwanted spam of requests from people wanting to just hammer the API.&lt;/p&gt;

&lt;h4&gt;
  
  
  The problem
&lt;/h4&gt;

&lt;p&gt;When the application is running, as it's a SPA, it can live longer than the JWT, meaning that I will have to refresh it, but because of that, I would have to create a new Apollo instance using the method linked above, and we know it, it's not going to work.&lt;/p&gt;

&lt;h4&gt;
  
  
  The solution
&lt;/h4&gt;

&lt;p&gt;When you think about it, graphQL requests are &lt;em&gt;just&lt;/em&gt; POST requests to the graphQL endoint, which means they use the http service from Angular. And this means that we can use interceptors !&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This allows the token to be reloaded anytime, without having to worry about the Apollo instance, as it can safely be kept as a singleton, with a proper authorization header that can be reloaded !&lt;/p&gt;

&lt;p&gt;You can now enjoy your graphQL API with the same application instance during several hours ! 🎉&lt;/p&gt;

&lt;p&gt;If you enjoyed this article do not hesitate to share it on twitter. If you have any questions or feedback please don't hesitate to tweet me &lt;a href="https://twitter.com/Supamiu_"&gt;@Supamiu_&lt;/a&gt;&lt;/p&gt;

</description>
      <category>angular</category>
      <category>apollo</category>
      <category>graphql</category>
      <category>hasura</category>
    </item>
  </channel>
</rss>
