Build REST API with Go Fiber and PlanetScale - Part 2


PlanetScale Go API Backend


Create a User model and create a file called user.go inside models directory, and define a struct with gorm.

package models
import ""
// User struct
type User struct {
Name string `json:"name"`
Email string `json:"email"`
Website string `json:"website"`

Connect to Database

Create a development database branch called add-users-table.

$ pscale branch create fiber-pscale add-users-table

Open a new terminal tab, we're going to connect to the database inside add-users-table branch and listen to 3309 PORT. See more Connect using client certificates.

$ pscale connect fiber-pscale add-users-table --port 3309

Create a file called database.go inside the models directory and add a function to connect to the database.

package models
import (
var DB *gorm.DB
func ConnectDatabase() {
// refer for details
dsn := "root:@tcp("
database, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect database")
// Migrate the users table
DB = database

open your main.go, and call the ConnectDatabase function to migrate the tables and connect to the database.

import (
+ ""
func main() {
// ...
+ models.ConnectDatabase()

then run the app go run cmd/main.go, Gorm will automatically migrate the table into add-users-table branch. How to know if the migration is success? You can check in the PlanetScale dashboard for the following branch, or use CLI to see the schema for add-users-table branch.

$ pscale branch schema fiber-pscale add-users-table
-- users --
CREATE TABLE `users` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`created_at` datetime(3) DEFAULT NULL,
`updated_at` datetime(3) DEFAULT NULL,
`deleted_at` datetime(3) DEFAULT NULL,
`name` longtext,
`email` longtext,
`website` longtext,
KEY `idx_users_deleted_at` (`deleted_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

The schema won't apply to the main branch until you create a deploy request. Lastly, stop the connection from add-users-table branch or Ctrl+C.

Gem: VersaFleet API

Ruby bindings for the VersaFleet API