Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ sqlite3InitModule({
});
```

The `db` object above implements the [Object Oriented API #1](https://sqlite.org/wasm/doc/tip/api-oo1.md).

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

> **Warning**
Expand Down Expand Up @@ -120,7 +122,7 @@ const sqliteWorkerPath = 'assets/js/sqlite-worker.js';
// This is the name of your database. It corresponds to the path in the OPFS.
const filename = '/test.sqlite3';

const sqlite = new Sqlite(filename, sqliteWorkerPath);
const sqlite = new SqliteClient(filename, sqliteWorkerPath);
await sqlite.init();

await sqlite.executeSql('CREATE TABLE IF NOT EXISTS test(a,b)');
Expand Down
9 changes: 7 additions & 2 deletions src/sqlite-client.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ export class SqliteClient {

dbFile = '';
sqliteWorkerPath = '';
rowMode = 'object';

constructor(dbFile, sqliteWorkerPath) {
constructor(dbFile, sqliteWorkerPath, rowMode) {
if (typeof dbFile !== 'string') {
throw new Error(
`The 'dbFile' parameter passed to the 'SqliteClient' constructor must be of type 'string'. Instead, you passed: '${typeof dbFile}'.`,
Expand All @@ -21,6 +22,10 @@ export class SqliteClient {

this.dbFile = dbFile;
this.sqliteWorkerPath = sqliteWorkerPath;
if (rowMode && rowMode !== 'array' && rowMode !== 'object') {
throw new Error('Invalid rowMode');
}
this.rowMode = rowMode || this.rowMode;
}

async init() {
Expand All @@ -32,7 +37,7 @@ export class SqliteClient {

this.sqliteWorker = await new SqliteWorker();

await this.sqliteWorker.init(this.dbFile);
await this.sqliteWorker.init(this.dbFile, this.rowMode);
}

async executeSql(sqlStatement, bindParameters = []) {
Expand Down
9 changes: 7 additions & 2 deletions src/sqlite-worker.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ const error = (...args) => console.error(...args);

class SqliteWorker {
db;
init(dbFile) {
rowMode = 'object';
init(dbFile, rowMode) {
if (rowMode && rowMode !== 'array' && rowMode !== 'object') {
throw new Error('Invalid rowMode');
}
this.rowMode = rowMode || this.rowMode;
return new Promise((resolve) => {
sqlite3InitModule({
print: log,
Expand All @@ -29,7 +34,7 @@ class SqliteWorker {
sql: sqlStatement,
bind: bindParameters,
returnValue: 'resultRows',
rowMode: 'array',
rowMode: this.rowMode,
}),
);
}
Expand Down