Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sphireinc/hydra
A Go library that dynamically hydrates structs with data from multiple databases, offering flexibility and ease for database integration in software development.
https://github.com/sphireinc/hydra
database hydration orm
Last synced: about 1 month ago
JSON representation
A Go library that dynamically hydrates structs with data from multiple databases, offering flexibility and ease for database integration in software development.
- Host: GitHub
- URL: https://github.com/sphireinc/hydra
- Owner: sphireinc
- License: mit
- Created: 2024-09-15T11:48:45.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2024-09-24T15:09:25.000Z (about 2 months ago)
- Last Synced: 2024-10-13T02:05:59.055Z (about 1 month ago)
- Topics: database, hydration, orm
- Language: Go
- Homepage: https://sphireinc.github.io/Hydra/
- Size: 782 KB
- Stars: 37
- Watchers: 2
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# Sphire Hydra
[![Build](https://github.com/sphireinc/Hydra/actions/workflows/build.yml/badge.svg)](https://github.com/sphireinc/Hydra/actions/workflows/build.yml)
[![Documentation](https://img.shields.io/badge/Pages-passing-green)](https://sphireinc.github.io/Hydra/)
[![License](https://img.shields.io/github/license/sphireinc/hydra)](https://github.com/sphireinc/Hydra/releases/latest)
[![Go Version](https://img.shields.io/github/go-mod/go-version/sphireinc/hydra)](https://github.com/sphireinc/Hydra/releases/latest)
[![Release Version](https://img.shields.io/github/v/release/sphireinc/hydra)](https://github.com/sphireinc/Hydra/releases/latest)
[![Release Date](https://img.shields.io/github/release-date/sphireinc/hydra)](https://github.com/sphireinc/Hydra/releases/latest)
Sphire Hydra is a Go library designed to dynamically hydrate Go structs with data from a variety of databases.
The library supports multiple databases, including MySQL, PostgreSQL, SQLite,
Microsoft SQL Server, Oracle, MariaDB, and CockroachDB. Using reflection and `hydra` tags,
it automatically fills struct fields with data fetched from database queries.> [!WARNING]
> Hydra went from idea to fruition in the span of 4 hours. It is still a very immature project, use it at your own risk. I welcome all opinions, contributions, and ideas on how to make this a better project.## Features
- **Automatic Hydration**: Automatically populates Go structs with data fetched from databases using reflection.
- **Multiple Database Support**: Supports MySQL, PostgreSQL, SQLite, Microsoft SQL Server, Oracle, MariaDB, and CockroachDB.
- **Flexible Queries**: Allows dynamic construction of SQL `WHERE` clauses.
- **Type Safety**: Ensures proper type conversions between database values and Go struct fields.
- **Easily Extendable**: Easily extendable to support more databases in the future.## Installation
To install Sphire Hydra, use `go get`:
```bash
go get github.com/sphireinc/Hydra
```## Supported Databases
- MySQL
- PostgreSQL
- SQLite
- Microsoft SQL Server
- Oracle
- MariaDB
- CockroachDB## Usage
### Struct Definition
Define your structs with hydra tags to map the struct fields to the corresponding database columns, annd
embed the hydra.Hydratable struct:```go
type Person struct {
Name string `json:"name" hydra:"name"`
Age int `json:"age" hydra:"age"`
Email string `json:"email" hydra:"email"`
hydra.Hydratable
}
```### Hydration
To hydrate a struct, initialize the struct and then call the Hydrate method, which automatically fetches the
data from the database and populates the fields:```go
package mainimport (
"database/sql"
"github.com/sphireinc/Hydra"
_ "github.com/go-sql-driver/mysql"
)type Person struct {
Name string `json:"name" hydra:"name"`
Age int `json:"age" hydra:"age"`
Email string `json:"email" hydra:"email"`
hydra.Hydratable
}func createDBConnection() *sql.DB {
db, _ := sql.Open("mysql", "user:password@/dbname")
return db
}func main() {
// Create a database connection
db := createDBConnection()// Create an addressable Person instance and initialize the hydra.Hydratable struct
p := &Person{}
p.Init(p)// Create a map of where clauses
whereClause := map[string]interface{}{"id": "U6"}// Call Hydrate to populate the struct with data from the database
p.Hydrate(db, whereClause)// Print the hydrated struct
fmt.Printf("Hydrated person: %+v\n", p)
}
```# Extensibility
Sphire Hydra is designed to be easily extensible. You can add support for additional databases by implementing a
fetch function specific to the database’s query syntax and integrating it with the existing hydration process.# Contributing
We welcome contributions! Feel free to open an issue or submit a pull request to improve the library.