examples

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2021 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package examples provides types and functions to facilitate the examples and test code in the model package.

Important take aways from this example package are the instantiation of the global Models variable and registering type(s) with it.

Model registration is performed via an init() function:

func init() {
	// Somewhere in your application you need to register all types to be used as models.
	Models.Register(&Address{})
	Models.Register(&Person{})
	Models.Register(&PersonAddress{})
}

Also important is the struct definition for the type Address; the struct definition combined with the Models global defines how SQL statements are generated and the expected column names in the generated SQL.

Index

Constants

This section is empty.

Variables

View Source
var (
	// Models is a sample model.Database.
	//
	// Most of the magic occurs in the Mapper (set.Mapper); if your models follow a consistent logic
	// for struct-tags to column-names then you may only need a single model.Database for your application.
	//
	// However creating a model.Database is relatively easy and there's nothing to stop you from creating
	// multiple of them with different Mapper (set.Mapper) to handle inconsistency in your models.
	Models = &model.Models{

		Mapper: &set.Mapper{
			Join: "_",
			Tags: []string{"db", "json"},
		},

		Grammar: grammar.Postgres,
	}
)

Functions

func DB_Insert

func DB_Insert(models interface{}) (*sql.DB, [][]driver.Value, error)

DB_Insert sets up our mock database for inserting records.

func DB_Update

func DB_Update(models interface{}) (*sql.DB, [][]driver.Value, error)

DB_Update sets up our mock database for updating records.

func NewModels

func NewModels() *model.Models

NewModels returns a model.Models type.

func ReturnArgs

func ReturnArgs(n int, columns ...string) []driver.Value

ReturnArgs creates enough return args for the specified number of models in n. Columns can be: pk (int), created (time.Time), modified (time.Time)

Types

type Address

type Address struct {
	// This member tells the model.Database the name of the table for this model; think of it
	// like xml.Name when using encoding/xml.
	model.TableName `json:"-" model:"addresses"`
	//
	// The struct fields and column names.  The example Mapper allows the db and json
	// to share names where they are the same; however db is higher in precedence than
	// json so where present that is the database column name.
	Id           int       `json:"id" db:"pk" model:"key,auto"`
	CreatedTime  time.Time `json:"created_time" db:"created_tmz" model:"inserted"`
	ModifiedTime time.Time `json:"modified_time" db:"modified_tmz" model:"inserted,updated"`
	Street       string    `json:"street"`
	City         string    `json:"city"`
	State        string    `json:"state"`
	Zip          string    `json:"zip"`
}

Address is a simple model representing an address.

type Person

type Person struct {
	model.TableName `json:"-" model:"people"`
	//
	Id           int       `json:"id" db:"pk" model:"key,auto"`
	SpouseId     int       `json:"spouse_id" db:"spouse_fk" model:"foreign"`
	CreatedTime  time.Time `json:"created_time" db:"created_tmz" model:"inserted"`
	ModifiedTime time.Time `json:"modified_time" db:"modified_tmz" model:"inserted,updated"`
	First        string    `json:"first"`
	Last         string    `json:"last"`
	Age          int       `json:"age"`
	SSN          string    `json:"ssn" model:"unique"`
}

Person is a simple model representing a person.

type PersonAddress

type PersonAddress struct {
	model.TableName `json:"-" model:"relate_people_addresses"`
	//
	PersonId  int `json:"person_id" db:"person_fk" model:"key"`
	AddressId int `json:"address_id" db:"address_fk" model:"key"`
}

PersonAddress links a person to an address.

Jump to

Keyboard shortcuts

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