ohmygorm

package module
v0.0.0-...-8db6072 Latest Latest
Warning

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

Go to latest
Published: May 3, 2015 License: MIT Imports: 8 Imported by: 3

README

#oh my gorm

Some setup helpers for Gorm using Confer

GoDoc

wercker status

Now featuring™:

  • Using Confer to gather database settings
  • Designed for one connection per app (for now)
  • MySQL and SQLite drivers
  • Built-in support for facebookgo/inject
  • Some helpers for building structs with the Repository Pattern

Maybe one day:

  • PostgreSQL driver
  • More advanced connection config
  • Multiple connections

Full Example:

Available at example/main.go:

package main

import (
	"github.com/etcinit/ohmygorm"
	"github.com/facebookgo/inject"
	"github.com/jacobstr/confer"
	"github.com/kr/pretty"
)

// User is a simple example user model
type User struct {
	ID    int
	Name  string
	Email string
}

// Post is a simple blog post
type Post struct {
	ID       int
	Author   User
	AuthorID int
	Title    string
	Content  string
}

func main() {
	// Create the configuration
	// In this case, we will be using the environment and some safe defaults
	config := confer.NewConfig()
	config.SetDefault("database.driver", "sqlite")
	config.SetDefault("database.file", ":memory:")
	config.AutomaticEnv()

	// Next, we setup the dependency graph
	// In this example, the graph won't have many nodes, but on more complex
	// applications it becomes more useful.
	var g inject.Graph
	var connections ohmygorm.ConnectionsService
	var migrator ohmygorm.MigrationsService
	g.Provide(
		&inject.Object{Value: config},
		&inject.Object{Value: &connections},
		&inject.Object{Value: &migrator},
	)
	g.Populate()

	// At this point, the DI library has automatically set the dependencies of
	// both structs (connections and migrator), so we can start using them!

	// Run migrations
	migrator.Run([]interface{}{&User{}, &Post{}})

	// Get a connection
	db, err := connections.Make()

	if err != nil {
		panic(err)
	}

	// Create a user
	user := User{Name: "Bobby Tables", Email: "bobby@tables.inc"}
	db.Create(&user)

	// Create a post
	post := Post{
		Author:  user,
		Title:   "SQL injection is bad for you",
		Content: "Its bad. Really bad.",
	}
	db.Create(&post)

	// Query all posts
	var posts []Post
	db.Preload("Author").Find(&posts)

	pretty.Println(posts)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConnectionsService

type ConnectionsService struct {
	Config            *confer.Config `inject:""`
	CurrentConnection *gorm.DB
}

ConnectionsService provides functions for connecting to database

func (*ConnectionsService) Make

func (c *ConnectionsService) Make() (*gorm.DB, error)

Make creates a new Gorm connection to the configured database

type DirectoryMigratorService

type DirectoryMigratorService struct {
	Migrations  *MigrationsService  `inject:""`
	Connections *ConnectionsService `inject:""`
	Directory   ModelDirectory      `inject:""`
}

DirectoryMigratorService provides the migration command

func (*DirectoryMigratorService) Run

Run performs all the migrations for the models listed in the directory.

func (*DirectoryMigratorService) RunCommand

func (m *DirectoryMigratorService) RunCommand(c *cli.Context)

RunCommand performs all the migrations for the models listed in the directory and provides some feedback through standard output.

type MigrationsService

type MigrationsService struct {
	Connections *ConnectionsService `inject:""`
}

MigrationsService provides migration-related functions

func (*MigrationsService) Run

func (m *MigrationsService) Run(models []interface{}) error

Run automatically runs migrations for the database. Note that some changes will not be applied automatically if they destroy data, such as removing a table or column.

type ModelDirectory

type ModelDirectory interface {
	GetModels() []interface{}
}

A ModelDirectory lists an application models, which can be used for migrations and administration of models.

type RepositoryService

type RepositoryService struct {
	Connections *ConnectionsService `inject:""`
}

RepositoryService provides function for building structures following the repository pattern.

func (*RepositoryService) Exists

func (r *RepositoryService) Exists(model interface{}, id int) bool

Exists checks if a model instance exists in the database by its ID

func (*RepositoryService) Find

func (r *RepositoryService) Find(model interface{}, id int) error

Find attempts to find a specific model instance by its ID.

func (*RepositoryService) FirstOrFail

func (r *RepositoryService) FirstOrFail(model interface{}, query *gorm.DB) error

FirstOrFail is a shortcut for using First and checking for the RecordNotFound error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL