Limits on automatic type conversion for custom objects in Bun.SQLite prepared statements
0 reputation · 12 Mar 2023, 22:19 UTC
0 reputation · 12 Mar 2023, 22:19 UTC
Goal: Determine whether Bun.SQLite automatically serializes custom JavaScript values (such as Date objects or class instances) when binding parameters to a prepared statement, or whether developers must manually convert them to SQLite‑compatible types.
Context: The Bun.SQLite documentation highlights the performance‑oriented prepared‑statement API and its compatibility with Node.js‑style bindings, but it does not specify how complex custom types are handled during the bind phase. This leaves uncertainty about whether the runtime invokes valueOf/toJSON, stores the raw object, or requires explicit serialization.
Specific questions:
toJSON or valueOf method when the value is passed as a bind parameter?[object Object], NULL, or an error)?34775 reputation · 13 Mar 2023, 07:35 UTC
run or get methods.
Unlike high-level ORMs that perform deep serialization, the bun:sqlite module is designed for performance and maps values directly to SQLite's storage classes. This means it does not look for JavaScript-specific serialization methods during the bind phase.
toJSON() or valueOf() on objects passed as parameters. If you pass a custom object, the runtime will not resolve it to a string automatically.[object Object] or NULL.number, string, null, boolean (converted to 0 or 1), and Uint8Array/Buffer (stored as BLOB).import { SQLite } from "bun";
const db = new SQLite(":memory:");
db.run("CREATE TABLE events (id INTEGER, created_at TEXT, data TEXT)");
const insert = db.prepare("INSERT INTO events (created_at, data) VALUES (?, ?)");
// Manual conversion is required
const eventDate = new Date();
const customData = { action: "login", priority: 10 };
insert.run(eventDate.toISOString(), JSON.stringify(customData));
try {
const db = new SQLite(":memory:");
db.run("CREATE TABLE test (val TEXT)");
const stmt = db.prepare("INSERT INTO test VALUES (?)");
// This is expected to throw an error in current versions
stmt.run({ custom: "object" });
} catch (e) {
console.log("Caught expected error:", e.message);
}
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.