Skip to content

phogolabs/orm

Folders and files

NameName
Last commit message
Last commit date
Nov 17, 2022
Nov 30, 2022
Mar 24, 2021
Mar 6, 2021
Dec 31, 2021
Apr 20, 2018
Mar 29, 2018
Mar 24, 2021
May 17, 2022
Apr 1, 2021
Dec 31, 2021
Mar 6, 2021
Nov 30, 2022
May 17, 2022
Mar 22, 2021
May 17, 2022
May 17, 2022
Jan 11, 2023
Jan 11, 2023
Dec 31, 2021

Repository files navigation

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