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 NewCsvFSSource(sys fs.FS, filePath string) *arraysource.Model
- func NewCsvFSSourceWithDelimiter(sys fs.FS, filePath string, delimiter rune) *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 NewJsonFSSource(sys fs.FS, filePath string) *arraysource.Model
- func NewJsonFileSource(filePath string) *arraysource.Model
- func NewJsonSource(jsonString string, tableName string, isJSONL bool) *arraysource.Model
- func NewXmlFSSource(sys fs.FS, filePath string) *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 NewCsvFSSource ¶ added in v0.40.0
func NewCsvFSSource(sys fs.FS, filePath string) *arraysource.Model
NewCsvFSSource reads a CSV file from an embedded filesystem (embed.FS / fs.FS) and returns an array-backed data source ready for querying.
func NewCsvFSSourceWithDelimiter ¶ added in v0.40.0
NewCsvFSSourceWithDelimiter reads a CSV file from an embedded filesystem (embed.FS / fs.FS) with a custom field delimiter and returns an array-backed data source.
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 NewJsonFSSource ¶ added in v0.40.0
func NewJsonFSSource(sys fs.FS, filePath string) *arraysource.Model
NewJsonFSSource reads a JSON or JSONL file from an embedded filesystem (embed.FS / fs.FS) and returns an array-backed data source.
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 NewXmlFSSource ¶ added in v0.40.0
func NewXmlFSSource(sys fs.FS, filePath string) *arraysource.Model
NewXmlFSSource reads an XML file from an embedded filesystem (embed.FS / fs.FS) and returns an array-backed data source.
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 contractsdb.Driver // "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
Tables any // GODB: godb.Tables or []godb.Table; ignored by other drivers
FS fs.FS // CSVDB/JSONDB/XMLDB: embedded filesystem (embed.FS / fs.FS); ignored by other drivers
}
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 Database ¶ added in v0.2.0
Database is an alias for the database.Database type.
func NewMemoryDB ¶ added in v0.38.0
NewMemoryDB creates an in-memory database with zero configuration. It is the simplest way to query slices of structs, maps, CSV, JSON, or XML data using the full query builder (Where, OrderBy, First, Get, JOINs, etc.).
Multiple sources can be loaded into the same database — each becomes a table, enabling JOINs across them.
database, err := neat.NewMemoryDB()
if err != nil { ... }
defer database.Close()
// Load multiple sources — each becomes a table in the same SQLite DB
database.Query().
Model(neat.NewArraySourceFrom(statuses)).
Where("name = ?", "Active").
First(&result)
database.Query().
Model(neat.NewCsvSource(csv, "users")).
Get(&users)
// JOIN across sources — both tables exist in the same in-memory DB
database.Query().
Table("statuses").
LeftJoin("users ON statuses.user_id = users.id").
Get(&joined)
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
|
|
|
godb-driver
command
|
|
|
json-queries
command
|
|
|
json-source
command
|
|
|
jsondb-driver
command
|
|
|
memory-db
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
|
|
|
support
|
|