Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/phogolabs/orm

Golang Query Executor and Database Connector
https://github.com/phogolabs/orm

golang migration orm sql

Last synced: about 1 month ago
JSON representation

Golang Query Executor and Database Connector

Awesome Lists containing this project

README

        

# ORM

[![Documentation][godoc-img]][godoc-url]
![License][license-img]
[![Build Status][action-img]][action-url]
[![Coverage][codecov-img]][codecov-url]
[![Go Report Card][report-img]][report-url]

The package facilitates execution of SQL scripts generated by
[prana][prana-url]. Also it provides a query builder and object relation mapper.
Note that it is in BETA. We may introduce breaking changes until we reach
version 1.0.

[![ORM][orm-img]][orm-url]

## Installation

```console
$ go get -u github.com/phogolabs/orm
```

## Getting Started

Let's first import all required packages:

```golang
import (
"github.com/phogolabs/orm"
)
```

and then establish the connection:

```golang
gateway, err := orm.Open("sqlite3", "example.db", orm.WithRoutine(routine.Statement))
if err != nil {
return err
}
```

## SQL Migrations

You can execute the migration generated by [prana][prana-url]. For that you have
to use either [embed][embed-url] package or [os][os-url] package.

```golang
if err := gateway.Migrate(resource); err != nil {
return err
}
```

## SQL Queries

The package provides a way to work with embeddable SQL scripts. It understands predefined files with [SQL Scripts](https://github.com/phogolabs/prana#sql-scripts-and-commands).

It executes them as standard SQL queries. Let's define a SQL routines named `insert-user` and `select-all-users`:

```
-- name: insert-user
INSERT INTO users (id, first_name, last_name)
VALUES (:id, :first_name, :last_name);

-- named: select-all-users
SELECT * FROM users;
```

Then you can execute the desired script by just passing its name:

```golang
routine := orm.Routine("select-all-users")
// execute the routine
_, err = gateway.All(context.TODO(), routine, &users)
```

```golang
routine := orm.Routine("insert-user", &user)
// execute the routine
_, err = gateway.Exec(context.TODO(), routine)
```

Also you can execute raw SQL Scripts from your code:

```golang
query := orm.Query("SELECT * FROM users WHERE id = ?", 5432)
// fetch the records as a slice of users
rows, err := gateway.Only(context.TODO(), query, &user)
```

## Example

You can check our [Getting Started Example](/example).

For more information, how you can change the default behavior you can read the
help documentation by executing:

## Contributing

We are open for any contributions. Just fork the
[project](https://github.com/phogolabs/orm).

*logo made by [Free Pik][logo-author-url]*

[report-img]: https://goreportcard.com/badge/github.com/phogolabs/orm
[report-url]: https://goreportcard.com/report/github.com/phogolabs/orm
[logo-author-url]: https://www.freepik.com/free-photos-vectors/tree
[logo-license]: http://creativecommons.org/licenses/by/3.0/
[orm-url]: https://github.com/phogolabs/orm
[orm-img]: media/img/logo.png
[codecov-url]: https://codecov.io/gh/phogolabs/orm
[codecov-img]: https://codecov.io/gh/phogolabs/orm/branch/master/graph/badge.svg
[action-img]: https://github.com/phogolabs/orm/workflows/main/badge.svg
[action-url]: https://github.com/phogolabs/orm/actions
[orm-url]: https://github.com/phogolabs/orm
[godoc-url]: https://godoc.org/github.com/phogolabs/orm
[godoc-img]: https://godoc.org/github.com/phogolabs/orm?status.svg
[license-img]: https://img.shields.io/badge/license-MIT-blue.svg
[software-license-url]: LICENSE
[embed-url]: https://golang.org/pkg/embed
[os-url]: https://golang.org/pkg/os
[prana-url]: https://github.com/phogolabs/prana
[sqlx-url]: https://github.com/jmoiron/sqlx