Database

Provides an API for accessing relational databases

Technology

The database module provides foundational database access and management capabilities for Project Forge applications. It serves as the core abstraction layer for relational database operations with support for multiple database engines.

Overview

This module provides engine-agnostic database functionality and requires a database engine module (such as postgres, mysql, sqlite, or sqlserver) to function. It offers:

Key Features

Engine Flexibility

Performance

Developer Experience

Observability

Package Structure

Core Infrastructure

Libraries

Query Framework

Database Operations

Basic Queries

1
2
3
4
5
6
7
8
// Select multiple rows
users, err := db.Select(ctx, "select * from users where active = ?", true)

// Get single row
user, err := db.Get(ctx, "select * from users where id = ?", userID)

// Execute statement
result, err := db.Exec(ctx, "update users set active = ? where id = ?", false, userID)

SQL Helpers

The database module provides SQL query building helpers for common operations:

Select Queries

Insert Operations

Update Operations

Upsert Operations

Delete Operations

Utility Functions

All functions handle database-specific placeholder syntax and SQL dialect differences automatically.

Transaction Management

1
2
3
4
5
6
// Automatic transaction handling
err := db.WithTransaction(ctx, func(tx *sql.Tx) error {
// Multiple operations in transaction
_, err := tx.Exec("INSERT INTO ...")
return err
})

Query Templates

SQL files in queries/ directory are compiled with quicktemplate:

1
2
3
4
5
-- queries/user.sql
SELECT {%s= columns %} FROM users
WHERE active = {%v active %}
{% if search %}AND name LIKE {%v search %}{% endif %}
ORDER BY created_at DESC

Configuration

The database module supports configuration through environment variables and database engine modules:

Connection Settings

Performance Tuning

Observability

Required Engine Module

The database module requires one of the following engine-specific modules:

Each engine module provides: - Driver-specific connection handling - Database schema migration support - Engine-optimized query patterns - Platform-specific configuration

Dependencies

The database module integrates with:

Source Code

See Also