Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

sql.js

Package Overview
Dependencies
Maintainers
2
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sql.js

SQLite library with support for opening and writing databases, prepared statements, and more. This SQLite library is in pure javascript (compiled with emscripten).

  • 1.12.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
26K
decreased by-58.97%
Maintainers
2
Weekly downloads
 
Created

What is sql.js?

sql.js is a JavaScript library that allows you to run SQLite databases directly in the browser or in Node.js. It is a port of SQLite to JavaScript using Emscripten, which compiles C/C++ code to WebAssembly. This makes it possible to use a full-featured SQL database without needing a server-side component.

What are sql.js's main functionalities?

Create and Query a Database

This feature allows you to create a new SQLite database, define tables, and execute SQL queries. The code sample demonstrates creating a table, inserting data, and querying the data.

const initSqlJs = require('sql.js');

initSqlJs().then(SQL => {
  const db = new SQL.Database();
  db.run('CREATE TABLE test (col1, col2);');
  db.run('INSERT INTO test VALUES (?, ?), (?, ?);', [1, 'foo', 2, 'bar']);
  const res = db.exec('SELECT * FROM test;');
  console.log(res);
});

Load and Save a Database

This feature allows you to export the database to a binary format and then reload it. The code sample shows how to export a database to a Uint8Array and then create a new database instance from that data.

const initSqlJs = require('sql.js');

initSqlJs().then(SQL => {
  const db = new SQL.Database();
  db.run('CREATE TABLE test (col1, col2);');
  db.run('INSERT INTO test VALUES (?, ?), (?, ?);', [1, 'foo', 2, 'bar']);
  const data = db.export();
  const db2 = new SQL.Database(data);
  const res = db2.exec('SELECT * FROM test;');
  console.log(res);
});

Use SQL Transactions

This feature demonstrates the use of SQL transactions to ensure that a series of operations are completed successfully. The code sample shows how to begin a transaction, execute multiple insert operations, and commit the transaction.

const initSqlJs = require('sql.js');

initSqlJs().then(SQL => {
  const db = new SQL.Database();
  db.run('CREATE TABLE test (col1, col2);');
  db.run('BEGIN TRANSACTION;');
  db.run('INSERT INTO test VALUES (?, ?);', [1, 'foo']);
  db.run('INSERT INTO test VALUES (?, ?);', [2, 'bar']);
  db.run('COMMIT;');
  const res = db.exec('SELECT * FROM test;');
  console.log(res);
});

Other packages similar to sql.js

Keywords

FAQs

Package last updated on 29 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc