Skip to content

Commit 9d90bb3

Browse files
committed
WIP: Update the README to show async usage and describe versions of the library
1 parent 2e3fa51 commit 9d90bb3

File tree

1 file changed

+105
-74
lines changed

1 file changed

+105
-74
lines changed

README.md

Lines changed: 105 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -15,53 +15,58 @@ A [full documentation](http://kripken.github.io/sql.js/documentation/#http://kri
1515
## Usage
1616

1717
```javascript
18-
var sql = require('sql.js');
19-
// or sql = window.SQL if you are in a browser
20-
21-
// Create a database
22-
var db = new sql.Database();
23-
// NOTE: You can also use new sql.Database(data) where
24-
// data is an Uint8Array representing an SQLite database file
25-
26-
// Execute some sql
27-
sqlstr = "CREATE TABLE hello (a int, b char);";
28-
sqlstr += "INSERT INTO hello VALUES (0, 'hello');"
29-
sqlstr += "INSERT INTO hello VALUES (1, 'world');"
30-
db.run(sqlstr); // Run the query without returning anything
31-
32-
var res = db.exec("SELECT * FROM hello");
33-
/*
34-
[
35-
{columns:['a','b'], values:[[0,'hello'],[1,'world']]}
36-
]
37-
*/
38-
39-
// Prepare an sql statement
40-
var stmt = db.prepare("SELECT * FROM hello WHERE a=:aval AND b=:bval");
41-
42-
// Bind values to the parameters and fetch the results of the query
43-
var result = stmt.getAsObject({':aval' : 1, ':bval' : 'world'});
44-
console.log(result); // Will print {a:1, b:'world'}
45-
46-
// Bind other values
47-
stmt.bind([0, 'hello']);
48-
while (stmt.step()) console.log(stmt.get()); // Will print [0, 'hello']
49-
50-
// You can also use javascript functions inside your SQL code
51-
// Create the js function you need
52-
function add(a, b) {return a+b;}
53-
// Specifies the SQL function's name, the number of it's arguments, and the js function to use
54-
db.create_function("add_js", add);
55-
// Run a query in which the function is used
56-
db.run("INSERT INTO hello VALUES (add_js(7, 3), add_js('Hello ', 'world'));"); // Inserts 10 and 'Hello world'
57-
58-
// free the memory used by the statement
59-
stmt.free();
60-
// You can not use your statement anymore once it has been freed.
61-
// But not freeing your statements causes memory leaks. You don't want that.
62-
63-
// Export the database to an Uint8Array containing the SQLite database file
64-
var binaryArray = db.export();
18+
var initSqlJs = require('sql-wasm.js');
19+
20+
// or initSqlJs = window.initSqlJs if you are in a browser
21+
22+
initSqlJs().then(function(SQL){
23+
24+
// Create a database
25+
var db = new SQL.Database();
26+
// NOTE: You can also use new SQL.Database(data) where
27+
// data is an Uint8Array representing an SQLite database file
28+
29+
// Execute some sql
30+
sqlstr = "CREATE TABLE hello (a int, b char);";
31+
sqlstr += "INSERT INTO hello VALUES (0, 'hello');"
32+
sqlstr += "INSERT INTO hello VALUES (1, 'world');"
33+
db.run(sqlstr); // Run the query without returning anything
34+
35+
var res = db.exec("SELECT * FROM hello");
36+
/*
37+
[
38+
{columns:['a','b'], values:[[0,'hello'],[1,'world']]}
39+
]
40+
*/
41+
42+
// Prepare an sql statement
43+
var stmt = db.prepare("SELECT * FROM hello WHERE a=:aval AND b=:bval");
44+
45+
// Bind values to the parameters and fetch the results of the query
46+
var result = stmt.getAsObject({':aval' : 1, ':bval' : 'world'});
47+
console.log(result); // Will print {a:1, b:'world'}
48+
49+
// Bind other values
50+
stmt.bind([0, 'hello']);
51+
while (stmt.step()) console.log(stmt.get()); // Will print [0, 'hello']
52+
53+
// You can also use javascript functions inside your SQL code
54+
// Create the js function you need
55+
function add(a, b) {return a+b;}
56+
// Specifies the SQL function's name, the number of it's arguments, and the js function to use
57+
db.create_function("add_js", add);
58+
// Run a query in which the function is used
59+
db.run("INSERT INTO hello VALUES (add_js(7, 3), add_js('Hello ', 'world'));"); // Inserts 10 and 'Hello world'
60+
61+
// free the memory used by the statement
62+
stmt.free();
63+
// You can not use your statement anymore once it has been freed.
64+
// But not freeing your statements causes memory leaks. You don't want that.
65+
66+
// Export the database to an Uint8Array containing the SQLite database file
67+
var binaryArray = db.export();
68+
});
69+
6570
```
6671

6772
## Demo
@@ -72,24 +77,27 @@ The test files provide up to date example of the use of the api.
7277
### Inside the browser
7378
#### Example **HTML** file:
7479
```html
75-
<script src='js/sql.js'></script>
80+
<script src='dist/sql-wasm.js'></script>
7681
<script>
77-
//Create the database
78-
var db = new SQL.Database();
79-
// Run a query without reading the results
80-
db.run("CREATE TABLE test (col1, col2);");
81-
// Insert two rows: (1,111) and (2,222)
82-
db.run("INSERT INTO test VALUES (?,?), (?,?)", [1,111,2,222]);
83-
84-
// Prepare a statement
85-
var stmt = db.prepare("SELECT * FROM test WHERE col1 BETWEEN $start AND $end");
86-
stmt.getAsObject({$start:1, $end:1}); // {col1:1, col2:111}
87-
88-
// Bind new values
89-
stmt.bind({$start:1, $end:2});
90-
while(stmt.step()) { //
91-
var row = stmt.getAsObject();
92-
// [...] do something with the row of result
82+
//the `initSqlJs` function is globally provided by all of the main dist files if loaded in the browser.
83+
initSqlJs().then(function(SQL){
84+
//Create the database
85+
var db = new SQL.Database();
86+
// Run a query without reading the results
87+
db.run("CREATE TABLE test (col1, col2);");
88+
// Insert two rows: (1,111) and (2,222)
89+
db.run("INSERT INTO test VALUES (?,?), (?,?)", [1,111,2,222]);
90+
91+
// Prepare a statement
92+
var stmt = db.prepare("SELECT * FROM test WHERE col1 BETWEEN $start AND $end");
93+
stmt.getAsObject({$start:1, $end:1}); // {col1:1, col2:111}
94+
95+
// Bind new values
96+
stmt.bind({$start:1, $end:2});
97+
while(stmt.step()) { //
98+
var row = stmt.getAsObject();
99+
// [...] do something with the row of result
100+
}
93101
}
94102
</script>
95103
```
@@ -136,11 +144,14 @@ Alternatively, you can simply download the file `sql.js`, from the download link
136144
#### read a database from the disk:
137145
```javascript
138146
var fs = require('fs');
139-
var SQL = require('sql.js');
147+
var initSqlJs = require('sql-wasm.js');
140148
var filebuffer = fs.readFileSync('test.sqlite');
149+
150+
initSqlJs().then(function(SqlJs){
151+
// Load the db
152+
var db = new SqlJs.Database(filebuffer);
153+
});
141154
142-
// Load the db
143-
var db = new SQL.Database(filebuffer);
144155
```
145156
146157
#### write a database to the disk
@@ -159,12 +170,12 @@ See : https://github.com/kripken/sql.js/blob/master/test/test_node_file.js
159170
If you don't want to run CPU-intensive SQL queries in your main application thread,
160171
you can use the *more limited* WebWorker API.
161172
162-
You will need to download `worker.sql.js`
173+
You will need to download `worker.sql-wasm.js`
163174
164175
Example:
165176
```html
166177
<script>
167-
var worker = new Worker("js/worker.sql.js"); // You can find worker.sql.js in this repo
178+
var worker = new Worker("dist/worker.sql-wasm.js"); // You can find worker.sql.js in this repo
168179
worker.onmessage = () => {
169180
console.log("Database opened");
170181
worker.onmessage = event => {
@@ -189,11 +200,31 @@ Example:
189200
190201
See : https://github.com/kripken/sql.js/blob/master/test/test_worker.js
191202
192-
## Downloads
193-
- You can download `sql.js` here : https://raw.githubusercontent.com/kripken/sql.js/master/js/sql.js
194-
- And the Web Worker version: https://raw.githubusercontent.com/kripken/sql.js/master/js/worker.sql.js
195-
- You can find a non minified or optimized version for debugging, `sql-debug.js` here : https://raw.githubusercontent.com/kripken/sql.js/master/js/sql-debug.js
196-
- If you see the message, `Cannot enlarge memory arrays`, try this version, `sql-memory-growth.js` here : https://raw.githubusercontent.com/kripken/sql.js/master/js/sql-memory-growth.js
203+
## Flavors/versions Targets/Downloads
204+
205+
This library includes both asm.js and WebAssembly versions of Sqlite. (WebAssembly is the newer, preferred way to compile to Javascript, and has superceded asm.js. It produces smaller, faster code.) Asm.js versions are included for compatibility.
206+
207+
## Upgrading from previous versions
208+
209+
Version 1.0 of sql.js introduces a number of breaking changes due primarily to the fact that WASM must be loaded asynchronously, whereas asm.js was able to be loaded synchronously.
210+
211+
TODO: More info here:
212+
213+
214+
## Versions of sql.js included in `dist/`
215+
- `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.
216+
- `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.
217+
- `sql-asm.js` : The older asm.js version of Sql.js. Slower and larger. Provided for compatiblity reasons.
218+
- `sql-asm-memory-growth.js` : Asm.js doesn't allow for memory to grow by default, because it is slower and de-optimizes. If you are using sql-asm.js and you see this error (`Cannot enlarge memory arrays`), use this file.
219+
- `sql-asm-debug.js` : The _Debug_ asm.js version of Sql.js. If using sql-asm.js, use this for local development.
220+
- `worker.*` - Web Worker versions of the above libraries
221+
Asm.js builds are included for backwards compatilbility.
222+
223+
## Compiling
224+
225+
- Install the EMSDK, [as described here](https://kripken.github.io/emscripten-site/docs/getting_started/downloads.html)
226+
- Run `npm rebuild`
227+
197228
198229
## Related
199230

0 commit comments

Comments
 (0)