CtrlK
CommunityDocumentationLog inGet started
Tessl Logo

tessl/golang-gorm-io--gorm

tessl install tessl/golang-gorm-io--gorm@1.31.0

Full-featured ORM library for Golang with associations, hooks, transactions, migrations, and developer-friendly chainable API

Agent Success

Agent success rate when using this tile

81%

Improvement

Agent success rate improvement when using this tile compared to baseline

1.13x

Baseline

Agent success rate without this tile

72%

task.mdevals/scenario-7/

Audit Logger

Build a database audit logging system that tracks changes to user records. The system should log all create and update operations with timestamps, operation types, and the affected data.

Requirements

Audit Log Storage

  • Create an AuditLog model with fields: ID, EntityType, EntityID, Operation (create/update), ChangedData (JSON), and Timestamp
  • Store audit logs in a separate database table

Automatic Audit Trail

  • Automatically create audit log entries whenever a User record is created or updated
  • For create operations: capture operation type as "create" and the user's ID, name, and email as JSON in ChangedData
  • For update operations: capture operation type as "update" and the user's ID in EntityID
  • Ensure audit logging executes after the main operation completes successfully

Query and Retrieval

  • Implement a function to retrieve all audit logs for a specific user ID
  • Return logs ordered by timestamp (most recent first)

Test Cases

  • Creating a new user with Name="Alice" and Email="alice@example.com" generates an audit log with Operation="create", EntityType="User", and ChangedData containing the user data @test
  • Updating an existing user generates an audit log with Operation="update" and EntityID set to the user's ID @test
  • Retrieving audit logs for a user returns all logs for that user ordered by Timestamp descending @test

Implementation

@generates

API

package main

import (
	"time"
	"gorm.io/gorm"
)

// User represents a user in the system
type User struct {
	ID        uint      `gorm:"primarykey"`
	Name      string
	Email     string
	UpdatedAt time.Time
	CreatedAt time.Time
}

// AuditLog stores audit trail information
type AuditLog struct {
	ID          uint      `gorm:"primarykey"`
	EntityType  string
	EntityID    uint
	Operation   string
	ChangedData string    // JSON string
	Timestamp   time.Time
}

// SetupAuditCallbacks configures the database to automatically create audit logs
func SetupAuditCallbacks(db *gorm.DB) error

// GetAuditLogs retrieves all audit logs for a specific user
func GetAuditLogs(db *gorm.DB, userID uint) ([]AuditLog, error)

Dependencies { .dependencies }

gorm.io/gorm { .dependency }

Provides ORM functionality for database operations and callback management.

@satisfied-by

Version

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
golangpkg:golang/gorm.io/gorm@v1.31.1
tile.json