Skip to content

Commit

Permalink
Merge pull request #4 from magieno/add-client-types
Browse files Browse the repository at this point in the history
- Exploring how the sqlite-client could work with different sqlite adapters
  • Loading branch information
etiennenoel authored Mar 9, 2024
2 parents 6b3e652 + dc48ac0 commit d2de3f9
Show file tree
Hide file tree
Showing 38 changed files with 18,081 additions and 651 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: build
on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
build:
name: Build
# Set the type of machine to run on
runs-on: ubuntu-latest
timeout-minutes: 15
if: github.ref != 'refs/heads/master'

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
always-auth: true
node-version: ${{ matrix.node-version }}
- run: npm -v
- run: npm ci
- run: npm run package

# Run the tests
- run: npm ci
working-directory: e2e
- run: npm run build
working-directory: e2e
- run: npm run test
working-directory: e2e
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ testem.log
Thumbs.db

*/node_modules/*
*/dist/
**/*/dist/

e2e/*/assets/*
e2e/coverage
70 changes: 62 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,100 @@

SQLite Client is a wrapper for [SQLite](https://github.com/sqlite/sqlite-wasm) that uses the Origin Private File System (OPFS) to persist the SQLite database file.

It only supports OPFS as a persistence mechanism.
This library supports the following backing mechanism:
* In Memory (Main Thread)
* In Memory (Worker)
* OPFS (Worker)
* OPFS SyncAccessHandles (Worker)

## Installation using NPM

This library has two important files: `sqlite-client.js` and `sqlite-client-worker.js`.
Due to some browser restrictions, SQLite WASM can only persist over OPFS when executed in a Worker.
Behind the scenes, Sqlite communicates with a worker to run the SQL statements and return you the results
on the main thread.
Behind the scenes, the SqliteClient communicates with a worker (if needed) to run the SQL statements and return you the results
on the main thread. It also supports the Memory Main Thread mode.

1- Install the NPM package

```
npm install @magieno/sqlite-client
```

2- Import the `sqlite-client` library in your code and use it as such:
1- Import the `sqlite-client` library in your code and use it as such:

**Memory Main Thread**
```
import {SqliteClient} from "@magieno/sqlite-client";
const sqliteWorkerPath = "assets/js/sqlite-client-worker.js"; // Must correspond to the path in your final deployed build.
const filename = "/test.sqlite3"; // This is the name of your database. It corresponds to the path in the OPFS.
const sqliteClient = new SqliteClient({
type: SqliteClientTypeEnum.MemoryMainThread,
filename,
flags: "c", // See sqlite documentation for which flags to use
})
```

**Memory Worker**
```
import {SqliteClient} from "@magieno/sqlite-client";
const sqliteWorkerPath = "assets/js/sqlite-client-worker.js"; // Must correspond to the path in your final deployed build.
const filename = "/test.sqlite3"; // This is the name of your database. It corresponds to the path in the OPFS.
const sqliteClient = new SqliteClient({
type: SqliteClientTypeEnum.MemoryWorker,
filename,
sqliteWorkerPath,
flags: "c", // See sqlite documentation for which flags to use
})
```

**OPFS Worker**
```
import {SqliteClient} from "@magieno/sqlite-client";
const sqliteWorkerPath = "assets/js/sqlite-client-worker.js"; // Must correspond to the path in your final deployed build.
const filename = "/test.sqlite3"; // This is the name of your database. It corresponds to the path in the OPFS.
const sqliteClient = new SqliteClient({
type: SqliteClientTypeEnum.OpfsWorker,
filename,
sqliteWorkerPath,
flags: "c", // See sqlite documentation for which flags to use
})
```

**OPFS Sync Access Handle Worker**
```
import {SqliteClient} from "@magieno/sqlite-client";
const sqliteWorkerPath = "assets/js/sqlite-client-worker.js"; // Must correspond to the path in your final deployed build.
const filename = "/test.sqlite3"; // This is the name of your database. It corresponds to the path in the OPFS.
const sqliteClient = new SqliteClient({
type: SqliteClientTypeEnum.OpfsSahWorker,
filename,
sqliteWorkerPath,
})
```

1- With the client instantiated, you need to initiate it and then you can execute SQL Queries

```
await sqliteClient.init();
await sqliteClient.executeSql("CREATE TABLE IF NOT EXISTS test(a,b)");
await sqliteClient.executeSql("INSERT INTO test VALUES(?, ?)", [6,7]);
const results = await sqliteClient.executeSql("SELECT * FROM test");
```

3- Copy the `node_modules/@magieno/sqlite-client/dist/bundle/sqlite-client-worker.js` to your final bundle
1- Copy the `node_modules/@magieno/sqlite-client/dist/bundle/sqlite-client-worker.js` to your final bundle
This is dependent on the framework you are using but the idea is that this .js file should be copied and available in your build.

4- Copy the files `node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/*` file to your final bundle next to `sqlite-client-worker.js`.
1- Copy the files `node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm/*` file to your final bundle next to `sqlite-client-worker.js`.

5- **Warning** Your server must set the following Http headers when serving your files
1- **Warning** Your server must set the following Http headers when serving your files

`Cross-Origin-Opener-Policy: same-origin`

Expand Down
6 changes: 6 additions & 0 deletions e2e/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
E2E testing
==========




6 changes: 6 additions & 0 deletions e2e/jest-puppeteer.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
launch: {
headless: true
},
browserContext: 'default',
}
8 changes: 8 additions & 0 deletions e2e/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type {Config} from 'jest';

export default async (): Promise<Config> => {
return {
verbose: true,
preset: "jest-puppeteer"
};
};
Loading

0 comments on commit d2de3f9

Please sign in to comment.