@tursodatabase/database-common 0.9.0-pre.2 → 0.9.0-pre.4
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/bind.js +1 -1
- package/dist/compat.d.ts.map +1 -1
- package/dist/compat.js +23 -38
- package/dist/promise.d.ts.map +1 -1
- package/dist/promise.js +25 -45
- package/dist/sqlite-error.d.ts +10 -0
- package/dist/sqlite-error.d.ts.map +1 -1
- package/dist/sqlite-error.js +51 -0
- package/package.json +1 -1
package/dist/bind.js
CHANGED
|
@@ -29,7 +29,7 @@ export function bindParams(stmt, params) {
|
|
|
29
29
|
}
|
|
30
30
|
// Check if object is plain (no prototype chain)
|
|
31
31
|
function isPlainObject(obj) {
|
|
32
|
-
if (!obj || typeof obj !== 'object')
|
|
32
|
+
if (!obj || typeof obj !== 'object' || ArrayBuffer.isView(obj))
|
|
33
33
|
return false;
|
|
34
34
|
const proto = Object.getPrototypeOf(obj);
|
|
35
35
|
return proto === Object.prototype || proto === null;
|
package/dist/compat.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compat.d.ts","sourceRoot":"","sources":["../compat.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,YAAY,EAA4C,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"compat.d.ts","sourceRoot":"","sources":["../compat.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,YAAY,EAA4C,MAAM,YAAY,CAAC;AA6BrH,MAAM,MAAM,SAAS,GAAG,OAAO,GAAG,MAAM,GAAG,UAAU,GAAG,WAAW,GAAG,WAAW,GAAG,YAAY,GAAG,MAAM,CAAC;AAE1G,MAAM,WAAW,YAAY;IAC3B,IAAI,CAAC,EAAE,SAAS,CAAC;IACjB,GAAG,CAAC,EAAE,OAAO,CAAC;CACf;AAED,MAAM,MAAM,QAAQ,GAAG,MAA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AO,CAAC,EAAE,SAAS,GAAG,YAAY,GACjC,SAAS,EAAE;IA2Ed;;OAEG;IACH,SAAS;IAIT;;;;OAIG;IACH,mBAAmB,CAAC,MAAM,KAAA;IAI1B;;OAEG;IACH,KAAK;CAGN;AAED;;GAEG;AACH,cAAM,SAAS;IACb,IAAI,EAAE,eAAe,CAAC;IACtB,EAAE,EAAE,cAAc,CAAC;gBAEP,IAAI,EAAE,eAAe,EAAE,EAAE,EAAE,cAAc;IAKrD;;;;OAIG;IACH,GAAG,CAAC,GAAG,KAAA;IAKP;;;;OAIG;IACH,KAAK,CAAC,SAAS,KAAA;IAKf;;;;OAIG;IACH,YAAY,CAAC,MAAM,KAAA;IAKnB;;;;OAIG;IACH,OAAO;IAIP,IAAI,MAAM,SAET;IAED,IAAI,MAAM,IAAI,OAAO,CAEpB;IAED,IAAI,QAAQ,mBAEX;IAED;;OAEG;IACH,GAAG,CAAC,GAAG,cAAc,OAAA;;;;IA4BrB;;;;OAIG;IACH,GAAG,CAAC,GAAG,cAAc,OAAA;IAsBrB;;;;OAIG;IACF,OAAO,CAAC,GAAG,cAAc,OAAA;IAqB1B;;;;OAIG;IACH,GAAG,CAAC,GAAG,cAAc,OAAA;IAsBrB;;OAEG;IACH,SAAS;IAKT;;;;;OAKG;IACH,IAAI,CAAC,GAAG,cAAc,OAAA;IAStB,KAAK;CAGN;AAED,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAA"}
|
package/dist/compat.js
CHANGED
|
@@ -1,21 +1,6 @@
|
|
|
1
1
|
import { bindParams } from "./bind.js";
|
|
2
|
-
import {
|
|
2
|
+
import { convertError, ioLoopSync, prepareStatement, resetStatement, stepSync } from "./sqlite-error.js";
|
|
3
3
|
import { STEP_IO, STEP_ROW, STEP_DONE, STEP_SLEEP } from "./types.js";
|
|
4
|
-
const convertibleErrorTypes = { TypeError };
|
|
5
|
-
const CONVERTIBLE_ERROR_PREFIX = "[TURSO_CONVERT_TYPE]";
|
|
6
|
-
function convertError(err) {
|
|
7
|
-
if ((err.code ?? "").startsWith(CONVERTIBLE_ERROR_PREFIX)) {
|
|
8
|
-
return createErrorByName(err.code.substring(CONVERTIBLE_ERROR_PREFIX.length), err.message);
|
|
9
|
-
}
|
|
10
|
-
return new SqliteError(err.message, err.code, err.rawCode);
|
|
11
|
-
}
|
|
12
|
-
function createErrorByName(name, message) {
|
|
13
|
-
const ErrorConstructor = convertibleErrorTypes[name];
|
|
14
|
-
if (!ErrorConstructor) {
|
|
15
|
-
throw new Error(`unknown error type ${name} from Turso`);
|
|
16
|
-
}
|
|
17
|
-
return new ErrorConstructor(message);
|
|
18
|
-
}
|
|
19
4
|
function isQueryOptions(value) {
|
|
20
5
|
return value != null
|
|
21
6
|
&& typeof value === "object"
|
|
@@ -100,7 +85,12 @@ class Database {
|
|
|
100
85
|
*/
|
|
101
86
|
constructor(db) {
|
|
102
87
|
this.db = db;
|
|
103
|
-
|
|
88
|
+
try {
|
|
89
|
+
this.db.connectSync();
|
|
90
|
+
}
|
|
91
|
+
catch (err) {
|
|
92
|
+
throw convertError(err);
|
|
93
|
+
}
|
|
104
94
|
Object.defineProperties(this, {
|
|
105
95
|
name: { get: () => this.db.path },
|
|
106
96
|
readonly: { get: () => this.db.readonly },
|
|
@@ -121,12 +111,7 @@ class Database {
|
|
|
121
111
|
if (!sql) {
|
|
122
112
|
throw new RangeError("The supplied SQL string contains no statements");
|
|
123
113
|
}
|
|
124
|
-
|
|
125
|
-
return new Statement(this.db.prepare(sql), this.db);
|
|
126
|
-
}
|
|
127
|
-
catch (err) {
|
|
128
|
-
throw convertError(err);
|
|
129
|
-
}
|
|
114
|
+
return new Statement(prepareStatement(this.db, sql), this.db);
|
|
130
115
|
}
|
|
131
116
|
/**
|
|
132
117
|
* Returns a function that executes the given function in a transaction.
|
|
@@ -215,9 +200,9 @@ class Database {
|
|
|
215
200
|
const exec = this.db.executor(sql, queryOptions);
|
|
216
201
|
try {
|
|
217
202
|
while (true) {
|
|
218
|
-
const [stepResult] =
|
|
203
|
+
const [stepResult] = stepSync(exec);
|
|
219
204
|
if (stepResult === STEP_IO || stepResult === STEP_SLEEP) {
|
|
220
|
-
this.db
|
|
205
|
+
ioLoopSync(this.db);
|
|
221
206
|
continue;
|
|
222
207
|
}
|
|
223
208
|
if (stepResult === STEP_DONE) {
|
|
@@ -283,7 +268,7 @@ class Database {
|
|
|
283
268
|
results.push(makeResultSet(columnNames, columnTypes, rows, rowsAffected));
|
|
284
269
|
}
|
|
285
270
|
finally {
|
|
286
|
-
stmt
|
|
271
|
+
resetStatement(stmt);
|
|
287
272
|
}
|
|
288
273
|
}
|
|
289
274
|
finally {
|
|
@@ -388,13 +373,13 @@ class Statement {
|
|
|
388
373
|
run(...bindParameters) {
|
|
389
374
|
const totalChangesBefore = this.db.totalChanges();
|
|
390
375
|
const { params, queryOptions } = splitBindParameters(bindParameters);
|
|
391
|
-
this.stmt
|
|
376
|
+
resetStatement(this.stmt);
|
|
392
377
|
this.stmt.setQueryTimeout(queryOptions);
|
|
393
378
|
bindParams(this.stmt, toBindArgs(params));
|
|
394
379
|
for (;;) {
|
|
395
|
-
const [stepResult] = this.stmt
|
|
380
|
+
const [stepResult] = stepSync(this.stmt);
|
|
396
381
|
if (stepResult === STEP_IO || stepResult === STEP_SLEEP) {
|
|
397
|
-
this.db
|
|
382
|
+
ioLoopSync(this.db);
|
|
398
383
|
continue;
|
|
399
384
|
}
|
|
400
385
|
if (stepResult === STEP_DONE) {
|
|
@@ -416,14 +401,14 @@ class Statement {
|
|
|
416
401
|
*/
|
|
417
402
|
get(...bindParameters) {
|
|
418
403
|
const { params, queryOptions } = splitBindParameters(bindParameters);
|
|
419
|
-
this.stmt
|
|
404
|
+
resetStatement(this.stmt);
|
|
420
405
|
this.stmt.setQueryTimeout(queryOptions);
|
|
421
406
|
bindParams(this.stmt, toBindArgs(params));
|
|
422
407
|
let row = undefined;
|
|
423
408
|
for (;;) {
|
|
424
|
-
const [stepResult] = this.stmt
|
|
409
|
+
const [stepResult] = stepSync(this.stmt);
|
|
425
410
|
if (stepResult === STEP_IO || stepResult === STEP_SLEEP) {
|
|
426
|
-
this.db
|
|
411
|
+
ioLoopSync(this.db);
|
|
427
412
|
continue;
|
|
428
413
|
}
|
|
429
414
|
if (stepResult === STEP_DONE) {
|
|
@@ -442,13 +427,13 @@ class Statement {
|
|
|
442
427
|
*/
|
|
443
428
|
*iterate(...bindParameters) {
|
|
444
429
|
const { params, queryOptions } = splitBindParameters(bindParameters);
|
|
445
|
-
this.stmt
|
|
430
|
+
resetStatement(this.stmt);
|
|
446
431
|
this.stmt.setQueryTimeout(queryOptions);
|
|
447
432
|
bindParams(this.stmt, toBindArgs(params));
|
|
448
433
|
while (true) {
|
|
449
|
-
const [stepResult] = this.stmt
|
|
434
|
+
const [stepResult] = stepSync(this.stmt);
|
|
450
435
|
if (stepResult === STEP_IO || stepResult === STEP_SLEEP) {
|
|
451
|
-
this.db
|
|
436
|
+
ioLoopSync(this.db);
|
|
452
437
|
continue;
|
|
453
438
|
}
|
|
454
439
|
if (stepResult === STEP_DONE) {
|
|
@@ -466,14 +451,14 @@ class Statement {
|
|
|
466
451
|
*/
|
|
467
452
|
all(...bindParameters) {
|
|
468
453
|
const { params, queryOptions } = splitBindParameters(bindParameters);
|
|
469
|
-
this.stmt
|
|
454
|
+
resetStatement(this.stmt);
|
|
470
455
|
this.stmt.setQueryTimeout(queryOptions);
|
|
471
456
|
bindParams(this.stmt, toBindArgs(params));
|
|
472
457
|
const rows = [];
|
|
473
458
|
for (;;) {
|
|
474
|
-
const [stepResult] = this.stmt
|
|
459
|
+
const [stepResult] = stepSync(this.stmt);
|
|
475
460
|
if (stepResult === STEP_IO || stepResult === STEP_SLEEP) {
|
|
476
|
-
this.db
|
|
461
|
+
ioLoopSync(this.db);
|
|
477
462
|
continue;
|
|
478
463
|
}
|
|
479
464
|
if (stepResult === STEP_DONE) {
|
package/dist/promise.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"promise.d.ts","sourceRoot":"","sources":["../promise.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAG5C,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,YAAY,EAA0D,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"promise.d.ts","sourceRoot":"","sources":["../promise.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAG5C,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,YAAY,EAA0D,MAAM,YAAY,CAAC;AAsCnI;;GAEG;AACH,MAAM,MAAM,SAAS,GAAG,OAAO,GAAG,MAAM,GAAG,UAAU,GAAG,WAAW,GAAG,WAAW,GAAG,YAAY,GAAG,MAAM,CAAC;AAE1G,MAAM,WAAW,YAAY;IAC3B,IAAI,CAAC,EAAE,SAAS,CAAC;IACjB,GAAG,CAAC,EAAE,OAAO,CAAC;CACf;AAED,KAAK,cAAc,GAAG,MAAM,GAAG;IAC7B,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,CAAC,EAAE,GAAG,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CACpC,CAAC;AAWF,MAAM,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,GAAG,EAAE,CAAC;AAEnD;;;GAGG;AACH,MAAM,WAAW,SAAS;IACxB,4EAA4E;IAC5E,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IACvB,oDAAoD;IACpD,WAAW,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IAC3B,oDAAoD;IACpD,IAAI,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAC;IACtB,8DAA8D;IAC9D,YAAY,EAAE,MAAM,CAAC;IACrB,wEAAwE;IACxE,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;yBAEqB;IACrB,QAAQ,CAAC,EAAE,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AE,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAM7C;AAED;;GAEG;AACH,cAAM,SAAS;IACb,OAAO,CAAC,IAAI,CAA6B;IACzC,OAAO,CAAC,EAAE,CAAiB;IAC3B,OAAO,CAAC,QAAQ,CAAW;IAC3B,OAAO,CAAC,MAAM,CAAsB;gBAExB,IAAI,EAAE,SAAS,CAAC,eAAe,CAAC,EAAE,EAAE,EAAE,cAAc,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC;IAOjH;;;;OAIG;IACH,GAAG,CAAC,GAAG,KAAA;IAKP;;;;OAIG;IACH,KAAK,CAAC,SAAS,KAAA;IAKf;;;;OAIG;IACH,YAAY,CAAC,MAAM,KAAA;IAKnB;;;;OAIG;IACH,OAAO;IAIP,IAAI,MAAM,SAET;IAED,IAAI,MAAM,IAAI,OAAO,CAEpB;IAED,IAAI,QAAQ,mBAEX;IAED;;OAEG;IACG,GAAG,CAAC,GAAG,cAAc,OAAA;;;;IAsC3B;;;;OAIG;IACG,GAAG,CAAC,GAAG,cAAc,OAAA;IAkC3B;;;;OAIG;IACI,OAAO,CAAC,GAAG,cAAc,OAAA;IA+BhC;;;;OAIG;IACG,GAAG,CAAC,GAAG,cAAc,OAAA;IAmCrB,EAAE;IAIR;;OAEG;IACH,SAAS;IAKT;;;;;OAKG;IACH,IAAI,CAAC,GAAG,cAAc,OAAA;IAStB,KAAK;CAUN;AAED,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,WAAW,EAAE,yBAAyB,EAAE,YAAY,EAAE,UAAU,EAAE,CAAA"}
|
package/dist/promise.js
CHANGED
|
@@ -1,22 +1,7 @@
|
|
|
1
1
|
import { AsyncLock } from "./async-lock.js";
|
|
2
2
|
import { bindParams } from "./bind.js";
|
|
3
|
-
import {
|
|
3
|
+
import { convertError, prepareStatement, resetStatement, stepSync } from "./sqlite-error.js";
|
|
4
4
|
import { STEP_IO, STEP_ROW, STEP_DONE, STEP_SLEEP } from "./types.js";
|
|
5
|
-
const convertibleErrorTypes = { TypeError };
|
|
6
|
-
const CONVERTIBLE_ERROR_PREFIX = "[TURSO_CONVERT_TYPE]";
|
|
7
|
-
function convertError(err) {
|
|
8
|
-
if ((err.code ?? "").startsWith(CONVERTIBLE_ERROR_PREFIX)) {
|
|
9
|
-
return createErrorByName(err.code.substring(CONVERTIBLE_ERROR_PREFIX.length), err.message);
|
|
10
|
-
}
|
|
11
|
-
return new SqliteError(err.message, err.code, err.rawCode);
|
|
12
|
-
}
|
|
13
|
-
function createErrorByName(name, message) {
|
|
14
|
-
const ErrorConstructor = convertibleErrorTypes[name];
|
|
15
|
-
if (!ErrorConstructor) {
|
|
16
|
-
throw new Error(`unknown error type ${name} from Turso`);
|
|
17
|
-
}
|
|
18
|
-
return new ErrorConstructor(message);
|
|
19
|
-
}
|
|
20
5
|
// The engine returned STEP_SLEEP: it wants the statement stepped again after a
|
|
21
6
|
// delay (busy-handler backoff, or a yield with no pending I/O). Park the step
|
|
22
7
|
// loop on a timer promise — unlike STEP_IO
|
|
@@ -202,7 +187,12 @@ class Database {
|
|
|
202
187
|
if (this.connected) {
|
|
203
188
|
return;
|
|
204
189
|
}
|
|
205
|
-
|
|
190
|
+
try {
|
|
191
|
+
await this.db.connectAsync();
|
|
192
|
+
}
|
|
193
|
+
catch (err) {
|
|
194
|
+
throw convertError(err);
|
|
195
|
+
}
|
|
206
196
|
this.connected = true;
|
|
207
197
|
}
|
|
208
198
|
/**
|
|
@@ -219,16 +209,11 @@ class Database {
|
|
|
219
209
|
if (!sql) {
|
|
220
210
|
throw new RangeError("The supplied SQL string contains no statements");
|
|
221
211
|
}
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
return new Statement(maybeValue(this.db.prepare(sql)), this.db, this.execLock, this.ioStep);
|
|
225
|
-
}
|
|
226
|
-
else {
|
|
227
|
-
return new Statement(maybePromise(() => this.connect().then(() => this.db.prepare(sql))), this.db, this.execLock, this.ioStep);
|
|
228
|
-
}
|
|
212
|
+
if (this.connected) {
|
|
213
|
+
return new Statement(maybeValue(prepareStatement(this.db, sql)), this.db, this.execLock, this.ioStep);
|
|
229
214
|
}
|
|
230
|
-
|
|
231
|
-
|
|
215
|
+
else {
|
|
216
|
+
return new Statement(maybePromise(() => this.connect().then(() => prepareStatement(this.db, sql))), this.db, this.execLock, this.ioStep);
|
|
232
217
|
}
|
|
233
218
|
}
|
|
234
219
|
/**
|
|
@@ -535,7 +520,7 @@ class Database {
|
|
|
535
520
|
const exec = this.db.executor(sql, queryOptions);
|
|
536
521
|
try {
|
|
537
522
|
while (true) {
|
|
538
|
-
const [stepResult, sleepMs] =
|
|
523
|
+
const [stepResult, sleepMs] = stepSync(exec);
|
|
539
524
|
if (stepResult === STEP_IO) {
|
|
540
525
|
await this.io();
|
|
541
526
|
continue;
|
|
@@ -596,7 +581,7 @@ async function executeBatch(native, io, statements, options) {
|
|
|
596
581
|
const exec = native.executor(sql);
|
|
597
582
|
try {
|
|
598
583
|
while (true) {
|
|
599
|
-
const [stepResult, sleepMs] =
|
|
584
|
+
const [stepResult, sleepMs] = stepSync(exec);
|
|
600
585
|
if (stepResult === STEP_IO) {
|
|
601
586
|
await io();
|
|
602
587
|
continue;
|
|
@@ -669,7 +654,7 @@ async function executeBatch(native, io, statements, options) {
|
|
|
669
654
|
const rows = [];
|
|
670
655
|
try {
|
|
671
656
|
while (true) {
|
|
672
|
-
const [stepResult, sleepMs] = await
|
|
657
|
+
const [stepResult, sleepMs] = await stepSync(nativeStmt);
|
|
673
658
|
if (stepResult === STEP_IO) {
|
|
674
659
|
await io();
|
|
675
660
|
continue;
|
|
@@ -693,7 +678,7 @@ async function executeBatch(native, io, statements, options) {
|
|
|
693
678
|
return makeResultSet(columnNames, columnTypes, rows, rowsAffected, lastInsertRowid);
|
|
694
679
|
}
|
|
695
680
|
finally {
|
|
696
|
-
nativeStmt
|
|
681
|
+
resetStatement(nativeStmt);
|
|
697
682
|
}
|
|
698
683
|
}
|
|
699
684
|
finally {
|
|
@@ -818,7 +803,7 @@ class Transaction {
|
|
|
818
803
|
const exec = this.db.executor(sql, queryOptions);
|
|
819
804
|
try {
|
|
820
805
|
while (true) {
|
|
821
|
-
const [stepResult, sleepMs] =
|
|
806
|
+
const [stepResult, sleepMs] = stepSync(exec);
|
|
822
807
|
if (stepResult === STEP_IO) {
|
|
823
808
|
await this.ioStep();
|
|
824
809
|
continue;
|
|
@@ -855,12 +840,7 @@ class Transaction {
|
|
|
855
840
|
if (!sql) {
|
|
856
841
|
throw new RangeError("The supplied SQL string contains no statements");
|
|
857
842
|
}
|
|
858
|
-
|
|
859
|
-
return new Statement(maybeValue(this.db.prepare(sql)), this.db, this.gate, this.ioStep);
|
|
860
|
-
}
|
|
861
|
-
catch (err) {
|
|
862
|
-
throw convertError(err);
|
|
863
|
-
}
|
|
843
|
+
return new Statement(maybeValue(prepareStatement(this.db, sql)), this.db, this.gate, this.ioStep);
|
|
864
844
|
}
|
|
865
845
|
/**
|
|
866
846
|
* Prepares the SQL and executes it as `Statement.run`, returning the run info.
|
|
@@ -1053,7 +1033,7 @@ class Statement {
|
|
|
1053
1033
|
bindParams(stmt, toBindArgs(params));
|
|
1054
1034
|
const totalChangesBefore = this.db.totalChanges();
|
|
1055
1035
|
while (true) {
|
|
1056
|
-
const [stepResult, sleepMs] = await
|
|
1036
|
+
const [stepResult, sleepMs] = await stepSync(stmt);
|
|
1057
1037
|
if (stepResult === STEP_IO) {
|
|
1058
1038
|
await this.io();
|
|
1059
1039
|
continue;
|
|
@@ -1075,7 +1055,7 @@ class Statement {
|
|
|
1075
1055
|
return { changes, lastInsertRowid };
|
|
1076
1056
|
}
|
|
1077
1057
|
finally {
|
|
1078
|
-
stmt
|
|
1058
|
+
resetStatement(stmt);
|
|
1079
1059
|
this.execLock.release();
|
|
1080
1060
|
}
|
|
1081
1061
|
}
|
|
@@ -1093,7 +1073,7 @@ class Statement {
|
|
|
1093
1073
|
stmt.setQueryTimeout(queryOptions);
|
|
1094
1074
|
bindParams(stmt, toBindArgs(params));
|
|
1095
1075
|
while (true) {
|
|
1096
|
-
const [stepResult, sleepMs] = await
|
|
1076
|
+
const [stepResult, sleepMs] = await stepSync(stmt);
|
|
1097
1077
|
if (stepResult === STEP_IO) {
|
|
1098
1078
|
await this.io();
|
|
1099
1079
|
continue;
|
|
@@ -1113,7 +1093,7 @@ class Statement {
|
|
|
1113
1093
|
return row;
|
|
1114
1094
|
}
|
|
1115
1095
|
finally {
|
|
1116
|
-
stmt
|
|
1096
|
+
resetStatement(stmt);
|
|
1117
1097
|
this.execLock.release();
|
|
1118
1098
|
}
|
|
1119
1099
|
}
|
|
@@ -1130,7 +1110,7 @@ class Statement {
|
|
|
1130
1110
|
stmt.setQueryTimeout(queryOptions);
|
|
1131
1111
|
bindParams(stmt, toBindArgs(params));
|
|
1132
1112
|
while (true) {
|
|
1133
|
-
const [stepResult, sleepMs] = await
|
|
1113
|
+
const [stepResult, sleepMs] = await stepSync(stmt);
|
|
1134
1114
|
if (stepResult === STEP_IO) {
|
|
1135
1115
|
await this.io();
|
|
1136
1116
|
continue;
|
|
@@ -1148,7 +1128,7 @@ class Statement {
|
|
|
1148
1128
|
}
|
|
1149
1129
|
}
|
|
1150
1130
|
finally {
|
|
1151
|
-
stmt
|
|
1131
|
+
resetStatement(stmt);
|
|
1152
1132
|
this.execLock.release();
|
|
1153
1133
|
}
|
|
1154
1134
|
}
|
|
@@ -1166,7 +1146,7 @@ class Statement {
|
|
|
1166
1146
|
stmt.setQueryTimeout(queryOptions);
|
|
1167
1147
|
bindParams(stmt, toBindArgs(params));
|
|
1168
1148
|
while (true) {
|
|
1169
|
-
const [stepResult, sleepMs] = await
|
|
1149
|
+
const [stepResult, sleepMs] = await stepSync(stmt);
|
|
1170
1150
|
if (stepResult === STEP_IO) {
|
|
1171
1151
|
await this.io();
|
|
1172
1152
|
continue;
|
|
@@ -1185,7 +1165,7 @@ class Statement {
|
|
|
1185
1165
|
return rows;
|
|
1186
1166
|
}
|
|
1187
1167
|
finally {
|
|
1188
|
-
stmt
|
|
1168
|
+
resetStatement(stmt);
|
|
1189
1169
|
this.execLock.release();
|
|
1190
1170
|
}
|
|
1191
1171
|
}
|
package/dist/sqlite-error.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { NativeDatabase, NativeExecutor, NativeStatement, StepResult } from "./types.js";
|
|
1
2
|
export declare class SqliteError extends Error {
|
|
2
3
|
name: string;
|
|
3
4
|
code: string;
|
|
@@ -16,4 +17,13 @@ export declare class SqliteError extends Error {
|
|
|
16
17
|
rollbackError?: Error;
|
|
17
18
|
constructor(message: any, code: any, rawCode: any);
|
|
18
19
|
}
|
|
20
|
+
/** Prepares SQL on a native database and throws its errors as `SqliteError`. */
|
|
21
|
+
export declare function prepareStatement(db: NativeDatabase, sql: string): NativeStatement;
|
|
22
|
+
/** Steps a native statement or executor and throws its errors as `SqliteError`. */
|
|
23
|
+
export declare function stepSync(stmt: NativeStatement | NativeExecutor): StepResult;
|
|
24
|
+
/** Resets a native statement and throws its errors as `SqliteError`. */
|
|
25
|
+
export declare function resetStatement(stmt: NativeStatement): void;
|
|
26
|
+
/** Runs the native I/O loop once and throws its errors as `SqliteError`. */
|
|
27
|
+
export declare function ioLoopSync(db: NativeDatabase): void;
|
|
28
|
+
export declare function convertError(err: any): any;
|
|
19
29
|
//# sourceMappingURL=sqlite-error.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sqlite-error.d.ts","sourceRoot":"","sources":["../sqlite-error.ts"],"names":[],"mappings":"AAAA,qBAAa,WAAY,SAAQ,KAAK;IACpC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB;oBACgB;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;;;2EAKuE;IACvE,YAAY,CAAC,EAAE,KAAK,CAAC,GAAG,GAAG,IAAI,CAAC,CAAC;IACjC,gEAAgE;IAChE,aAAa,CAAC,EAAE,KAAK,CAAC;gBACV,OAAO,KAAA,EAAE,IAAI,KAAA,EAAE,OAAO,KAAA;CAQnC"}
|
|
1
|
+
{"version":3,"file":"sqlite-error.d.ts","sourceRoot":"","sources":["../sqlite-error.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAEzF,qBAAa,WAAY,SAAQ,KAAK;IACpC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB;oBACgB;IAChB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;;;2EAKuE;IACvE,YAAY,CAAC,EAAE,KAAK,CAAC,GAAG,GAAG,IAAI,CAAC,CAAC;IACjC,gEAAgE;IAChE,aAAa,CAAC,EAAE,KAAK,CAAC;gBACV,OAAO,KAAA,EAAE,IAAI,KAAA,EAAE,OAAO,KAAA;CAQnC;AAED,gFAAgF;AAChF,wBAAgB,gBAAgB,CAAC,EAAE,EAAE,cAAc,EAAE,GAAG,EAAE,MAAM,GAAG,eAAe,CAMjF;AAED,mFAAmF;AACnF,wBAAgB,QAAQ,CAAC,IAAI,EAAE,eAAe,GAAG,cAAc,GAAG,UAAU,CAM3E;AAED,wEAAwE;AACxE,wBAAgB,cAAc,CAAC,IAAI,EAAE,eAAe,QAMnD;AAED,4EAA4E;AAC5E,wBAAgB,UAAU,CAAC,EAAE,EAAE,cAAc,QAM5C;AAKD,wBAAgB,YAAY,CAAC,GAAG,KAAA,OAS/B"}
|
package/dist/sqlite-error.js
CHANGED
|
@@ -22,3 +22,54 @@ export class SqliteError extends Error {
|
|
|
22
22
|
Error.captureStackTrace(this, SqliteError);
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
|
+
/** Prepares SQL on a native database and throws its errors as `SqliteError`. */
|
|
26
|
+
export function prepareStatement(db, sql) {
|
|
27
|
+
try {
|
|
28
|
+
return db.prepare(sql);
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
throw convertError(err);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/** Steps a native statement or executor and throws its errors as `SqliteError`. */
|
|
35
|
+
export function stepSync(stmt) {
|
|
36
|
+
try {
|
|
37
|
+
return stmt.stepSync();
|
|
38
|
+
}
|
|
39
|
+
catch (err) {
|
|
40
|
+
throw convertError(err);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** Resets a native statement and throws its errors as `SqliteError`. */
|
|
44
|
+
export function resetStatement(stmt) {
|
|
45
|
+
try {
|
|
46
|
+
stmt.reset();
|
|
47
|
+
}
|
|
48
|
+
catch (err) {
|
|
49
|
+
throw convertError(err);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/** Runs the native I/O loop once and throws its errors as `SqliteError`. */
|
|
53
|
+
export function ioLoopSync(db) {
|
|
54
|
+
try {
|
|
55
|
+
db.ioLoopSync();
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
throw convertError(err);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const convertibleErrorTypes = { TypeError };
|
|
62
|
+
const CONVERTIBLE_ERROR_PREFIX = "[TURSO_CONVERT_TYPE]";
|
|
63
|
+
export function convertError(err) {
|
|
64
|
+
if ((err.code ?? "").startsWith(CONVERTIBLE_ERROR_PREFIX)) {
|
|
65
|
+
return createErrorByName(err.code.substring(CONVERTIBLE_ERROR_PREFIX.length), err.message);
|
|
66
|
+
}
|
|
67
|
+
return new SqliteError(err.message, err.code, err.rawCode);
|
|
68
|
+
}
|
|
69
|
+
function createErrorByName(name, message) {
|
|
70
|
+
const ErrorConstructor = convertibleErrorTypes[name];
|
|
71
|
+
if (!ErrorConstructor) {
|
|
72
|
+
throw new Error(`unknown error type ${name} from Turso`);
|
|
73
|
+
}
|
|
74
|
+
return new ErrorConstructor(message);
|
|
75
|
+
}
|