Introduction to SQL js
Posted By : Shubham Singh | 24-Dec-2018
SQL.js is a port of SQLite to JavaScript, by compiling the SQLite C code with Emscripten means it is completely implemented in javascript and provide us the version of SQLite to work in the browser. SQLite’s C code translated using the Emscripten tool which converts LLVM-generated bytecode into Javascript. SQL.js is an SQLite library with support for opening and writing databases, prepared statements, and more. For storage in memory it uses a virtual database file, and thus it doesn't persist any changes made to the database.
Zakai did not encapsulate the SQL.js in the closure because it can slow down the execution in Javascript engines.
In this project, the curious thing is how it stores the database file. Well, it will create a new database when we do SQL.open(). But we can pass data to SQL.open to start with the pre-filled database. So I think by using this library would look like this:
- It fetches the initial database from the server (e.g using AJAX) at least DB schema and some important data.
- Then open the database with SQL.js
- It mostly does SELECT operations probably on the database
- It may fetch some more data from the server using AJAX when needed
- To save changes, It may send some data to the server using a form or ajax
So this means that we move some more stuff to the client. Just like we do on the server-side, it would allow us to use SQL on the client side. So that we can use the same queries for javascript-based clients on the client and for HTML-only-clients on the server.
The demo in the repository http://kripken.github.io/sql.js/GUI/ is an online SQL interpreter which lets you import an existing SQLite database from your filesystem and run queries against it directly in your browser.
We are able to create a database from a file
We are able to load a database from the server
Use Case
- Users can import any existing SQLite file, and export the created database as a javascript typed array.
- If the user is building a native application in javascript (using Electron for instance), or are working in node.js, you will likely prefer to use a native binding of SQLite to javascript.
- We can build an Electron Application using SQL.js with a Local SQLite3 Database
- The use-case is simple widespread: load a database file from a server and exploit its data on the client.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Shubham Singh
Shubham working as Java Developer. He is always ready to face new problem and keen to explore new technologies. He has knowledge of Java,HTML,CSS, JavaScript. He likes to work with full dedication and coordination.