Categorygithub.com/phogolabs/orm
modulepackage
0.0.0-20230111045308-694567495a48
Repository: https://github.com/phogolabs/orm.git
Documentation: pkg.go.dev

# README

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

# Packages

No description provided by the author
No description provided by the author

# Functions

Connect creates a new gateway connecto to the provided URL.
IsConstraintViolation returns a boolean indicating whether the error is a constraint failure.
IsNotFound returns a boolean indicating whether the error is a not found error.
IsNotLoaded returns a boolean indicating whether the error is a not loaded error.
IsNotSingular returns a boolean indicating whether the error is a not singular error.
MaskNotFound masks nor found error.
Open creates a new gateway connected to the provided source.
WithConnMaxLifetime sets the maximum amount of time a connection may be reused.
WithLogger sets the logger.
WithMaxIdleConns sets the maximum number of connections in the idle connection pool.
WithMaxOpenConns sets the maximum number of open connections to the database.
WithRoutine creates the gateway with a given routine.

# Variables

NewDelete creates a Mutation that deletes the entity with given primary key.
NewInsert creates a Mutation that will save the entity src into the db.
NewUpdate creates a Mutation that updates the entity into the db.
SQL represents an SQL command.
Routine represents an SQL routine.

# Structs

ConstraintError returns when trying to create/update one or more entities and one or more of their constraints failed.
Gateway is connected to a database and can executes SQL queries against it.
GatewayTx represents a gateway in transaction.
NotFoundError returns when trying to fetch a specific entity and it was not found in the database.
NotLoadedError returns when trying to get a node that was not loaded by the query.
NotSingularError returns when trying to fetch a singular entity and more then one was found in the database.

# Interfaces

Option represents a Gateway option.
Querier executes the commands.

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author
OptionFunc represents a function that can be used to set option.
No description provided by the author
RunTxFunc is a transaction function.