Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does callbacks run for raw db call #7221

Open
SOG-web opened this issue Oct 3, 2024 · 0 comments
Open

Does callbacks run for raw db call #7221

SOG-web opened this issue Oct 3, 2024 · 0 comments
Assignees
Labels
type:question general questions

Comments

@SOG-web
Copy link

SOG-web commented Oct 3, 2024

Your Question

Am trying to register a create callback hook for my db but it seems not to be working

package main

import (
	"log"

	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
	"serverkit.com/cmd/api"
)

func main() {

	db, err := gorm.Open(sqlite.Open("./test.db"), &gorm.Config{})

	if err != nil {
		log.Fatal(err)
	}

	addr := ":8080"
	apiUrl := "api/v1"
	logTimeFormat := "02-Jan-2006"
	logTimeZone := "America/New_York"

	server := api.NewAPIServer(addr, db, apiUrl, logTimeFormat, logTimeZone)
	if err := server.Run(); err != nil {
		log.Fatalf("Error starting server: %v", err)
	}
}
package api

import (
	"log"
	"os"
	"path/filepath"

	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/logger"
	"gorm.io/gorm"
	"serverkit.com/services/database/sqlhandler"
)

type APIServer struct {
	addr          string
	sqlDb         *gorm.DB
	apiUrl        string
	logTimeFormat string
	logTimeZone   string
}

func NewAPIServer(addr string, db *gorm.DB, apiUrl string, logTimeFormat string, logTimeZone string) *APIServer {
	return &APIServer{
		addr:          addr,
		sqlDb:         db,
		apiUrl:        apiUrl,
		logTimeFormat: logTimeFormat,
		logTimeZone:   logTimeZone,
	}
}

func registerCallbacks(db *gorm.DB) {
	// Generic callback for create operation
	db.Callback().Create().After("gorm:create").Register("global_after_create", afterCreateCallback)
	// Similarly, you can add callbacks for update and delete
}

// Generic after-create callback
func afterCreateCallback(tx *gorm.DB) {
	tableName := tx.Statement.Table
	// Fetch the created record details, you can access `tx.Statement.Dest` to get the data
	createdData := tx.Statement.Dest

	log.Printf("Table '%s' was updated with data: %v", tableName, createdData)

	// Broadcast table name and created data to SSE
	// sseManager.BroadcastToClients(fmt.Sprintf("Table '%s' was updated with data: %v", tableName, createdData))
}

func (s *APIServer) Run() error {
	app := fiber.New()

	// Register global callbacks
	registerCallbacks(s.sqlDb)

	// Ensure the logs directory exists
	logDir := "./logs"
	if err := os.MkdirAll(logDir, os.ModePerm); err != nil {
		log.Fatalf("error creating log directory: %v", err)
	}

	// Custom File Writer
	logFilePath := filepath.Join(logDir, "fiber.log")
	file, err := os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
	if err != nil {
		log.Fatalf("error opening file: %v", err)
	}
	defer file.Close()

	// Create a subrouter for /api/v1
	api := app.Group(s.apiUrl)

	// Middleware
	api.Use(logger.New(logger.Config{
		Output:     file,
		Format:     "[${ip}]:${port} ${status} - ${method} ${path}\n",
		TimeFormat: s.logTimeFormat,
		TimeZone:   s.logTimeZone,
	}))

	// Register routes
	databaseRepo := sqlhandler.NewSqlRepo(s.sqlDb)
	databaseHandler := sqlhandler.NewDBAPIHandler(databaseRepo)
	databaseHandler.RegisterRoutes(api)

	log.Println("Starting server on", s.addr)
	return app.Listen(s.addr)
}
package sqlhandler

import (
	"time"

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

func (h *SqlAPIHandler) handlePerformSqlQuery(c *fiber.Ctx) error {

	var req PerformSqlQueryRequest
	if err := c.BodyParser(&req); err != nil {
		return c.Status(fiber.StatusBadRequest).SendString(err.Error())
	}

	// Validate if the query is allowed
	if !isValidQuery(req.Query) {
		return c.Status(fiber.StatusForbidden).SendString("Query type not allowed")
	}

	// Log the query for tracking
	logQuery(req.Query)

	// Measure the execution time
	startTime := time.Now()

	// Perform the SQL query and capture result
	result, err := h.repo.PerformSqlQuery(req.Query)
	if err != nil {
		return c.Status(fiber.StatusInternalServerError).SendString("Failed to execute query: " + err.Error())
	}

	// Calculate execution duration
	executionTime := time.Since(startTime).Milliseconds()

	// Send detailed response
	return c.JSON(fiber.Map{
		"message":        "Query executed successfully",
		"query":          req.Query,
		"rows_affected":  result.RowsAffected,
		"execution_time": executionTime, // In milliseconds
	})
}

http://127.0.0.1:8080/api/v1/db/tables

{
  "query" : "INSERT INTO users (name, email, age) VALUES ('Alice', '[email protected]', 30); INSERT INTO users (name, email, age) VALUES ('Bob', '[email protected]', 25); INSERT INTO users (name, email, age) VALUES ('Charlie', '[email protected]', 35);"
}

am not receiving any logs in the hook, any idea on why it not working

am trying to do a realtime db update

The document you expected this should be explained

[]

https://gorm.io/docs/write_plugins.html

Expected answer

am not receiving any logs in the hook, any idea on why it not working

am trying to do a realtime db update

@SOG-web SOG-web added the type:question general questions label Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

2 participants