Skip to main content

Database Package Guidelines

The karma/database package simplifies PostgreSQL interaction using idiomatic Go. Follow these conventions when defining your database schema.

1. Schema Design Using Go Structs

Each database table is defined as a Go struct. Karma uses struct tags to generate queries and handle data serialization.

2. Struct Field Tag Guidelines

  • Field Names: Use CamelCase (e.g., AudioId, ConversationId)
  • JSON Tags: Use snake_case to match DB columns
  • Complex Types: Use both json and db tags for slices, maps, and nested structs
  • Primary Key: Mark primary key field using karma:"primary"
  • Table Name: Include a dummy struct{} field with the karma_table tag

Environment Configuration Guidelines

The Karma suite uses environment variables for all credentials and configuration. No secrets should be hardcoded in your codebase.

1. Using the Config Package

Use the github.com/MelloB1989/karma/config package to access environment variables. This package provides utility functions to securely load values from .env.

2. Required Environment Variables

  • DATABASE_URL: default fallback connection string
  • ENVIRONMENT: one of dev, prod, or staging
  • <ENVIRONMENT>_DATABASE_URL: environment-specific connection string

3. Connection Resolution Logic

  • If ENVIRONMENT is not set → uses DATABASE_URL
  • If ENVIRONMENT is set → uses {ENVIRONMENT}_DATABASE_URL

4. Sample .env File

5. Example Usage