npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jsqlite

v2.1.0

Published

JavaScript wrapper for SQLite compiled to JavaScript with Emscripten SDK.

Downloads

18

Readme

jsqlite

JavaScript wrapper for SQLite compiled to JavaScript with Emscripten SDK.

Installation

npm i --save jsqlite

Usage

/* import { Database, Filesystem } from 'jsqlite'; */
const { Database, Filesystem } = require('jsqlite');

/* create a new database */
const db = new Database('example.db');

/* open, execute statements and close */
if (db.open().ok) {
  const tableName = 'person';
  console.log(db.exec('CREATE TABLE ?? (id INTEGER PRIMARY KEY, name TEXT NOT NULL);', tableName));
  console.log(db.exec('INSERT INTO ?? VALUES (null, ?);', tableName, 'John'));
  console.log(db.exec('INSERT INTO ?? VALUES (null, ?);', tableName, 'Jane'));
  console.log(db.exec('SELECT * FROM ??;', tableName));
  console.log(db.exec('SELECT SQLITE_VERSION();'));
  db.close();
}

/* delete the database file to avoid useless memory usage */
Filesystem.unlink('example.db');

(Re)Compilation

Prerequisites: Emscripten SDK, tar and wget(only if updating) available on PATH.

If you want to use a modified copy of SQLite or to simply update the library cd into ./lib/. Then:

  • To compile a modified copy of SQLite place an archive named sqlite.tar.gz in ./lib/ containing the modified source code.
  • In ./lib/ execute the following commands make && make clean to rebuild the sqlite.js. If there is no sqlite.tar.gz in ./lib/ one containing the latest version of SQLite will be pulled from here.

API Reference

  • Filesystem

    • Check here for Emscripten filesystem API.
  • Database { constructor(fileName = '') }

    • Main class offering SQLite database abstraction and manipulation methods. fileName should be a string representing the database's file name in the virtual filesystem. If it is missing then it is initialized with an empty string and the database becomes temprary(it cannot be dumped, its contents being lost when closed). Check here for other special strings for temporary databases.
    • Note: Instances of this class are denoted as db from now on.
  • db.open() => {code, ok}

    • Open the connection to the database. Returns an object containing the return code of sqlite3_open in code, a boolean ok to simplify error checking, being false when there is an error, true otherwise.
    • Note: The database is created in memory and managed internally. To save it to a file use db.dump() if it isn't temporary.
  • db.exec(statement, ...parameters) => {code, ok, statement, result}

    • Execute the SQL statement string, replacing every ?? and ? with every value given in parameters in the same order. ?? is used for escaping identfiers and ? is used for escaping strings. Every parameter will be converted to string before escaping. Returns an object which contains the return code of sqlite3_exec in code, the boolean ok, the final(escaped) executed statement in statement and the result of the execution as an array of objects in result.
    • Note: To execute a statement the database should be opened first.
  • db.close() => {code, ok}

    • Close the connection to the database. Returns an object containing the return code of sqlite3_close in code and the boolean ok.
    • Note: The database isn't destroyed when closing it except when the fileName represents a temporary database.
  • db.dump() => Uint8Array

    • Dump the database. Returns an Uint8Array which is the binary representation of the database. Throws a linux error code if there is an error.
    • Note: The returned Uint8Array is an actual SQLite database which can be saved to a file and opened with a SQLite database manager.
  • db.load(data)

    • Restore a previous dumped database or an Uint8Array obtained by reading another SQLite database. Throws a linux error code if there is an error.
    • Note: This action will overwrite the db actual content.

Error codes reference