models

package
v0.1.12 Latest Latest
Warning

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

Go to latest
Published: May 7, 2020 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ModelRegistry = make(map[string]*Reg)

ModelRegistry is model registry

View Source
var OwnerTyp reflect.Type

OwnerTyp is the model of the Owner table

View Source
var OwnershipTyp reflect.Type

OwnershipTyp is the model of ownership table, the table that has many to many links users with other models

View Source
var UserTyp reflect.Type

UserTyp is the model of the User table

Functions

func AddBatchDeleteBeforeAndAfterHookPoints

func AddBatchDeleteBeforeAndAfterHookPoints(typeString string,
	before func(ms []IModel, db *gorm.DB, oid *datatypes.UUID, typeString string, cargo *BatchHookCargo) error,
	after func(ms []IModel, db *gorm.DB, oid *datatypes.UUID, typeString string, cargo *BatchHookCargo) error)

AddBatchDeleteBeforeAndAfterHookPoints adds hookpoints which are called before and after batch delete. Either one can be left as nil

func AddBatchUpdateBeforeAndAfterHookPoints

func AddBatchUpdateBeforeAndAfterHookPoints(typeString string,
	before func(ms []IModel, db *gorm.DB, oid *datatypes.UUID, typeString string, cargo *BatchHookCargo) error,
	after func(ms []IModel, db *gorm.DB, oid *datatypes.UUID, typeString string, cargo *BatchHookCargo) error)

AddBatchUpdateBeforeAndAfterHookPoints adds hookpoints which are called before and after batch update. Either one can be left as nil

func AddModelRegistry

func AddModelRegistry(typeString string, typ reflect.Type)

AddModelRegistry adds a New function for an IModel

func AddOwnerToModelRegistry added in v0.1.8

func AddOwnerToModelRegistry(typeString string, typ reflect.Type)

AddOwnerToModelRegistry adds a New function for an owner

func AddUserToModelRegistry added in v0.1.3

func AddUserToModelRegistry(typeString string, typ reflect.Type)

AddUserToModelRegistry adds a New function for a user

func RegisterOwnershipModel added in v0.1.3

func RegisterOwnershipModel(ownership reflect.Type)

RegisterOwnershipModel register the ownership table so the library can init base on it

Types

type BaseModel

type BaseModel struct {
	ID        *datatypes.UUID `gorm:"type:binary(16);primary_key;" json:"id"`
	CreatedAt time.Time       `sql:"index" json:"createdAt"`
	UpdatedAt time.Time
	DeletedAt *time.Time `sql:"index"`
}

BaseModel is the base class domain model which has standard ID

func (*BaseModel) BeforeCreate

func (b *BaseModel) BeforeCreate(scope *gorm.Scope) error

BeforeCreate sets a UUID if no ID is set (this is Gorm's hookpoint)

func (*BaseModel) GetID

func (b *BaseModel) GetID() *datatypes.UUID

GetID Get the ID field of the model (useful when using interface)

func (*BaseModel) SetID

func (b *BaseModel) SetID(id *datatypes.UUID)

SetID Set the ID field of the model (useful when using interface)

func (*BaseModel) Validate

func (b *BaseModel) Validate() error

Validate validates the model

type BatchHookCargo

type BatchHookCargo struct {
	Payload interface{}
}

BatchHookCargo is payload between batch update and batch delete hookpoints

type Client

type Client struct {
	gorm.Model  // Includes ID, CreatedAt, UpdatedAt, DeletedAt
	Name        string
	Secret      string `gorm:"not null" json:"-"`
	RedirectURI string // TODO: RedirectURI can be multiples
}

Client is the program that makes request to the API So iOS would be one client, android would be another Any website making API request would have its own client ID This needs to be inserted into db beforehand. So we can validate the app making the request. Any such app has the permission to create the user

type IAfterDelete

type IAfterDelete interface {
	AfterDeleteDB(db *gorm.DB, oid *datatypes.UUID, typeString string, cargo *ModelCargo) error
}

IAfterDelete supports method to be called after data is deleted from the database

type IAfterInsert

type IAfterInsert interface {
	AfterInsertDB(db *gorm.DB, oid *datatypes.UUID, typeString string, cargo *ModelCargo) error
}

IAfterInsert supports method to be called after data is inserted (created) into the database

type IAfterPatch

type IAfterPatch interface {
	AfterPatchDB(db *gorm.DB, oid *datatypes.UUID, typeString string, cargo *ModelCargo) error
}

IAfterPatch supports method to be called before data is patched in the database

type IAfterUpdate

type IAfterUpdate interface {
	AfterUpdateDB(db *gorm.DB, oid *datatypes.UUID, typeString string, cargo *ModelCargo) error
}

IAfterUpdate supports method to be called after data is updated in the database

type IBeforeDelete

type IBeforeDelete interface {
	BeforeDeleteDB(db *gorm.DB, oid *datatypes.UUID, typeString string, cargo *ModelCargo) error
}

IBeforeDelete supports method to be called before data is deleted from the database

type IBeforeInsert

type IBeforeInsert interface {
	BeforeInsertDB(db *gorm.DB, oid *datatypes.UUID, typeString string, cargo *ModelCargo) error
}

IBeforeInsert supports method to be called before data is inserted (created) into the database

type IBeforePatch

type IBeforePatch interface {
	BeforePatchDB(db *gorm.DB, oid *datatypes.UUID, typeString string, cargo *ModelCargo) error
}

IBeforePatch supports method to be called before data is patched in the database

type IBeforeUpdate

type IBeforeUpdate interface {
	BeforeUpdateDB(db *gorm.DB, oid *datatypes.UUID, typeString string, cargo *ModelCargo) error
}

IBeforeUpdate supports method to be called before data is updated in the database

type IDoRealDelete added in v0.1.4

type IDoRealDelete interface {
	DoRealDelete() bool
}

DoRealDelete is an interface to customize specification for real db delete

type IModel

type IModel interface {
	Permissions(r UserRole) jsontransform.JSONFields

	// The following two avoids having to use reflection to access ID
	GetID() *datatypes.UUID
	SetID(id *datatypes.UUID)
}

IModel is the interface for all domain models

func NewFromTypeString

func NewFromTypeString(typeString string) IModel

NewFromTypeString instantiate a new IModel object from type registry

func NewSliceFromDB

func NewSliceFromDB(typeString string, f func(interface{}, ...interface{}) *gorm.DB) ([]IModel, error)

NewSliceFromDB queries the database for an array of models func(dest interface{}) *gorm.DB

func NewSliceStructFromTypeString

func NewSliceStructFromTypeString(typeString string) []IModel

NewSliceStructFromTypeString : return something originally like this obj := make(map[string][]Room) obj["content"] = make([]Room, 0, 0) https://stackoverflow.com/questions/50233285/create-a-map-in-go-using-reflection func NewSliceStructFromTypeString(typeString string) map[string][]IModel {

func NewSliceStructFromTypeStringAndJSON

func NewSliceStructFromTypeStringAndJSON(typeString string, jsn []byte) ([]IModel, error)

NewSliceStructFromTypeStringAndJSON unmarshals a JSON collection

type IRole added in v0.1.3

type IRole interface {
	GetRole() UserRole
	SetRole(UserRole)
}

IRole is what IRole tables should satisfy.

type IValidate

type IValidate interface {
	Validate() error
}

IValidate supports validation with govalidator

type ModelCargo

type ModelCargo struct {
	Payload interface{}
}

ModelCargo is payload between hookpoints

type OwnershipModelBase added in v0.1.3

type OwnershipModelBase struct {
	gorm.Model          // uses standard int id (cuz I started with it and it works)
	Role       UserRole // an int
}

OwnershipModelBase has a role

func (*OwnershipModelBase) GetRole added in v0.1.3

func (o *OwnershipModelBase) GetRole() UserRole

GetRole gets the role field of the model, comforms to IRole

func (*OwnershipModelBase) SetRole added in v0.1.3

func (o *OwnershipModelBase) SetRole(r UserRole)

SetRole sets the role field of the model, comforms to IRole

type Reg

type Reg struct {
	Typ          reflect.Type
	BeforeUpdate func(ms []IModel, db *gorm.DB, oid *datatypes.UUID, typeString string, cargo *BatchHookCargo) error
	AfterUpdate  func(ms []IModel, db *gorm.DB, oid *datatypes.UUID, typeString string, cargo *BatchHookCargo) error
	BeforeDelete func(ms []IModel, db *gorm.DB, oid *datatypes.UUID, typeString string, cargo *BatchHookCargo) error
	AfterDelete  func(ms []IModel, db *gorm.DB, oid *datatypes.UUID, typeString string, cargo *BatchHookCargo) error
}

Reg is a registry item

type UserRole

type UserRole int

UserRole type with enum

const (
	// Admin is admin UserRole
	Admin UserRole = 0
	// Guest is guest UserRole (screw go-lint man)
	Guest UserRole = 1
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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