aboutsummaryrefslogtreecommitdiff
path: root/docs/guides/ecosystem
diff options
context:
space:
mode:
Diffstat (limited to 'docs/guides/ecosystem')
-rw-r--r--docs/guides/ecosystem/discordjs.md77
-rw-r--r--docs/guides/ecosystem/index.json4
-rw-r--r--docs/guides/ecosystem/mongoose.md80
-rw-r--r--docs/guides/ecosystem/prisma.md110
-rw-r--r--docs/guides/ecosystem/vite.md71
5 files changed, 342 insertions, 0 deletions
diff --git a/docs/guides/ecosystem/discordjs.md b/docs/guides/ecosystem/discordjs.md
new file mode 100644
index 000000000..7d9541861
--- /dev/null
+++ b/docs/guides/ecosystem/discordjs.md
@@ -0,0 +1,77 @@
+---
+name: Create a Discord bot
+---
+
+Discord.js works [out of the box](https://bun.sh/blog/bun-v0.6.7) with Bun. Let's write a simple bot. First create a directory and initialize it with `bun init`.
+
+```bash
+mkdir my-bot
+cd my-bot
+bun init
+```
+
+---
+
+Now install Discord.js.
+
+```bash
+bun add discord.js
+```
+
+---
+
+Before we go further, we need to go to the [Discord developer portal](https://discord.com/developers/applications), login/signup, create a new _Application_, then create a new _Bot_ within that application. Follow the [official guide](https://discordjs.guide/preparations/setting-up-a-bot-application.html#creating-your-bot) for step-by-step instructions.
+
+---
+
+Once complete, you'll be presented with your bot's _private key_. Let's add this to a file called `.env.local`. Bun automatically reads this file and loads it into `process.env`.
+
+{% callout %}
+This is an example token that has already been invalidated.
+{% /callout %}
+
+```txt#.env.local
+DISCORD_TOKEN=NzkyNzE1NDU0MTk2MDg4ODQy.X-hvzA.Ovy4MCQywSkoMRRclStW4xAYK7I
+```
+
+---
+
+Be sure to add `.env.local` to your `.gitignore`! It is dangerous to check your bot's private key into version control.
+
+```txt#.gitignore
+node_modules
+.env.local
+```
+
+---
+
+Now let's actually write our bot in a new file called `bot.ts`.
+
+```ts#bot.ts
+// import discord.js
+import {Client, Events, GatewayIntentBits} from 'discord.js';
+
+// create a new Client instance
+const client = new Client({intents: [GatewayIntentBits.Guilds]});
+
+// listen for the client to be ready
+client.once(Events.ClientReady, (c) => {
+ console.log(`Ready! Logged in as ${c.user.tag}`);
+});
+
+// login with the token from .env.local
+client.login(process.env.DISCORD_TOKEN);
+```
+
+---
+
+Now we can run our bot with `bun run`. It may take a several seconds for the client to initialize the first time you run the file.
+
+```bash
+$ bun run bot.ts
+Ready! Logged in as my-bot#1234
+```
+
+---
+
+You're up and running with a bare-bones Discord.js bot! This is a basic guide to setting up your bot with Bun; we recommend the [official Discord docs](https://discordjs.guide/) for complete information on the `discord.js` API.
diff --git a/docs/guides/ecosystem/index.json b/docs/guides/ecosystem/index.json
new file mode 100644
index 000000000..4099acec3
--- /dev/null
+++ b/docs/guides/ecosystem/index.json
@@ -0,0 +1,4 @@
+{
+ "name": "Ecosystem",
+ "description": "A collection of guides for using various tools and frameworks with Bun"
+}
diff --git a/docs/guides/ecosystem/mongoose.md b/docs/guides/ecosystem/mongoose.md
new file mode 100644
index 000000000..c2916ce82
--- /dev/null
+++ b/docs/guides/ecosystem/mongoose.md
@@ -0,0 +1,80 @@
+---
+name: Use MongoDB and Mongoose
+---
+
+MongoDB and Mongoose work out of the box with Bun. This guide assumes you've already installed MongoDB and are running it as background process/service on your development machine. Follow [this guide](https://www.mongodb.com/docs/manual/installation/) for details.
+
+---
+
+Once MongoDB is running, create a directory and initialize it with `bun init`.
+
+```bash
+mkdir mongoose-app
+cd mongoose-app
+bun init
+```
+
+---
+
+Then add Mongoose as a dependency.
+
+```bash
+bun add mongoose
+```
+
+---
+
+In `schema.ts` we'll declare and export a simple `Animal` model.
+
+```ts#schema.ts
+import * as mongoose from 'mongoose';
+
+const animalSchema = new mongoose.Schema(
+ {
+ name: {type: String, required: true},
+ sound: {type: String, required: true},
+ }
+);
+
+export type Animal = mongoose.InferSchemaType<typeof animalSchema>;
+export const Animal = mongoose.model('Kitten', animalSchema);
+```
+
+---
+
+Now from `index.ts` we can import `Animal`, connect to MongoDB, and add some data to our database.
+
+```ts#index.ts
+import * as mongoose from 'mongoose';
+import {Animal} from './schema';
+
+// connect to database
+await mongoose.connect('mongodb://127.0.0.1:27017/mongoose-app');
+
+// create new Animal
+const cow = new Animal({
+ name: 'Cow',
+ sound: 'Moo',
+});
+await cow.save(); // saves to the database
+
+// read all Animals
+const animals = await Animal.find();
+animals[0].speak(); // logs "Moo!"
+
+// disconect
+await mongoose.disconnect();
+```
+
+---
+
+Lets run this with `bun run`.
+
+```bash
+$ bun run index.ts
+Moo!
+```
+
+---
+
+This is a simple introduction to using Mongoose with TypeScript and Bun. As you build your application, refer to the official [MongoDB](https://docs.mongodb.com/) and [Mongoose](https://mongoosejs.com/docs/) sites for complete documentation.
diff --git a/docs/guides/ecosystem/prisma.md b/docs/guides/ecosystem/prisma.md
new file mode 100644
index 000000000..39627b806
--- /dev/null
+++ b/docs/guides/ecosystem/prisma.md
@@ -0,0 +1,110 @@
+---
+name: Use Prisma
+---
+
+Prisma works our of the box with Bun. First, create a directory and initialize it with `bun init`.
+
+```bash
+mkdir prisma-app
+cd prisma-app
+bun init
+```
+
+---
+
+Then add Prisma as a dependency.
+
+```bash
+bun add prisma
+```
+
+---
+
+We'll use the Prisma CLI with `bunx` to initialize our schema and migration directory. For simplicity we'll be using an in-memory SQLite database.
+
+```bash
+bunx prisma init --datasource-provider sqlite
+```
+
+---
+
+Open `prisma/schema.prisma` and add a simple `User` model.
+
+```prisma-diff#prisma/schema.prisma
+ generator client {
+ provider = "prisma-client-js"
+ }
+
+ datasource db {
+ provider = "sqlite"
+ url = env("DATABASE_URL")
+ }
+
++ model User {
++ id Int @id @default(autoincrement())
++ email String @unique
++ name String?
++ }
+```
+
+---
+
+Then generate and run initial migration.
+
+This will generate a `.sql` migration file in `prisma/migrations`, create a new SQLite instance, and execute the migration against the new instance.
+
+```bash
+bunx prisma migrate dev --name init
+```
+
+---
+
+Prisma automatically generates our _Prisma client_ whenever we execute a new migration. The client provides a fully typed API for reading and writing from our database.
+
+It can be imported from `@prisma/client`.
+
+```ts#src/index.ts
+import {PrismaClient} from "@prisma/client";
+```
+
+---
+
+Let's write a simple script to create a new user, then count the number of users in the database.
+
+```ts#index.ts
+import { PrismaClient } from "@prisma/client";
+
+const prisma = new PrismaClient();
+
+// create a new user
+await prisma.user.create({
+ data: {
+ name: "John Dough",
+ email: `john-${Math.random()}@example.com`,
+ },
+});
+
+// count the number of users
+const count = await prisma.user.count();
+console.log(`There are ${count} users in the database.`);
+```
+
+---
+
+Let's run this script with `bun run`. Each time we run it, a new user is created.
+
+```bash
+$ bun run index.ts
+Created john-0.12802932895402364@example.com
+There are 1 users in the database.
+$ bun run index.ts
+Created john-0.8671308799782803@example.com
+There are 2 users in the database.
+$ bun run index.ts
+Created john-0.4465968383115295@example.com
+There are 3 users in the database.
+```
+
+---
+
+That's it! Now that you've set up Prisma using Bun, we recommend referring to the [official Prisma docs](https://www.prisma.io/docs/concepts/components/prisma-client) as you continue to develop your application.
diff --git a/docs/guides/ecosystem/vite.md b/docs/guides/ecosystem/vite.md
new file mode 100644
index 000000000..cee5e6013
--- /dev/null
+++ b/docs/guides/ecosystem/vite.md
@@ -0,0 +1,71 @@
+---
+name: Use Vite
+---
+
+{% callout %}
+While Vite currently works with Bun, it has not been heavily optimized, nor has Vite been adapted to use Bun's bundler, module resolver, or transpiler.
+{% /callout %}
+
+---
+
+Vite works out of the box with Bun (v0.7 and later). Get started with one of Vite's templates.
+
+```bash
+$ bunx create-vite my-app
+✔ Select a framework: › React
+✔ Select a variant: › TypeScript + SWC
+Scaffolding project in /path/to/my-app...
+```
+
+---
+
+Then `cd` into the project directory and install dependencies.
+
+```bash
+cd my-app
+bun install
+```
+
+---
+
+Start the development server with the `vite` CLI using `bunx`.
+
+The `--bun` flag tells Bun to run Vite's CLI using `bun` instead of `node`; by default Bun respects Vite's `#!/usr/bin/env node` [shebang line](<https://en.wikipedia.org/wiki/Shebang_(Unix)>). After Bun 1.0 this flag will no longer be necessary.
+
+```bash
+bunx --bun vite
+```
+
+---
+
+To simplify this command, update the `"dev"` script in `package.json` to the following.
+
+```json-diff#package.json
+ "scripts": {
+- "dev": "vite",
++ "dev": "bunx --bun vite",
+ "build": "vite build",
+ "serve": "vite preview"
+ },
+ // ...
+```
+
+---
+
+Now you can start the development server with `bun run dev`.
+
+```bash
+bun run dev
+```
+
+---
+
+The following command will build your app for production.
+
+```sh
+$ bunx --bun vite build
+```
+
+---
+
+This is a stripped down guide to get you started with Vite + Bun. For more information, see the [Vite documentation](https://vitejs.dev/guide/).