Bun is an incredibly fast JavaScript runtime, bundler, transpiler and package manager similar to Node.js and Deno . It has a lot of promising features (check them out on the Roadmap) and aims to replace Node.js. It was created by Jarred Sumner in Zig and uses JavaScript core instead of v8
The cool thing about this is it has built-in TypeScript support, Yes you no longer need tsc, Moreover you can write packages in Typescript and publish it to the NPM Registry as-is, Though your package will then be limited to Bun users.
The benchmarks can be seen on the website
Let’s install Bun now,
1
curl -fsSL https://bun.sh/install | bash
Verify your install and let’s dive into creating the web application.
First of all, Create a new directory and change your path to it and then run the bun init command.
It will create a Typescript based new project, Now let’s begin writing some code. Open the directory in your IDE (mine is VSCode).
You’ll see something like this:
Now, Let’s install our dependencies:
elysia - The web framework we’re using.
@elysiajs/html - HTML Plugin for the web framework
1
bun a elysia @elysiajs/html
Now’s let’s create the backend first, We’ll create an api which can be called from the frontend to store,retrieve,edit and delete books stored in SQLite Database (BTW, Bun provides the fastest SQLite in JavaScript ecosystem)
We create a db.ts to store the Database stuff which should look like this:
constructor() { this.db = new Database('books.db'); // Initialize the database this.init() .then(() =>console.log('Database initialized')) .catch(console.error); }
// Get all books asyncgetBooks() { returnthis.db.query('SELECT * FROM books').all(); }
// Add a book asyncaddBook(book: Book) { // q: Get id type safely returnthis.db.query(`INSERT INTO books (name, author) VALUES (?, ?) RETURNING id`).get(book.name, book.author) as Book; }
// Update a book asyncupdateBook(id: number, book: Book) { returnthis.db.run(`UPDATE books SET name = '${book.name}', author = '${book.author}' WHERE id = ${id}`) }
// Delete a book asyncdeleteBook(id: number) { returnthis.db.run(`DELETE FROM books WHERE id = ${id}`) }
// Initialize the database asyncinit() { returnthis.db.run('CREATE TABLE IF NOT EXISTS books (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, author TEXT)'); } }
Here, We create a class BooksDatabase and create a Database instance in it, Add methods to create,retrieve,edit books and initialize the database (create tables).
What does each function do?
addBook - Creates a new book in the database and return the autoincremented id
getBooks - Get all the books in the database
updateBook - Update a existing book in the database
deleteBook - Delete a book from the database
Now, Let’s begin creating the API, Elysia is a minimalistic web framework and does not require any extra knowledge, It is full of awesome features, Check out the available plugins here
This is a simple overview of how the file will look, Make sure to add <!DOCTYPE html> in the starting of the file so that the html plugin can add the Content type header.
Now, moving back to our index.ts we create a route to serve this index.html . Elysia provides a static plugin as well which we won’t use since we have only 2 static files.
Create a new GET route which will read index.html file and send the text