<?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 Georgy Koychev on Medium]]></title>
        <description><![CDATA[Stories by Georgy Koychev on Medium]]></description>
        <link>https://medium.com/@geokoychev?source=rss-74660c45d02f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*gcbZLbZJ7-D7F5AA.</url>
            <title>Stories by Georgy Koychev on Medium</title>
            <link>https://medium.com/@geokoychev?source=rss-74660c45d02f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 25 Nov 2026 02:48:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@geokoychev/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 I migrated old Mysql tables into Realm DB]]></title>
            <link>https://medium.com/@geokoychev/how-i-migrated-old-mysql-tables-into-realm-db-b319b0fb494b?source=rss-74660c45d02f------2</link>
            <guid isPermaLink="false">https://medium.com/p/b319b0fb494b</guid>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[realm]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[data-migration]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[Georgy Koychev]]></dc:creator>
            <pubDate>Wed, 09 Jan 2019 11:18:46 GMT</pubDate>
            <atom:updated>2019-01-09T11:18:46.566Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*g7i_5F-1Wsrgpt6l" /><figcaption>Photo by <a href="https://unsplash.com/photos/aKcVSSDotgo?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Harshil Gudka</a> on <a href="https://unsplash.com/search/photos/animal-migration?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>I`ve been developing Rect Native applications quite a while and been always thinking how to manage local application data effectively. Of course, we have <strong>AsyncStorage</strong> and several solutions built on top of it, but all of them was not powerful enough for my needs.</p><blockquote>The problem that I was trying to solve is to get 4 million records from MySql table in a mobile application. This App supposed to be a dictionary.</blockquote><p>I googled a bit and found <a href="https://realm.io/products/realm-database"><strong>Realm DB</strong></a>. I was so impressed with its capabilities so I decided to give it a try.</p><p><strong>So Realm DB:</strong></p><ul><li>can be embedded into RN application</li><li>can do quite complex and fast queries</li><li>can handle really big amounts of data</li></ul><p>And this sounds perfect. That&#39;s what I want. I just need to get my 4 million words in a realm database.</p><h3>Export and import (attempt #1)</h3><h4>Export from Mysql</h4><p>I have found a solution to export SQL tables into JSON and CSV. So we&#39;ll have the respective .json and .csv files.</p><h4>Import in Realm Studio</h4><p>Ream studio have an ability to import from CSV. But actually, I could not find any good description of how to do it. Anyway, <strong>data.csv</strong> will require additional processing to get importing working. So I decided not to proceed this way because it requires too many manual steps.</p><h4>Use JSON Directly</h4><p>Ok, this was not a good idea at all. Opening huge JSON directly from React Native Application and trying to iterate it and insert into Realm causing huge memory consumption and not very practical and repeatable solution.</p><h3>Writing my Own Migration (Node.js)</h3><p>All right, <strong>Realm</strong> supports <strong>React Native</strong> and also <strong>Node</strong>, so let&#39;s write some migration script.</p><p>First, add dependencies</p><pre>yarn add realm promise-mysql</pre><p>than init MySQL and Realm</p><pre><strong>const</strong> mysql = require(&quot;promise-mysql&quot;);<br><strong>const</strong> Realm = require(&quot;realm&quot;);</pre><pre>// create Mysql connection<strong><br>const</strong> conn = mysql.createConnection({<br>  host: &#39;localhost&#39;, <br>  user: &#39;root&#39;, <br>  password: &#39;root&#39;<br>})</pre><pre><em>// Define realm model and its properties</em><br><strong>const</strong> CarSchema <strong>=</strong> <strong>{</strong><br>  name<strong>:</strong> &#39;Car&#39;<strong>,</strong><br>  properties<strong>:</strong> <strong>{</strong><br>    make<strong>:</strong>  &#39;string&#39;<strong>,</strong><br>    model<strong>:</strong> &#39;string&#39;<strong>,</strong><br>    miles<strong>:</strong> <strong>{</strong>type<strong>:</strong> &#39;int&#39;<strong>,</strong> default<strong>:</strong> 0<strong>},</strong><br>  <strong>}</strong><br><strong>}; </strong></pre><pre>Realm<strong>.</strong>open<strong>({</strong>schema<strong>:</strong> <strong>[</strong>CarSchema<strong>]})<br>  .</strong>then<strong>( async (</strong>realm<strong>)</strong> <strong>=&gt;</strong> <strong>{ </strong></pre><pre><strong>    // conversion goes here</strong></pre><pre><strong>  });</strong></pre><p>and finally, convert it</p><pre><strong>.....<br>  .</strong>then<strong>( async (</strong>realm<strong>)</strong> <strong>=&gt;</strong> <strong>{<br></strong> <br>    // query<br>    <strong>const</strong> connectoon = <strong>await</strong> conn; <br>    <strong>const</strong> results = <strong>await</strong> connectoon.query<strong>(</strong>&#39;select * from cars&#39;<strong>)</strong>;</pre><pre>    realm.write<strong>(()</strong> =&gt; <strong>{</strong><br>      results.forEach<strong>(</strong>element =&gt; <strong>{</strong><br>        // <strong>here we can do some mapping</strong><br>        <strong>const</strong> newElement = convert<strong>(</strong>element, convertRules<strong>)</strong>;</pre><pre>        realm.create<strong>(</strong>&#39;Car&#39;, newElement<strong>)</strong>;<br>      <strong>})</strong>;<br>    <strong>})</strong>; <br>  <br>  <strong>})</strong>;</pre><p>and so far I got local <strong>‘realm.db’</strong> that I can bundle in a mobile application. Voila!</p><h3>Sql2Realm</h3><p>As an engineer, I couldn’t stop, so I ended up building a <strong>CLI</strong> tool that can accept different mapping options via <strong>.json</strong> configuration. Here it is published on NPM — <a href="https://www.npmjs.com/package/sql2realm"><strong>sql2realm</strong></a></p><p>Feel free to check my <a href="https://github.com/gkoychev/sql2realm"><strong>repo</strong></a>, suggest a feature and create Pull Request, and try to use it and tell me that you think.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/818/0*LOFHECafy8I5RuCP" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b319b0fb494b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>