Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

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)


logo

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 main

import (
"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.