Skip to content

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

Merged
Jarred-Sumner merged 10 commits into
mainfrom
jarred/prefix
Jun 17, 2024
Merged

Jarred-Sumner merged 10 commits into
mainfrom
jarred/prefix

Conversation

@Jarred-Sumner

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

Copy link
Copy Markdown
Collaborator

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

Comment thread test/js/bun/sqlite/sqlite.test.js
@github-actions

github-actions Bot commented Jun 15, 2024 •

Copy link
Copy Markdown
Contributor

❌ @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

Comment thread src/bun.js/bindings/sqlite/JSSQLStatement.cpp Outdated
@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
Copy Markdown
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.

Comment thread src/bun.js/bindings/sqlite/JSSQLStatement.cpp
Comment thread src/bun.js/bindings/sqlite/JSSQLStatement.cpp
Comment thread src/bun.js/bindings/sqlite/JSSQLStatement.cpp
Comment thread src/bun.js/bindings/sqlite/JSSQLStatement.cpp
Comment thread packages/bun-types/sqlite.d.ts
Comment thread packages/bun-types/sqlite.d.ts Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

4 participants