database

package module
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 30, 2026 License: MIT Imports: 7 Imported by: 0

README

Database Module

This module handles data persistence for the auth-middleware project. It uses PostgreSQL as the underlying database.

Prerequisites

  • PostgreSQL: A running PostgreSQL instance.
  • UUID Extension: The database must support the uuid-ossp extension.

Initialization

You must execute the init.sql script to create the necessary tables and extensions:

# Example using psql
psql -h <host> -U <username> -d <database_name> -f init.sql

Schema Details

The module relies on two tables:

  1. users: Stores user information.
  2. user_devices: Stores device information, including the secret used for JWT signing.

Important Note: Ensure your database user has sufficient privileges to execute CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; and to perform CRUD operations on these tables.

Implementation Requirements

This module is designed to be injected into other modules (like jwt). Ensure your implementation of the data access layer properly handles:

  • Connection Pooling: Use a library like pgx with a connection pool for better performance.
  • Error Handling: Properly map database errors to application-level errors, especially for missing records (e.g., when GetDeviceSecret fails).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Database

type Database struct {
	// contains filtered or unexported fields
}

func NewDatabase

func NewDatabase(dsn string, opts ...DatabaseOption) (*Database, error)

func (*Database) Close

func (d *Database) Close() error

func (*Database) CreateUser

func (d *Database) CreateUser(username, email string) (*User, error)

func (*Database) DeleteAllDevices added in v1.1.0

func (d *Database) DeleteAllDevices(userID string) error

func (*Database) DeleteAllDevicesReturningIDs added in v1.2.0

func (d *Database) DeleteAllDevicesReturningIDs(userID string) ([]string, error)

func (*Database) DeleteDevice

func (d *Database) DeleteDevice(userID, deviceID string) error

func (*Database) DeleteUser

func (d *Database) DeleteUser(userID string) error

func (*Database) GetDeviceSecret

func (d *Database) GetDeviceSecret(deviceID string) (string, error)

func (*Database) GetUserFromEmail

func (d *Database) GetUserFromEmail(email string) (*User, error)

func (*Database) GetUserFromID

func (d *Database) GetUserFromID(userID string) (*User, error)

func (*Database) SaveDeviceSecret

func (d *Database) SaveDeviceSecret(userID, deviceName, secret string) (string, error)

func (*Database) UpdateDeviceSecret

func (d *Database) UpdateDeviceSecret(deviceID, secret string) error

func (*Database) UpdateUsername

func (d *Database) UpdateUsername(userID string, newUsername string) error

type DatabaseOption

type DatabaseOption func(*databaseOptions)

func FromEnv

func FromEnv() DatabaseOption

func WithCleanupInterval

func WithCleanupInterval(d time.Duration) DatabaseOption

func WithConnMaxIdleTime

func WithConnMaxIdleTime(d time.Duration) DatabaseOption

func WithConnMaxLifetime

func WithConnMaxLifetime(d time.Duration) DatabaseOption

func WithMaxIdleConns

func WithMaxIdleConns(n int) DatabaseOption

func WithMaxOpenConns

func WithMaxOpenConns(n int) DatabaseOption

type User

type User struct {
	UserID   string `json:"user_id"`
	Username string `json:"username"`
	Email    string `json:"email"`
}

type UserDevice

type UserDevice struct {
	UserID     string `json:"user_id"`
	DeviceID   string `json:"device_id"`
	DeviceName string `json:"device_name"`
	Secret     string `json:"secret,omitempty"`
	UpdatedAt  string `json:"updated_at,omitempty"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL