@tursodatabase/database-common 0.7.0-pre.7 → 0.7.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/README.md +1 -1
- package/dist/compat.d.ts +16 -0
- package/dist/compat.d.ts.map +1 -1
- package/dist/compat.js +111 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/promise.d.ts +26 -11
- package/dist/promise.d.ts.map +1 -1
- package/dist/promise.js +66 -23
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,5 +4,5 @@ This package is the Turso embedded database common JS library which is shared be
|
|
|
4
4
|
|
|
5
5
|
Do not use this package directly - instead you must use `@tursodatabase/database` or `@tursodatabase/database-wasm`.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Turso Database runs in production today at multiple organizations. It has not yet reached 1.0, so as with any database we recommend keeping backups — see the [FAQ](https://github.com/tursodatabase/turso#faq) for where the project stands.
|
|
8
8
|
|
package/dist/compat.d.ts
CHANGED
|
@@ -1,4 +1,16 @@
|
|
|
1
1
|
import { NativeDatabase, NativeStatement, QueryOptions } from "./types.js";
|
|
2
|
+
export type BatchMode = "write" | "read" | "deferred" | "immediate" | "exclusive" | "concurrent" | string;
|
|
3
|
+
export interface BatchOptions {
|
|
4
|
+
mode?: BatchMode;
|
|
5
|
+
raw?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export type BatchRow = Record<string, any> | any[];
|
|
8
|
+
export interface ResultSet {
|
|
9
|
+
columns: Array<string>;
|
|
10
|
+
columnTypes: Array<string>;
|
|
11
|
+
rows: Array<BatchRow>;
|
|
12
|
+
rowsAffected: number;
|
|
13
|
+
}
|
|
2
14
|
/**
|
|
3
15
|
* Database represents a connection that can prepare and execute SQL statements.
|
|
4
16
|
*/
|
|
@@ -48,6 +60,10 @@ declare class Database {
|
|
|
48
60
|
* @param {string} sql - The string containing SQL statements to execute
|
|
49
61
|
*/
|
|
50
62
|
exec(sql: any, queryOptions?: QueryOptions): void;
|
|
63
|
+
batch(statements: Array<string | {
|
|
64
|
+
sql: string;
|
|
65
|
+
args?: any[] | Record<string, any>;
|
|
66
|
+
}>, options?: BatchMode | BatchOptions): ResultSet[];
|
|
51
67
|
/**
|
|
52
68
|
* Interrupts the database connection.
|
|
53
69
|
*/
|
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,EAAgC,MAAM,YAAY,CAAC;AAoDzG;;GAEG;AACH,cAAM,QAAQ;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,OAAO,CAAC;IAClB,IAAI,EAAE,OAAO,CAAC;IACd,MAAM,EAAE,OAAO,CAAC;IAChB,aAAa,EAAE,OAAO,CAAC;IAEvB,OAAO,CAAC,EAAE,CAAiB;IAE3B;;;;;;;;;;OAUG;gBACS,EAAE,EAAE,cAAc;IAa9B;;;;OAIG;IACH,OAAO,CAAC,GAAG,KAAA;IAeX;;;;OAIG;IACH,WAAW,CAAC,EAAE,KAAA;IAgCd,MAAM,CAAC,MAAM,KAAA,EAAE,OAAO,KAAA;IAoBtB,MAAM,CAAC,QAAQ,KAAA,EAAE,OAAO,KAAA;IAIxB,SAAS,CAAC,OAAO,KAAA;IAIjB,QAAQ,CAAC,IAAI,KAAA,EAAE,OAAO,KAAA,EAAE,EAAE,KAAA;IAI1B,SAAS,CAAC,IAAI,KAAA,EAAE,OAAO,KAAA;IAIvB,KAAK,CAAC,IAAI,KAAA,EAAE,OAAO,KAAA;IAInB,aAAa,CAAC,IAAI,KAAA;IAIlB,wBAAwB;IAIxB;;;;;OAKG;IACH,IAAI,CAAC,GAAG,KAAA,EAAE,YAAY,CAAC,EAAE,YAAY;IAyBrC;;OAEG;IACH,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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
|
@@ -40,6 +40,43 @@ function toBindArgs(params) {
|
|
|
40
40
|
}
|
|
41
41
|
return [params];
|
|
42
42
|
}
|
|
43
|
+
function normalizeBatchMode(mode) {
|
|
44
|
+
switch (String(mode).toLowerCase()) {
|
|
45
|
+
case "write":
|
|
46
|
+
return "IMMEDIATE";
|
|
47
|
+
case "read":
|
|
48
|
+
case "deferred":
|
|
49
|
+
return "DEFERRED";
|
|
50
|
+
case "immediate":
|
|
51
|
+
return "IMMEDIATE";
|
|
52
|
+
case "exclusive":
|
|
53
|
+
return "EXCLUSIVE";
|
|
54
|
+
case "concurrent":
|
|
55
|
+
return "CONCURRENT";
|
|
56
|
+
default:
|
|
57
|
+
return String(mode).toUpperCase();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function normalizeBatchOptions(options) {
|
|
61
|
+
if (options != null && typeof options === "object") {
|
|
62
|
+
return {
|
|
63
|
+
mode: options.mode,
|
|
64
|
+
raw: options.raw === true,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
mode: options,
|
|
69
|
+
raw: false,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
function makeResultSet(columns, columnTypes, rows, rowsAffected) {
|
|
73
|
+
return {
|
|
74
|
+
columns,
|
|
75
|
+
columnTypes,
|
|
76
|
+
rows,
|
|
77
|
+
rowsAffected,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
43
80
|
/**
|
|
44
81
|
* Database represents a connection that can prepare and execute SQL statements.
|
|
45
82
|
*/
|
|
@@ -196,6 +233,80 @@ class Database {
|
|
|
196
233
|
exec.reset();
|
|
197
234
|
}
|
|
198
235
|
}
|
|
236
|
+
batch(statements, options) {
|
|
237
|
+
if (!Array.isArray(statements)) {
|
|
238
|
+
throw new TypeError("Expected first argument to be an array of statements");
|
|
239
|
+
}
|
|
240
|
+
if (!this.open) {
|
|
241
|
+
throw new TypeError("The database connection is not open");
|
|
242
|
+
}
|
|
243
|
+
const { mode, raw } = normalizeBatchOptions(options);
|
|
244
|
+
const wrap = mode != null && !this.db.inTransaction();
|
|
245
|
+
if (wrap) {
|
|
246
|
+
this.exec(`BEGIN ${normalizeBatchMode(mode)}`);
|
|
247
|
+
}
|
|
248
|
+
const results = [];
|
|
249
|
+
try {
|
|
250
|
+
for (const statement of statements) {
|
|
251
|
+
const sql = typeof statement === "string" ? statement : statement.sql;
|
|
252
|
+
const args = typeof statement === "string" ? undefined : statement.args;
|
|
253
|
+
const stmt = this.db.prepare(sql);
|
|
254
|
+
try {
|
|
255
|
+
if (args !== undefined) {
|
|
256
|
+
bindParams(stmt, [args]);
|
|
257
|
+
}
|
|
258
|
+
const cols = stmt.columns();
|
|
259
|
+
const columnNames = cols.map((c) => c.name);
|
|
260
|
+
const columnTypes = cols.map((c) => c.type ?? "");
|
|
261
|
+
if (columnNames.length > 0) {
|
|
262
|
+
stmt.raw(raw);
|
|
263
|
+
}
|
|
264
|
+
const totalChangesBefore = this.db.totalChanges();
|
|
265
|
+
const rows = [];
|
|
266
|
+
try {
|
|
267
|
+
while (true) {
|
|
268
|
+
const stepResult = stmt.stepSync();
|
|
269
|
+
if (stepResult === STEP_IO) {
|
|
270
|
+
this.db.ioLoopSync();
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
if (stepResult === STEP_DONE) {
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
if (stepResult === STEP_ROW) {
|
|
277
|
+
rows.push(stmt.row());
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
const rowsAffected = columnNames.length > 0
|
|
281
|
+
? 0
|
|
282
|
+
: this.db.totalChanges() !== totalChangesBefore ? this.db.changes() : 0;
|
|
283
|
+
results.push(makeResultSet(columnNames, columnTypes, rows, rowsAffected));
|
|
284
|
+
}
|
|
285
|
+
finally {
|
|
286
|
+
stmt.reset();
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
finally {
|
|
290
|
+
stmt.finalize();
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
if (wrap) {
|
|
294
|
+
this.exec("COMMIT");
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
catch (err) {
|
|
298
|
+
if (wrap) {
|
|
299
|
+
try {
|
|
300
|
+
this.exec("ROLLBACK");
|
|
301
|
+
}
|
|
302
|
+
catch {
|
|
303
|
+
// Keep the original statement error.
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
throw convertError(err);
|
|
307
|
+
}
|
|
308
|
+
return results;
|
|
309
|
+
}
|
|
199
310
|
/**
|
|
200
311
|
* Interrupts the database connection.
|
|
201
312
|
*/
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { NativeDatabase, NativeStatement, DatabaseOpts, EncryptionCipher, EncryptionOpts } from "./types.js";
|
|
1
|
+
import { NativeDatabase, NativeStatement, DatabaseOpts, EncryptionCipher, EncryptionOpts, ExperimentalFeature } from "./types.js";
|
|
2
2
|
import { Database as DatabaseCompat, Statement as StatementCompat } from "./compat.js";
|
|
3
3
|
import { Database as DatabasePromise, Statement as StatementPromise, TransactionFunction } from "./promise.js";
|
|
4
4
|
import { SqliteError } from "./sqlite-error.js";
|
|
5
5
|
import { AsyncLock } from "./async-lock.js";
|
|
6
|
-
export { DatabaseOpts, EncryptionCipher, EncryptionOpts, DatabaseCompat, StatementCompat, DatabasePromise, StatementPromise, TransactionFunction, NativeDatabase, NativeStatement, SqliteError, AsyncLock };
|
|
6
|
+
export { DatabaseOpts, ExperimentalFeature, EncryptionCipher, EncryptionOpts, DatabaseCompat, StatementCompat, DatabasePromise, StatementPromise, TransactionFunction, NativeDatabase, NativeStatement, SqliteError, AsyncLock };
|
|
7
7
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,YAAY,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,YAAY,EAAE,gBAAgB,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAClI,OAAO,EAAE,QAAQ,IAAI,cAAc,EAAE,SAAS,IAAI,eAAe,EAAE,MAAM,aAAa,CAAC;AACvF,OAAO,EAAE,QAAQ,IAAI,eAAe,EAAE,SAAS,IAAI,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AAC/G,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,OAAO,EACH,YAAY,EACZ,mBAAmB,EACnB,gBAAgB,EAChB,cAAc,EACd,cAAc,EAAE,eAAe,EAC/B,eAAe,EAAE,gBAAgB,EAAE,mBAAmB,EACtD,cAAc,EAAE,eAAe,EAC/B,WAAW,EACX,SAAS,EACZ,CAAA"}
|
package/dist/promise.d.ts
CHANGED
|
@@ -1,11 +1,28 @@
|
|
|
1
1
|
import { AsyncLock } from "./async-lock.js";
|
|
2
2
|
import { NativeDatabase, NativeStatement, QueryOptions } from "./types.js";
|
|
3
3
|
/**
|
|
4
|
-
* Locking mode for `Database.batch(stmts,
|
|
5
|
-
* `Database.transaction(...)`. When passed to `batch()`, the statements are
|
|
6
|
-
* wrapped in `BEGIN <mode>` / `COMMIT` (with `ROLLBACK` on failure).
|
|
4
|
+
* Locking mode for `Database.batch(stmts, options)`.
|
|
7
5
|
*/
|
|
8
|
-
export type BatchMode = "deferred" | "immediate" | "exclusive" | "concurrent";
|
|
6
|
+
export type BatchMode = "write" | "read" | "deferred" | "immediate" | "exclusive" | "concurrent" | string;
|
|
7
|
+
export interface BatchOptions {
|
|
8
|
+
mode?: BatchMode;
|
|
9
|
+
raw?: boolean;
|
|
10
|
+
}
|
|
11
|
+
export type BatchRow = Record<string, any> | any[];
|
|
12
|
+
/**
|
|
13
|
+
* The result of executing a single statement within `batch()`, matching the
|
|
14
|
+
* libSQL client's `ResultSet` shape.
|
|
15
|
+
*/
|
|
16
|
+
export interface ResultSet {
|
|
17
|
+
/** Column names of the result, empty for statements that return no rows. */
|
|
18
|
+
columns: Array<string>;
|
|
19
|
+
/** Declared column types, parallel to `columns`. */
|
|
20
|
+
columnTypes: Array<string>;
|
|
21
|
+
/** Result rows; empty for non-SELECT statements. */
|
|
22
|
+
rows: Array<BatchRow>;
|
|
23
|
+
/** Number of rows changed by the statement (0 for SELECT). */
|
|
24
|
+
rowsAffected: number;
|
|
25
|
+
}
|
|
9
26
|
/**
|
|
10
27
|
* A wrapped transaction function. Calling it runs the wrapped function inside a
|
|
11
28
|
* `BEGIN`/`COMMIT` block; the mode properties (`deferred`, `concurrent`, etc.)
|
|
@@ -32,7 +49,7 @@ declare class Database {
|
|
|
32
49
|
inTransaction: boolean;
|
|
33
50
|
private db;
|
|
34
51
|
private ioStep;
|
|
35
|
-
|
|
52
|
+
protected execLock: AsyncLock;
|
|
36
53
|
protected connected: boolean;
|
|
37
54
|
constructor(db: NativeDatabase, ioStep?: () => Promise<void>);
|
|
38
55
|
/**
|
|
@@ -74,8 +91,9 @@ declare class Database {
|
|
|
74
91
|
* @param mode - When set, wraps the batch in `BEGIN <mode>` / `COMMIT`
|
|
75
92
|
* (with `ROLLBACK` on failure). Ignored when already inside a
|
|
76
93
|
* transaction.
|
|
77
|
-
* @returns An
|
|
78
|
-
*
|
|
94
|
+
* @returns An array of `ResultSet`s — one per input statement, in order —
|
|
95
|
+
* matching the libsql-js batch contract. Each `ResultSet` carries that
|
|
96
|
+
* statement's `columns`, `columnTypes`, `rows`, and `rowsAffected`.
|
|
79
97
|
*
|
|
80
98
|
* @example
|
|
81
99
|
* // Plain SQL strings (non-atomic).
|
|
@@ -109,10 +127,7 @@ declare class Database {
|
|
|
109
127
|
batch(statements: Array<string | {
|
|
110
128
|
sql: string;
|
|
111
129
|
args?: any[] | Record<string, any>;
|
|
112
|
-
}>,
|
|
113
|
-
rowsAffected: number;
|
|
114
|
-
lastInsertRowid: number | bigint | undefined;
|
|
115
|
-
}>;
|
|
130
|
+
}>, options?: BatchMode | BatchOptions): Promise<ResultSet[]>;
|
|
116
131
|
/**
|
|
117
132
|
* Prepares the SQL and executes it as `Statement.run`, returning the run info.
|
|
118
133
|
*
|
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,EAA8C,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,EAA8C,MAAM,YAAY,CAAC;AAoDvH;;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,oDAAoD;IACpD,IAAI,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAC;IACtB,8DAA8D;IAC9D,YAAY,EAAE,MAAM,CAAC;CACtB;AAkDD;;;;;GAKG;AACH,MAAM,WAAW,mBAAmB,CAClC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,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
}
|
|
44
|
+
function normalizeBatchMode(mode) {
|
|
45
|
+
switch (String(mode).toLowerCase()) {
|
|
46
|
+
case "write":
|
|
47
|
+
return "IMMEDIATE";
|
|
48
|
+
case "read":
|
|
49
|
+
case "deferred":
|
|
50
|
+
return "DEFERRED";
|
|
51
|
+
case "immediate":
|
|
52
|
+
return "IMMEDIATE";
|
|
53
|
+
case "exclusive":
|
|
54
|
+
return "EXCLUSIVE";
|
|
55
|
+
case "concurrent":
|
|
56
|
+
return "CONCURRENT";
|
|
57
|
+
default:
|
|
58
|
+
return String(mode).toUpperCase();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function normalizeBatchOptions(options) {
|
|
62
|
+
if (options != null && typeof options === "object") {
|
|
63
|
+
return {
|
|
64
|
+
mode: options.mode,
|
|
65
|
+
raw: options.raw === true,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
mode: options,
|
|
70
|
+
raw: false,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Builds a libSQL-style `ResultSet` for a single statement in a `batch()`.
|
|
75
|
+
*/
|
|
76
|
+
function makeResultSet(columns, columnTypes, rows, rowsAffected) {
|
|
77
|
+
return {
|
|
78
|
+
columns,
|
|
79
|
+
columnTypes,
|
|
80
|
+
rows,
|
|
81
|
+
rowsAffected,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
44
84
|
/**
|
|
45
85
|
* Database represents a connection that can prepare and execute SQL statements.
|
|
46
86
|
*/
|
|
@@ -52,6 +92,11 @@ class Database {
|
|
|
52
92
|
inTransaction;
|
|
53
93
|
db;
|
|
54
94
|
ioStep;
|
|
95
|
+
// Serializes native calls on this connection. Subclasses that also drive
|
|
96
|
+
// the same underlying core database from async worker tasks (the sync
|
|
97
|
+
// engine on wasm) must hold this lock around those windows too: on
|
|
98
|
+
// browser wasm the main thread cannot block on a contended core lock, so
|
|
99
|
+
// main-thread native calls must never overlap an in-flight worker task.
|
|
55
100
|
execLock;
|
|
56
101
|
connected = false;
|
|
57
102
|
constructor(db, ioStep) {
|
|
@@ -163,8 +208,9 @@ class Database {
|
|
|
163
208
|
* @param mode - When set, wraps the batch in `BEGIN <mode>` / `COMMIT`
|
|
164
209
|
* (with `ROLLBACK` on failure). Ignored when already inside a
|
|
165
210
|
* transaction.
|
|
166
|
-
* @returns An
|
|
167
|
-
*
|
|
211
|
+
* @returns An array of `ResultSet`s — one per input statement, in order —
|
|
212
|
+
* matching the libsql-js batch contract. Each `ResultSet` carries that
|
|
213
|
+
* statement's `columns`, `columnTypes`, `rows`, and `rowsAffected`.
|
|
168
214
|
*
|
|
169
215
|
* @example
|
|
170
216
|
* // Plain SQL strings (non-atomic).
|
|
@@ -195,7 +241,7 @@ class Database {
|
|
|
195
241
|
* });
|
|
196
242
|
* await txn.immediate();
|
|
197
243
|
*/
|
|
198
|
-
async batch(statements,
|
|
244
|
+
async batch(statements, options) {
|
|
199
245
|
if (!Array.isArray(statements)) {
|
|
200
246
|
throw new TypeError("Expected first argument to be an array of statements");
|
|
201
247
|
}
|
|
@@ -228,12 +274,12 @@ class Database {
|
|
|
228
274
|
exec.reset();
|
|
229
275
|
}
|
|
230
276
|
};
|
|
231
|
-
const
|
|
277
|
+
const { mode, raw } = normalizeBatchOptions(options);
|
|
278
|
+
const wrap = mode != null && !this.db.inTransaction();
|
|
232
279
|
if (wrap) {
|
|
233
|
-
await runRawSql(`BEGIN ${mode
|
|
280
|
+
await runRawSql(`BEGIN ${normalizeBatchMode(mode)}`);
|
|
234
281
|
}
|
|
235
|
-
|
|
236
|
-
let lastInsertRowid;
|
|
282
|
+
const results = [];
|
|
237
283
|
try {
|
|
238
284
|
for (const statement of statements) {
|
|
239
285
|
const sql = typeof statement === "string" ? statement : statement.sql;
|
|
@@ -249,8 +295,14 @@ class Database {
|
|
|
249
295
|
if (args !== undefined) {
|
|
250
296
|
bindParams(nativeStmt, [args]);
|
|
251
297
|
}
|
|
298
|
+
const cols = nativeStmt.columns();
|
|
299
|
+
const columnNames = cols.map((c) => c.name);
|
|
300
|
+
const columnTypes = cols.map((c) => c.type ?? "");
|
|
301
|
+
if (columnNames.length > 0) {
|
|
302
|
+
nativeStmt.raw(raw);
|
|
303
|
+
}
|
|
252
304
|
const totalChangesBefore = this.db.totalChanges();
|
|
253
|
-
const
|
|
305
|
+
const rows = [];
|
|
254
306
|
try {
|
|
255
307
|
while (true) {
|
|
256
308
|
const stepResult = await nativeStmt.stepSync();
|
|
@@ -261,21 +313,12 @@ class Database {
|
|
|
261
313
|
if (stepResult === STEP_DONE) {
|
|
262
314
|
break;
|
|
263
315
|
}
|
|
264
|
-
|
|
265
|
-
// surface rows.
|
|
266
|
-
}
|
|
267
|
-
if (this.db.totalChanges() !== totalChangesBefore) {
|
|
268
|
-
rowsAffected += this.db.changes();
|
|
269
|
-
}
|
|
270
|
-
// SQLite keeps last_insert_rowid() sticky across
|
|
271
|
-
// non-INSERT statements, so just checking the value is
|
|
272
|
-
// not enough — UPDATE/DELETE would inherit a stale
|
|
273
|
-
// rowid. Use the delta as the per-statement INSERT
|
|
274
|
-
// signal.
|
|
275
|
-
const lastInsertRowidAfter = this.db.lastInsertRowid();
|
|
276
|
-
if (lastInsertRowidAfter !== lastInsertRowidBefore) {
|
|
277
|
-
lastInsertRowid = lastInsertRowidAfter;
|
|
316
|
+
rows.push(nativeStmt.row());
|
|
278
317
|
}
|
|
318
|
+
const rowsAffected = columnNames.length > 0
|
|
319
|
+
? 0
|
|
320
|
+
: this.db.totalChanges() !== totalChangesBefore ? this.db.changes() : 0;
|
|
321
|
+
results.push(makeResultSet(columnNames, columnTypes, rows, rowsAffected));
|
|
279
322
|
}
|
|
280
323
|
finally {
|
|
281
324
|
nativeStmt.reset();
|
|
@@ -298,7 +341,7 @@ class Database {
|
|
|
298
341
|
}
|
|
299
342
|
throw err;
|
|
300
343
|
}
|
|
301
|
-
return
|
|
344
|
+
return results;
|
|
302
345
|
}
|
|
303
346
|
finally {
|
|
304
347
|
this.execLock.release();
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type ExperimentalFeature = 'views' | 'strict' | 'encryption' | 'index_method' | 'autovacuum' | 'vacuum' | 'triggers' | 'attach' | 'multiprocess_wal' | 'without_rowid';
|
|
1
|
+
export type ExperimentalFeature = 'views' | 'strict' | 'encryption' | 'index_method' | 'custom_types' | 'autovacuum' | 'vacuum' | 'triggers' | 'attach' | 'generated_columns' | 'multiprocess_wal' | 'without_rowid';
|
|
2
2
|
/** Supported encryption ciphers for local database encryption. */
|
|
3
3
|
export type EncryptionCipher = 'aes128gcm' | 'aes256gcm' | 'aegis256' | 'aegis256x2' | 'aegis128l' | 'aegis128x2' | 'aegis128x4';
|
|
4
4
|
/** Encryption configuration for local encryption. */
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,mBAAmB,GAAG,OAAO,GAAG,QAAQ,GAAG,YAAY,GAAG,cAAc,GAAG,YAAY,GAAG,QAAQ,GAAG,UAAU,GAAG,QAAQ,GAAG,kBAAkB,GAAG,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,mBAAmB,GAAG,OAAO,GAAG,QAAQ,GAAG,YAAY,GAAG,cAAc,GAAG,cAAc,GAAG,YAAY,GAAG,QAAQ,GAAG,UAAU,GAAG,QAAQ,GAAG,mBAAmB,GAAG,kBAAkB,GAAG,eAAe,CAAC;AAErN,kEAAkE;AAClE,MAAM,MAAM,gBAAgB,GAAG,WAAW,GAAG,WAAW,GAAG,UAAU,GAAG,YAAY,GAAG,WAAW,GAAG,YAAY,GAAG,YAAY,CAAA;AAEhI,qDAAqD;AACrD,MAAM,WAAW,cAAc;IAC3B,MAAM,EAAE,gBAAgB,CAA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