Unlock the Power of Relational Databases in Your Browser with SQL.js
What is SQL.js?
SQL.js is a JavaScript library that allows you to create and query relational databases entirely in your browser. It uses a virtual database file stored in the browser memory, making it possible to work with databases without requiring any server-side processes. With SQL.js, you can easily bring in an existing SQLite database and convert a database created in SQL.js to SQLite.
The Advantages of Using SQL.js
One of the significant benefits of using SQL.js is that it’s built for and works entirely on the client side, eliminating the need for third-party software. Setting up SQL.js is as easy as installing jQuery in an existing HTML project. Additionally, SQL.js provides support for executing single SQL strings that contain multiple statements.
The Catch: Non-Persistent Changes
However, there’s an important consideration to keep in mind: changes to your database when using SQL.js are not persistent. This means that all changes made to your database will cease to exist when you reload your browser. But don’t worry, you can import any existing SQLite file and export the created database as a JavaScript typed array.
Getting Started with SQL.js
Browser Installation
Integrating SQL.js into a new client-side-based project is super easy. You can get started by including the CDN or downloading the source files and linking them to your markup page. By default, SQL.js uses WebAssembly and needs to load a .wasm
file in addition to the JavaScript library.
// Include SQL.js from CDN
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/sql.min.js"></script>
Node.js Installation
Installing SQL.js in a Node-based project is also straightforward. To install it, you can simply run the command:
npm install sql.js
or download sql-wasm.js
and sql-wasm.wasm
and use the Node.js require
function to load them in your project.
Writing SQL Queries and Prepared Statements
Now that we’ve followed all the required steps to install and initialize SQL.js, let’s dive into its usage.
Creating a Database
const db = new SQL.Database();
db.run("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT, email TEXT);");
Running SQL Statements
With SQL.js, you can easily run a statement without reading its results. The syntax is as written below:
db.run("INSERT INTO users (name, email) VALUES ('John Doe', '[email protected]');");
Prepared SQL Statements
You can use prepared statements to execute the same or similar SQL statements repeatedly with high efficiency. Prepared statements have a much shorter parsing time than running SQL statements because the preparation on the query is done only once.
const stmt = db.prepare("INSERT INTO users (name, email) VALUES (?,?);");
stmt.run(['Jane Doe', '[email protected]']);
stmt.run(['Bob Smith', '[email protected]']);
Writing a Database to the Disk
SQL.js also provides an option for exporting/writing a database to disk as a .sqlite
file via the db.export()
method.
const data = db.export();
const blob = new Blob([data], { type: 'application/octet-stream' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'database.sqlite';
a.click();
Real-World Applications
SQL.js is perfect for building offline-first applications such as a note-taking app. However, you may want to consider using other relational databases (MySQL, PostgreSQL) in cases where you want to manage user data from an admin panel.
- Offline-first applications
- Note-taking apps
- Data analysis tools
SQL.js provides a powerful tool for building robust and efficient client-side applications. With its ease of use and flexibility, it’s definitely worth considering for your next project.