Skip to content

7thFox/gorm

Repository files navigation

gorm

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...

usage

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 (
	"fmt"

	"github.com/7thFox/gorm"
)

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()

	db.DropSchema()
	db.CreateSchema()
	db.CreateTable(Foobar{})

	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}}
}

About

Simple golang ORM for PostgreSQL

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy