Documentation
¶
Overview ¶
Package gorm provides a GORM-based implementation of the UsersService. It supports any database that GORM supports (PostgreSQL, MySQL, SQLite, etc.) and is suitable for production deployments requiring relational database storage.
Database Schema ¶
The service uses auto-migration to create/update the users table:
CREATE TABLE users (
id VARCHAR PRIMARY KEY,
created_at TIMESTAMP,
updated_at TIMESTAMP,
name VARCHAR,
description TEXT,
tags JSON, -- Stored as JSON array
image_url VARCHAR,
email VARCHAR,
extras JSON -- App-specific data as JSON
);
Generated Code ¶
This package uses protoc-gen-dal-gorm generated code:
- UserGORM: GORM model with proper tags
- UserGORMToUser/UserToUserGORM: conversion functions
- UserGORMDAL: data access layer with CRUD operations
Usage ¶
db, _ := gorm.Open(postgres.Open(dsn), &gorm.Config{})
userService := gormbe.NewUsersService(db)
resp, err := userService.CreateUser(ctx, &v1.CreateUserRequest{
User: &v1.User{Name: "John Doe", Email: "john@example.com"},
})
Index ¶
- type UsersService
- func (s *UsersService) CreateUser(ctx context.Context, req *v1.CreateUserRequest) (*v1.CreateUserResponse, error)
- func (s *UsersService) DeleteFromStorage(ctx context.Context, id string) error
- func (s *UsersService) ListAllUsers(ctx context.Context) ([]*v1.User, error)
- func (s *UsersService) LoadUser(ctx context.Context, id string) (*v1.User, error)
- func (s *UsersService) SaveUser(ctx context.Context, id string, user *v1.User) error
- func (s *UsersService) UpdateUser(ctx context.Context, req *v1.UpdateUserRequest) (*v1.UpdateUserResponse, error)
- func (s *UsersService) UserExists(ctx context.Context, id string) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type UsersService ¶
type UsersService struct {
services.BaseUsersService
MaxPageSize int
UserDAL v1dal.UserGORMDAL
// contains filtered or unexported fields
}
UsersService implements services.UsersService using GORM. It embeds BaseUsersService to inherit caching and shared operations.
func NewUsersService ¶
func NewUsersService(db *gorm.DB) *UsersService
NewUsersService creates a new GORM-backed UsersService. It automatically runs database migrations to ensure the schema is up to date.
func (*UsersService) CreateUser ¶
func (s *UsersService) CreateUser(ctx context.Context, req *v1.CreateUserRequest) (*v1.CreateUserResponse, error)
CreateUser creates a new user profile.
func (*UsersService) DeleteFromStorage ¶
func (s *UsersService) DeleteFromStorage(ctx context.Context, id string) error
DeleteFromStorage implements UserStorageProvider.
func (*UsersService) ListAllUsers ¶
ListAllUsers implements UserStorageProvider.
func (*UsersService) UpdateUser ¶
func (s *UsersService) UpdateUser(ctx context.Context, req *v1.UpdateUserRequest) (*v1.UpdateUserResponse, error)
UpdateUser updates an existing user profile.
func (*UsersService) UserExists ¶
func (s *UsersService) UserExists(ctx context.Context, id string) bool
UserExists implements UserStorageProvider.