Skip to content

Commit

Permalink
Slightly more modern init code
Browse files Browse the repository at this point in the history
Fixes #63. (Avoids top-level `await` for the time being…)
  • Loading branch information
tomayac authored May 15, 2024
1 parent 64e8a46 commit 63f713e
Showing 1 changed file with 47 additions and 48 deletions.
95 changes: 47 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

SQLite Wasm conveniently wrapped as an ES Module.

> **Warning**
> [!Warning]
>
> This project wraps the code of
> [SQLite Wasm](https://sqlite.org/wasm/doc/trunk/index.md) with _no_ changes,
Expand Down Expand Up @@ -32,7 +32,7 @@ storage back-end.

### In a wrapped worker (with OPFS if available):

> **Warning**
> [!Warning]
>
> For this to work, you need to set the following headers on your server:
>
Expand All @@ -43,35 +43,29 @@ storage back-end.
```js
import { sqlite3Worker1Promiser } from '@sqlite.org/sqlite-wasm';

const log = (...args) => console.log(...args);
const error = (...args) => console.error(...args);
const log = console.log;
const error = console.error;

(async () => {
const initializeSQLite = async () => {
try {
log('Loading and initializing SQLite3 module...');

const promiser = await new Promise((resolve) => {
const _promiser = sqlite3Worker1Promiser({
onready: () => {
resolve(_promiser);
},
});
const _promiser = sqlite3Worker1Promiser({ onready: () => resolve(_promiser) });
});

log('Done initializing. Running demo...');

let response;
const configResponse = await promiser('config-get', {});
log('Running SQLite3 version', configResponse.result.version.libVersion);

response = await promiser('config-get', {});
log('Running SQLite3 version', response.result.version.libVersion);

response = await promiser('open', {
const openResponse = await promiser('open', {
filename: 'file:mydb.sqlite3?vfs=opfs',
});
const { dbId } = response;
const { dbId } = openResponse;
log(
'OPFS is available, created persisted database at',
response.result.filename.replace(/^file:(.*?)\?vfs=opfs$/, '$1'),
openResponse.result.filename.replace(/^file:(.*?)\?vfs=opfs$/, '$1')
);
// Your SQLite code here.
} catch (err) {
Expand All @@ -80,15 +74,17 @@ const error = (...args) => console.error(...args);
}
error(err.name, err.message);
}
})();
};

initializeSQLite();
```

The `promiser` object above implements the
[Worker1 API](https://sqlite.org/wasm/doc/trunk/api-worker1.md#worker1-methods).

### In a worker (with OPFS if available):

> **Warning**
> [!Warning]
>
> For this to work, you need to set the following headers on your server:
>
Expand All @@ -105,34 +101,34 @@ const worker = new Worker('worker.js', { type: 'module' });
// In `worker.js`.
import sqlite3InitModule from '@sqlite.org/sqlite-wasm';

const log = (...args) => console.log(...args);
const error = (...args) => console.error(...args);
const log = console.log;
const error = console.error;

const start = function (sqlite3) {
const start = (sqlite3) => {
log('Running SQLite3 version', sqlite3.version.libVersion);
let db;
if ('opfs' in sqlite3) {
db = new sqlite3.oo1.OpfsDb('/mydb.sqlite3');
log('OPFS is available, created persisted database at', db.filename);
} else {
db = new sqlite3.oo1.DB('/mydb.sqlite3', 'ct');
log('OPFS is not available, created transient database', db.filename);
}
const db = 'opfs' in sqlite3
? new sqlite3.oo1.OpfsDb('/mydb.sqlite3')
: new sqlite3.oo1.DB('/mydb.sqlite3', 'ct');
log(
'opfs' in sqlite3
? `OPFS is available, created persisted database at ${db.filename}`
: `OPFS is not available, created transient database ${db.filename}`
);
// Your SQLite code here.
};

log('Loading and initializing SQLite3 module...');
sqlite3InitModule({
print: log,
printErr: error,
}).then((sqlite3) => {
log('Done initializing. Running demo...');
const initializeSQLite = async () => {
try {
log('Loading and initializing SQLite3 module...');
const sqlite3 = await sqlite3InitModule({ print: log, printErr: error });
log('Done initializing. Running demo...');
start(sqlite3);
} catch (err) {
error(err.name, err.message);
error('Initialization error:', err.name, err.message);
}
});
};

initializeSQLite();
```

The `db` object above implements the
Expand All @@ -143,27 +139,30 @@ The `db` object above implements the
```js
import sqlite3InitModule from '@sqlite.org/sqlite-wasm';

const log = (...args) => console.log(...args);
const error = (...args) => console.error(...args);
const log = console.log;
const error = console.error;

const start = function (sqlite3) {
const start = (sqlite3) => {
log('Running SQLite3 version', sqlite3.version.libVersion);
const db = new sqlite3.oo1.DB('/mydb.sqlite3', 'ct');
// Your SQLite code here.
};

log('Loading and initializing SQLite3 module...');
sqlite3InitModule({
print: log,
printErr: error,
}).then((sqlite3) => {
const initializeSQLite = async () => {
try {
log('Loading and initializing SQLite3 module...');
const sqlite3 = await sqlite3InitModule({
print: log,
printErr: error,
});
log('Done initializing. Running demo...');
start(sqlite3);
} catch (err) {
error(err.name, err.message);
error('Initialization error:', err.name, err.message);
}
});
};

initializeSQLite();
```

The `db` object above implements the
Expand Down

0 comments on commit 63f713e

Please sign in to comment.