<?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 Mitar on Medium]]></title>
        <description><![CDATA[Stories by Mitar on Medium]]></description>
        <link>https://medium.com/@mitar?source=rss-eb8849eb5e15------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*vI4yf7-koNOfSXfj.png</url>
            <title>Stories by Mitar on Medium</title>
            <link>https://medium.com/@mitar?source=rss-eb8849eb5e15------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 16:18:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mitar/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[Slovenian TDM exceptions and content available online]]></title>
            <link>https://medium.com/mitar/slovenian-tdm-exceptions-and-content-available-online-d55ae66bb639?source=rss-eb8849eb5e15------2</link>
            <guid isPermaLink="false">https://medium.com/p/d55ae66bb639</guid>
            <category><![CDATA[web]]></category>
            <category><![CDATA[slovenia]]></category>
            <category><![CDATA[tdm]]></category>
            <category><![CDATA[law]]></category>
            <dc:creator><![CDATA[Mitar]]></dc:creator>
            <pubDate>Wed, 24 Sep 2025 08:34:26 GMT</pubDate>
            <atom:updated>2025-09-24T14:02:17.781Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-sa/4.0/</cc:license>
            <content:encoded><![CDATA[<p>Does lawful access under the Slovenian TDM exceptions also include content that is freely available online?</p><p>To answer that question, I submitted a complaint to the European Commission in June 2024 alleging that the Republic of Slovenia had not properly implement the <a href="https://eur-lex.europa.eu/legal-content/EN/TXT/?uri=CELEX:32019L0790">Directive (EU) 2019/790 on copyright and related rights in the Digital Single Market</a> (hereinafter: the CDSM Directive). The complaint argued that Articles 57.a and 57.b of the <a href="https://www.gov.si/assets/organi-v-sestavi/URSIL/Dokumenti/Avtorska-in-sorodne-pravice/Copyright-and-related-rights-act-unofficial-consolidated-version.docx">Slovenian Copyright and Related Rights Act</a> (hereinafter: ZASP), which regulate the exceptions for text and data mining (hereinafter: TDM exceptions), fail to explicitly provide that the notion of “lawful access” includes content that is freely available online. The European Commission did not initiate infringement proceedings against Slovenia, reasoning that although the Slovenian implementation does not explicitly mention freely available online content as part of the notion of lawful access, it also does not expressly exclude it. Consequently, it may be interpreted <strong>that freely available online content forms part of lawful access under the TDM exceptions in ZASP</strong>.</p><p>For researchers and others building artificial intelligence models in Slovenia, it is of significant importance whether the TDM exceptions in Articles 57.a and 57.b of ZASP permit text and data mining on freely available online content, even though the Slovenian legislator did not expressly include such content when defining “lawful access” in ZASP.</p><p>Indeed, the Slovenian legislator has defined the notion of lawful access in ZASP more narrowly than the European legislator in Recital 14 of the CDSM Directive. Under ZASP, lawful access is understood to mean access based on free and open licenses, contracts, or other legal grounds such as copyright limitations and exceptions, or special legislation (e.g. the Legal Deposit Act). The wording does not explicitly extend to content that is freely available online. Nevertheless, it remains possible that, in the event of litigation, Slovenian courts would interpret ZASP more broadly and, taking into account Recital 14 of the CDSM Directive, conclude that lawful access also encompasses freely available online content.</p><p>To enhance legal certainty for researchers, I raised the question of whether “lawful access” under Articles 57.a and 57.b of ZASP includes freely available online content in the complaint (Ref. CPLT(2024)01829) of 30 June 2024 against the Republic of Slovenia for alleged improper transposition of the CDSM Directive. The European Commission concluded the procedure on 2 April 2025 and issued its position on the interpretation of “lawful access” in Slovenian law. The Commission decided not to initiate infringement proceedings against Slovenia, concluding that although the Slovenian implementation does not expressly mention freely available online content as part of lawful access, neither does it explicitly exclude it. It therefore considered that the CDSM Directive had been implemented into ZASP in accordance with its purpose as reflected in the recitals.</p><p><strong>As a result, it shall be inferred that the relevant provisions of ZASP should be interpreted in light of Recital 14 of the CDSM Directive, which suggests that lawful access under ZASP may indeed include access to content that is freely available online.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d55ae66bb639" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mitar/slovenian-tdm-exceptions-and-content-available-online-d55ae66bb639">Slovenian TDM exceptions and content available online</a> was originally published in <a href="https://medium.com/mitar">Mitar’s point</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[dinit]]></title>
            <link>https://medium.com/mitar/dinit-a3314efe95f4?source=rss-eb8849eb5e15------2</link>
            <guid isPermaLink="false">https://medium.com/p/a3314efe95f4</guid>
            <category><![CDATA[itin]]></category>
            <category><![CDATA[projects]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[docker]]></category>
            <dc:creator><![CDATA[Mitar]]></dc:creator>
            <pubDate>Sun, 09 Jul 2023 18:50:36 GMT</pubDate>
            <atom:updated>2023-11-09T12:06:55.403Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-sa/4.0/</cc:license>
            <content:encoded><![CDATA[<p>I made an opinionated init specially for Docker containers: <a href="https://gitlab.com/tozd/dinit">dinit</a>. It supports running and managing multiple programs, their stdout and stderr, and handling signals, all in the way more suitable for Docker containers. Using it means no more resource exhaustion from zombie processes, data loss by uncleanly terminated containers, stray daemonized processes that keep running, or hidden issues because failed processes inside a container gets restarted instead of taking the whole container with them.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a3314efe95f4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mitar/dinit-a3314efe95f4">dinit</a> was originally published in <a href="https://medium.com/mitar">Mitar’s point</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[regex2json]]></title>
            <link>https://medium.com/mitar/regex2json-10dac19e9f49?source=rss-eb8849eb5e15------2</link>
            <guid isPermaLink="false">https://medium.com/p/10dac19e9f49</guid>
            <category><![CDATA[json]]></category>
            <category><![CDATA[regexp]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[projects]]></category>
            <dc:creator><![CDATA[Mitar]]></dc:creator>
            <pubDate>Wed, 14 Jun 2023 09:35:36 GMT</pubDate>
            <atom:updated>2023-06-14T09:46:51.676Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-sa/4.0/</cc:license>
            <content:encoded><![CDATA[<p>I made a simple tool <a href="https://gitlab.com/tozd/regex2json">regex2json</a> in Go to convert traditional text-based (and line-based) logs to JSON for programs which do not support JSON logs themselves. But the tool is more general and can enable any workflow where you prefer operating on JSON instead of text. It works especially great when combined with <a href="https://jqlang.github.io/jq/">jq</a>. I was inspired by this <a href="https://blog.kellybrazil.com/2019/11/26/bringing-the-unix-philosophy-to-the-21st-century/">blog post</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=10dac19e9f49" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mitar/regex2json-10dac19e9f49">regex2json</a> was originally published in <a href="https://medium.com/mitar">Mitar’s point</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Nonenumerable database]]></title>
            <link>https://medium.com/mitar/nonenumerable-database-20800e098223?source=rss-eb8849eb5e15------2</link>
            <guid isPermaLink="false">https://medium.com/p/20800e098223</guid>
            <category><![CDATA[security]]></category>
            <category><![CDATA[voting]]></category>
            <category><![CDATA[dns]]></category>
            <category><![CDATA[database]]></category>
            <category><![CDATA[primitive]]></category>
            <dc:creator><![CDATA[Mitar]]></dc:creator>
            <pubDate>Thu, 23 Jun 2022 22:29:17 GMT</pubDate>
            <atom:updated>2022-06-24T05:24:11.950Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-sa/4.0/</cc:license>
            <content:encoded><![CDATA[<p>Sometimes it is useful to first define an abstraction. A good abstraction can reveal that some problems could be solved by the same solution. A good abstraction also makes it easier for us to communicate about the solution and understand its properties.</p><p>One such abstraction which I think should be defined and named is <em>nonenumerable database</em>. A database where you can query a value by a key, but you cannot enumerate all keys (nor all values). Even a database operator/administrator is not be able to do so.</p><p>At the same time, some problems are more intuitive than others and can help us better understand or even inform the abstraction. We will look at three examples where nonenumerable database can help: DNS/DNSSEC, gun ownership registry, and voting.</p><h4>Not just a key-value database</h4><p>A nonenumerable database is not just a <a href="https://en.wikipedia.org/wiki/Key%E2%80%93value_database">key-value database</a> and does not just provide an efficient and durable way of storing and retrieving the value given a key, but it also prevents one to learn about all keys stored in the database. Without knowing a key it is also not possible to obtain the value or really any value from the database. All this holds even for a database operator/administrator (or cloud provider). This last requirement should hold even when the traffic to the database is observed. Many key-value databases still support various forms of querying, but a nonenumerable database by design does not.</p><h4>DNS, DNSSEC and enumeration of names in a zone</h4><p><a href="https://en.wikipedia.org/wiki/Domain_Name_System_Security_Extensions">Domain Name System Security Extensions</a> (DNSSEC) adds cryptographic authentication and integrity to <a href="https://en.wikipedia.org/wiki/Domain_Name_System">DNS</a>, preventing various attacks when resolving a domain name to its IP. The issue is that because DNSSEC also has to cryptographically prove that a name does <strong>not</strong> resolve (when it does not), it opens a door to domain walking: enumerating all names available.</p><p>Could DNS then be seen as an example of a nonenumerable database? Given interest in addressing domain walking issue introduced by DNSSEC it seems many would like to see it as such, but it is not quite: longer names might not be enumerable, but for short names one can enumerate them simply by trying them (e.g., aa.com, bb.com, cc.com). Note as well that, in DNS, author of the value (DNS record) picks the key (name) as well: this is probably a realistic requirement for a nonenumerable database despite allowing the author to subvert security guarantees otherwise afforded by the database. This informs us that for our nonenumerable database abstraction to be practically secure we have to put some guidelines/limits on the minimal key size/entropy and/or maximal allowed query rate.</p><p>DNSSEC made domain walking easier than just trying names but it also raises a question: what should a nonenumerable database return when a key does not exist in the database? DNSSEC requires an authoritative (negative) answer, but our definition of nonenumerable database is silent on that. Moreover, it does not even require authenticated answers. Should it?</p><p>DNSSEC does not provide confidentiality and large DNS providers (e.g., Google at 8.8.8.8 and CloudFlare at 1.1.1.1) probably know all names stored in DNS, without having to enumerate them themselves. Users did that for them. A nonenumerable database could prevent that.</p><p>Because DNS is also a distributed database it might be hard to guarantee all properties of a nonenumerable database, but future implementations of a nonenumerable database might still inform future versions of DNSSEC.</p><h4>Gun ownership registry</h4><p>In USA, <a href="https://www.gq.com/story/inside-federal-bureau-of-way-too-many-guns">there is no searchable database of gun owners</a>, no national gun registry. Tracing a gun owner from the serial number of the gun is all done by hand, searching through boxes and boxes of forms. The gun lobby worries that a searchable registry would give the government a tool to confiscate guns.</p><p>A nonenumerable database addresses both the gun lobby’s concerns and allow fast tracing of a gun owner when trace is required. In fact, if the worry of the gun lobby is that the government could confiscate guns of everyone, then current approach with boxes full of paper forms does not really prevent that: the government can still go box by box and confiscate guns from everyone found in the box. It would be not very efficient (each box probably contains forms from people living in different places) but it is doable. With a nonenumerable database even that is not be possible.</p><p>On the other hand, once a gun is found and tracing the owner is required, lookup by gun’s serial number would be very fast. Moreover, having database online could allow sellers to directly input ownership information immediately after the sale, which would help in cases when a crime happens soon after selling the gun. This could be a good use case for a blockchain solution, where records could also be updated in a decentralized way as guns are resold.</p><p>To make enumeration harder by simply trying serial numbers in order, guns’ serial numbers should probably be changed to something closer to UUID, e.g., a random 128-bit number.</p><h4>Verifiable voting</h4><p>Online voting is hard: it is hard to ensure anonymity of votes, it is hard to prevent vote-buying and coercion, it is hard to prevent compromised identities and computers, it is hard to prevent phishing, it is hard to prevent compromised servers and manipulating the vote tally, etc. One piece of a solution might be a nonenumerable database.</p><p>Simply storing votes into a nonenumerable database is useless by design: one cannot tally such votes. Instead, you could store receipts for cast votes and proofs that they have been included in the tally, keyed by a ballot number.</p><p>Only knowing the ballot number one could check the cast vote. Based on the guarantees of the nonenumerable database, just checking the vote using the ballot number does not reveal your identity. If the vote does not match what you cast or if the proof is invalid, you can make the ballot number public and others can check as well that there was something wrong during the vote.</p><p>A downside is that it makes vote-buying easier: a voter can give their ballot number to the buyer for them to verify the (bought) vote. Because it is hard to obtain a ballot number of someone else, the buyer can have high confidence they are spending their money well.</p><h4>Possible design</h4><p>One possible design of a nonenumerable database is to use (a hash of) an encryption key as a database key and the corresponding value is then encrypted with that key. Without knowing the (encryption) key, you cannot obtain the corresponding value. You cannot enumerate values as well.</p><p>As noted above, we should also allow a mapping between an arbitrary key and the corresponding encryption key. This would allow storing a DNS name which maps to an encryption key which can decrypt the corresponding IP address stored as a value. But how do we store this mapping so that it cannot be used to enumerate the database?</p><p>Moreover, our definition of a nonenumerable database requires that even when the traffic to the database is observed, its guarantees should still hold. Both this and the mapping are the core design issues of a nonenumerable database. Maybe they can be addressed by using a <a href="https://en.wikipedia.org/wiki/Trusted_execution_environment">trusted execution environment</a>.</p><p>When interoperability is needed between multiple parties using a nonenumerable database, a blockchain-based solution might be reasonable. Especially if querying such blockchain-based nonenumerable database requires a non-free transaction: then there are both technical and economical limits on query rate which helps prevent enumeration by trying keys.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=20800e098223" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mitar/nonenumerable-database-20800e098223">Nonenumerable database</a> was originally published in <a href="https://medium.com/mitar">Mitar’s point</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[PeerDoc — Scaling real-time text editing]]></title>
            <link>https://medium.com/mitar/peerdoc-scaling-real-time-text-editing-9af2ee4387b1?source=rss-eb8849eb5e15------2</link>
            <guid isPermaLink="false">https://medium.com/p/9af2ee4387b1</guid>
            <category><![CDATA[projects]]></category>
            <category><![CDATA[editor]]></category>
            <category><![CDATA[editing]]></category>
            <category><![CDATA[collaboration]]></category>
            <category><![CDATA[peerdoc]]></category>
            <dc:creator><![CDATA[Mitar]]></dc:creator>
            <pubDate>Tue, 12 Oct 2021 20:27:30 GMT</pubDate>
            <atom:updated>2023-11-09T12:08:28.597Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-sa/4.0/</cc:license>
            <content:encoded><![CDATA[<p><a href="https://gitlab.com/peer/doc">PeerDoc</a> is a collaborative real-time rich-text editor with undo/redo, cursor tracking, inline comments, permissions/sharing control over documents, a change history. Things you would expect from a modern collaborative editor on the web.</p><p>But the main difference is that it combines two types of collaboration:</p><ul><li>real-time collaboration between collaborators on the draft of the document (push-based collaboration)</li><li>fork and merge request style of collaboration with others, allowing collaboration to scale beyond a small group of collaborators (pull-based collaboration)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*v9kyec3a_E-KU578qna2LA.png" /></figure><h4>Push-based and pull-based collaboration</h4><p>Push-based collaboration is collaboration where a change can be made directly (pushed). <a href="https://docs.google.com/">Google Docs</a> and <a href="https://www.wikipedia.org/">Wikipedia</a> (and wikis in general), for example, work this way by default. If the change is deemed problematic by others, it can be reverted or further changes can be made to fix the problems.</p><p>Pull-based collaboration instead works by offering a change that must be approved before it is integrated (pulled). <a href="https://github.com/">GitHub</a> has popularized this model through its <a href="https://docs.github.com/en/github/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests">pull-requests</a> (which <a href="https://gitlab.com/">GitLab</a> calls merge-requests because integrating a change is called merging).</p><p>Both types of collaboration are widely used. Both offer some advantages and disadvantages. Push-based collaboration:</p><ul><li>Works best with a small group of collaborators (Google Docs limits collaborators to 100) who generally trust each other (Wikipedia needs editors who patrol <a href="https://en.wikipedia.org/wiki/Special:RecentChanges">recent changes</a> to detect vandalism).</li><li>It has less procedural overhead and is generally easier for a novice to understand and do.</li><li>When done in real-time, potential conflicts (due to changes to the same section of the document) can be resolved more easily and quickly, often without having to coordinate the resolution at all.</li><li>Each individual change is made by one user and there is no collaboration on the change itself. This can be less friendly to novice collaborators, who might prefer to have some help with their change. If their change is then further changed or even reverted, they may feel hurt.</li></ul><p>Pull-based collaboration:</p><ul><li>Can scale up to many collaborators who can independently offer changes.</li><li>Often, changes can be made, discussed, and iterated upon by collaborators, before they are integrated.</li><li>Each change requires a review process before it is integrated (or rejected), which can become a bottleneck. This process must be learned by novice collaborators before they can contribute.</li><li>Multiple independent changes may be made to the same section of the document and resolving conflicts is difficult to coordinate (since each change is usually part of a separate review process) and lengthy (can be repetitive).</li><li>Collaborators need only trust those reviewing and approving the changes.</li><li>There is usually a time and place to discuss the changes next to the changes themselves. These discussions can remain available for future reference.</li></ul><h4>Combining push- and pull-based collaboration</h4><p>PeerDoc supports real-time editing for those who have permission to edit the document directly (given to them by the initial editor of the document). Others (if they have permission to see the document) can fork the document and edit the fork (the user who made the fork can also grant permission to other users to edit the fork). Later, an editor of the fork can suggest that all changes from the fork be merged back into the parent document. If an editor of the parent document approves, the merge is performed. A fork itself can also be further forked. This can soon lead to a tree of documents like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/471/1*0TIa_g1ThaIG40SY1mtnOQ.png" /></figure><p>PeerDoc uses the great <a href="https://prosemirror.net/">ProseMirror</a> library for its editor, which already includes features for <a href="https://marijnhaverbeke.nl/blog/collaborative-editing.html">real-time collaboration</a>. For a single document this works great, but making things work across a tree of documents requires a precise cross-document sharing of changes.</p><p>When a <em>change x</em> in <em>Document A</em> is made, it is made after all existing <em>changes a</em>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/471/1*29i4_eQd0dyhvQNj-sSzbA.png" /></figure><p><em>Change x</em> then has to propagate to <em>Fork B</em> and <em>Fork C</em>, integrating with other additional changes already there, <em>changes b</em> and <em>changes c</em>, respectively. This is done by rebasing those additional changes on top of changes from <em>Document A</em>. So all changes from <em>Document A</em> are always the oldest in the history, with forks adding changes on top of that history. Rebasing <em>changes b</em> on top of <em>change x</em> makes <em>changes b’</em>, and rebasing <em>changes c</em> on top of <em>change x</em> makes <em>changes c’</em>. After rebasing, <em>changes b’</em> and <em>changes c’</em> follow after <em>changes a</em> and <em>change x</em>.</p><p>Integrating changes from <em>Document A</em> into <em>Fork C</em> changes <em>Fork C</em>. Because of that, those changes (<em>change x</em> and <em>changes c’</em>) have to propagate in turn to <em>Fork D</em>. This is where rebasing becomes more complicated. Rebasing requires branches to have a shared base, but <em>changes d</em> have base <em>a + c</em> while to-be-rebased <em>changes d’</em> have base <em>a + x + c’</em>. To get to a shared base we start with <em>a + c + d</em> and then invert (undo) <em>changes c</em> and <em>changes d</em> and add them to get <em>a + c + d + (-d) + (-c)</em>. Then we add new <em>change x</em> from <em>Document A</em> to get <em>a + c + d + (-d) + (-c) + x</em>. With that we rebase <em>changes c</em> into <em>changes c’</em> (which should produce same <em>c’</em> as we got for <em>Fork C</em>) and now we can rebase <em>changes d</em> into <em>changes d’</em>. This gives us <em>a + c + d + (-d) + (-c) + x + c’ + d’</em>. We now add <em>changes d’</em> as rebased changes to <em>a + x + c’</em> to get fork’s changes <em>a + x + c’ + d’</em> and forget those temporary transformations we made.</p><p>Merging a fork into the parent document is easier. First, we have to make sure all parent changes are propagated to forks. Then, merging is simply adding additional changes to the parent document. For example, merging <em>Fork B</em> into <em>Document A</em> would mean that we add <em>changes b’</em> to <em>changes a + x</em>, making <em>Document A</em> have the same content as <em>Fork B</em> (until another change to <em>Document A</em>). Merging changes <em>Document A</em>, so now we have to propagate <em>changes b’</em> to other forks, i.e., <em>Fork C</em> (and in turn <em>Fork D</em>).</p><p>This explains what is happening on the server-side. Client-side also has to adapt to these changes. For example, an editor which has <em>Fork D</em> open and displayed it is showing <em>changes a + c + d + y</em>, where <em>changes y</em> are local changes the editor might have and have not yet been send to the server-side. When server-side propagates <em>change x</em> and then sends updated <em>Fork D</em> to the client-side, the editor has to update showing <em>changes a + c + d + y</em> to <em>changes a + x + c’ + d’ + y’</em> while preserving any other editor state. PeerDoc does not yet implement this and just resets the editor.</p><h4>Discussion</h4><p>As you can see, cross-document sharing of changes can be very active. Changes can be small (e.g., the user adds an extra character to <em>Document A</em> in real-time, which then propagates to the forks) or large (e.g., the user merges a large set of <em>changes b’</em> into <em>Document A</em>, which then propagate to the forks). All of this not only puts a burden on the server, but can also confuse users, since large chunks of content can be changed while they are editing the document. Moreover, the interaction with existing changes (e.g., the interaction of <em>changes b’</em> with <em>changes c’</em> after <em>changes b’</em> have been merged into <em>Document A</em> and propagated to <em>Fork B</em>) can sometimes be surprising. All conflicts between changes to the same section of the document are resolved mechanically, without regard to how a human understands the content. When editing in real-time, this is fine because editors can usually observe and adjust to the results of conflict resolution in real-time (e.g., “Oh, I got pushed into a bulleted list by another user, let me get out before I type more.”). Furthermore, conflicts tend to be small anyway. However, when mechanically resolving conflicts for larger chunks of content, this can lead to surprises and require followup fixes.</p><p>When we think about collaboration, we also need to think about the process and the goal of collaboration. Who can edit and when, and in the context of PeerDoc, who can edit and when in real-time, and who can edit and when using forks and merges. One approach is to decide this based on trust between users, and edit in real-time if they have high trust, and edit through forks and merges if they do not. But if the collaboration is part of a wider community, we may need more control over when something can happen. For example, should any editor of the parent document really be able to approve the merge request, or should there be a process for that? After the merge, can the parent document editors continue to edit the document directly, possibly modifying the just merged (and community-approved) change?</p><p>PeerDoc supports being embedded inside another tool, which can then control the collaborative process more precisely. Moreover, it can operate in two different modes. In one mode, documents can be forked and merged at any time, and editors can still edit documents directly. In the other mode, documents can be marked as <em>published</em> and only then can they be forked and merged, but they can no longer be edited directly. The idea is that editors can directly edit the document only while it is not yet published (it is a draft). Then they publish the document and others can see the document, fork it, edit the fork in real-time, and then suggest changes that can be approved or rejected by the editors of the document.</p><p>When a fork is merged, it can no longer be edited and changes from the parent document are not propagated to it. It remains frozen and archived. If fork is not merged, it can be published instead. What this means for the community depends on the community, but it could mean that the fork editors want to make it visible to the community as an alternative to the parent document (rather than retracting the proposed changes).</p><h4>Deployment</h4><p>PeerDoc <a href="https://peerdoc.tnode.com/document">has been used</a> by <a href="http://www.unifesp.br/">The Federal University of São Paulo</a>, Brazil, to write their 5 years strategic plan allowing everyone at the university to collaborate and contribute. It has been used integrated with <a href="https://pb.appcivist.org">AppCivist</a> which guided the community through multiple phases of participation.</p><h4>Collaboration is not easy</h4><p>PeerDoc does not have all the answers (yet) about how exactly to best use a combination of these two types of collaboration. Are forks created by working groups that meet and collaborate and then propose changes to the main document? How are the proposed changes approved? Does an editor of the parent document make a decision or is there some other decision-making process, e.g., voting? What happens if a change is not approved? Does the community vote to choose between the parent document and a fork? Or is the fork published as an alternative document? Or is it simply forgotten? Is PeerDoc used as-is, or is it integrated with another application that can help organize the community and coordinate the process? PeerDoc currently rebases all changes and maintains a linear history of all documents, but would a branched history without rebasing be better when trying to understand how documents came about? Can one effectively proceed in followup fixes of poorly resolved conflicts without having access to both versions (parent and fork) of the content prior to the merge, or is it sufficient to see only the (poorly) merged result? More experimentation is needed to answer these questions. Try it out and please report your experiences.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9af2ee4387b1" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mitar/peerdoc-scaling-real-time-text-editing-9af2ee4387b1">PeerDoc — Scaling real-time text editing</a> was originally published in <a href="https://medium.com/mitar">Mitar’s point</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JSON decoding in node is fast]]></title>
            <link>https://medium.com/mitar/json-decoding-in-node-is-fast-7368740d0d1a?source=rss-eb8849eb5e15------2</link>
            <guid isPermaLink="false">https://medium.com/p/7368740d0d1a</guid>
            <category><![CDATA[json]]></category>
            <category><![CDATA[node]]></category>
            <category><![CDATA[benchmark]]></category>
            <category><![CDATA[go]]></category>
            <dc:creator><![CDATA[Mitar]]></dc:creator>
            <pubDate>Sun, 09 May 2021 08:34:27 GMT</pubDate>
            <atom:updated>2021-05-09T10:15:05.639Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-sa/4.0/</cc:license>
            <content:encoded><![CDATA[<p>I <a href="https://gitlab.com/mitar/benchmark-json-decode">made a benchmark</a> comparing how long it takes to decode JSON in <a href="https://nodejs.org/en/">node.js</a> and <a href="https://golang.org/">Go</a>. As I have noticed <a href="/https/medium.com/post/in-nodejs-always-query-in-json-from-postgresql/">in the past</a>, JSON decoding in node.js is really fast.</p><p>I used node v16.0.0 and Go v1.16. For Go, I compared both the structure of the data (providing a struct type) and not knowing it (using map[string]interface{} interface). Moreover, I compared <a href="https://golang.org/pkg/encoding/json/">standard </a><a href="https://golang.org/pkg/encoding/json/">encoding/json</a> and <a href="https://github.com/json-iterator/go">jsoniter</a>. I measured on a range of JSON files from <a href="https://gitlab.com/mitar/benchmark-json-decode/-/blob/main/10.json">10 regular and 10 array fields</a> (~23 KB) to <a href="https://gitlab.com/mitar/benchmark-json-decode/-/blob/main/200.json">200 regular and 200 array fields</a> (~1.3 MB). I repeated each run 10 times and averaged.</p><h4>Results</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6vX65vd2yQ-c9Zhp7vYF6Q.png" /></figure><p>Parsing JSON in node is really fast and beats Go when no structure about JSON is provided (which is how parsing in node is done). Only with information about the structure, the non-standard jsoniter library is faster.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7368740d0d1a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mitar/json-decoding-in-node-is-fast-7368740d0d1a">JSON decoding in node is fast</a> was originally published in <a href="https://medium.com/mitar">Mitar’s point</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Google search across languages]]></title>
            <link>https://medium.com/mitar/google-search-across-languages-1214f7249b7?source=rss-eb8849eb5e15------2</link>
            <guid isPermaLink="false">https://medium.com/p/1214f7249b7</guid>
            <category><![CDATA[web]]></category>
            <category><![CDATA[language]]></category>
            <category><![CDATA[search]]></category>
            <category><![CDATA[google]]></category>
            <category><![CDATA[languagebubble]]></category>
            <dc:creator><![CDATA[Mitar]]></dc:creator>
            <pubDate>Fri, 08 Jan 2021 07:54:22 GMT</pubDate>
            <atom:updated>2021-01-08T08:06:57.688Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-sa/4.0/</cc:license>
            <content:encoded><![CDATA[<p>In 2013, I wrote <a href="https://mitar.tnode.com/post/crowdsourcing-the-language-bubble-breakout/">how crowdsourcing could help break out of the language bubble</a> we all find ourselves in when we search online. How our search results only come from the language we write our search keywords in. But since then, machine translation between languages has greatly improved. So why are we still in our language bubbles?</p><p>There are some rays of hope. Google is reportedly able to return you results from other languages when there are no good hits in your primary language. I have to say, though, that I have never experienced this. Maybe because I usually search in English. I would guess that when you search in smaller languages, you sometimes get English results added.</p><p>But I want the opposite. I want to see ideas and thoughts and solutions that might be available in other languages, the languages I do not speak, when I search in English. I want diversity. I think all the pieces to build this are available. Why is this not already available? Am I overlooking any obstacle to this?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1214f7249b7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mitar/google-search-across-languages-1214f7249b7">Google search across languages</a> was originally published in <a href="https://medium.com/mitar">Mitar’s point</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Store IDs in MongoDB as binary or as string?]]></title>
            <link>https://medium.com/mitar/store-ids-in-mongodb-as-binary-or-as-string-59a5c2fe59d9?source=rss-eb8849eb5e15------2</link>
            <guid isPermaLink="false">https://medium.com/p/59a5c2fe59d9</guid>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[benchmark]]></category>
            <category><![CDATA[compression]]></category>
            <dc:creator><![CDATA[Mitar]]></dc:creator>
            <pubDate>Sat, 07 Nov 2020 07:51:12 GMT</pubDate>
            <atom:updated>2020-11-08T03:34:24.865Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-sa/4.0/</cc:license>
            <content:encoded><![CDATA[<p>I was curious if <a href="https://docs.mongodb.com/manual/core/wiredtiger/#compression">MongoDB compression</a> can efficiently store IDs if they are represented as string instead in a more compact binary form. So I made a <a href="https://gitlab.com/mitar/benchmark-mongo-id">benchmark</a> and measure compression performance of three available compressors: zlib, snappy, and zstd.</p><p>Results for MongoDB 4.4.1 of storing 128 bit random values (e.g., UUIDs) as binary (16 bytes) or as base-58 encoded (22 characters):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mz4kVRByTXhdmoyPVYZlNQ.png" /></figure><p><a href="https://github.com/facebook/zstd">zstd</a> compression looks really good. Moreover, it is clear that <strong>storing values as binary is more efficient than as string, even with compression, because compression can compress also binary representation despite values being random</strong>. The most compressed string size (zstd, 5410816 B) is still larger than the least compressed binary size (snappy, 4923392 B). Do note though that zlib compressed string (5537792 B) and zstd compressed string (5410816 B) are smaller than uncompressed binary (6168576 B), meaning that those compression algorithms can recover storage lost in string representation. But given that they can compress binary values even more, it seems there are still things to improve in those algorithms.</p><p>Note: Compression algorithms generally perform poorly on small data and here we had very small object sizes. This means that insights here cannot be generalized to performance with larger amounts of binary (or string) data stored in MongoDB. (MongoDB does combine objects into blocks to compress to alleviate this issue.)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=59a5c2fe59d9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mitar/store-ids-in-mongodb-as-binary-or-as-string-59a5c2fe59d9">Store IDs in MongoDB as binary or as string?</a> was originally published in <a href="https://medium.com/mitar">Mitar’s point</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Can humans grow up in zero gravity?]]></title>
            <link>https://medium.com/mitar/can-humans-grow-up-in-zero-gravity-640e27b1a7fe?source=rss-eb8849eb5e15------2</link>
            <guid isPermaLink="false">https://medium.com/p/640e27b1a7fe</guid>
            <category><![CDATA[zero-gravity]]></category>
            <category><![CDATA[learning]]></category>
            <category><![CDATA[growing]]></category>
            <category><![CDATA[humans]]></category>
            <dc:creator><![CDATA[Mitar]]></dc:creator>
            <pubDate>Sun, 25 Oct 2020 06:54:33 GMT</pubDate>
            <atom:updated>2020-10-25T06:59:53.976Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-sa/4.0/</cc:license>
            <content:encoded><![CDATA[<p>Gravity seems to be our first and most important teacher. Patient, consistent and always present. Children can learn cause and effect through it. What happens if I lift an object and let go? Over and over again. On repeat. Always the same thing.</p><p>When we think about life in space. Can we raise children there? Can children grow up in zero gravity? Can they develop their mental abilities without having this teacher around them? Or will they develop in other ways? A different kind of logic?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=640e27b1a7fe" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mitar/can-humans-grow-up-in-zero-gravity-640e27b1a7fe">Can humans grow up in zero gravity?</a> was originally published in <a href="https://medium.com/mitar">Mitar’s point</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Database-abstraction APIs should not exist]]></title>
            <link>https://medium.com/mitar/database-abstraction-apis-should-not-exist-16c7b2da34f7?source=rss-eb8849eb5e15------2</link>
            <guid isPermaLink="false">https://medium.com/p/16c7b2da34f7</guid>
            <category><![CDATA[orm]]></category>
            <category><![CDATA[abstraction]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Mitar]]></dc:creator>
            <pubDate>Mon, 05 Oct 2020 06:44:26 GMT</pubDate>
            <atom:updated>2020-10-14T04:18:08.929Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-sa/4.0/</cc:license>
            <content:encoded><![CDATA[<p>Database-abstraction APIs where you write a query using the host programming language should not exist. Or more precisely, should not have to exist. For example, in <a href="https://docs.djangoproject.com/en/3.1/topics/db/queries/">Django</a> you can query the database using the following Python code:</p><pre>Entry.objects.filter(is_draft=True)</pre><p>Which Django translates (roughly) into the following SQL:</p><pre>SELECT *<br>  FROM blog_entry<br>  WHERE is_draft = true;</pre><p>But why we cannot write SQL query directly as an SQL query, while retaining all other features Django offers through its database-abstraction API (database agnostic code, inputs to queries and outputs from queries being Python objects, etc.)? I claim there is no reason anymore for that.</p><h4>Babel</h4><p>I have confidence in this claim because of the success of <a href="https://babeljs.io/">Babel JavaScript transpiler</a> and the ecosystem around it. Babel can take a newer version of JavaScript as input and converts it so that it can run in older JavaScript runtimes. It maps new features to old ones. It allows one to use syntax of new or even just proposed features of the JavaScript language. It allows experimentation around the language.</p><p>The approach it takes for this is that Babel first parses the input code into <a href="https://en.wikipedia.org/wiki/Abstract_syntax_tree">abstract syntax tree</a> (AST), an internal representation of the code, then converts the tree so that it represents the code which can run in older JavaScript runtimes, and then renders AST back to textual representation of the code.</p><p>Babel is successful because it has an ecosystem of plugins that make it even more powerful, allowing integration with various other libraries and programming language patterns, so that you can tailor programing in JavaScript to your needs and preferences.</p><h4>Why we have database-abstraction APIs?</h4><p>Such high-level database-abstraction APIs exist in almost all programming languages and are often paired with <a href="https://en.wikipedia.org/wiki/Object-relational_mapping">object-relation mapping</a> (ORM) capabilities to present data from the database as objects in the host language.</p><p>What features do we currently get (or are at least promised) from these database-abstraction APIs?[¹]</p><ol><li>Database engine independence: the Django query above can be converted to SQL dialects for many popular database engines without having to change Python code.</li><li>Queries are part of the rest of the code, which allows tight integration with the host language. For example, type checking of the code can type check queries as well, which is not possible with opaque SQL queries represented as strings in the host language. Moreover, other types of query validation can be done before the query hits the database engine.</li><li>It is easier to organize and compose such code. E.g., often there is support for various hooks to register code to be called before and after queries. Such features allow better modularity of code based on concerns (a module for logging all queries, a module for checking permissions, etc.). Such hooks are also written in the host language itself and not some database engine specific language.</li><li>Support for custom data types which are transparently converted to and from the underlying data types supported by a database engine.</li><li>Can have a more user friendly and familiar syntax than SQL, especially if you do not know SQL.</li></ol><p>But in practice there are many limits:</p><ol><li>Database engine independence means that one can use only common/shared features between different database engines and if one wants to use a particular special feature, they have to use a “raw query as string” escape hatch. Which of course makes the code depend on a particular database engine and you loose many other features (like query validation/type checking). Database-abstraction API can add high-level support for such special feature and map it to different database engines, but wouldn’t it be better if you could just write the raw SQL query without loosing other features?</li><li>Because queries can be very different and inputs and outputs to queries even more so, high-level database-abstraction API functions are generally very broadly typed, allowing inputs with types which do not even match the query, failing only at runtime (e.g., *args, **kwargs in Python).</li><li>Hooks in the host language are nice, but because they are run outside of the database engine an issue of recovering from failures arises. What if a database transaction completes but a hook fails? What if a database transaction fails but a hook succeeds? In general synchronizing between such external hooks and a database engine can be tricky.[²]</li></ol><h4>Parsing SQL into AST</h4><p>If you look at the Python code example above, it uses Python to generate AST and then renders this AST to SQL. But why not use SQL itself to generate this AST? And then use plugins to transform AST to implement various features we want?</p><p>First, it is important to note that there are two types of SQL statements: <a href="https://en.wikipedia.org/wiki/Data_definition_language">data definition statements</a> and queries. Parsing data definitions statements (e.g., CREATE TABLE) allows us to extract typing information about data. We can use this typing information to type check queries as well. Moreover, we can use that information to know how to convert query results to host language objects. Depending on the integration with the host language, type checking and validation of both queries and argument values might be possible at the compile time, or at least at runtime before even hitting the database engine.</p><h4>Examples</h4><p>Having plugins which can operate on AST allows developer to write simple queries which are then transformed further. This achieves decoupling of concerns where original query deals with what data is needed at that point in the program while how is that data obtained, queried, structured in the database, or if query is even allowed, can all be delegated to plugins.</p><h4>Permission checks</h4><p>If original simple query looks like:</p><pre>SELECT *<br>  FROM blog_entry<br>  WHERE is_draft = true;</pre><p>But only the owner of a blog entry should be able to see it while it is draft. A plugin for row-level permission checks could transform this query automatically into:</p><pre>SELECT *<br>  FROM blog_entry<br>  WHERE is_draft = true<br>    AND owner_id = current_user_id();</pre><p>Furthermore, the plugin could change definition of blog_entry table to include necessary owner_id column.</p><p>Doing such checks manually is tedious and error prone, especially when you have nested queries or joins and you have to make sure you add check to everywhere.</p><p>We can expand this example further if we introduce groups of users and allow multiple users to own a blog entry:</p><pre>SELECT DISTINCT id, title, body, pub_date, is_draft, internal_comment<br>  FROM blog_entry<br>    LEFT JOIN blog_entry_owner_users<br>      ON blog_entry.id = blog_entry_owner_users.entry_id<br>    LEFT JOIN blog_entry_owner_groups<br>      ON blog_entry.id = blog_entry_owner_groups.entry_id<br>    LEFT JOIN groups_to_users<br>      ON blog_entry_owner_groups.group_id = groups_to_users.group_id<br>  WHERE is_draft = true<br>    AND (<br>      blog_entry_owner_users.user_id = current_user_id()<br>      OR groups_to_users.user_id = current_user_id()<br>    );</pre><p>The plugin has information about blog_entry table and can enumerate all columns when necessary. Here it makes sure that returned columns match the original query.</p><p>Enjoy writing this again and again. This is why people prefer doing permission checks in application code.</p><p>Plugin could also support permissions on columns. For example, internal_comment field should be visible only to blog entry owners.</p><pre>SELECT *<br>  FROM blog_entry <br>  WHERE id = 42;</pre><p>Could get transformed into:</p><pre>SELECT DISTINCT id, title, body, pub_date, is_draft,<br>    CASE blog_entry_owner_users.user_id = current_user_id()<br>        OR groups_to_users.user_id = current_user_id()<br>      WHEN true THEN internal_comment<br>    END AS internal_comment<br>  FROM blog_entry<br>    LEFT JOIN blog_entry_owner_users<br>      ON blog_entry.id = blog_entry_owner_users.entry_id<br>    LEFT JOIN blog_entry_owner_groups<br>      ON blog_entry.id = blog_entry_owner_groups.entry_id<br>    LEFT JOIN groups_to_users<br>      ON blog_entry_owner_groups.group_id = groups_to_users.group_id<br>  WHERE id = 42<br>    AND (<br>      is_draft = false<br>      OR (<br>        is_draft = true<br>        AND (<br>          blog_entry_owner_users.user_id = current_user_id()<br>          OR groups_to_users.user_id = current_user_id()<br>        )<br>      )<br>    );</pre><h4>Keeping historical versions</h4><p>If you want to keep all historical versions of all data, you can setup a database view which modifies UPDATE queries into INSERT queries and for SELECT queries only returns the latest version. But managing views and their migration can add additional complexity, so we can instead use a plugin which transforms queries in this way before they hit the database engine.</p><p>The plugin could automatically add version field to all tables and then a query like:</p><pre>UPDATE blog_entry<br>  SET title = &quot;Hello world&quot;<br>  WHERE id = 42;</pre><p>Gets transformed into:</p><pre>INSERT INTO blog_entry<br>  SELECT b1.id, b1.version + 1, &quot;Hello world&quot;,<br>      b1.body, b1.pub_date, b1.is_draft, b1.internal_comment<br>    FROM blog_entry b1<br>    WHERE b1.id = 42<br>      AND b1.version = (<br>        SELECT MAX(version) FROM blog_entry b2 WHERE b2.id = b1.id<br>      );</pre><p>The query:</p><pre>SELECT *<br>  FROM blog_entry <br>  WHERE id = 42;</pre><p>Gets transformed into:</p><pre>SELECT *<br>  FROM blog_entry b1<br>  WHERE b1.id = 42<br>    AND b1.version = (<br>      SELECT MAX(version) FROM blog_entry b2 WHERE b2.id = b1.id<br>    );</pre><p>Such plugin can also help with database engines which do not support views with INSTEAD OF triggers.</p><p>Do note that storing all history in the same table can be inefficient if only the latest version is almost always queried.</p><h4>More ideas</h4><p>A wide range of plugins could be made:</p><ul><li>SQL is case insensitive. A plugin could automatically quote all identifiers to make queries case sensitive instead.</li><li>Generating query documentation from query placeholders and their expected types.</li><li>Reordering table fields to optimize the table.</li><li>Pretty printing of queries.</li><li>Static analyzer. Instead of rewriting the query, just validate all queries for some properties (e.g., that they do correct permission checks, or type check them). Or perform some other linting checks.</li></ul><p>Queries can be made from the client side (e.g., browser) or on the server side as prepared queries. The latter could even be exposed as JSON RPC endpoints automatically.</p><p>Such approach (and general enough AST) could work also for parsing various SQL dialects into the same AST. But we do not even have to use SQL to get this AST. Other query languages like <a href="https://docs.mongodb.com/manual/tutorial/query-documents/">MongoDB queries</a> and <a href="https://graphql.org/">GraphQL</a> could be parsed into AST as well?[³] Or SQL-like query language <a href="https://partiql.org/">PartiQL</a>, too. One could even see REST as just a simple query language you can map to AST and then have a chain of plugins to add permission checks and everything else before querying the database, getting quickly a REST server implementation. <a href="https://json-schema.org/">JSON Schema</a> could also be used to describe types. Many JSON query languages could be mapped to this AST as well.</p><p>Moreover, this AST does not have to be converted to just SQL at the end, but could in fact be converted into any query language for other database engines, or even just to query static files like JSON or CSV. You could run same query on the client side against an in-memory state and on the server side against an SQL-based database engine.</p><p>In a way all those technologies exist just to describe data structure and queries in different ways, but underlying principles are the same. Why then have those technologies so separated? Can we simply translate between them automatically as needed?</p><p>[¹]: Have I missed any major feature? Leave a comment if there is an important feature missing. [²]: These days many database engines support triggers and writing them in a wide range of languages, e.g., <a href="https://www.postgresql.org/docs/current/plpython.html">PL/Python</a>, <a href="https://plv8.github.io/">PLV8</a>. [³]: I would even claim that their popularity is because they do not require parsing and are already in JSON-compatible representation.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=16c7b2da34f7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mitar/database-abstraction-apis-should-not-exist-16c7b2da34f7">Database-abstraction APIs should not exist</a> was originally published in <a href="https://medium.com/mitar">Mitar’s point</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>