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

@gemini-testing/sql.js

Package Overview
Dependencies
Maintainers
10
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@gemini-testing/sql.js - npm Package Compare versions

Comparing version 1.0.1 to 1.8.1

.devcontainer/devcontainer.json

35

package.json
{
"name": "@gemini-testing/sql.js",
"version": "1.0.1",
"version": "1.8.1",
"description": "SQLite library with support for opening and writing databases, prepared statements, and more. This SQLite library is in pure javascript (compiled with emscripten).",

@@ -23,10 +23,13 @@ "keywords": [

"build": "make",
"prepublish": "npm run rebuild",
"rebuild": "make clean && make",
"test": "npm run test-asm && npm run test-asm-debug && npm run test-wasm && npm run test-wasm-debug && npm run test-asm-memory-growth",
"test-asm": "node test/all.js asm",
"test-asm-debug": "node test/all.js asm-debug",
"test-asm-memory-growth": "node test/all.js asm-memory-growth",
"test-wasm": "node test/all.js wasm",
"test-wasm-debug": "node test/all.js wasm-debug"
"rebuild": "npm run clean && npm run build",
"clean": "make clean",
"test": "npm run lint && npm run test-asm && npm run test-asm-debug && npm run test-wasm && npm run test-wasm-debug && npm run test-asm-memory-growth",
"lint": "eslint .",
"prettify": "eslint . --fix",
"test-asm": "node --unhandled-rejections=strict test/all.js asm",
"test-asm-debug": "node --unhandled-rejections=strict test/all.js asm-debug",
"test-asm-memory-growth": "node --unhandled-rejections=strict test/all.js asm-memory-growth",
"test-wasm": "node --unhandled-rejections=strict test/all.js wasm",
"test-wasm-debug": "node --unhandled-rejections=strict test/all.js wasm-debug",
"doc": "jsdoc -c .jsdoc.config.json"
},

@@ -36,13 +39,17 @@ "homepage": "https://github.com/gemini-testing/sql.js",

"type": "git",
"url": "https://github.com/gemini-testing/sql.js.git"
"url": "http://github.com/gemini-testing/sql.js.git"
},
"bugs": {
"url": "https://github.com/gemini-testing/sql.js/issues",
"email": "valentina.pakhomova@gmail.com"
"url": "https://github.com/gemini-testing/sql.js/issues"
},
"devDependencies": {
"test": ">=0.6",
"tiny-worker": "^2.1.2"
"clean-jsdoc-theme": "^3.3.4",
"eslint": "^7.32.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-plugin-import": "^2.26.0",
"jsdoc": "^3.6.10",
"puppeteer": "^10.2.0",
"test": "=0.6.0"
},
"dependencies": {}
}

266

README.md

@@ -0,81 +1,111 @@

<img src="https://user-images.githubusercontent.com/552629/76405509-87025300-6388-11ea-86c9-af882abb00bd.png" width="40" height="40" />
# SQLite compiled to JavaScript
[![Build Status](https://travis-ci.org/kripken/sql.js.svg?branch=master)](http://travis-ci.org/kripken/sql.js) [![CDNJS version](https://img.shields.io/cdnjs/v/sql.js.svg)](https://cdnjs.com/libraries/sql.js)
For the impatients, try the demo here: http://kripken.github.io/sql.js/examples/GUI
[![CI status](https://github.com/sql-js/sql.js/workflows/CI/badge.svg)](https://github.com/sql-js/sql.js/actions)
[![npm](https://img.shields.io/npm/v/sql.js)](https://www.npmjs.com/package/sql.js)
[![CDNJS version](https://img.shields.io/cdnjs/v/sql.js.svg)](https://cdnjs.com/libraries/sql.js)
*sql.js* is a port of [SQLite](http://sqlite.org/about.html) to Webassembly, by compiling the SQLite C code with [Emscripten](http://kripken.github.io/emscripten-site/docs/introducing_emscripten/about_emscripten.html). It uses a [virtual database file stored in memory](https://kripken.github.io/emscripten-site/docs/porting/files/file_systems_overview.html), and thus **doesn't persist the changes** made to the database. However, it allows you to **import** any existing sqlite file, and to **export** the created database as a [JavaScript typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays).
*sql.js* is a javascript SQL database. It allows you to create a relational database and query it entirely in the browser. You can try it in [this online demo](https://sql.js.org/examples/GUI/). It uses a [virtual database file stored in memory](https://emscripten.org/docs/porting/files/file_systems_overview.html), and thus **doesn't persist the changes** made to the database. However, it allows you to **import** any existing sqlite file, and to **export** the created database as a [JavaScript typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays).
There are no C bindings or node-gyp compilation here, sql.js is a simple JavaScript file, that can be used like any traditional JavaScript library. If you are 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](https://www.npmjs.com/package/sqlite3).
*sql.js* uses [emscripten](https://emscripten.org/docs/introducing_emscripten/about_emscripten.html) to compile [SQLite](http://sqlite.org/about.html) to webassembly (or to javascript code for compatibility with older browsers). It includes [contributed math and string extension functions](https://www.sqlite.org/contrib?orderby=date).
sql.js can be used like any traditional JavaScript library. If you are 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](https://www.npmjs.com/package/sqlite3). A native binding will not only be faster because it will run native code, but it will also be able to work on database files directly instead of having to load the entire database in memory, avoiding out of memory errors and further improving performances.
SQLite is public domain, sql.js is MIT licensed.
Sql.js predates WebAssembly, and thus started as an [asm.js](https://en.wikipedia.org/wiki/Asm.js) project. It still supports asm.js for backwards compatibility.
## API documentation
A [full API documentation](https://sql.js.org/documentation/) for all the available classes and methods is available.
It is generated from comments inside the source code, and is thus always up to date.
## Version of binaries
Sql.js was last built with:
Emscripten version 1.38.47 (2019-10-03) [Release History](https://emscripten.org/docs/introducing_emscripten/release_notes.html)
SqlLite version: 3.28.0 (2019-04-16) [Release History](https://www.sqlite.org/changes.html)
## Usage
## Documentation
A [full documentation](http://kripken.github.io/sql.js/documentation/#http://kripken.github.io/sql.js/documentation/class/Database.html) generated from comments inside the source code, is available.
By default, *sql.js* uses [wasm](https://developer.mozilla.org/en-US/docs/WebAssembly), and thus needs to load a `.wasm` file in addition to the javascript library. You can find this file in `./node_modules/sql.js/dist/sql-wasm.wasm` after installing sql.js from npm, and instruct your bundler to add it to your static assets or load it from [a CDN](https://cdnjs.com/libraries/sql.js). Then use the [`locateFile`](https://emscripten.org/docs/api_reference/module.html#Module.locateFile) property of the configuration object passed to `initSqlJs` to indicate where the file is. If you use an asset builder such as webpack, you can automate this. See [this demo of how to integrate sql.js with webpack (and react)](https://github.com/sql-js/react-sqljs-demo).
## Usage
```javascript
var initSqlJs = require('sql.js');
const initSqlJs = require('sql.js');
// or if you are in a browser:
// var initSqlJs = window.initSqlJs;
// const initSqlJs = window.initSqlJs;
initSqlJs().then(SQL => {
const SQL = await initSqlJs({
// Required to load the wasm binary asynchronously. Of course, you can host it wherever you want
// You can omit locateFile completely when running in node
locateFile: file => `https://sql.js.org/dist/${file}`
});
// Create a database
var db = new SQL.Database();
// NOTE: You can also use new SQL.Database(data) where
// data is an Uint8Array representing an SQLite database file
// Create a database
const db = new SQL.Database();
// NOTE: You can also use new SQL.Database(data) where
// data is an Uint8Array representing an SQLite database file
// Execute some sql
sqlstr = "CREATE TABLE hello (a int, b char);";
sqlstr += "INSERT INTO hello VALUES (0, 'hello');"
sqlstr += "INSERT INTO hello VALUES (1, 'world');"
db.run(sqlstr); // Run the query without returning anything
var res = db.exec("SELECT * FROM hello");
/*
[
{columns:['a','b'], values:[[0,'hello'],[1,'world']]}
]
*/
// Execute a single SQL string that contains multiple statements
let sqlstr = "CREATE TABLE hello (a int, b char); \
INSERT INTO hello VALUES (0, 'hello'); \
INSERT INTO hello VALUES (1, 'world');";
db.run(sqlstr); // Run the query without returning anything
// Prepare an sql statement
var stmt = db.prepare("SELECT * FROM hello WHERE a=:aval AND b=:bval");
// Prepare an sql statement
const stmt = db.prepare("SELECT * FROM hello WHERE a=:aval AND b=:bval");
// Bind values to the parameters and fetch the results of the query
var result = stmt.getAsObject({':aval' : 1, ':bval' : 'world'});
console.log(result); // Will print {a:1, b:'world'}
// Bind values to the parameters and fetch the results of the query
const result = stmt.getAsObject({':aval' : 1, ':bval' : 'world'});
console.log(result); // Will print {a:1, b:'world'}
// Bind other values
stmt.bind([0, 'hello']);
while (stmt.step()) console.log(stmt.get()); // Will print [0, 'hello']
// Bind other values
stmt.bind([0, 'hello']);
while (stmt.step()) console.log(stmt.get()); // Will print [0, 'hello']
// free the memory used by the statement
stmt.free();
// You can not use your statement anymore once it has been freed.
// But not freeing your statements causes memory leaks. You don't want that.
// You can also use JavaScript functions inside your SQL code
// Create the js function you need
function add(a, b) {return a+b;}
// Specifies the SQL function's name, the number of it's arguments, and the js function to use
db.create_function("add_js", add);
// Run a query in which the function is used
db.run("INSERT INTO hello VALUES (add_js(7, 3), add_js('Hello ', 'world'));"); // Inserts 10 and 'Hello world'
const res = db.exec("SELECT * FROM hello");
/*
[
{columns:['a','b'], values:[[0,'hello'],[1,'world']]}
]
*/
// free the memory used by the statement
stmt.free();
// You can not use your statement anymore once it has been freed.
// But not freeing your statements causes memory leaks. You don't want that.
// You can also use JavaScript functions inside your SQL code
// Create the js function you need
function add(a, b) {return a+b;}
// Specifies the SQL function's name, the number of it's arguments, and the js function to use
db.create_function("add_js", add);
// Run a query in which the function is used
db.run("INSERT INTO hello VALUES (add_js(7, 3), add_js('Hello ', 'world'));"); // Inserts 10 and 'Hello world'
// Export the database to an Uint8Array containing the SQLite database file
var binaryArray = db.export();
});
// You can create custom aggregation functions, by passing a name
// and a set of functions to `db.create_aggregate`:
//
// - an `init` function. This function receives no argument and returns
// the initial value for the state of the aggregate function.
// - a `step` function. This function takes two arguments
// - the current state of the aggregation
// - a new value to aggregate to the state
// It should return a new value for the state.
// - a `finalize` function. This function receives a state object, and
// returns the final value of the aggregate. It can be omitted, in which case
// the final value of the state will be returned directly by the aggregate function.
//
// Here is an example aggregation function, `json_agg`, which will collect all
// input values and return them as a JSON array:
db.create_aggregate(
"json_agg",
{
init: () => [],
step: (state, val) => [...state, val],
finalize: (state) => JSON.stringify(state),
}
);
db.exec("SELECT json_agg(column1) FROM (VALUES ('hello'), ('world'))");
// -> The result of the query is the string '["hello","world"]'
// Export the database to an Uint8Array containing the SQLite database file
const binaryArray = db.export();
```
## Demo
There are a few examples [available here](https://kripken.github.io/sql.js/index.html). The most full-featured is the [Sqlite Interpreter](https://kripken.github.io/sql.js/examples/GUI/index.html).
There are a few examples [available here](https://sql-js.github.io/sql.js/index.html). The most full-featured is the [Sqlite Interpreter](https://sql-js.github.io/sql.js/examples/GUI/index.html).

@@ -92,3 +122,3 @@ ## Examples

config = {
locateFile: filename => `/dist/${filename}`
locateFile: filename => `/dist/${filename}`
}

@@ -99,3 +129,3 @@ // The `initSqlJs` function is globally provided by all of the main dist files if loaded in the browser.

//Create the database
var db = new SQL.Database();
const db = new SQL.Database();
// Run a query without reading the results

@@ -105,11 +135,11 @@ db.run("CREATE TABLE test (col1, col2);");

db.run("INSERT INTO test VALUES (?,?), (?,?)", [1,111,2,222]);
// Prepare a statement
var stmt = db.prepare("SELECT * FROM test WHERE col1 BETWEEN $start AND $end");
const stmt = db.prepare("SELECT * FROM test WHERE col1 BETWEEN $start AND $end");
stmt.getAsObject({$start:1, $end:1}); // {col1:1, col2:111}
// Bind new values
stmt.bind({$start:1, $end:2});
while(stmt.step()) { //
var row = stmt.getAsObject();
const row = stmt.getAsObject();
console.log('Here is a row: ' + JSON.stringify(row));

@@ -130,6 +160,6 @@ }

dbFileElm.onchange = () => {
var f = dbFileElm.files[0];
var r = new FileReader();
const f = dbFileElm.files[0];
const r = new FileReader();
r.onload = function() {
var Uints = new Uint8Array(r.result);
const Uints = new Uint8Array(r.result);
db = new SQL.Database(Uints);

@@ -140,8 +170,21 @@ }

```
See : http://kripken.github.io/sql.js/examples/GUI/gui.js
See : https://sql-js.github.io/sql.js/examples/GUI/gui.js
#### Loading a database from a server
##### using fetch
```javascript
var xhr = new XMLHttpRequest();
const sqlPromise = initSqlJs({
locateFile: file => `https://path/to/your/dist/folder/dist/${file}`
});
const dataPromise = fetch("/path/to/database.sqlite").then(res => res.arrayBuffer());
const [SQL, buf] = await Promise.all([sqlPromise, dataPromise])
const db = new SQL.Database(new Uint8Array(buf));
```
##### using XMLHttpRequest
```javascript
const xhr = new XMLHttpRequest();
// For example: https://github.com/lerocha/chinook-database/raw/master/ChinookDatabase/DataSources/Chinook_Sqlite.sqlite

@@ -152,5 +195,5 @@ xhr.open('GET', '/path/to/database.sqlite', true);

xhr.onload = e => {
var uInt8Array = new Uint8Array(this.response);
var db = new SQL.Database(uInt8Array);
var contents = db.exec("SELECT * FROM my_table");
const uInt8Array = new Uint8Array(xhr.response);
const db = new SQL.Database(uInt8Array);
const contents = db.exec("SELECT * FROM my_table");
// contents is now [{columns:['col1','col2',...], values:[[first row], [second row], ...]}]

@@ -160,3 +203,3 @@ };

```
See: https://github.com/kripken/sql.js/wiki/Load-a-database-from-the-server
See: https://github.com/sql-js/sql.js/wiki/Load-a-database-from-the-server

@@ -171,9 +214,9 @@

```javascript
var fs = require('fs');
var initSqlJs = require('sql-wasm.js');
var filebuffer = fs.readFileSync('test.sqlite');
const fs = require('fs');
const initSqlJs = require('sql-wasm.js');
const filebuffer = fs.readFileSync('test.sqlite');
initSqlJs().then(function(SQL){
// Load the db
var db = new SQL.Database(filebuffer);
const db = new SQL.Database(filebuffer);
});

@@ -186,10 +229,10 @@

```javascript
var fs = require("fs");
const fs = require("fs");
// [...] (create the database)
var data = db.export();
var buffer = new Buffer(data);
const data = db.export();
const buffer = Buffer.from(data);
fs.writeFileSync("filename.sqlite", buffer);
```
See : https://github.com/kripken/sql.js/blob/master/test/test_node_file.js
See : https://github.com/sql-js/sql.js/blob/master/test/test_node_file.js

@@ -200,3 +243,3 @@ ### Use as web worker

You will need to download [dist/worker.sql-wasm.js](dist/worker.sql-wasm.js) [dist/worker.sql-wasm.wasm](dist/worker.sql-wasm.wasm).
You will need to download `worker.sql-wasm.js` and `worker.sql-wasm.wasm` from the [release page](https://github.com/sql-js/sql.js/releases).

@@ -206,3 +249,3 @@ Example:

<script>
var worker = new Worker("/dist/worker.sql-wasm.js");
const worker = new Worker("/dist/worker.sql-wasm.js");
worker.onmessage = () => {

@@ -213,7 +256,8 @@ console.log("Database opened");

};
worker.postMessage({
id: 2,
action: 'exec',
sql: 'SELECT * FROM test'
action: "exec",
sql: "SELECT age,name FROM test WHERE id=$id",
params: { "$id": 1 }
});

@@ -225,3 +269,3 @@ };

id:1,
action:'open',
action:"open",
buffer:buf, /*Optional. An ArrayBuffer representing an SQLite Database file*/

@@ -231,3 +275,30 @@ });

```
### Enabling BigInt support
If you need ```BigInt``` support, it is partially supported since most browsers now supports it including Safari.Binding ```BigInt``` is still not supported, only getting ```BigInt``` from the database is supported for now.
```html
<script>
const stmt = db.prepare("SELECT * FROM test");
const config = {useBigInt: true};
/*Pass optional config param to the get function*/
while (stmt.step()) console.log(stmt.get(null, config));
/*OR*/
const result = db.exec("SELECT * FROM test", config);
console.log(results[0].values)
</script>
```
On WebWorker, you can just add ```config``` param before posting a message. With this, you wont have to pass config param on ```get``` function.
```html
<script>
worker.postMessage({
id:1,
action:"exec",
sql: "SELECT * FROM test",
config: {useBigInt: true}, /*Optional param*/
});
</script>
```
See [examples/GUI/gui.js](examples/GUI/gui.js) for a full working example.

@@ -241,3 +312,3 @@

Version 1.0 of sql.js must be loaded asynchronously, whereas asm.js was able to be loaded synchronously.
Version 1.0 of sql.js must be loaded asynchronously, whereas asm.js was able to be loaded synchronously.

@@ -248,3 +319,3 @@ So in the past, you would:

<script>
var db = new SQL.Database();
const db = new SQL.Database();
//...

@@ -255,4 +326,4 @@ </script>

```javascript
var SQL = require('sql.js');
var db = new SQL.Database();
const SQL = require('sql.js');
const db = new SQL.Database();
//...

@@ -266,3 +337,3 @@ ```

initSqlJs({ locateFile: filename => `/dist/${filename}` }).then(function(SQL){
var db = new SQL.Database();
const db = new SQL.Database();
//...

@@ -274,5 +345,5 @@ });

```javascript
var initSqlJs = require('sql-wasm.js');
const initSqlJs = require('sql-wasm.js');
initSqlJs().then(function(SQL){
var db = new SQL.Database();
const db = new SQL.Database();
//...

@@ -285,3 +356,3 @@ });

### Downloading/Using: ###
Although asm.js files were distributed as a single Javascript file, WebAssembly libraries are most efficiently distributed as a pair of files, the `.js` loader and the `.wasm` file, like [dist/sql-wasm.js]([dist/sql-wasm.js]) and [dist/sql-wasm.wasm]([dist/sql-wasm.wasm]). The `.js` file is responsible for wrapping/loading the `.wasm` file.
Although asm.js files were distributed as a single Javascript file, WebAssembly libraries are most efficiently distributed as a pair of files, the `.js` loader and the `.wasm` file, like `sql-wasm.js` and `sql-wasm.wasm`. The `.js` file is responsible for loading the `.wasm` file. You can find these files on our [release page](https://github.com/sql-js/sql.js/releases)

@@ -291,3 +362,6 @@

## Versions of sql.js included in `dist/`
## Versions of sql.js included in the distributed artifacts
You can always find the latest published artifacts on https://github.com/sql-js/sql.js/releases/latest.
For each [release](https://github.com/sql-js/sql.js/releases/), you will find a file called `sqljs.zip` in the *release assets*. It will contain:
- `sql-wasm.js` : The Web Assembly version of Sql.js. Minified and suitable for production. Use this. If you use this, you will need to include/ship `sql-wasm.wasm` as well.

@@ -300,6 +374,6 @@ - `sql-wasm-debug.js` : The Web Assembly, Debug version of Sql.js. Larger, with assertions turned on. Useful for local development. You will need to include/ship `sql-wasm-debug.wasm` if you use this.

## Compiling
## Compiling/Contributing
- Install the EMSDK, [as described here](https://kripken.github.io/emscripten-site/docs/getting_started/downloads.html)
- Run `npm run rebuild`
General consumers of this library don't need to read any further. (The compiled files are available via the [release page](https://github.com/sql-js/sql.js/releases).)
If you want to compile your own version of SQLite for WebAssembly, or want to contribute to this project, see [CONTRIBUTING.md](CONTRIBUTING.md).

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

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