📜 ⬆️ ⬇️

The reasons and advantages of the third baihuist way of using SQLite in Node.js

Those who attained Zen Python believe that there must be one (and, preferably, only one) obvious way to achieve the desired.

Those who have comprehended the list of Node.js modules can be convinced that the creators of these modules are spiritually closer not to Zen Buddhists, but to Bayhouists - to fans of the Baihua Yundong movement (百花 运动), proclaimed by Mao Zedong in 1957 based on the motives of the classical Chinese poems "let a hundred flowers bloom, let a hundred schools compete," beginning with the words "bai hua" ("百花", "one hundred flowers"). In other words, modules for Node.js, as a rule, provide several ways to do the same thing, and from them the consumer chooses the one that is most suitable for him.

But why there is no such one way that would be suitable for all?
')
I propose to consider the answer to this question using the example of using the SQLite database.

The Node.js engine (unlike, for example, JSDB ) does not have a built-in tool for accessing SQLite databases, providing its implementation to third-party modules. The most popular of these is the sqlite3 module, supported by the well-known cartographic company MapBox . According to npm statistics , it has tens of thousands of downloads per month.

If you look at it closely at all, it is easy to notice that the code of this popular module is not entirely Javascript. It is based on connecting the source (sish) SQLite code as a compiled add-on (addon) for Node. This is the reason why the module is not widely usable: under Linux and under Mac OS X, program building tools are abundant, but for Windows, the need to meet the node-gyp requirements can sometimes be very painful. A special problem is the 64-bit version of Windows, which will require not only Microsoft Visual Studio C ++ 2010 (free Express version), but also heavy Windows 7 64-bit SDK, the ISO image of which takes more than 570 to build add-ons. megabytes.

In response to this inconvenience, a second way to use SQLite in Node appeared - the node-sqlite-purejs module , whose code does not require assembly, because it consists (as reflected in the module name) of pure javascript that is executed by the Node engine directly. This code is based on the SQL.js script , which Alon Zakai (the creator of Emscripten) received, passing the SQLite code through Emscripten - I mentioned this achievement in Habrahabr in March of the last (2012) year.

Unfortunately, the lack of C code is almost the only advantage of this module: it expects to want a lot better in terms of stability, and the list of still unsolved problems includes data loss and the impossibility of a soft shutdown. All this is because during the automatic translation from C to JavaScript, the transaction atomicity seems to be lost, which is the main useful property of SQLite. Until this problem is solved, we can sadly consider this second method as a dead end.

More recently (last week on Sunday - October 27), it became possible to read about the appearance of the third method of using SQLite in Node, which is devoid of the main drawbacks of both its predecessors in the nodejs googlegroup. This new module ( opendatabase ) does not contain SQLite code in either a sishnaya or even javascript form — instead, it calls the sqlite program (in Windows called sqlite.exe ) through the good old command line interface, throwing SQL queries into it and parsing the javascript its response.

It is not hard to guess that this module does not require means for compiling and assembling (which were required by me to the sqlite3 module): it is enough to download the sqlite program from the SQLite website in a ready-made form. For the same reason, there will be no problems with non-ideal implementation (in node-sqlite-purejs observed): the downloaded program contains the official implementation.

That is why, in this module, I see the triumph of the baihuist principle of diversity and co-flourishing, once again confirming the correctness of the Chinese saying Ib i ibudi - huydao mudi ” (“一步 一步 地 会 到 目的”, “step by step you can achieve the goal”).

To this third module, for the time being, I can only blame the location of its source code on Bitbucket and in Mercurial (and not on GitHub and Git, like most other Node modules), which is not usual for me (and therefore makes it difficult to get familiar with the giblets and capabilities module, will complicate tracking its further development). Other programmers (more familiar with Bitbucket and Mercurial) will certainly not consider this drawback to be significant (but when looking at the source code they will find there, perhaps, other shortcomings that are not yet known to me). I have not yet read the source code of this module, so here my story ends.

Source: https://habr.com/ru/post/200344/


All Articles