Documentation
ΒΆ
Index ΒΆ
- Constants
- func New(cfg DBConfig, opts ...database.Option) (*database.Database, error)
- func NewArraySource(rows []map[string]any) *arraysource.Model
- func NewArraySourceFrom[T any](items []T) *arraysource.Model
- func NewArraySourceWithSchema(rows []map[string]any, schema map[string]string) *arraysource.Model
- func NewCsvFileSource(filePath string) *arraysource.Model
- func NewCsvFileSourceWithDelimiter(filePath string, delimiter rune) *arraysource.Model
- func NewCsvSource(csvString string, tableName string) *arraysource.Model
- func NewFromDSN(dsn string, opts ...database.Option) (*database.Database, error)
- func NewFromSQLDB(sqlDB *sql.DB, opts ...database.Option) (*database.Database, error)
- func NewJsonFileSource(filePath string) *arraysource.Model
- func NewJsonSource(jsonString string, tableName string, isJSONL bool) *arraysource.Model
- func NewXmlFileSource(filePath string) *arraysource.Model
- func NewXmlSource(xmlString string, tableName string) *arraysource.Model
- type ArraySourceModel
- type ConnectionConfig
- type DBConfig
- func (c *DBConfig) Add(name string, configuration any)
- func (c *DBConfig) Env(envName string, defaultValue ...any) any
- func (c *DBConfig) Get(path string, defaultValue ...any) any
- func (c *DBConfig) GetBool(path string, defaultValue ...any) bool
- func (c *DBConfig) GetInt(path string, defaultValue ...any) int
- func (c *DBConfig) GetString(path string, defaultValue ...any) string
- type Database
- type EventBus
- type EventHandler
- type MigrationConfig
- type PoolConfig
- type ReplicaConfig
Constants ΒΆ
const ( SortAsc = orm.SortAsc SortDesc = orm.SortDesc )
Sort directions accepted by orm.Query.OrderBy. These are aliases for orm.SortAsc and orm.SortDesc β the canonical source of truth lives in contracts/database/orm.
const ( NullDate = "0002-01-01" NullDateTime = "0002-01-01 00:00:00" MaxDate = "9999-12-31" MaxDateTime = "9999-12-31 23:59:59" )
Sentinel date/time values for use as column defaults, sentinel values, and soft-delete strategies.
NullDate / NullDateTime represent the earliest valid date in the Gregorian calendar (1 AD β there is no year 0). Use these as NOT NULL sentinels for "no value" instead of NULL.
MaxDate / MaxDateTime represent the latest representable date/time. Use these as NOT NULL sentinels for "not deleted" in max-date soft-delete strategies.
const ( Yes = "yes" No = "no" )
Common string constants for yes/no values.
const ( EventCreating = "model.creating" EventCreated = "model.created" EventUpdating = "model.updating" EventUpdated = "model.updated" EventSaving = "model.saving" EventSaved = "model.saved" EventDeleting = "model.deleting" EventDeleted = "model.deleted" EventRestoring = "model.restoring" EventRestored = "model.restored" )
Event names for model lifecycle events.
Variables ΒΆ
This section is empty.
Functions ΒΆ
func New ΒΆ added in v0.2.0
New creates a new Database instance from a DBConfig. It converts the neat.DBConfig to the internal database.db.DBConfig and initializes the database.
func NewArraySource ΒΆ added in v0.35.0
func NewArraySource(rows []map[string]any) *arraysource.Model
func NewArraySourceFrom ΒΆ added in v0.35.0
func NewArraySourceFrom[T any](items []T) *arraysource.Model
NewArraySourceFrom creates an array-backed data source from a slice of structs or map[string]any. This is the primary entry point for array-backed queries β the third constructor in the NewArraySource family.
func NewArraySourceWithSchema ΒΆ added in v0.35.0
func NewCsvFileSource ΒΆ added in v0.36.0
func NewCsvFileSource(filePath string) *arraysource.Model
NewCsvFileSource reads a CSV file and returns an array-backed data source. The first row must be a header defining column names. Column types are inferred from the data (int, float, bool, time, string). The table name is derived from the filename (e.g., "data/users.csv" β "users").
database.Query().
Model(neat.NewCsvFileSource("data/users.csv")).
Where("active = ?", true).
Get(&users)
Panics if the file cannot be opened or is empty.
func NewCsvFileSourceWithDelimiter ΒΆ added in v0.36.0
func NewCsvFileSourceWithDelimiter(filePath string, delimiter rune) *arraysource.Model
NewCsvFileSourceWithDelimiter is like NewCsvFileSource but allows specifying a custom field delimiter (e.g., '\t' for TSV files).
func NewCsvSource ΒΆ added in v0.36.0
func NewCsvSource(csvString string, tableName string) *arraysource.Model
NewCsvSource parses a CSV string and returns an array-backed data source. The first line must be a header defining column names. Column types are inferred from the data (int, float, bool, time, string). The table name must be provided explicitly since there is no filename to derive it from.
database.Query().
Model(neat.NewCsvSource(csvString, "users")).
Where("active = ?", true).
Get(&users)
Panics if the CSV string is empty or has no header row.
func NewFromDSN ΒΆ added in v0.2.0
NewFromDSN creates a new Database instance from a DSN string. It parses the DSN and initializes the database connection.
func NewFromSQLDB ΒΆ added in v0.9.0
NewFromSQLDB creates a new Database instance from an already-open *sql.DB. The driver is auto-detected via reflection. Use database.WithDriver to override when auto-detection is not reliable. Neat does not close sqlDB or modify its connection-pool settings.
func NewJsonFileSource ΒΆ added in v0.36.0
func NewJsonFileSource(filePath string) *arraysource.Model
NewJsonFileSource reads a JSON or JSONL file and returns an array-backed data source. The file must contain a JSON array of objects (for .json) or one JSON object per line (for .jsonl/.ndjson). JSON native types are preserved. RFC3339 strings are converted to time.Time. Nested objects/arrays are stored as JSON strings. The table name is derived from the filename (e.g., "data/users.json" β "users").
database.Query().
Model(neat.NewJsonFileSource("data/users.json")).
Where("active = ?", true).
Get(&users)
Panics if the file cannot be opened or parsed.
func NewJsonSource ΒΆ added in v0.36.0
func NewJsonSource(jsonString string, tableName string, isJSONL bool) *arraysource.Model
NewJsonSource parses a JSON or JSONL string and returns an array-backed data source. Pass isJSONL=true for JSONL content (one object per line), false for a JSON array. JSON native types are preserved. RFC3339 strings are converted to time.Time. Nested objects/arrays are stored as JSON strings. The table name must be provided explicitly.
database.Query().
Model(neat.NewJsonSource(jsonString, "users", false)).
Where("active = ?", true).
Get(&users)
Panics if the content cannot be parsed.
func NewXmlFileSource ΒΆ added in v0.36.0
func NewXmlFileSource(filePath string) *arraysource.Model
NewXmlFileSource reads an XML file and returns an array-backed data source. The XML must have a root element containing repeated child elements. Each child becomes a row. Attributes and leaf sub-elements become columns. The table name is derived from the filename (e.g., "data/users.xml" β "users").
database.Query().
Model(neat.NewXmlFileSource("data/users.xml")).
Where("active = ?", true).
Get(&users)
Panics if the file cannot be opened, parsed, or has no child elements.
func NewXmlSource ΒΆ added in v0.36.0
func NewXmlSource(xmlString string, tableName string) *arraysource.Model
NewXmlSource parses an XML string and returns an array-backed data source. The XML must have a root element containing repeated child elements. Each child becomes a row. Attributes and leaf sub-elements become columns. Nested sub-elements are stored as JSON strings. Column types are inferred (int, float, bool, time, string). The table name must be provided explicitly.
database.Query().
Model(neat.NewXmlSource(xmlString, "users")).
Where("active = ?", true).
Get(&users)
Panics if the XML cannot be parsed or has no child elements.
Types ΒΆ
type ArraySourceModel ΒΆ added in v0.35.0
type ArraySourceModel = arraysource.Model
type ConnectionConfig ΒΆ
type ConnectionConfig struct {
Driver string // "postgres", "mysql", "sqlite", "sqlserver", "turso"
Dsn string
Host string
Port int
Database string
Username string
Password string
Charset string
Schema string // postgres only
SSLMode string // postgres only
Loc string // mysql only
Timezone string // postgres only
Prefix string
Singular bool
NoLowerCase bool
NameReplacer any
Read []ReplicaConfig
Write []ReplicaConfig
}
ConnectionConfig holds configuration for a single database connection.
func (ConnectionConfig) String ΒΆ added in v0.7.0
func (c ConnectionConfig) String() string
String returns a string representation of ConnectionConfig with password masked.
type DBConfig ΒΆ
type DBConfig struct {
// Default connection name
Default string
// Connection configurations
Connections map[string]ConnectionConfig
// Migration configuration
Migrations MigrationConfig
// Pool configuration
Pool PoolConfig
// Debug mode
Debug bool
// Slow query threshold in milliseconds
SlowThreshold int
}
DBConfig holds the database configuration for the standalone module.
func (*DBConfig) Add ΒΆ
Add implements config.Config interface for DBConfig (stub). It adds a new configuration entry.
func (*DBConfig) Env ΒΆ
Env implements config.Config interface for DBConfig (stub). It retrieves an environment variable value.
func (*DBConfig) Get ΒΆ
Get implements config.Config interface for DBConfig. It retrieves any value from the configuration based on the given path.
func (*DBConfig) GetBool ΒΆ
GetBool implements config.Config interface for DBConfig. It retrieves a boolean value from the configuration based on the given path.
type EventBus ΒΆ added in v0.2.0
type EventBus struct {
// contains filtered or unexported fields
}
EventBus is a lightweight internal event bus for model lifecycle events.
func NewEventBus ΒΆ added in v0.2.0
func NewEventBus() *EventBus
NewEventBus creates a new EventBus. It initializes an empty event bus with no registered listeners.
func (*EventBus) Dispatch ΒΆ added in v0.2.0
Dispatch dispatches an event to all registered listeners. It calls each handler synchronously in the order they were registered.
func (*EventBus) Forget ΒΆ added in v0.2.0
Forget removes all listeners for the given event name. This clears all handlers registered for the specified event.
func (*EventBus) Listen ΒΆ added in v0.2.0
func (e *EventBus) Listen(eventName string, handler EventHandler)
Listen registers a handler for the given event name. The handler will be called whenever the event is dispatched.
type EventHandler ΒΆ added in v0.2.0
type EventHandler func(event any)
EventHandler is a function that handles an event.
type MigrationConfig ΒΆ
type MigrationConfig struct {
Driver string // "sql" or "orm"
Table string // default: "migrations"
}
MigrationConfig holds migration configuration.
Directories
ΒΆ
| Path | Synopsis |
|---|---|
|
examples
|
|
|
advanced-queries
command
|
|
|
array-driver
command
|
|
|
array-driver-advanced
command
|
|
|
array-driver-dotted-columns
command
|
|
|
basic-orm
command
|
|
|
configuration
command
|
|
|
csv-source
command
|
|
|
csvdb-driver
command
|
|
|
factory
command
|
|
|
json-queries
command
|
|
|
json-source
command
|
|
|
jsondb-driver
command
|
|
|
migrator-migrations
command
|
|
|
migrator-transaction-failure
command
|
|
|
migrator-transactions
command
|
|
|
models
command
|
|
|
observers
command
|
|
|
schema-builder
command
|
|
|
seeders
command
|
|
|
soft-delete-alt-deleted-at
command
|
|
|
soft-delete-max-date
command
|
|
|
soft-deletes
command
|
|
|
sugar-methods
command
|
|
|
xml-source
command
|
|
|
xmldb-driver
command
|
|
|
integration_tests
|
|
|
support
|
|