package module
Version: v0.0.0-...-ec7f3b6 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Feb 1, 2019 License: MIT Imports: 7 Imported by: 0



GORM is a simple golang ORM for PostgreSQL built for very simple SQL interactions. If the usage section below isn't enough for you, I would recommend reading the source. It's <500 lines and is basically just a exercise in golang's reflection.

There are a few quirks, such as the PK not being able to reside within a nested Struct. Also I don't have any feature to delete a record, but I'm sure I'll do that one day...


The following example is also available in example/main.go

Note: out of date since adding DataObject requirements, but it will still work the same after those functions are added to the structs

package main

import (


type Bar string

type Foobar struct {
	FoobarID int    `primary:"true"`
	Name     string `unique:"true" size:"100"`
	Debug    bool   `exclude:"true"`

	NestedStruct struct {
		Bar Bar
		Baz float64

func main() {
	// db and user should be set up beforehand
	db, _ := gorm.Open("myfoobarschema")
	defer db.Close()


	foo := Foobar{
		Name:  "example1",
		Debug: true,
	foo.NestedStruct.Baz = 5.6

	db.Save(&foo) // record inserted; struct's PK has been updated

	// myfoobarschema=# SELECT * FROM myfoobarschema.Foobar;
	// foobarid |   name   | bar | baz
	// ----------+----------+-----+-----
	//        1 | example1 |     | 5.6
	// (1 row)

	foo.Name = "example2"

	db.Save(&foo) // record updated

	// myfoobarschema=# SELECT * FROM myfoobarschema.Foobar;
	// foobarid |   name   | bar | baz
	// ----------+----------+-----+-----
	//        1 | example2 |     | 5.6
	// (1 row)

	foo2 := Foobar{}
	db.Populate(&foo2, 1) // selects with ID 1
	fmt.Printf("%#v\n", foo2)
	// $ main.Foobar{FoobarID:1, Name:"example2", Debug:false, NestedStruct:struct { Bar main.Bar; Baz float64 }{Bar:"", Baz:5.599999904632568}}




This section is empty.


This section is empty.


This section is empty.


type DataObject

type DataObject interface {
	Save(*DatabaseConnection) error
	SaveRecursive(*DatabaseConnection) error
	GetByID(*DatabaseConnection, int) error
	GetByIDRecursive(*DatabaseConnection, int) error

DataObject is defines the requirement of structs to be used with this package. Currently this interface is empty, but may be used in the future.

type DatabaseConnection

type DatabaseConnection struct {
	Schema string
	// contains filtered or unexported fields

DatabaseConnection represents an open DB connection

func Open

func Open(uds string) (*DatabaseConnection, error)

Open opens a DB connection using the same user/db/schema

func OpenDebug

func OpenDebug(uds string) (*DatabaseConnection, error)

OpenDebug opens a DB connection using the same user/db/schema and enables outputing debug messages

func OpenVerbose

func OpenVerbose(user, database, schema string, debug bool) (*DatabaseConnection, error)

OpenVerbose opens a DB allowing to specify different user/db/schema and enable debug log output

func (*DatabaseConnection) Close

func (db *DatabaseConnection) Close() error

Close closes the db connection

func (*DatabaseConnection) CreateSchema

func (db *DatabaseConnection) CreateSchema() error

CreateSchema will create the schema of the DatabaseConnection

func (*DatabaseConnection) CreateTable

func (db *DatabaseConnection) CreateTable(obj DataObject) error

CreateTable will create a new table based upon the DataObject given

func (*DatabaseConnection) DropSchema

func (db *DatabaseConnection) DropSchema() error

DropSchema will drop the schema of the DatabaseConnection

func (*DatabaseConnection) DropTable

func (db *DatabaseConnection) DropTable(obj DataObject) error

DropTable will drop the table related the given DataObject

func (*DatabaseConnection) GetRelationshipIDs

func (db *DatabaseConnection) GetRelationshipIDs(fkName string, fkValue int, relList interface{}) ([]int, error)

GetRelationshipIDs will return a list of FK values of a relationship, as well as fill the given list with empty structs of that object to the same size as the returned ID list

func (*DatabaseConnection) Populate

func (db *DatabaseConnection) Populate(obj DataObject, index int) error

Populate fills the given DataObject with the info from the DB with PK of index

func (*DatabaseConnection) Save

func (db *DatabaseConnection) Save(obj DataObject) error

Save creates or updates a DB entry for the given DataObject. Create/Update logic is based on the PK being > 0 for updates.


Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL