@zukhruf/sqlite-open 0.8.14 → 0.8.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -80,17 +80,19 @@ true
80
80
 
81
81
  Each journal is a strategy object: the caller gives an instance, for example `open(path, { journal: new RollbackJournal() })`. A new journal is a class that implements `Journal`, with one method: `apply(database)`.
82
82
 
83
- | Journal | What it does | Use it for |
84
- | ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------- |
85
- | `WriteAheadLog` (default) | `PRAGMA journal_mode = WAL` and `PRAGMA journal_size_limit = 67108864` (64 MiB). SQLite deletes `-wal` and `-shm` at the last close. | An owner whose readers can write the folder. |
86
- | `PersistentWriteAheadLog` | The same steps, after the native library turns on persistent WAL. `-wal` and `-shm` stay after the last close. | An owner whose readers cannot write the folder, for example agents that run `sqlite3 -readonly` in a sandbox. |
87
- | `RollbackJournal` | `PRAGMA journal_mode = DELETE`. The first access rolls back a hot journal. | A file that the owner builds and then renames or deletes. A copy that must lose a hot journal. |
88
- | `UnchangedJournal` | Nothing. | An owner that sets its own journal, for example a lock file whose `PRAGMA` must wait for the lock. |
83
+ | Journal | What it does | Use it for |
84
+ | ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------- |
85
+ | `WriteAheadLog` (default) | `PRAGMA journal_mode = WAL`, `PRAGMA journal_size_limit = 67108864` (64 MiB) and `PRAGMA synchronous = NORMAL`. SQLite deletes `-wal` and `-shm` at the last close. | An owner whose readers can write the folder. |
86
+ | `PersistentWriteAheadLog` | The same steps, after the native library turns on persistent WAL. `-wal` and `-shm` stay after the last close. | An owner whose readers cannot write the folder, for example agents that run `sqlite3 -readonly` in a sandbox. |
87
+ | `RollbackJournal` | `PRAGMA journal_mode = DELETE`. The first access rolls back a hot journal. | A file that the owner builds and then renames or deletes. A copy that must lose a hot journal. |
88
+ | `UnchangedJournal` | Nothing. | An owner that sets its own journal, for example a lock file whose `PRAGMA` must wait for the lock. |
89
89
 
90
90
  The size limit keeps a write-ahead log small after a large commit. Without it, the `-wal` file of a 20 MB commit stays 20 MB.
91
91
 
92
92
  `WriteAheadLog` and `PersistentWriteAheadLog` take an optional page size, for example `new WriteAheadLog({ pageSize: 32768 })`. SQLite sets the page size of a file only before its first write, and never in a write-ahead log. The write-ahead log step is that first write, so the journal sets the page size just before it. An existing file keeps its own page size.
93
93
 
94
+ A write-ahead log syncs the `-wal` file to the disk before each checkpoint, not at each commit (ADR 0007). A crash of the program loses no commit. A power loss or a crash of the operating system can roll back the commits after the last checkpoint. The database file stays consistent. An owner whose commits must survive a power loss gives `synchronous: 'full'`, for example `new WriteAheadLog({ synchronous: 'full' })`. SQLite then syncs at each commit. On Linux, a commit of one row is about 12 times slower with `'full'`.
95
+
94
96
  ## Errors
95
97
 
96
98
  | Error | When |
@@ -124,7 +126,7 @@ A bundler that bundles this package does not copy the `lib` folder. A bundle tha
124
126
  | Apple's system SQLite | It keeps persistent WAL by default. Its `sqlite3` leaves `-wal` and `-shm` after it closes. |
125
127
  | `better-sqlite3` | Its busy timeout is 5000 milliseconds by default. |
126
128
  | Python `sqlite3` | `sqlite3.connect(path, timeout=5.0)`: its busy timeout is 5 seconds by default. |
127
- | Rails 8 | A SQLite database gets a write-ahead log and `journal_size_limit = 67108864` by default. |
129
+ | Rails 8 | A SQLite database gets a write-ahead log, `journal_size_limit = 67108864` and `synchronous = NORMAL` by default. |
128
130
  | `sqlite3 -readonly` | It opens a reader. A missing file fails with "unable to open database file", the text of CANTOPEN. |
129
131
 
130
132
  ## Documentation
@@ -135,6 +137,7 @@ A bundler that bundles this package does not copy the `lib` folder. A bundle tha
135
137
  - [0004 The journal is a strategy](./docs/adr/0004-the-journal-is-a-strategy.md)
136
138
  - [0005 One map of failures and a busy timeout of 5 seconds](./docs/adr/0005-one-map-of-failures-and-a-busy-timeout-of-5-seconds.md)
137
139
  - [0006 Zig builds every native library in one Linux job](./docs/adr/0006-zig-builds-every-native-library-in-one-linux-job.md)
140
+ - [0007 A write-ahead log syncs at checkpoints](./docs/adr/0007-a-write-ahead-log-syncs-at-checkpoints.md)
138
141
 
139
142
  ## Development
140
143
 
@@ -1,15 +1,13 @@
1
1
  import type { DatabaseSync } from 'node:sqlite';
2
2
  import type { Journal } from './journal.ts';
3
- import { type PageSize } from './write-ahead-log.ts';
3
+ import { type WriteAheadLogOptions } from './write-ahead-log.ts';
4
4
  /**
5
5
  * A write-ahead log whose -wal and -shm files stay after the last connection
6
6
  * closes, for readers that cannot write the folder (ADR 0003).
7
7
  */
8
8
  export declare class PersistentWriteAheadLog implements Journal {
9
9
  #private;
10
- constructor(options?: {
11
- readonly pageSize?: PageSize;
12
- });
10
+ constructor(options?: WriteAheadLogOptions);
13
11
  apply(database: DatabaseSync): void;
14
12
  }
15
13
  //# sourceMappingURL=persistent-write-ahead-log.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"persistent-write-ahead-log.d.ts","sourceRoot":"","sources":["../src/persistent-write-ahead-log.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGhD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,KAAK,QAAQ,EAAiB,MAAM,sBAAsB,CAAC;AAWpE;;;GAGG;AACH,qBAAa,uBAAwB,YAAW,OAAO;;gBAGzC,OAAO,GAAE;QAAE,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAA;KAAO;IAI1D,KAAK,CAAC,QAAQ,EAAE,YAAY,GAAG,IAAI;CAsBpC"}
1
+ {"version":3,"file":"persistent-write-ahead-log.d.ts","sourceRoot":"","sources":["../src/persistent-write-ahead-log.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGhD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,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CAAC,QAAQ,CAAC,EAAE,EACvD,GAAG,CACJ,CAAC;QACF,IAAI,CAAC;YACH,QAAQ,CAAC,aAAa,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC,CAAC;QACjD,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IACE,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;gBACrB,CAAC,CAAC,MAAM,IAAI,KAAK,CAAC;gBAClB,KAAK,CAAC,IAAI,KAAK,2BAA2B;gBAE1C,MAAM,KAAK,CAAC;YACd,MAAM,IAAI,KAAK,CACb,kDAAkD,QAAQ,oBAAoB,WAAW,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAC5G,EAAE,KAAK,EAAE,CACV,CAAC;QACJ,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IAC5B,CAAC;CACF"}
@@ -2,12 +2,20 @@ import type { DatabaseSync } from 'node:sqlite';
2
2
  import type { Journal } from './journal.ts';
3
3
  /** A page size that SQLite accepts: a power of two from 512 to 65536. */
4
4
  export type PageSize = 512 | 1024 | 2048 | 4096 | 8192 | 16384 | 32768 | 65536;
5
+ /**
6
+ * When SQLite syncs the -wal file to the disk: `'normal'` before each
7
+ * checkpoint, `'full'` at each commit (ADR 0007).
8
+ */
9
+ export type Synchronous = 'normal' | 'full';
10
+ /** The options of a write-ahead log. */
11
+ export interface WriteAheadLogOptions {
12
+ readonly pageSize?: PageSize;
13
+ readonly synchronous?: Synchronous;
14
+ }
5
15
  /** The default journal of an owner: readers and the owner do not wait for each other. */
6
16
  export declare class WriteAheadLog implements Journal {
7
17
  #private;
8
- constructor(options?: {
9
- readonly pageSize?: PageSize;
10
- });
18
+ constructor(options?: WriteAheadLogOptions);
11
19
  apply(database: DatabaseSync): void;
12
20
  }
13
21
  //# sourceMappingURL=write-ahead-log.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"write-ahead-log.d.ts","sourceRoot":"","sources":["../src/write-ahead-log.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAK5C,yEAAyE;AACzE,MAAM,MAAM,QAAQ,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,CAAC;AAE/E,yFAAyF;AACzF,qBAAa,aAAc,YAAW,OAAO;;gBAG/B,OAAO,GAAE;QAAE,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAA;KAAO;IAI1D,KAAK,CAAC,QAAQ,EAAE,YAAY,GAAG,IAAI;CAiBpC"}
1
+ {"version":3,"file":"write-ahead-log.d.ts","sourceRoot":"","sources":["../src/write-ahead-log.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAK5C,yEAAyE;AACzE,MAAM,MAAM,QAAQ,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,KAAK,GAAG,KAAK,GAAG,KAAK,CAAC;AAE/E;;;GAGG;AACH,MAAM,MAAM,WAAW,GAAG,QAAQ,GAAG,MAAM,CAAC;AAE5C,wCAAwC;AACxC,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IAC7B,QAAQ,CAAC,WAAW,CAAC,EAAE,WAAW,CAAC;CACpC;AAED,yFAAyF;AACzF,qBAAa,aAAc,YAAW,OAAO;;gBAI/B,OAAO,GAAE,oBAAyB;IAK9C,KAAK,CAAC,QAAQ,EAAE,YAAY,GAAG,IAAI;CAkBpC"}
@@ -3,8 +3,10 @@ const sizeLimit = 64 * 1024 * 1024;
3
3
  /** The default journal of an owner: readers and the owner do not wait for each other. */
4
4
  export class WriteAheadLog {
5
5
  #pageSize;
6
+ #synchronous;
6
7
  constructor(options = {}) {
7
8
  this.#pageSize = options.pageSize;
9
+ this.#synchronous = options.synchronous ?? 'normal';
8
10
  }
9
11
  apply(database) {
10
12
  // SQLite takes the page size of a file only before its first write, and
@@ -20,6 +22,7 @@ export class WriteAheadLog {
20
22
  if (journal !== 'wal')
21
23
  throw new Error(`SQLite kept the journal ${JSON.stringify(journal)} of ${JSON.stringify(database.location())}, not a write-ahead log.`);
22
24
  database.exec(`PRAGMA journal_size_limit = ${sizeLimit}`);
25
+ database.exec(`PRAGMA synchronous = ${this.#synchronous.toUpperCase()}`);
23
26
  }
24
27
  }
25
28
  //# sourceMappingURL=write-ahead-log.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"write-ahead-log.js","sourceRoot":"","sources":["../src/write-ahead-log.ts"],"names":[],"mappings":"AAIA,2EAA2E;AAC3E,MAAM,SAAS,GAAG,EAAE,GAAG,IAAI,GAAG,IAAI,CAAC;AAKnC,yFAAyF;AACzF,MAAM,OAAO,aAAa;IACf,SAAS,CAAuB;IAEzC,YAAY,UAA4C,EAAE;QACxD,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,QAAQ,CAAC;IACpC,CAAC;IAED,KAAK,CAAC,QAAsB;QAC1B,wEAAwE;QACxE,sEAAsE;QACtE,sEAAsE;QACtE,IAAI,IAAI,CAAC,SAAS,KAAK,SAAS;YAC9B,QAAQ,CAAC,IAAI,CAAC,sBAAsB,IAAI,CAAC,SAAS,EAAE,CAAC,CAAC;QACxD,sEAAsE;QACtE,sEAAsE;QACtE,MAAM,SAAS,GAAG,QAAQ,CAAC,OAAO,CAAC,2BAA2B,CAAC,CAAC;QAChE,SAAS,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;QACjC,MAAM,OAAO,GAAG,SAAS,CAAC,GAAG,EAAE,EAAE,YAAY,CAAC;QAC9C,IAAI,OAAO,KAAK,KAAK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,CAAC,IAAI,CAAC,+BAA+B,SAAS,EAAE,CAAC,CAAC;QAC1D,QAAQ,CAAC,IAAI,CAAC,wBAAwB,IAAI,CAAC,YAAY,CAAC,WAAW,EAAE,EAAE,CAAC,CAAC;IAC3E,CAAC;CACF"}
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zukhruf/sqlite-open",
3
- "version": "0.8.14",
3
+ "version": "0.8.16",
4
4
  "description": "One open for every node:sqlite connection: a reader, or an owner whose journal is a strategy (WAL by default, persistent WAL for readers that cannot write the folder), a 5 s busy timeout, and UnavailableError.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -41,6 +41,6 @@
41
41
  "access": "public"
42
42
  },
43
43
  "devDependencies": {
44
- "@zukhruf/testing": "^0.8.14"
44
+ "@zukhruf/testing": "^0.8.16"
45
45
  }
46
46
  }