# Setting database menggunahkan gorm.io di Gofiber

Berikut adalah langkah-langkah lengkap untuk menginstal GORM dan godotenv, serta menambahkan struktur folder dan file:

1. **Instal GORM dan godotenv**:

   * Pastikan Anda telah mengatur GOPATH dan PATH dengan benar.
   * Buka terminal Anda dan jalankan perintah berikut:

     ```
     go get -u gorm.io/gorm
     go get -u gorm.io/driver/postgres
     go get github.com/joho/godotenv
     ```

   Perintah ini akan mengunduh dan menginstal paket GORM, driver PostgreSQL untuk GORM, dan godotenv.
2. **Tambahkan File-file Berikut**:
   * Buat file-file berikut di dalam direktori yang sesuai dengan struktur folder:

     * **Src/models/Products.go**
     * **Src/configs/db.go**
     * **Src/helpers/migration.go**
     * **main.go**

     Berikut adalah struktur folder yang diminta beserta penjelasan singkat untuk setiap direktori dan file:

     ```
     project
     │   
     ├── src
     |   ├── configs
     |   |   └── db.go
     |   ├── controllers
     |   |   └── ProductController.go
     |   ├── helpers
     |   |   └── migration.go
     |   ├── models
     |   |   └── Products.go
     |   └── routes
     |       └── main.go
     └── main.go
     ```

     1. **src**: Direktori utama yang berisi seluruh kode aplikasi.
        * **configs**: Direktori untuk menyimpan konfigurasi terkait dengan pengaturan aplikasi, seperti koneksi database.
        * **controllers**: Direktori untuk menyimpan file-file yang berisi logika aplikasi terkait dengan pengaturan dan manipulasi data.
        * **helpers**: Direktori untuk menyimpan file-file yang berisi fungsi-fungsi bantuan atau helper yang digunakan dalam aplikasi.
        * **models**: Direktori untuk menyimpan definisi struktur data atau model yang digunakan dalam aplikasi.
        * **routes**: Direktori untuk menyimpan file-file yang berisi definisi route atau endpoint dari aplikasi.
     2. **main.go**: File utama yang berfungsi sebagai entry point atau titik awal dari aplikasi. Biasanya digunakan untuk menginisialisasi server dan setup awal lainnya.
3. **Tambahkan Kode ke File-file**:

   * Tambahkan kode yang diberikan sebelumnya ke dalam file-file yang sesuai.
   * Pastikan untuk mengimpor paket-paket yang diperlukan dengan benar di setiap file.

   #### src/models/Products.go

   ```go
   package models

   import "gorm.io/gorm"

   type Product struct {
   	gorm.Model
   	Name  string  `json:"name"`
   	Price float64 `json:"price"`
   	Stock int     `json:"stock"`
   }
   ```

   #### src/configs/db.go

   ```go
   package configs

   import (
   	"log"
   	"os"

   	"gorm.io/driver/postgres"
   	"gorm.io/gorm"
   )

   var DB *gorm.DB

   func InitDB() {
   	url := os.Getenv("URL")
   	var err error
   	DB, err = gorm.Open(postgres.Open(url), &gorm.Config{})
   	if err != nil {
   		log.Fatalf("Failed to connect to the database: %v", err)
   	}
   }
   ```

   #### src/helpers/migration.go

   ```go
   package helpers

   import (
   	"gofiber/src/configs"
   	"gofiber/src/models"
   )

   func Migration() {
   	configs.DB.AutoMigrate(&models.Product{})
   }
   ```

   #### main.go

   ```go
   package main

   import (
   	"gofiber/src/configs"
   	"gofiber/src/helpers"
   	"gofiber/src/routes"
   	"log"

   	"github.com/gofiber/fiber/v2"
   	"github.com/joho/godotenv"
   )

   func main() {
   	err := godotenv.Load()
   	if err != nil {
   		log.Fatal("Error loading .env file")
   	}
   	app := fiber.New()
   	configs.InitDB()
   	helpers.Migration()
   	routes.Router(app)

   	app.Listen(":3000")
   }
   ```

Setelah langkah-langkah di atas selesai, struktur folder Anda akan lengkap dengan file-file yang diperlukan, GORM dan godotenv akan terinstal, dan Anda siap untuk memulai pengembangan aplikasi dengan Fiber framework menggunakan GORM untuk interaksi dengan database dan godotenv untuk mengelola variabel lingkungan.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zakimaliki.gitbook.io/gofiber/basic-backend-dengan-gofiber-part-2/setting-database-menggunahkan-gorm.io-di-gofiber.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
