Skip to content

Conversation

@fabrv
Copy link
Contributor

@fabrv fabrv commented Sep 22, 2025

Bindings for Bun's SQL API

Unfortunately, it has some helper functions with raw JS to make it work properly, but DX does feel pretty good.

The query creator and helper functions are separated into these 3 functions instead of using overloads:

  1. query
  2. object
  3. values

Basic example:

let mysql = SQL.make("mysql://user:pass@localhost:3306/mydb")
let mysqlResults = await mysql->SQL.query`
  SELECT * FROM users 
  WHERE active = ${Boolean(true)}
`

With helper functions:

let userData = {
  "name": "Alice",
  "email": "alice@example.com",
}

let newUser = await db->SQL.query`
  INSERT INTO users ${Query(db->SQL.object(userData, ["name", "email"]))}
  RETURNING *
`

@fabrv
Copy link
Contributor Author

fabrv commented Sep 22, 2025

@zth Please review 🙏

Copy link
Owner

@zth zth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

| Number(float)
| Dict(Core__Dict.t<param>)
| Array(array<param>)
| Query(SQLQuery.t<param>)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can another query be a param? Interesting!

@@ -0,0 +1,159 @@
open Globals
module SQLQuery = {
type t<'a> = promise<'a>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this primarily for autocomplete?

@zth zth merged commit 30f25fb into zth:main Sep 23, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants