vsql-node 1.2.9 → 1.2.10

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
@@ -34,6 +34,17 @@ when it's finished (like `node:sqlite` and better-sqlite3). To run queries at
34
34
  the same time, use a connection in each
35
35
  [worker thread](https://nodejs.org/api/worker_threads.html).
36
36
 
37
+ This includes waiting for a lock: a `SERIALIZABLE` statement or transaction
38
+ (see [Transactions](#transactions)) waits up to 5 seconds for the other
39
+ connections to the file to finish their transactions, and the event loop is
40
+ blocked while it waits. If the other connection is on the same thread, it
41
+ can't finish, so the wait always fails with `40001` after 5 seconds (see
42
+ [#10](https://github.com/elliotchance/vsql-node/issues/10)).
43
+
44
+ Each call is one statement. More than one (`INSERT ...; INSERT ...`) is a
45
+ syntax error (`42601`), although a `;` at the end is allowed. A `--` comment
46
+ is an error (`42000`), so use `/* ... */` instead.
47
+
37
48
  | Connection | Statement | |
38
49
  |---------------------------------|---------------------------|-----------------------------------------------------|
39
50
  | `db.prepare(sql)` | | Prepares a statement |
@@ -86,7 +97,9 @@ db.info();
86
97
  ```
87
98
 
88
99
  A statement that hasn't been closed is freed when it's garbage collected, as is
89
- a connection (which rolls back its transaction). Both can be declared with
100
+ a connection (which rolls back its transaction). A statement with rows that
101
+ haven't all been fetched isn't, since the connection keeps it until its rows
102
+ are closed (see [Transactions](#transactions)). Both can be declared with
90
103
  `using` (on Node 24 or later).
91
104
 
92
105
  ## Transactions
@@ -106,6 +119,23 @@ db.transaction(() => {
106
119
  });
107
120
  ```
108
121
 
122
+ Until a query's rows have all been fetched (or the statement is closed, or the
123
+ iterator is left with `break` or `return`), its transaction is still going. An
124
+ iterator that's left part way through (such as one that only had `next()`
125
+ called once) keeps the file locked, so the other connections to it wait and
126
+ fail with `40001` after 5 seconds, and `db.begin()` (or `db.transaction(fn)`)
127
+ on the same connection fails with `25001`. Fetch all the rows, or close the
128
+ statement, first (see
129
+ [#5](https://github.com/elliotchance/vsql-node/issues/5)):
130
+
131
+ ```js
132
+ const it = db.iterate('SELECT x FROM foo');
133
+ it.next();
134
+ db.begin(); // VsqlError 25001: active SQL-transaction
135
+ it.return(); // closes the statement
136
+ db.begin();
137
+ ```
138
+
109
139
  A transaction in another one (`db.transaction(fn)` while there's already a
110
140
  transaction) is a savepoint, which is released when `fn` returns, or rolled back
111
141
  to if it throws, so only what `fn` did is undone and the outer transaction
@@ -157,7 +187,8 @@ a `TypeError` (rather than being committed one at a time), and so does
157
187
 
158
188
  `db.begin(options)` and `db.transaction(fn, options)` take `readOnly` (`false`
159
189
  by default) and `isolationLevel` (`"READ COMMITTED"` by default, or
160
- `"READ UNCOMMITTED"`, `"REPEATABLE READ"` or `"SERIALIZABLE"`):
190
+ `"READ UNCOMMITTED"`, `"REPEATABLE READ"` or `"SERIALIZABLE"`). The names are
191
+ case sensitive, so `'serializable'` is a `RangeError`:
161
192
 
162
193
  ```js
163
194
  db.transaction(() => report(), { readOnly: true, isolationLevel: 'SERIALIZABLE' });
@@ -165,8 +196,11 @@ db.transaction(() => report(), { readOnly: true, isolationLevel: 'SERIALIZABLE'
165
196
 
166
197
  vsql runs `READ UNCOMMITTED` (which must be `readOnly`) as `READ COMMITTED`, and
167
198
  `REPEATABLE READ` as `SERIALIZABLE`. A statement outside of a transaction is
168
- `SERIALIZABLE`. A `SERIALIZABLE` transaction waits for the other connections to
169
- the file, while a `READ COMMITTED` one fails if it can't commit.
199
+ `SERIALIZABLE` (vsql's default). A `SERIALIZABLE` transaction waits (up to 5
200
+ seconds) for the other connections to the file, while a `READ COMMITTED` one
201
+ doesn't wait, and fails with `40001` if another transaction changed the same
202
+ rows. Since a wait blocks the event loop (see [API](#api)), the default for
203
+ `db.begin()` is `READ COMMITTED` instead of vsql's `SERIALIZABLE`.
170
204
 
171
205
  ## Parameters
172
206
 
@@ -187,6 +221,12 @@ type, which is cast to the type it's used as:
187
221
  | `vsql.time(value)` | `TIME` (`WITH TIME ZONE` if it has an offset) |
188
222
  | `vsql.timestamp(value)` | `TIMESTAMP` (`WITH TIME ZONE` if it has an offset) |
189
223
 
224
+ The type of a parameter comes from where it's used, and the value is cast to
225
+ it. So a `number` that isn't an integer is truncated when it's used as an
226
+ integer (`WHERE x = ?` with `1.5` is `x = 1` when `x` is an `INTEGER`), and a
227
+ `?` that has no type from where it's used (such as `SELECT ?`) is a syntax
228
+ error (`42601`). Use `CAST(? AS DOUBLE PRECISION)` to give it a type.
229
+
190
230
  A string is cast to the type of the parameter (from where it's used), so it can
191
231
  be a parameter for any type that isn't binary, in vsql's format (such as
192
232
  `"2024-01-02"` for a `DATE`, `"1.5E+400"` for a `DECFLOAT` or `"-1-02"` for an
@@ -316,7 +356,7 @@ threads, so don't listen for them with `process.on`.
316
356
  ## Requirements
317
357
 
318
358
  Node 22 or later. The addon is prebuilt for macOS (ARM64 and x86-64), Linux
319
- (ARM64 and x86-64, with glibc 2.29 or later) and Windows (x86-64), so npm
359
+ (ARM64 and x86-64, with glibc 2.31 or later) and Windows (x86-64), so npm
320
360
  doesn't need to compile anything. It uses Node-API, so the same prebuild works
321
361
  for every version of Node.
322
362
 
package/index.d.ts CHANGED
@@ -80,8 +80,10 @@ export interface TransactionOptions {
80
80
  /** A READ ONLY transaction (it's READ WRITE by default). */
81
81
  readOnly?: boolean;
82
82
  /**
83
- * "READ COMMITTED" by default. vsql runs REPEATABLE READ as SERIALIZABLE,
84
- * and READ UNCOMMITTED (which must be readOnly) as READ COMMITTED.
83
+ * "READ COMMITTED" by default (vsql's default is SERIALIZABLE, which waits
84
+ * for other connections and blocks the event loop while it does). vsql runs
85
+ * REPEATABLE READ as SERIALIZABLE, and READ UNCOMMITTED (which must be
86
+ * readOnly) as READ COMMITTED.
85
87
  */
86
88
  isolationLevel?: IsolationLevel;
87
89
  }
package/lib/connection.js CHANGED
@@ -28,8 +28,10 @@ const infoFields = [
28
28
  ];
29
29
 
30
30
  // Handles that weren't freed (with close) are freed when their connection or
31
- // statement is garbage collected. Handles are numbers that libvsql reuses, so
32
- // a statement's handle is only freed while its connection is open.
31
+ // statement is garbage collected. A statement with rows that haven't all been
32
+ // fetched isn't garbage collected, since it's in its connection's _cursors.
33
+ // Handles are numbers that libvsql reuses, so a statement's handle is only
34
+ // freed while its connection is open.
33
35
  const connections = new FinalizationRegistry((state) => {
34
36
  try {
35
37
  closeConnection(state);
@@ -201,7 +203,9 @@ class Connection {
201
203
  // begin starts a transaction, which is ended by commit or rollback. Until
202
204
  // then, each statement is committed when it finishes (or when all the rows of
203
205
  // a query have been fetched). The options are readOnly (false by default)
204
- // and isolationLevel ("READ COMMITTED" by default).
206
+ // and isolationLevel ("READ COMMITTED" by default, rather than vsql's
207
+ // SERIALIZABLE, which waits for other connections and blocks the event loop
208
+ // while it does). The isolation level's name is case sensitive.
205
209
  begin(options = {}) {
206
210
  this._check();
207
211
  if (this._state.inTransaction) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vsql-node",
3
- "version": "1.2.9",
3
+ "version": "1.2.10",
4
4
  "description": "A Node.js driver for vsql, a single file or in-memory SQL database.",
5
5
  "keywords": [
6
6
  "vsql",