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

Expose TransactionSQL #18065

Open
LarsKumbier opened this issue Mar 11, 2025 · 4 comments
Open

Expose TransactionSQL #18065

LarsKumbier opened this issue Mar 11, 2025 · 4 comments
Labels
bug Something isn't working sql Something to do with `sql` in the "bun" module types An issue with TypeScript types

Comments

@LarsKumbier
Copy link

What version of Bun is running?

1.2.4+fd9a5ea66

What platform is your computer?

Linux 6.6.74-1-MANJARO x86_64 unknown

What steps can reproduce the bug?

When trying to run NextJS with Turbopack on bun, the direct import of bun-specific APIs does not work - but a namespaced import works, if the interfaces are exposed (kudos to @MichalBastrzyk). The Turbopack-Team already knows about this: vercel/next.js#75220

// broken if turbopack is involved
// import { sql } from "bun";
$ bun --bun run node_modules/.bin/next dev --turbopack
[...]
 ○ Compiling /seed ...
 ✓ Compiled /seed in 687ms
 ⨯ ./app/seed/route.ts:2:1
Module not found: Can't resolve 'bun'
  1 | import bcrypt from "bcrypt";
> 2 | import { sql, TransactionSQL } from "bun";
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[...]

Namespaced version:

// works with sql, since it's exposed in the type definition file
const sql = new Bun.SQL();

// does NOT work
const TransactionSQL = Bun.TransactionSQL;

While this should be in general solved in turbopack (AFAIK), it would be nice to have a proper workaround for bun.

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

@LarsKumbier LarsKumbier added bug Something isn't working types An issue with TypeScript types labels Mar 11, 2025
@alii
Copy link
Member

alii commented Mar 11, 2025

Just to clarify - are you saying that turbopack, as part of the build process, reads type definitions to figure out what exports are available?

@LarsKumbier
Copy link
Author

I am not involved enough to clarify that - @samcx: Could you shed some light on this?

@LarsKumbier
Copy link
Author

addendum: this comment could clarify it: vercel/next.js#75220 (comment)

@RiskyMH RiskyMH added the sql Something to do with `sql` in the "bun" module label Mar 12, 2025
@RiskyMH
Copy link
Member

RiskyMH commented Mar 12, 2025

Bun.TransactionSQL isn't a thing in bun, looks like it is an interface in the types though.


You can do transactions like this though:

await sql.begin(async tx => {
 // All queries in this function run in a transaction
 await tx`INSERT INTO users (name) VALUES (${"Alice"})`;
 await tx`UPDATE accounts SET balance = balance - 100 WHERE user_id = 1`;

 // Transaction automatically commits if no errors are thrown
 // Rolls back if any error occurs
});

https://bun.sh/docs/api/sql#transactions


Edit: oh i see this is just a types issue with namespace my bad,

But I'd recommend importing with type to make it very clear to the builder (import { type TransactionSQL } from "bun";)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working sql Something to do with `sql` in the "bun" module types An issue with TypeScript types
Projects
None yet
Development

No branches or pull requests

3 participants