@tursodatabase/database 0.8.0-pre.7 → 0.8.0-pre.9
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.test.js +169 -0
- package/package.json +7 -7
package/dist/promise.test.js
CHANGED
|
@@ -439,3 +439,172 @@ test('transactionAsync() rejects callbacks that do not declare the handle', asyn
|
|
|
439
439
|
expect(() => db.transactionAsync(async () => { })).toThrow(/Transaction handle/);
|
|
440
440
|
expect(() => db.transactionAsync((async (...args) => { }))).toThrow(/Transaction handle/);
|
|
441
441
|
});
|
|
442
|
+
test('batch returns per-statement details and stops at the first failure', async () => {
|
|
443
|
+
const db = await connect(":memory:");
|
|
444
|
+
const results = await db.batch([
|
|
445
|
+
"CREATE TABLE t_batch (id INTEGER PRIMARY KEY, name TEXT)",
|
|
446
|
+
{ sql: "INSERT INTO t_batch (name) VALUES (?)", args: ["Alice"] },
|
|
447
|
+
{ sql: "INSERT INTO t_batch (name) VALUES (?)", args: ["Bob"] },
|
|
448
|
+
"SELECT name FROM t_batch ORDER BY id",
|
|
449
|
+
]);
|
|
450
|
+
expect(results.length).toBe(4);
|
|
451
|
+
expect(results[1].rowsAffected).toBe(1);
|
|
452
|
+
expect(results[1].lastInsertRowid).toBe(1);
|
|
453
|
+
expect(results[2].lastInsertRowid).toBe(2);
|
|
454
|
+
expect(results[3].rows).toEqual([{ name: "Alice" }, { name: "Bob" }]);
|
|
455
|
+
// The embedded engine does not report per-statement execution
|
|
456
|
+
// statistics; the serverless driver fills these from the server.
|
|
457
|
+
expect(results[1].rowsRead).toBeUndefined();
|
|
458
|
+
expect(results[1].rowsWritten).toBeUndefined();
|
|
459
|
+
expect(results[1].queryDurationMs).toBeUndefined();
|
|
460
|
+
let error = null;
|
|
461
|
+
try {
|
|
462
|
+
await db.batch([
|
|
463
|
+
{ sql: "INSERT INTO t_batch (name) VALUES (?)", args: ["Carol"] },
|
|
464
|
+
"INSERT INTO no_such_table VALUES (1)",
|
|
465
|
+
{ sql: "INSERT INTO t_batch (name) VALUES (?)", args: ["Dave"] },
|
|
466
|
+
]);
|
|
467
|
+
}
|
|
468
|
+
catch (e) {
|
|
469
|
+
error = e;
|
|
470
|
+
}
|
|
471
|
+
expect(error).not.toBeNull();
|
|
472
|
+
// The error identifies the failing statement and carries one entry
|
|
473
|
+
// per statement: the completed first statement's ResultSet, null for
|
|
474
|
+
// the failing statement and the skipped one after it.
|
|
475
|
+
expect(error.batchIndex).toBe(1);
|
|
476
|
+
expect(error.batchResults.length).toBe(3);
|
|
477
|
+
expect(error.batchResults[0].rowsAffected).toBe(1);
|
|
478
|
+
expect(error.batchResults[1]).toBeNull();
|
|
479
|
+
expect(error.batchResults[2]).toBeNull();
|
|
480
|
+
// Execution stopped at the failure: Carol committed, Dave never ran.
|
|
481
|
+
const stmt = await db.prepare("SELECT COUNT(*) AS n FROM t_batch");
|
|
482
|
+
expect(await stmt.all([])).toEqual([{ n: 3 }]);
|
|
483
|
+
});
|
|
484
|
+
test('atomic batch failure rolls back and reports the failing statement', async () => {
|
|
485
|
+
const db = await connect(":memory:");
|
|
486
|
+
await db.exec("CREATE TABLE t_atomic (x)");
|
|
487
|
+
let error = null;
|
|
488
|
+
try {
|
|
489
|
+
await db.batch([
|
|
490
|
+
{ sql: "INSERT INTO t_atomic VALUES (?)", args: [1] },
|
|
491
|
+
"INSERT INTO no_such_table VALUES (1)",
|
|
492
|
+
], "immediate");
|
|
493
|
+
}
|
|
494
|
+
catch (e) {
|
|
495
|
+
error = e;
|
|
496
|
+
}
|
|
497
|
+
expect(error).not.toBeNull();
|
|
498
|
+
expect(error.batchIndex).toBe(1);
|
|
499
|
+
expect(error.batchResults.length).toBe(2);
|
|
500
|
+
const stmt = await db.prepare("SELECT COUNT(*) AS n FROM t_atomic");
|
|
501
|
+
expect(await stmt.all([])).toEqual([{ n: 0 }]);
|
|
502
|
+
});
|
|
503
|
+
test('batch validates every bind value before executing the first statement', async () => {
|
|
504
|
+
const db = await connect(":memory:");
|
|
505
|
+
await db.exec("CREATE TABLE t_batch_bind_validation (x)");
|
|
506
|
+
let error;
|
|
507
|
+
try {
|
|
508
|
+
await db.batch([
|
|
509
|
+
{ sql: "INSERT INTO t_batch_bind_validation VALUES (?)", args: [1] },
|
|
510
|
+
{ sql: "INSERT INTO t_batch_bind_validation VALUES (?)", args: [Number.POSITIVE_INFINITY] },
|
|
511
|
+
]);
|
|
512
|
+
}
|
|
513
|
+
catch (caught) {
|
|
514
|
+
error = caught;
|
|
515
|
+
}
|
|
516
|
+
expect(error.batchIndex).toBe(1);
|
|
517
|
+
expect(error.batchResults).toEqual([]);
|
|
518
|
+
const stmt = await db.prepare("SELECT COUNT(*) AS n FROM t_batch_bind_validation");
|
|
519
|
+
expect(await stmt.all([])).toEqual([{ n: 0 }]);
|
|
520
|
+
});
|
|
521
|
+
test('batch validates every bigint before executing the first statement', async () => {
|
|
522
|
+
const db = await connect(":memory:");
|
|
523
|
+
await db.exec("CREATE TABLE t_batch_bigint_validation (x)");
|
|
524
|
+
let error;
|
|
525
|
+
try {
|
|
526
|
+
await db.batch([
|
|
527
|
+
{ sql: "INSERT INTO t_batch_bigint_validation VALUES (?)", args: [1] },
|
|
528
|
+
{ sql: "INSERT INTO t_batch_bigint_validation VALUES (?)", args: [1n << 63n] },
|
|
529
|
+
]);
|
|
530
|
+
}
|
|
531
|
+
catch (caught) {
|
|
532
|
+
error = caught;
|
|
533
|
+
}
|
|
534
|
+
expect(error.batchIndex).toBe(1);
|
|
535
|
+
expect(error.batchResults).toEqual([]);
|
|
536
|
+
const stmt = await db.prepare("SELECT COUNT(*) AS n FROM t_batch_bigint_validation");
|
|
537
|
+
expect(await stmt.all([])).toEqual([{ n: 0 }]);
|
|
538
|
+
});
|
|
539
|
+
test('batch coerces bind values once during validation', async () => {
|
|
540
|
+
const db = await connect(":memory:");
|
|
541
|
+
await db.exec("CREATE TABLE t_batch_coercion (x TEXT)");
|
|
542
|
+
let coercions = 0;
|
|
543
|
+
const value = {
|
|
544
|
+
toString() {
|
|
545
|
+
coercions++;
|
|
546
|
+
if (coercions > 1)
|
|
547
|
+
throw new Error("value was coerced twice");
|
|
548
|
+
return "coerced";
|
|
549
|
+
},
|
|
550
|
+
};
|
|
551
|
+
await db.batch([
|
|
552
|
+
{ sql: "INSERT INTO t_batch_coercion VALUES (?)", args: [value] },
|
|
553
|
+
]);
|
|
554
|
+
expect(coercions).toBe(1);
|
|
555
|
+
const stmt = await db.prepare("SELECT x FROM t_batch_coercion");
|
|
556
|
+
expect(await stmt.all([])).toEqual([{ x: "coerced" }]);
|
|
557
|
+
});
|
|
558
|
+
test('atomic batch rejects transaction control before BEGIN', async () => {
|
|
559
|
+
const db = await connect(":memory:");
|
|
560
|
+
await db.exec("CREATE TABLE t_batch_control (x)");
|
|
561
|
+
const controls = [
|
|
562
|
+
"BEGIN",
|
|
563
|
+
"COMMIT",
|
|
564
|
+
"END",
|
|
565
|
+
"ROLLBACK",
|
|
566
|
+
"SAVEPOINT nested",
|
|
567
|
+
"RELEASE nested",
|
|
568
|
+
"\ufeffCOMMIT",
|
|
569
|
+
];
|
|
570
|
+
for (const control of controls) {
|
|
571
|
+
let error;
|
|
572
|
+
try {
|
|
573
|
+
await db.batch([
|
|
574
|
+
"INSERT INTO t_batch_control VALUES (1)",
|
|
575
|
+
` ; \n-- leading line comment\n; /* leading block comment */ ${control}`,
|
|
576
|
+
], "immediate");
|
|
577
|
+
}
|
|
578
|
+
catch (caught) {
|
|
579
|
+
error = caught;
|
|
580
|
+
}
|
|
581
|
+
expect(error.batchIndex).toBe(1);
|
|
582
|
+
expect(error.batchResults).toEqual([]);
|
|
583
|
+
}
|
|
584
|
+
const stmt = await db.prepare("SELECT COUNT(*) AS n FROM t_batch_control");
|
|
585
|
+
expect(await stmt.all([])).toEqual([{ n: 0 }]);
|
|
586
|
+
});
|
|
587
|
+
test('atomic batch attaches a rollback failure to the primary error', async () => {
|
|
588
|
+
const db = await connect(":memory:");
|
|
589
|
+
const native = db.db;
|
|
590
|
+
const originalExecutor = native.executor.bind(native);
|
|
591
|
+
native.executor = (sql, ...args) => {
|
|
592
|
+
if (sql === "ROLLBACK")
|
|
593
|
+
throw new Error("rollback failed");
|
|
594
|
+
return originalExecutor(sql, ...args);
|
|
595
|
+
};
|
|
596
|
+
let error;
|
|
597
|
+
try {
|
|
598
|
+
await db.batch(["INSERT INTO no_such_batch_table VALUES (1)"], "immediate");
|
|
599
|
+
}
|
|
600
|
+
catch (caught) {
|
|
601
|
+
error = caught;
|
|
602
|
+
}
|
|
603
|
+
finally {
|
|
604
|
+
native.executor = originalExecutor;
|
|
605
|
+
}
|
|
606
|
+
expect(error).toBeInstanceOf(Error);
|
|
607
|
+
expect(error.message).toMatch(/no_such_batch_table/);
|
|
608
|
+
expect(error.rollbackError).toBeInstanceOf(Error);
|
|
609
|
+
expect(error.rollbackError.message).toBe("rollback failed");
|
|
610
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tursodatabase/database",
|
|
3
|
-
"version": "0.8.0-pre.
|
|
3
|
+
"version": "0.8.0-pre.9",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/tursodatabase/turso"
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"drizzle-kit": "^0.31.4",
|
|
27
27
|
"drizzle-orm": "^0.45.2",
|
|
28
28
|
"typescript": "^5.9.2",
|
|
29
|
-
"vitest": "^3.2.
|
|
29
|
+
"vitest": "^3.2.7"
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
32
|
"napi-build": "napi build --platform --profile release-official --esm --manifest-path ../../Cargo.toml --output-dir .",
|
|
@@ -49,15 +49,15 @@
|
|
|
49
49
|
]
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@tursodatabase/database-common": "^0.8.0-pre.
|
|
52
|
+
"@tursodatabase/database-common": "^0.8.0-pre.9"
|
|
53
53
|
},
|
|
54
54
|
"imports": {
|
|
55
55
|
"#index": "./index.js"
|
|
56
56
|
},
|
|
57
57
|
"optionalDependencies": {
|
|
58
|
-
"@tursodatabase/database-linux-x64-gnu": "0.8.0-pre.
|
|
59
|
-
"@tursodatabase/database-win32-x64-msvc": "0.8.0-pre.
|
|
60
|
-
"@tursodatabase/database-darwin-arm64": "0.8.0-pre.
|
|
61
|
-
"@tursodatabase/database-linux-arm64-gnu": "0.8.0-pre.
|
|
58
|
+
"@tursodatabase/database-linux-x64-gnu": "0.8.0-pre.9",
|
|
59
|
+
"@tursodatabase/database-win32-x64-msvc": "0.8.0-pre.9",
|
|
60
|
+
"@tursodatabase/database-darwin-arm64": "0.8.0-pre.9",
|
|
61
|
+
"@tursodatabase/database-linux-arm64-gnu": "0.8.0-pre.9"
|
|
62
62
|
}
|
|
63
63
|
}
|