oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.17k stars 2.68k forks source link

bun:sqlite Database .as(Class) parses boolean as number (does not honor type) #13737

Open 8549 opened 1 week ago

8549 commented 1 week ago

What version of Bun is running?

1.1.21+70ca2b76c

What platform is your computer?

Linux 5.15.153.1-microsoft-standard-WSL2 x86_64 x86_64

What steps can reproduce the bug?

import { Database } from "bun:sqlite";

export class Test {
  valid: boolean;
}

const db = new Database(":memory:", { strict: true });
db.query("CREATE TABLE IF NOT EXISTS test (valid BOOLEAN)").run();

const obj = {
  valid: true,
};

db.query("INSERT INTO test VALUES ($valid)").run(obj);

const result = db.query("SELECT * FROM test").as(Test).get();

console.log(typeof result?.valid);

What is the expected behavior?

Output: boolean

What do you see instead?

Output: number

Additional information

I know that SQLite does not have a native boolean type. But the issue is that the .as(Class) method does not adhere to the class types.

lkwr commented 1 week ago

These kind of types are only exists in TypeScript. Without any extra work JavaScript can't figure out the type declaration of a class property because it does not exists in JavaScript.

You can solve it with a validator library or convert it yourself.