Yes, it is possible to embed SQLite databases in Bun.js desktop apps. Here's how you can do it:
Embedding SQLite Databases
To embed a SQLite database in a Bun.js desktop app, follow these steps:
1. Import the SQLite database in your Bun.js code using the `import` statement and specify the `type` as `"sqlite"` and `embed` as `"true"`:
javascript
import myEmbeddedDb from "./my.db" with { type: "sqlite", embed: "true" };
2. Use the embedded database in your code, for example to query data:
javascript
console.log(myEmbeddedDb.query("select * from users LIMIT 1").get());
3. Build your Bun.js app into a single executable using `bun build --compile`. This will embed the SQLite database into the executable.
bash
bun build --compile ./app.ts
./app
The embedded database is read-write, but all changes are lost when the executable exits since it's stored in memory[3].
Advantages of Embedding SQLite
Embedding the SQLite database has several benefits:
- Simplifies deployment: You can distribute a single executable that includes both the application code and the database.
- Avoids external dependencies: The app is self-contained and doesn't require a separate database file or server.
- Improves portability: The embedded database works without any external setup on the target machine.
So in summary, Bun.js makes it easy to embed SQLite databases directly into desktop applications, allowing you to create self-contained, portable programs with minimal deployment complexity.
Citations:[1] https://bun.sh/blog/bun-v1.0.23
[2] https://bun.sh/blog/bun-v1.1
[3] https://bun.sh/docs/bundler/executables
[4] https://www.youtube.com/watch?v=xE5SP2VYMMo
[5] https://bun.sh/docs/api/sqlite
[6] https://dev.to/patarapolw/a-stable-alternative-to-sqlite-for-offline-desktop-app-5gbg
[7] https://stackoverflow.com/questions/37963488/javascript-desktop-app-with-database-no-server
[8] https://github.com/tursodatabase/libsql-js