Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bun:sqlite] Support unprefixed bindings, safe integers / BigInt, as(Class) #11887

Merged
merged 10 commits into from
Jun 17, 2024

Conversation

Jarred-Sumner
Copy link
Collaborator

@Jarred-Sumner Jarred-Sumner commented Jun 15, 2024

What does this PR do?

Fixes #5661
Fixes #5261
Fixes #6591
Fixes #5256 (bigint)
Fixes #1536
Fixes #8284
Fixes #3284

This adds a new strict?: boolean to the Database constructor which lets you omit the $, @, or : prefix when binding values to prepared statements.

After:

import { Database } from "bun:sqlite";

const db = new Database(":memory:", {
  // bind values without prefixes
  // throw if a field is missing in the parameters
  strict: true,
});

const query = db.query(`select $message;`);

query.all({ message: "Hello world" });

Before:

import { Database } from "bun:sqlite";

const db = new Database(":memory:", {
  // bind values without prefixes
  strict: false,
});

const query = db.query(`select $message;`);

query.all({ $message: "Hello world" });

safeIntegers: true

When safeIntegers is true, bun:sqlite will return integers as bigint types:

import { Database } from "bun:sqlite";

const db = new Database(":memory:", { safeIntegers: true });
const query = db.query(
  `SELECT ${BigInt(Number.MAX_SAFE_INTEGER) + 102n} as max_int`,
);
const result = query.get();
console.log(result.max_int); // => 9007199254741093n

When safeIntegers is true, bun:sqlite will throw an error if a bigint value in a bound parameter exceeds 64 bits:

import { Database } from "bun:sqlite";

const db = new Database(":memory:", { safeIntegers: true });
db.run("CREATE TABLE test (id INTEGER PRIMARY KEY, value INTEGER)");

const query = db.query("INSERT INTO test (value) VALUES ($value)");

try {
  query.run({ $value: BigInt(Number.MAX_SAFE_INTEGER) ** 2n });
} catch (e) {
  console.log(e.message); // => BigInt value '81129638414606663681390495662081' is out of range
}

.as(Class) - attach methods & getters/setters to results

Use .as(Class) to run a query and get back the results as instances of a class.

class Movie {
  title: string;
  year: number;

  get isMarvel() {
    return this.title.includes("Marvel");
  }
}

const query = db.query("SELECT title, year FROM movies").as(Movie);
const movies = query.all();
const first = query.get();
console.log(movies[0].isMarvel); // => true
console.log(first.isMarvel); // => true

As a performance optimization, the class constructor is not called, default initializers are not run, and private fields are not accessible. This is more like using Object.create than new. The class's prototype is assigned to the object, methods are attached, and getters/setters are set up, but the constructor is not called.

The database columns are set as properties on the class instance.

How did you verify your code works?

There are tests

Copy link
Contributor

github-actions bot commented Jun 15, 2024

@Jarred-Sumner, your commit has failing tests :(

💪 1 failing tests Darwin AARCH64

  • test/js/bun/sqlite/sql-raw.test.js 2 failing

💻 1 failing tests Darwin x64 baseline

  • test/js/bun/sqlite/sql-raw.test.js 2 failing

💻 1 failing tests Darwin x64

  • test/js/bun/sqlite/sql-raw.test.js 2 failing

🐧💪 1 failing tests Linux AARCH64

  • test/js/bun/sqlite/sql-raw.test.js 2 failing

🐧🖥 1 failing tests Linux x64 baseline

  • test/js/bun/sqlite/sql-raw.test.js 2 failing

🐧🖥 1 failing tests Linux x64

  • test/js/bun/sqlite/sql-raw.test.js 2 failing

🪟💻 1 failing tests Windows x64 baseline

  • test/js/bun/sqlite/sql-raw.test.js 2 failing

🪟💻 1 failing tests Windows x64

  • test/js/bun/sqlite/sql-raw.test.js 2 failing

View logs

@Jarred-Sumner Jarred-Sumner changed the title [bun:sqlite] Support binding values without $, @, : prefix in values [bun:sqlite] Support unprefixed bindings, safe integers / BigInt, as(Class) Jun 16, 2024
@Jarred-Sumner
Copy link
Collaborator Author

The failing sql-raw test should probably be deleted. It is now difficult to use the bindings directly that way, and we shouldn't really expose it anyway.

@Jarred-Sumner Jarred-Sumner merged commit 7719207 into main Jun 17, 2024
1 check passed
@Jarred-Sumner Jarred-Sumner deleted the jarred/prefix branch June 17, 2024 06:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment