{"id":15101787,"url":"https://github.com/Fylga/Workshop_BunJS","last_synced_at":"2025-09-27T00:30:46.794Z","repository":{"id":216448010,"uuid":"722100884","full_name":"JorisFRANCIN/Workshop_BunJS","owner":"JorisFRANCIN","description":"Bun is an all-in-one toolkit for JavaScript and TypeScript apps. It ships as a single executable called bun​. The workshop will cover most of it functionnalities in order to create an API/Front Web.","archived":false,"fork":false,"pushed_at":"2024-01-10T08:35:23.000Z","size":67,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-09-25T18:40:12.490Z","etag":null,"topics":["bunjs","sqlite"],"latest_commit_sha":null,"homepage":"https://bun.sh/","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JorisFRANCIN.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-11-22T12:43:52.000Z","updated_at":"2023-11-22T12:44:59.000Z","dependencies_parsed_at":"2024-01-10T09:41:11.128Z","dependency_job_id":null,"html_url":"https://github.com/JorisFRANCIN/Workshop_BunJS","commit_stats":null,"previous_names":["jorisfrancin/workshop_bunjs"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JorisFRANCIN%2FWorkshop_BunJS","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JorisFRANCIN%2FWorkshop_BunJS/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JorisFRANCIN%2FWorkshop_BunJS/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JorisFRANCIN%2FWorkshop_BunJS/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JorisFRANCIN","download_url":"https://codeload.github.com/JorisFRANCIN/Workshop_BunJS/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219871828,"owners_count":16554457,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bunjs","sqlite"],"created_at":"2024-09-25T18:40:36.333Z","updated_at":"2025-09-27T00:30:41.530Z","avatar_url":"https://github.com/JorisFRANCIN.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Workshop REST API BunJS\n\nBun is an all-in-one toolkit for JavaScript and TypeScript apps. It ships as a single executable called bun​. The workshop will cover most of it functionnalities in order to create an API/Front Web.\n#\n\n✔️ Discover the basic features of bunjs\n\n✔️ Learn the basics of a REST API\n\n✔️ Read and write files in JS\n\n\u003e 💡 A quick [documentation](https://www.ibm.com/topics/rest-apis) about REST API\n\n## Step 0: Install \u0026 setup bunjs\n\nAll the required information to install the workshop's dependencies are given in [SETUP.md](./SETUP.md)\n\n## Step 1: Hello World\n\nA starter for Bun is available. Start your project with \"bun create\" command.\n**From the recent versions of bun you'll be asked to choose a template, youll choose the bun template of course**.\n\n```shell\n$ bun create hono my-app\n```\n\nMove into my-app and install the dependencies.\n\n```shell\n$ cd my-app\n$ bun install\n$ bun run dev\n```\nYou'll be running your app in port 3000 by default but you can change it like that:\n\n```ts\n# index.ts\n\nimport { Hono } from 'hono'\n\nconst app = new Hono()\n\napp.get('/', (c) =\u003e c.JSON({ message: 'Hello Hono!'}))\n\nexport default {\n    port: 8080,         # HERE\n    fetch: app.fetch,\n}\n```\n\n\u003e Try it on the browser http://localhost:8080 or on Postman\n\n💡 Bun reads your .env files automatically and provides idiomatic ways to read and write your environment variables programmatically. So no dependencies required ! You also don't need to recompile it when things change !\n\n```ts\nimport { Hono } from 'hono'\n\nconst app = new Hono()\n\nconst port = parseInt(process.env.port || 8080)\n\napp.get('/', (c) =\u003e c.json({ message: 'Hello Hono!'}))\n\nexport default {\n    port: port,         # HERE\n    fetch: app.fetch,\n```\n\n## Step 2: HTTP methods\n\nNice job ! You created an endpoint, what you've done for now is a simple GET request that give \"Hello Hono!\" in json format. Let's create route to our API with a POST request on ```/todo/create```.\n\nThe todo model will only contain the following fields **name** \u0026 **description**.\n\n```json\n{\n    name: \"tata\",\n    description: \"toto\"\n}\n```\n\n\u003e Let's try to code properly shall we ?\n\n💡  Here's the ideal structure you expect to have in an API:\n\n```\nproject-name/\n|-- src/\n|   |-- controllers/\n|   |   |-- userController.ts\n|   |   |-- postController.ts\n|   |   |-- ...\n|\n|   |-- models/\n|   |   |-- userModel.ts\n|   |   |-- postModel.ts\n|   |   |-- ...\n|\n|   |-- routes/\n|   |   |-- userRoutes.ts\n|   |   |-- postRoutes.ts\n|   |   |-- index.ts\n|\n|   |-- middleware/\n|   |   |-- authentication.ts\n|   |   |-- validation.ts\n|   |   |-- ...\n|\n|   |-- services/\n|   |   |-- userService.ts\n|   |   |-- postService.ts\n|   |   |-- ...\n|\n|   |-- utils/\n|   |   |-- errorHandlers.ts\n|   |   |-- logger.ts\n|   |   |-- ...\n|\n|   |-- index.ts\n|\n|-- config/\n|   |-- env.ts\n|   |-- database.ts\n|   |-- ...\n|\n|-- tests/\n|   |-- userTests.ts\n|   |-- postTests.ts\n|   |-- ...\n|\n|-- .gitignore\n|-- package.json\n|-- README.md\n\n```\n\n1.  **src/**: This directory contains the source code of your application.\n\n    **controllers/**: Contains the logic for handling different routes. Each file typically corresponds to a resource.\n\n    **models/**: Defines the data structures of your application. Each file corresponds to a model.\n\n    **routes/**: Defines the API routes and links them to the appropriate controllers. The index.js file is used to aggregate and export all route files.\n\n    **middleware/**: Contains reusable middleware functions, such as authentication and validation.\n\n    **services/**: Business logic that can be reused across controllers. Handles interactions with the models and may include additional business logic.\n\n    **utils/**: Utility functions that can be used across the application, such as error handling and logging.\n\n    **index.ts**: Entry point of your application where you set up Express and configure middleware.\n\n2.  **config/**: Configuration files for your application, such as        environment-specific settings and database configurations.\n\n    - **env.ts**: Environment-specific configurations, such as database   connection strings, API keys, etc.\n\n    - **database.js**: Database configuration file where you set up the connection to your database.\n\n3.  **tests/**: Contains your test files, organized by resource or feature. For example, userTests.js and postTests.js would include tests for user-related and post-related functionalities.\n\nThis is how you get values from a POST Request:\n\n```ts\nconst { name, description } = await c.req.json()\n```\n\n![](assets/swappy-20240109-150215.png)\n\n\n## Step 3: Authorization middleware\n\nLet's create an Auth middleware that will check if the user is authorized to access the endpoint ```/todo/*```.\n\n\u003e We won't use real token like a JWT here, but we'll rather just check if the request contains a Bearer header with a any name to it.\n\n💡  It is fairly easy with Bun... Try to check bearerAuth :)\n\n## Step 4: Database Interaction\n\nBun natively implements a high-performance SQLite3 driver. To use it import from the built-in **bun:sqlite** module.\n\nExample:\n\n```ts\nimport { Database } from \"bun:sqlite\";\n\nconst db = new Database(\":memory:\");\nconst query = db.query(\"select 'Hello world' as message;\");\nquery.get(); // =\u003e { message: \"Hello world\" }\n```\n\nTry to add the Todo (that contains the name \u0026 description) to the Database in the ```/todo/create``` request\n\nOnce it is done, try to create ```/todo``` GET request that displays all the todos in the Database. Here's the code below to verify it:\n\n```ts\napp.get('/todo', (c) =\u003e {\n    const stmt = db.query(\"SELECT * FROM name-of-your-db\")\n    return c.json({ users: stmt.all() })\n})\n```\n\nYou're expected to get this:\n\n![](assets/swappy-20240109-155454.png)\n\n## Bonus\n\nNice job you finished the workshop !\nYou can go further by adding other methods to complete your database:\n* a DELETE request on /todo: remove an todo from the database.\n* a PUT request on /todo to update an todo.\n* a POST request on /todos to create multiple todos at the same time","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FFylga%2FWorkshop_BunJS","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FFylga%2FWorkshop_BunJS","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FFylga%2FWorkshop_BunJS/lists"}