initial commit, adding SQL connection and a simple testing on main
This commit is contained in:
34
app/main.go
Normal file
34
app/main.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
database "cloud.etaviaporte.com/api/libs/database"
|
||||
rbac "cloud.etaviaporte.com/api/libs/database/schemas/rbac"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var connection = database.Init()
|
||||
|
||||
// Prepare a variable to hold the fetched user record.
|
||||
// Replace rbac.User with the actual struct name if different.
|
||||
var user rbac.User
|
||||
|
||||
// Fetch the user whose primary key is 1.
|
||||
// GORM's First method, when given a numeric id as the second argument, queries by primary key.
|
||||
result := connection.First(&user, 1)
|
||||
|
||||
// result.Error holds any error from the DB operation (including "record not found").
|
||||
// result.RowsAffected tells how many rows were returned.
|
||||
if result.Error != nil {
|
||||
// Handle the error (could be gorm.ErrRecordNotFound or a DB/connection error).
|
||||
// For learning purposes we print the error and exit main.
|
||||
fmt.Printf("failed to fetch user with id=1: %v\n", result.Error)
|
||||
return
|
||||
}
|
||||
|
||||
// If no error, user now contains the row from the database.
|
||||
// Print the struct with field names and values to inspect the loaded data.
|
||||
fmt.Printf("User fetched: %+v\n", user)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user