Skip to content

Latest commit

 

History

History
121 lines (91 loc) · 3.38 KB

README.md

File metadata and controls

121 lines (91 loc) · 3.38 KB

ORM

Documentation License Build Status Coverage Go Report Card

The package facilitates execution of SQL scripts generated by prana. 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

Installation

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

Getting Started

Let's first import all required packages:

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

and then establish the connection:

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. For that you have to use either embed package or os package.

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.

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:

routine := orm.Routine("select-all-users")
// execute the routine
_, err = gateway.All(context.TODO(), routine, &users)
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:

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.

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.

logo made by Free Pik