examples

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2022 License: MIT Imports: 9 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,
	}
)
View Source
var SentinalTime time.Time = time.Date(2006, 1, 2, 3, 4, 5, 7, time.Local)

SentinalTime is a set time value used to generate times.

Functions

func Connect added in v0.3.0

func Connect(e Example) (DB *sql.DB, err error)

Connect creates a sqlmock DB and configures it for the example.

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 Example added in v0.3.0

type Example int

Example is a specific example.

const (
	ExNone Example = iota
	ExAddressInsert
	ExAddressInsertSlice
	ExAddressUpdate
	ExAddressUpdateSlice
	ExAddressSave
	ExAddressSaveSlice
	ExLogEntrySave
	ExRelationshipInsert
	ExRelationshipInsertSlice
	ExRelationshipUpdate
	ExRelationshipUpdateSlice
	ExRelationshipUpsert
	ExRelationshipUpsertSlice
	ExRelationshipSave
	ExUpsert
	ExUpsertSlice
)

type LogEntry added in v0.5.0

type LogEntry struct {
	model.TableName `json:"-" model:"log"`

	Message string `json:"message"`
}

LogEntry is a model with no key fields defined in the Go struct. When such a model is saved with Models.Save the Insert method will be used.

Note that there's no reason a model like LogEntry can't be a partial model with just enough information to insert a record without caring about any key,auto field within the database table.

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.

type Relationship added in v0.3.0

type Relationship struct {
	model.TableName `json:"-" model:"relationship"`
	//
	LeftId  int `json:"left_id" db:"left_fk" model:"key"`
	RightId int `json:"right_id" db:"right_fk" model:"key"`
	// Such a table might have other columns.
	Toggle bool `json:"toggle"`
}

Relationship is a model with a composite primary key and no fields that auto update. Such a model might exist for relationship tables.

type TimeGenerator added in v0.5.0

type TimeGenerator struct {
	// contains filtered or unexported fields
}

TimeGenerator uses SentinalTime to return deterministic time values.

func (*TimeGenerator) Next added in v0.5.0

func (tg *TimeGenerator) Next() time.Time

Next returns the next time.Time.

type Upsertable added in v0.3.0

type Upsertable struct {
	model.TableName `json:"-" model:"upsertable"`
	//
	Id           string    `json:"id" db:"pk" model:"key"`
	CreatedTime  time.Time `json:"created_time" db:"created_tmz" model:"inserted"`
	ModifiedTime time.Time `json:"modified_time" db:"modified_tmz" model:"inserted,updated"`
	String       string    `json:"string"`
	Number       int       `json:"number"`
}

Upsertable is a model that can use UPSERT style queries because it only has "key" and not "key,auto" columns.

Jump to

Keyboard shortcuts

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