Skip to content

Commit 5e0d84d

Browse files
committed
Improve example code.
1 parent be03bf4 commit 5e0d84d

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

readme.md

+24-7
Original file line numberDiff line numberDiff line change
@@ -20,48 +20,65 @@ You also need to install an IO provider.
2020
See `./test/all` for detailed usage.
2121

2222

23+
#### Get a file ref
2324
```javascript
24-
2525
// Note: Example is for Node.js.
2626
const {smpi} = require("sqlite-mpi-client-js");
2727
const iop = require("smpi-iop-node-ffi");
2828

2929
const c = smpi.newClient(iop);
3030
const f = c.newFileRef("/tmp/db-file.sqlite");
31+
```
32+
33+
#### Write transactions
34+
35+
- There is only one active write transaction at a time per SQLite file.
36+
- This applies at an OS level, for all processes.
37+
38+
- Write tx requests will queue when there is an active outstanding write tx.
39+
- Queued write tx requests will resolve in First In First Out (FIFO) manner.
40+
- When the promise returned from `f.getWriteTx()` resolves, the `wtx` is active.
3141

3242

33-
// Write Transaction.
43+
```javascript
3444
const wtx = await f.getWriteTx();
3545

3646
// `q` can be a read or write with write txs.
3747
await wtx.q("SELECT 1+1");
48+
3849
await wtx.read("SELECT 1+1");
50+
3951
await wtx.write("INSERT ...");
4052

4153
await wtx.commit();
4254
// await wtx.rollback();
55+
```
4356

57+
#### Read transactions
4458

59+
- Read transactions are concurrent; you can have many read transactions active and reading from the same database file.
4560

46-
// Read Transaction.
61+
```javascript
4762
const rtx = await f.getReadTx();
4863

4964
// `q` must be a read.
50-
const rtx = await f.getReadTx();
5165
await rtx.q("SELECT 1+1");
5266

5367
await rtx.read("SELECT 1+1");
5468

5569
// No writes.
5670
// await tx.write("INSERT ...");
5771

58-
5972
await rtx.commit();
6073
// await rtx.rollback();
74+
```
75+
6176

6277

78+
#### Params
6379

64-
// Params
80+
81+
```javascript
6582
const wtx = await f.getWriteTx();
6683

6784
// Index based.
@@ -71,7 +88,7 @@ await wtx.write("INSERT INTO t1 (b) VALUES (?1), (?2)", [3, 4]);
7188
// Key based.
7289
await wtx.write("INSERT INTO t1 (b) VALUES (:x), (:y)", {x: 1, y: 2});
7390
await wtx.write("INSERT INTO t1 (b) VALUES (@x), (@y)", {x: 1, y: 2});
74-
await rtx.commit();
91+
await wtx.commit();
7592
```
7693

7794
### Notes

0 commit comments

Comments
 (0)