@procedures/sqlite 0.4.0 → 0.6.0

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/dist/storage.d.ts CHANGED
@@ -3,28 +3,31 @@
3
3
  *
4
4
  * `SqliteStorage` keeps the items of one collection in one table of a SQLite database: a file,
5
5
  * or `:memory:`. It implements `CollectionStorage` of `@procedures/client`, so a
6
- * `ProcedureServer`, an `ApiStorage` server and a `HybridStorage` can use it. The engine is
7
- * sql.js (SQLite in WebAssembly), through `@mark1russell7/docker-sqlite`: no native build.
6
+ * `ProcedureServer`, an `ApiStorage` server and a `HybridStorage` can use it.
8
7
  *
8
+ * - The storage has two engines (the option `engine`). The engine `node` is `node:sqlite` of
9
+ * Node.js: it writes only the changed pages of the file. The engine `sqljs` is sql.js (SQLite in
10
+ * WebAssembly) through `@mark1russell7/docker-sqlite`: it keeps the database in memory and
11
+ * writes the whole file after a write. The default `auto` uses `node` when Node.js has it.
12
+ * - The two engines use the same file format: a file of one engine opens with the other.
9
13
  * - The table of a collection has the columns `id` (the key) and `value` (the JSON text of the
10
14
  * item). A value goes through `JSON.stringify` and `JSON.parse`.
11
15
  * - A change log gives `getChangesSince()`. The table `_storage_log` has the last change of each
12
16
  * id, and `_storage_versions` has the version of each collection: `<epoch>:<seq>`, as
13
17
  * `InMemoryStorage` has. The epoch stays in the file, so a version stays valid after a restart.
14
18
  * - Each write runs in a transaction. A batch that fails changes nothing.
15
- * - The storage keeps the database in memory and writes the file after each write. A write ends
16
- * after its file write. One file write can hold the changes of several writes.
17
- * - The storages of one file in one process share one database, so the storages of two tables in
18
- * one file do not write over the changes of each other.
19
+ * - The storages of one file in one process share one database. Thus the storages of two tables
20
+ * in one file keep the changes of each other.
21
+ * - A storage that only reads does not make the file.
19
22
  */
20
- import type { CollectionStorage, StorageChanges, StorageMetadata } from "@procedures/client";
21
- /** The path of a database that is only in memory. */
22
- export declare const MEMORY_PATH = ":memory:";
23
+ import { type CollectionStorage, type Duration, type StorageChanges, type StorageMetadata } from "@procedures/client";
24
+ import { type SqliteEngine, type SqliteEngineName } from "./engines.js";
25
+ export { MEMORY_PATH, isNodeSqliteAvailable, type SqliteEngine, type SqliteEngineName } from "./engines.js";
23
26
  /** The options of a `SqliteStorage`. */
24
27
  export interface SqliteStorageOptions {
25
28
  /**
26
29
  * The database file, or `:memory:` for a database that lives as long as the storage. The
27
- * storage makes the file, and its folder, at the first write.
30
+ * storage makes the file, and its folder, at the first write that changes an item.
28
31
  */
29
32
  path: string;
30
33
  /**
@@ -38,6 +41,28 @@ export interface SqliteStorageOptions {
38
41
  * @default 10000
39
42
  */
40
43
  maxTombstones?: number;
44
+ /**
45
+ * The SQLite engine.
46
+ *
47
+ * - `node`: the module `node:sqlite` of Node.js 22.13 or later. It writes only the changes, and
48
+ * the file is in WAL mode. Two processes can use one file.
49
+ * - `sqljs`: sql.js, SQLite in WebAssembly. It keeps the database in memory, and it writes the
50
+ * whole file after a write.
51
+ * - `auto`: `node` when Node.js has `node:sqlite`, and else `sqljs`.
52
+ *
53
+ * The storages of one file in one process share one database, and thus one engine. The first
54
+ * storage of the file sets it. A storage with another engine than `auto` refuses a database
55
+ * of the other engine.
56
+ * @default "auto"
57
+ */
58
+ engine?: SqliteEngine;
59
+ /**
60
+ * The time that a write of the engine `node` waits for the lock of another process, for example
61
+ * `seconds(5)`. After it, the write fails with the error `database is locked`. The first
62
+ * storage of a file sets it.
63
+ * @default 5 seconds
64
+ */
65
+ busyTimeout?: Duration;
41
66
  }
42
67
  /**
43
68
  * This function gives the name when it is a permitted table name. Else it throws a `TypeError`. The name goes
@@ -53,6 +78,7 @@ export declare function checkTableName(name: unknown): string;
53
78
  * await users.set("123", { id: "123", name: "John" });
54
79
  * const user = await users.get("123"); // { id: "123", name: "John" }
55
80
  * const changes = await users.getChangesSince(); // { version, full: true, changed, deleted: [] }
81
+ * await users.getEngine(); // "node" on Node.js 22.13 or later, else "sqljs"
56
82
  * await users.close();
57
83
  * ```
58
84
  */
@@ -61,6 +87,8 @@ export declare class SqliteStorage<T> implements CollectionStorage<T> {
61
87
  readonly path: string;
62
88
  /** The table of the items. */
63
89
  readonly table: string;
90
+ /** The engine choice of the options. `getEngine()` gives the engine that the storage uses. */
91
+ readonly engine: SqliteEngine;
64
92
  private readonly maxTombstones;
65
93
  /** The quoted table name, for the SQL text. */
66
94
  private readonly quoted;
@@ -71,10 +99,13 @@ export declare class SqliteStorage<T> implements CollectionStorage<T> {
71
99
  getAll(): Promise<T[]>;
72
100
  /** Every item with its id, in the order of the first write of each id. */
73
101
  getEntries(): Promise<Array<[string, T]>>;
102
+ /** The ids of the items, in the order of the entries. The storage does not read the values. */
103
+ getIds(): Promise<string[]>;
74
104
  /**
75
105
  * The changes after a version (roadmap R28). A version of another storage, of another database
76
106
  * file, or older than the dropped deletes gives all items (`full: true`). A version stays valid
77
- * after a restart, because the change log is in the file.
107
+ * after a restart, because the change log is in the file. The reads run in one read
108
+ * transaction, so a write of another process does not come between them.
78
109
  */
79
110
  getChangesSince(since?: string): Promise<StorageChanges<T>>;
80
111
  find(predicate: (item: T) => boolean): Promise<T[]>;
@@ -88,30 +119,40 @@ export declare class SqliteStorage<T> implements CollectionStorage<T> {
88
119
  /** This method deletes several items in one transaction. */
89
120
  deleteBatch(ids: string[]): Promise<number>;
90
121
  getBatch(ids: string[]): Promise<Map<string, T>>;
122
+ /** The engine that the storage uses: `node` or `sqljs`. With the option `auto`, the open decides it. */
123
+ getEngine(): Promise<SqliteEngineName>;
91
124
  /**
92
125
  * This method writes the database file. Each write ends after its file write already, so a
93
- * caller needs it only after a failed file write.
126
+ * caller needs it only after a failed file write of the engine `sqljs`. The engine `node`
127
+ * writes each change at its commit.
94
128
  */
95
129
  flush(): Promise<void>;
96
130
  /**
97
131
  * This method writes the file and releases the database. The items stay in the file. A new
98
132
  * storage of the same file and table reads them, and the versions of this storage stay valid.
99
- * After `close()`, each operation throws.
133
+ * After `close()`, each operation rejects, and a second `close()` does nothing.
100
134
  */
101
135
  close(): Promise<void>;
102
136
  getMetadata(): Promise<StorageMetadata>;
103
137
  /** This method opens the database, and makes the tables of the collection and of the change log. */
104
138
  private open;
105
- /** The open database. It throws after `close()`. */
139
+ /** The open database. It throws after `close()`, so the promise of the operation rejects. */
106
140
  private database;
141
+ private sharedDatabase;
107
142
  private checkOpen;
108
143
  private entries;
109
144
  private counter;
110
145
  /**
111
- * One write: the change in a transaction with the change log, then the file write. `next` gives
112
- * the next number of the change log. A write that changes nothing writes no file.
146
+ * One write: the change in a transaction with the change log, then the file write of the engine
147
+ * `sqljs`. `next` gives the next number of the change log. A write that changes nothing writes
148
+ * no file.
149
+ *
150
+ * Before the first write, the engine `node` keeps the database of a missing file in memory, with
151
+ * no item. A write first tests its change there. When it changes an item, the storage makes the
152
+ * file and runs the write in it.
113
153
  */
114
154
  private write;
155
+ private runWrite;
115
156
  /** This method writes an item. An existing id keeps its place in the order. */
116
157
  private put;
117
158
  /** This method deletes an item. True when it existed. */
@@ -1 +1 @@
1
- {"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../src/storage.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAKH,OAAO,KAAK,EAAE,iBAAiB,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAE7F,qDAAqD;AACrD,eAAO,MAAM,WAAW,aAAa,CAAC;AAEtC,wCAAwC;AACxC,MAAM,WAAW,oBAAoB;IACnC;;;OAGG;IACH,IAAI,EAAE,MAAM,CAAC;IACb;;;OAGG;IACH,KAAK,EAAE,MAAM,CAAC;IACd;;;;OAIG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAKD;;;GAGG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAOpD;AA8MD;;;;;;;;;;;GAWG;AACH,qBAAa,aAAa,CAAC,CAAC,CAAE,YAAW,iBAAiB,CAAC,CAAC,CAAC;IAC3D,wCAAwC;I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