@tursodatabase/database-common 0.8.0-pre.7 → 0.8.0-pre.8
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/promise.d.ts +28 -13
- package/dist/promise.d.ts.map +1 -1
- package/dist/promise.js +186 -51
- package/dist/sqlite-error.d.ts +12 -0
- package/dist/sqlite-error.d.ts.map +1 -1
- package/dist/sqlite-error.js +12 -0
- package/package.json +2 -2
package/dist/promise.d.ts
CHANGED
|
@@ -8,6 +8,10 @@ export interface BatchOptions {
|
|
|
8
8
|
mode?: BatchMode;
|
|
9
9
|
raw?: boolean;
|
|
10
10
|
}
|
|
11
|
+
type BatchStatement = string | {
|
|
12
|
+
sql: string;
|
|
13
|
+
args?: any[] | Record<string, any>;
|
|
14
|
+
};
|
|
11
15
|
export type BatchRow = Record<string, any> | any[];
|
|
12
16
|
/**
|
|
13
17
|
* The result of executing a single statement within `batch()`, matching the
|
|
@@ -22,6 +26,20 @@ export interface ResultSet {
|
|
|
22
26
|
rows: Array<BatchRow>;
|
|
23
27
|
/** Number of rows changed by the statement (0 for SELECT). */
|
|
24
28
|
rowsAffected: number;
|
|
29
|
+
/** Rowid inserted by the statement, when the statement inserted one. */
|
|
30
|
+
lastInsertRowid?: number;
|
|
31
|
+
/** Rows read while executing the statement. The embedded engine does
|
|
32
|
+
* not report this per statement; the serverless driver reports the
|
|
33
|
+
* server's value. */
|
|
34
|
+
rowsRead?: number;
|
|
35
|
+
/** Rows written while executing the statement. The embedded engine
|
|
36
|
+
* does not report this per statement; the serverless driver reports
|
|
37
|
+
* the server's value. */
|
|
38
|
+
rowsWritten?: number;
|
|
39
|
+
/** Server-side execution time in milliseconds. The embedded engine
|
|
40
|
+
* does not report this per statement; the serverless driver reports
|
|
41
|
+
* the server's value. */
|
|
42
|
+
queryDurationMs?: number;
|
|
25
43
|
}
|
|
26
44
|
/**
|
|
27
45
|
* Rejects transaction callbacks that cannot be using the `Transaction`
|
|
@@ -157,10 +175,8 @@ declare class Database {
|
|
|
157
175
|
* When `mode` is set, `batch()` owns the surrounding
|
|
158
176
|
* `BEGIN`/`COMMIT`/`ROLLBACK`, so the `statements` array must not
|
|
159
177
|
* contain its own transaction-control SQL (`BEGIN`, `COMMIT`,
|
|
160
|
-
* `ROLLBACK`, `SAVEPOINT`, `RELEASE`).
|
|
161
|
-
*
|
|
162
|
-
* transaction mid-batch and leave earlier statements committed,
|
|
163
|
-
* defeating the all-or-nothing contract.
|
|
178
|
+
* `END`, `ROLLBACK`, `SAVEPOINT`, `RELEASE`). Such input is rejected
|
|
179
|
+
* before the batch starts.
|
|
164
180
|
*
|
|
165
181
|
* @param statements - An array of SQL strings or `{ sql, args }` objects.
|
|
166
182
|
* @param mode - When set, wraps the batch in `BEGIN <mode>` / `COMMIT`
|
|
@@ -168,7 +184,12 @@ declare class Database {
|
|
|
168
184
|
* transaction.
|
|
169
185
|
* @returns An array of `ResultSet`s — one per input statement, in order —
|
|
170
186
|
* matching the libsql-js batch contract. Each `ResultSet` carries that
|
|
171
|
-
* statement's `columns`, `columnTypes`, `rows`,
|
|
187
|
+
* statement's `columns`, `columnTypes`, `rows`, `rowsAffected`, and
|
|
188
|
+
* `lastInsertRowid`. Execution stops at the first statement that
|
|
189
|
+
* fails: the thrown error carries `batchIndex` (the zero-based index
|
|
190
|
+
* of the failing statement) and `batchResults` (one entry per
|
|
191
|
+
* statement — the completed statement's `ResultSet`, or `null` for
|
|
192
|
+
* the failing statement and the statements that did not run).
|
|
172
193
|
*
|
|
173
194
|
* @example
|
|
174
195
|
* // Plain SQL strings (non-atomic).
|
|
@@ -199,10 +220,7 @@ declare class Database {
|
|
|
199
220
|
* });
|
|
200
221
|
* await txn.immediate();
|
|
201
222
|
*/
|
|
202
|
-
batch(statements:
|
|
203
|
-
sql: string;
|
|
204
|
-
args?: any[] | Record<string, any>;
|
|
205
|
-
}>, options?: BatchMode | BatchOptions): Promise<ResultSet[]>;
|
|
223
|
+
batch(statements: BatchStatement[], options?: BatchMode | BatchOptions): Promise<ResultSet[]>;
|
|
206
224
|
/**
|
|
207
225
|
* Prepares the SQL and executes it as `Statement.run`, returning the run info.
|
|
208
226
|
*
|
|
@@ -339,10 +357,7 @@ declare class Transaction {
|
|
|
339
357
|
*
|
|
340
358
|
* @param statements - An array of SQL strings or `{ sql, args }` objects.
|
|
341
359
|
*/
|
|
342
|
-
batch(statements:
|
|
343
|
-
sql: string;
|
|
344
|
-
args?: any[] | Record<string, any>;
|
|
345
|
-
}>, options?: BatchMode | BatchOptions): Promise<ResultSet[]>;
|
|
360
|
+
batch(statements: BatchStatement[], options?: BatchMode | BatchOptions): Promise<ResultSet[]>;
|
|
346
361
|
}
|
|
347
362
|
interface MaybeLazy<T> {
|
|
348
363
|
apply(fn: (value: T) => void): any;
|
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;AA6DnI;;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,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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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;;;;IAuC3B;;;;OAIG;IACG,GAAG,CAAC,GAAG,cAAc,OAAA;IAmC3B;;;;OAIG;IACI,OAAO,CAAC,GAAG,cAAc,OAAA;IAgChC;;;;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
|
@@ -49,6 +49,14 @@ function toBindArgs(params) {
|
|
|
49
49
|
}
|
|
50
50
|
return [params];
|
|
51
51
|
}
|
|
52
|
+
const TRANSACTION_CONTROL_KEYWORDS = new Set([
|
|
53
|
+
"BEGIN",
|
|
54
|
+
"COMMIT",
|
|
55
|
+
"END",
|
|
56
|
+
"ROLLBACK",
|
|
57
|
+
"SAVEPOINT",
|
|
58
|
+
"RELEASE",
|
|
59
|
+
]);
|
|
52
60
|
function normalizeBatchMode(mode) {
|
|
53
61
|
switch (String(mode).toLowerCase()) {
|
|
54
62
|
case "write":
|
|
@@ -78,16 +86,65 @@ function normalizeBatchOptions(options) {
|
|
|
78
86
|
raw: false,
|
|
79
87
|
};
|
|
80
88
|
}
|
|
89
|
+
function firstSqlKeyword(sql) {
|
|
90
|
+
let offset = 0;
|
|
91
|
+
while (offset < sql.length) {
|
|
92
|
+
if (/\s/.test(sql[offset]) || sql[offset] === ";") {
|
|
93
|
+
offset++;
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
if (sql.startsWith("--", offset)) {
|
|
97
|
+
const newline = sql.indexOf("\n", offset + 2);
|
|
98
|
+
if (newline < 0)
|
|
99
|
+
return undefined;
|
|
100
|
+
offset = newline + 1;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
if (sql.startsWith("/*", offset)) {
|
|
104
|
+
const end = sql.indexOf("*/", offset + 2);
|
|
105
|
+
if (end < 0)
|
|
106
|
+
return undefined;
|
|
107
|
+
offset = end + 2;
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
return /^[A-Za-z]+/.exec(sql.slice(offset))?.[0].toUpperCase();
|
|
113
|
+
}
|
|
114
|
+
function batchInputError(index, cause) {
|
|
115
|
+
const message = cause instanceof Error ? cause.message : String(cause);
|
|
116
|
+
const error = new TypeError(`batch statement ${index} failed: ${message}`);
|
|
117
|
+
error.batchIndex = index;
|
|
118
|
+
error.batchResults = [];
|
|
119
|
+
return error;
|
|
120
|
+
}
|
|
121
|
+
function attachRollbackError(primary, rollback) {
|
|
122
|
+
const rollbackError = rollback instanceof Error ? rollback : new Error(String(rollback));
|
|
123
|
+
if (primary instanceof Error) {
|
|
124
|
+
primary.rollbackError = rollbackError;
|
|
125
|
+
return primary;
|
|
126
|
+
}
|
|
127
|
+
const error = new Error(String(primary));
|
|
128
|
+
error.cause = primary;
|
|
129
|
+
error.rollbackError = rollbackError;
|
|
130
|
+
return error;
|
|
131
|
+
}
|
|
81
132
|
/**
|
|
82
133
|
* Builds a libSQL-style `ResultSet` for a single statement in a `batch()`.
|
|
83
134
|
*/
|
|
84
|
-
function makeResultSet(columns, columnTypes, rows, rowsAffected) {
|
|
85
|
-
|
|
135
|
+
function makeResultSet(columns, columnTypes, rows, rowsAffected, lastInsertRowid) {
|
|
136
|
+
const resultSet = {
|
|
86
137
|
columns,
|
|
87
138
|
columnTypes,
|
|
88
139
|
rows,
|
|
89
140
|
rowsAffected,
|
|
90
141
|
};
|
|
142
|
+
// Only statements that inserted carry the key, so callers can use
|
|
143
|
+
// `"lastInsertRowid" in resultSet` to detect an insert.
|
|
144
|
+
if (lastInsertRowid !== undefined) {
|
|
145
|
+
resultSet.lastInsertRowid = lastInsertRowid;
|
|
146
|
+
}
|
|
147
|
+
return resultSet;
|
|
91
148
|
}
|
|
92
149
|
/**
|
|
93
150
|
* Rejects transaction callbacks that cannot be using the `Transaction`
|
|
@@ -300,10 +357,8 @@ class Database {
|
|
|
300
357
|
* When `mode` is set, `batch()` owns the surrounding
|
|
301
358
|
* `BEGIN`/`COMMIT`/`ROLLBACK`, so the `statements` array must not
|
|
302
359
|
* contain its own transaction-control SQL (`BEGIN`, `COMMIT`,
|
|
303
|
-
* `ROLLBACK`, `SAVEPOINT`, `RELEASE`).
|
|
304
|
-
*
|
|
305
|
-
* transaction mid-batch and leave earlier statements committed,
|
|
306
|
-
* defeating the all-or-nothing contract.
|
|
360
|
+
* `END`, `ROLLBACK`, `SAVEPOINT`, `RELEASE`). Such input is rejected
|
|
361
|
+
* before the batch starts.
|
|
307
362
|
*
|
|
308
363
|
* @param statements - An array of SQL strings or `{ sql, args }` objects.
|
|
309
364
|
* @param mode - When set, wraps the batch in `BEGIN <mode>` / `COMMIT`
|
|
@@ -311,7 +366,12 @@ class Database {
|
|
|
311
366
|
* transaction.
|
|
312
367
|
* @returns An array of `ResultSet`s — one per input statement, in order —
|
|
313
368
|
* matching the libsql-js batch contract. Each `ResultSet` carries that
|
|
314
|
-
* statement's `columns`, `columnTypes`, `rows`,
|
|
369
|
+
* statement's `columns`, `columnTypes`, `rows`, `rowsAffected`, and
|
|
370
|
+
* `lastInsertRowid`. Execution stops at the first statement that
|
|
371
|
+
* fails: the thrown error carries `batchIndex` (the zero-based index
|
|
372
|
+
* of the failing statement) and `batchResults` (one entry per
|
|
373
|
+
* statement — the completed statement's `ResultSet`, or `null` for
|
|
374
|
+
* the failing statement and the statements that did not run).
|
|
315
375
|
*
|
|
316
376
|
* @example
|
|
317
377
|
* // Plain SQL strings (non-atomic).
|
|
@@ -556,60 +616,109 @@ async function executeBatch(native, io, statements, options) {
|
|
|
556
616
|
};
|
|
557
617
|
const { mode, raw } = normalizeBatchOptions(options);
|
|
558
618
|
const wrap = mode != null && !native.inTransaction();
|
|
619
|
+
const normalizedStatements = statements.map((statement, index) => {
|
|
620
|
+
if (typeof statement === "string" || statement.args === undefined) {
|
|
621
|
+
return statement;
|
|
622
|
+
}
|
|
623
|
+
try {
|
|
624
|
+
const args = Array.isArray(statement.args)
|
|
625
|
+
? statement.args.map(normalizeBatchBindValue)
|
|
626
|
+
: Object.fromEntries(Object.entries(statement.args).map(([name, value]) => [name, normalizeBatchBindValue(value)]));
|
|
627
|
+
return { sql: statement.sql, args };
|
|
628
|
+
}
|
|
629
|
+
catch (error) {
|
|
630
|
+
throw batchInputError(index, error);
|
|
631
|
+
}
|
|
632
|
+
});
|
|
633
|
+
if (wrap) {
|
|
634
|
+
for (let index = 0; index < normalizedStatements.length; index++) {
|
|
635
|
+
const statement = normalizedStatements[index];
|
|
636
|
+
const sql = typeof statement === "string" ? statement : statement.sql;
|
|
637
|
+
const keyword = firstSqlKeyword(sql);
|
|
638
|
+
if (keyword !== undefined && TRANSACTION_CONTROL_KEYWORDS.has(keyword)) {
|
|
639
|
+
throw batchInputError(index, new Error(`${keyword} is not allowed in an atomic batch`));
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
}
|
|
559
643
|
if (wrap) {
|
|
560
644
|
await runRawSql(`BEGIN ${normalizeBatchMode(mode)}`);
|
|
561
645
|
}
|
|
562
646
|
const results = [];
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
647
|
+
const executeStatement = async (statement) => {
|
|
648
|
+
const sql = typeof statement === "string" ? statement : statement.sql;
|
|
649
|
+
const args = typeof statement === "string" ? undefined : statement.args;
|
|
650
|
+
let nativeStmt;
|
|
651
|
+
try {
|
|
652
|
+
nativeStmt = native.prepare(sql);
|
|
653
|
+
}
|
|
654
|
+
catch (err) {
|
|
655
|
+
throw convertError(err);
|
|
656
|
+
}
|
|
657
|
+
try {
|
|
658
|
+
if (args !== undefined) {
|
|
659
|
+
bindParams(nativeStmt, [args]);
|
|
570
660
|
}
|
|
571
|
-
|
|
572
|
-
|
|
661
|
+
const cols = nativeStmt.columns();
|
|
662
|
+
const columnNames = cols.map((c) => c.name);
|
|
663
|
+
const columnTypes = cols.map((c) => c.type ?? "");
|
|
664
|
+
if (columnNames.length > 0) {
|
|
665
|
+
nativeStmt.raw(raw);
|
|
573
666
|
}
|
|
667
|
+
const totalChangesBefore = native.totalChanges();
|
|
668
|
+
const rowidBefore = native.lastInsertRowid();
|
|
669
|
+
const rows = [];
|
|
574
670
|
try {
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
const columnTypes = cols.map((c) => c.type ?? "");
|
|
581
|
-
if (columnNames.length > 0) {
|
|
582
|
-
nativeStmt.raw(raw);
|
|
583
|
-
}
|
|
584
|
-
const totalChangesBefore = native.totalChanges();
|
|
585
|
-
const rows = [];
|
|
586
|
-
try {
|
|
587
|
-
while (true) {
|
|
588
|
-
const [stepResult, sleepMs] = await nativeStmt.stepSync();
|
|
589
|
-
if (stepResult === STEP_IO) {
|
|
590
|
-
await io();
|
|
591
|
-
continue;
|
|
592
|
-
}
|
|
593
|
-
if (stepResult === STEP_SLEEP) {
|
|
594
|
-
await sleepBeforeRetry(sleepMs);
|
|
595
|
-
continue;
|
|
596
|
-
}
|
|
597
|
-
if (stepResult === STEP_DONE) {
|
|
598
|
-
break;
|
|
599
|
-
}
|
|
600
|
-
rows.push(nativeStmt.row());
|
|
671
|
+
while (true) {
|
|
672
|
+
const [stepResult, sleepMs] = await nativeStmt.stepSync();
|
|
673
|
+
if (stepResult === STEP_IO) {
|
|
674
|
+
await io();
|
|
675
|
+
continue;
|
|
601
676
|
}
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
677
|
+
if (stepResult === STEP_SLEEP) {
|
|
678
|
+
await sleepBeforeRetry(sleepMs);
|
|
679
|
+
continue;
|
|
680
|
+
}
|
|
681
|
+
if (stepResult === STEP_DONE) {
|
|
682
|
+
break;
|
|
683
|
+
}
|
|
684
|
+
rows.push(nativeStmt.row());
|
|
609
685
|
}
|
|
686
|
+
const rowsAffected = columnNames.length > 0
|
|
687
|
+
? 0
|
|
688
|
+
: native.totalChanges() !== totalChangesBefore ? native.changes() : 0;
|
|
689
|
+
// The engine tracks the inserted rowid per connection, not per
|
|
690
|
+
// statement; a change across this statement means it inserted.
|
|
691
|
+
const rowidAfter = native.lastInsertRowid();
|
|
692
|
+
const lastInsertRowid = rowidAfter !== rowidBefore ? rowidAfter : undefined;
|
|
693
|
+
return makeResultSet(columnNames, columnTypes, rows, rowsAffected, lastInsertRowid);
|
|
610
694
|
}
|
|
611
695
|
finally {
|
|
612
|
-
nativeStmt.
|
|
696
|
+
nativeStmt.reset();
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
finally {
|
|
700
|
+
nativeStmt.finalize();
|
|
701
|
+
}
|
|
702
|
+
};
|
|
703
|
+
try {
|
|
704
|
+
for (let index = 0; index < normalizedStatements.length; index++) {
|
|
705
|
+
try {
|
|
706
|
+
results.push(await executeStatement(normalizedStatements[index]));
|
|
707
|
+
}
|
|
708
|
+
catch (err) {
|
|
709
|
+
// Identify the failing statement and carry the results of the
|
|
710
|
+
// statements that completed, matching the serverless driver:
|
|
711
|
+
// one entry per statement, null for the failing statement and
|
|
712
|
+
// the ones that never ran.
|
|
713
|
+
if (err instanceof Error) {
|
|
714
|
+
const batchErr = err;
|
|
715
|
+
batchErr.batchIndex = index;
|
|
716
|
+
batchErr.batchResults = [
|
|
717
|
+
...results.map((result) => result),
|
|
718
|
+
...Array(normalizedStatements.length - results.length).fill(null),
|
|
719
|
+
];
|
|
720
|
+
}
|
|
721
|
+
throw err;
|
|
613
722
|
}
|
|
614
723
|
}
|
|
615
724
|
if (wrap) {
|
|
@@ -621,12 +730,38 @@ async function executeBatch(native, io, statements, options) {
|
|
|
621
730
|
try {
|
|
622
731
|
await runRawSql("ROLLBACK");
|
|
623
732
|
}
|
|
624
|
-
catch {
|
|
733
|
+
catch (rollbackError) {
|
|
734
|
+
throw attachRollbackError(err, rollbackError);
|
|
735
|
+
}
|
|
625
736
|
}
|
|
626
737
|
throw err;
|
|
627
738
|
}
|
|
628
739
|
return results;
|
|
629
740
|
}
|
|
741
|
+
function normalizeBatchBindValue(value) {
|
|
742
|
+
if (value === null || value === undefined)
|
|
743
|
+
return null;
|
|
744
|
+
if (typeof value === "number") {
|
|
745
|
+
if (!Number.isFinite(value)) {
|
|
746
|
+
throw new TypeError("Only finite numbers (not Infinity or NaN) can be passed as arguments");
|
|
747
|
+
}
|
|
748
|
+
return value;
|
|
749
|
+
}
|
|
750
|
+
if (typeof value === "bigint") {
|
|
751
|
+
if (value < -(1n << 63n) || value > (1n << 63n) - 1n) {
|
|
752
|
+
throw new TypeError("BigInt value is outside SQLite's signed 64-bit integer range");
|
|
753
|
+
}
|
|
754
|
+
return value;
|
|
755
|
+
}
|
|
756
|
+
if (typeof value === "boolean" ||
|
|
757
|
+
typeof value === "string" ||
|
|
758
|
+
(ArrayBuffer.isView(value) && !(value instanceof DataView))) {
|
|
759
|
+
return value;
|
|
760
|
+
}
|
|
761
|
+
if (value instanceof ArrayBuffer)
|
|
762
|
+
return new Uint8Array(value);
|
|
763
|
+
return String(value);
|
|
764
|
+
}
|
|
630
765
|
/**
|
|
631
766
|
* A handle to an open transaction, passed as the first argument to the
|
|
632
767
|
* callback of `Database.transactionAsync()`. All SQL of the transaction
|
package/dist/sqlite-error.d.ts
CHANGED
|
@@ -2,6 +2,18 @@ export declare class SqliteError extends Error {
|
|
|
2
2
|
name: string;
|
|
3
3
|
code: string;
|
|
4
4
|
rawCode: string;
|
|
5
|
+
/** For errors raised by `batch()`: the zero-based index of the failing
|
|
6
|
+
* statement. */
|
|
7
|
+
batchIndex?: number;
|
|
8
|
+
/** For errors raised by `batch()`: one entry per input statement, in
|
|
9
|
+
* order — the completed statement's `ResultSet`, or `null` for the
|
|
10
|
+
* failing statement and the statements that did not run. In a
|
|
11
|
+
* non-atomic batch the completed statements' effects are committed; in
|
|
12
|
+
* an atomic batch they were rolled back unless `rollbackError` is set.
|
|
13
|
+
* Empty when the batch failed client-side before anything was sent. */
|
|
14
|
+
batchResults?: Array<any | null>;
|
|
15
|
+
/** A rollback error that occurred while handling this error. */
|
|
16
|
+
rollbackError?: Error;
|
|
5
17
|
constructor(message: any, code: any, rawCode: any);
|
|
6
18
|
}
|
|
7
19
|
//# 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;
|
|
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"}
|
package/dist/sqlite-error.js
CHANGED
|
@@ -2,6 +2,18 @@ export class SqliteError extends Error {
|
|
|
2
2
|
name;
|
|
3
3
|
code;
|
|
4
4
|
rawCode;
|
|
5
|
+
/** For errors raised by `batch()`: the zero-based index of the failing
|
|
6
|
+
* statement. */
|
|
7
|
+
batchIndex;
|
|
8
|
+
/** For errors raised by `batch()`: one entry per input statement, in
|
|
9
|
+
* order — the completed statement's `ResultSet`, or `null` for the
|
|
10
|
+
* failing statement and the statements that did not run. In a
|
|
11
|
+
* non-atomic batch the completed statements' effects are committed; in
|
|
12
|
+
* an atomic batch they were rolled back unless `rollbackError` is set.
|
|
13
|
+
* Empty when the batch failed client-side before anything was sent. */
|
|
14
|
+
batchResults;
|
|
15
|
+
/** A rollback error that occurred while handling this error. */
|
|
16
|
+
rollbackError;
|
|
5
17
|
constructor(message, code, rawCode) {
|
|
6
18
|
super(message);
|
|
7
19
|
this.name = 'SqliteError';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tursodatabase/database-common",
|
|
3
|
-
"version": "0.8.0-pre.
|
|
3
|
+
"version": "0.8.0-pre.8",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/tursodatabase/turso"
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
],
|
|
17
17
|
"devDependencies": {
|
|
18
18
|
"typescript": "^5.9.2",
|
|
19
|
-
"vitest": "^3.2.
|
|
19
|
+
"vitest": "^3.2.7"
|
|
20
20
|
},
|
|
21
21
|
"scripts": {
|
|
22
22
|
"tsc-build": "npm exec tsc",
|