models

package
v0.3.4 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2021 License: MIT Imports: 13 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 UserTyp reflect.Type

UserTyp is the model of the User table

View Source
var (
	Validate *validator.Validate
)

use a single instance , it caches struct info

Functions

func AddBatchDeleteBeforeAndAfterHookPoints

func AddBatchDeleteBeforeAndAfterHookPoints(typeString string,
	before func(bhpData BatchHookPointData) error,
	after func(bhpData BatchHookPointData) error)

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

func AddBatchInsertBeforeAndAfterHookPoints added in v0.1.24

func AddBatchInsertBeforeAndAfterHookPoints(typeString string,
	before func(bhpData BatchHookPointData) error,
	after func(bhpData BatchHookPointData) error)

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

func AddBatchReadAfterHookPoint added in v0.1.18

func AddBatchReadAfterHookPoint(typeString string,
	after func(bhpData BatchHookPointData) error)

AddBatchReadAfterHookPoint adds hookpoints which are called after and read, can be left as nil

func AddBatchUpdateBeforeAndAfterHookPoints

func AddBatchUpdateBeforeAndAfterHookPoints(typeString string,
	before func(bhpData BatchHookPointData) error,
	after func(bhpData BatchHookPointData) 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 AddModelRegistryWithOptions added in v0.1.14

func AddModelRegistryWithOptions(typeString string, typ reflect.Type, options ModelRegistryOptions)

AddModelRegistryWithOptions 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 GetJoinTableName added in v0.1.34

func GetJoinTableName(modelObj IHasOwnershipLink) string

GetJoinTableName if comforms to IHasOwnershipLink

func GetOrganizationTableName added in v0.1.34

func GetOrganizationTableName(modelObj IHasOrganizationLink) string

GetOrganizationTableName if comforms to IHasOrganizationLink

func GetTableNameFromIModel added in v0.1.20

func GetTableNameFromIModel(model IModel) string

GetTableNameFromIModel gets table name from an IModel

func GetTableNameFromType added in v0.1.39

func GetTableNameFromType(typ reflect.Type) string

GetTableNameFromType get table name from the model reflect.type

func GetTableNameFromTypeString added in v0.1.39

func GetTableNameFromTypeString(typeString string) string

GetTableNameFromTypeString get table name from typeString

Types

type BaseModel

type BaseModel struct {

	// For Postgres
	ID        *datatypes.UUID `gorm:"type:uuid;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 BatchHookPointData added in v0.2.0

type BatchHookPointData struct {
	// Ms is the slice of IModels
	Ms []IModel
	// DB is the DB handle
	DB *gorm.DB
	// OID is owner ID, the user accessing the API right now
	OID *datatypes.UUID
	// Scope included in the token who is accessing right now
	Scope *string
	// Scope included in the token who is accessing right now
	TypeString string
	// Cargo between Before and After hookpoints (not used in AfterRead since there is before read hookpoint.)
	Cargo *BatchHookCargo
	// Role of this user in relation to this data, only available during read
	Roles []UserRole
}

BatchHookPointData is the data send to batch model 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 HookPointData added in v0.2.0

type HookPointData struct {
	// DB handle
	DB *gorm.DB
	// OID is owner ID, the user accessing the API right now
	// Not available in BeforeLogin
	OID *datatypes.UUID
	// Scope included in the token who is accessing right now
	Scope *string
	// TypeString is the typeString (model string) of this model
	TypeString string
	// Cargo between Before and After hookpoints (not used in IAfterRead since there is no IBeforeRead.)
	Cargo *ModelCargo
	// Role of this user in relation to this data, only available during read
	Role *UserRole
}

HookPointData is the data send to single model hookpoints

type IAfterCreate added in v0.3.2

type IAfterCreate interface {
	AfterInsertDB(hpdata HookPointData) error
}

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

type IAfterDelete

type IAfterDelete interface {
	AfterDeleteDB(hpdata HookPointData) error
}

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

type IAfterLogin added in v0.1.47

type IAfterLogin interface {
	AfterLogin(hpdata HookPointData, payload map[string]interface{}) (map[string]interface{}, error)
}

IAfterLogin has a function that is a hookpoint for actions after login but before marshalling

type IAfterLoginFailed added in v0.2.0

type IAfterLoginFailed interface {
	AfterLoginFailed(hpdata HookPointData) error
}

IAfterLoginFailed has a function that is a hookpoint for actions after login but before marshalling

type IAfterPasswordUpdate added in v0.2.2

type IAfterPasswordUpdate interface {
	AfterPasswordUpdateDB(hpdata HookPointData) error
}

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

type IAfterPatch

type IAfterPatch interface {
	AfterPatchDB(hpdata HookPointData) error
}

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

type IAfterRead added in v0.1.18

type IAfterRead interface {
	AfterReadDB(hpdata HookPointData) error
}

IAfterRead supports method to be called after data is read from the database

type IAfterUpdate

type IAfterUpdate interface {
	AfterUpdateDB(hpdata HookPointData) error
}

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

type IBeforeCreate added in v0.3.2

type IBeforeCreate interface {
	BeforeInsertDB(hpdata HookPointData) error
}

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

type IBeforeDelete

type IBeforeDelete interface {
	BeforeDeleteDB(hpdata HookPointData) error
}

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

type IBeforeLogin added in v0.1.47

type IBeforeLogin interface {
	BeforeLogin(hpdata HookPointData) error
}

IBeforeLogin has a function that is a hookpoint for actions after login but before marshalling

type IBeforePasswordUpdate added in v0.2.2

type IBeforePasswordUpdate interface {
	BeforePasswordUpdateDB(hpdata HookPointData) error
}

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

type IBeforePatch

type IBeforePatch interface {
	BeforePatchDB(hpdata HookPointData) error
}

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

type IBeforeUpdate

type IBeforeUpdate interface {
	BeforeUpdateDB(hpdata HookPointData) 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
}

IDoRealDelete is an interface to customize specification for real db delete

type IGuardAPIEntry added in v0.1.25

type IGuardAPIEntry interface {
	GuardAPIEntry(scope *string, endpoint string, method string) bool
}

IGuardAPIEntry supports method which guard access to API based on scope

type IHasOrganizationLink interface {
	OrganizationType() reflect.Type
	GetOrganizationID() *datatypes.UUID
	GetOrganizationIDFieldName() string
}

IHasOrganizationLink has a function that returns the organization table usable for OrganizationMapper

type IHasOwnershipLink interface {
	OwnershipType() reflect.Type
}

IHasOwnershipLink has a function that returns the ownership table usable for OwnershipMapper

type IHasTableName added in v0.1.18

type IHasTableName interface {
	TableName() string
}

IHasTableName we know if there is Gorm's defined custom TableName

type IModel

type IModel interface {
	Permissions(r UserRole, scope *string) 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 NewSliceFromDBByType added in v0.1.28

func NewSliceFromDBByType(modelType reflect.Type, f func(interface{}, ...interface{}) *gorm.DB) ([]IModel, error)

NewSliceFromDBByType queries the database for an array of models based on modelType func(dest interface{}) *gorm.DB

func NewSliceFromDBByTypeString added in v0.1.28

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

NewSliceFromDBByTypeString queries the database for an array of models based on typeString 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 {

type IOwnership added in v0.1.18

type IOwnership interface {
	GetRole() UserRole
	SetRole(UserRole)

	GetUserID() *datatypes.UUID
	SetUserID(*datatypes.UUID)

	GetModelID() *datatypes.UUID
	SetModelID(*datatypes.UUID)

	GetID() *datatypes.UUID
	SetID(*datatypes.UUID)
}

IOwnership is what OwnershipModelBase tables should satisfy.

type IValidate

type IValidate interface {
	Validate(scope *string, endpoint string, method string) error
}

IValidate supports validation with govalidator

type JSONIDPatch added in v0.3.1

type JSONIDPatch struct {
	ID    *datatypes.UUID `json:"id"`
	Patch json.RawMessage `json:"patch"` // json.RawMessage is actually just typedefed to []byte
}

JSONIDPatch is the stuff inside "content" for PatchMany operation

type MapperType added in v0.1.28

type MapperType int

MapperType is the mapper type

const (
	// MapperTypeViaOwnership is for type which user owns something
	MapperTypeViaOwnership MapperType = iota

	// MapperTypeViaOrganization is for type where an organization owns something
	MapperTypeViaOrganization

	// MapperTypeGlobal is for type where data is public to all
	MapperTypeGlobal

	// MapperTypeLinkTable is for table linking user and regular models
	MapperTypeLinkTable
)

type ModelCargo

type ModelCargo struct {
	Payload interface{}
}

ModelCargo is payload between hookpoints

type ModelRegistryOptions added in v0.1.28

type ModelRegistryOptions struct {
	BatchEndpoints string // Batch endpoints, "CRUD" for create, batch read, batch update, batch delete
	IDEndPoints    string //  ID end points, "RUD" for read one, update one, and delete one
	Mapper         MapperType
}

ModelRegistryOptions is options when you want to add a model to registry

type OwnershipModelBase added in v0.1.3

type OwnershipModelBase struct {
	ID *datatypes.UUID `gorm:"type:uuid;primary_key;" json:"id"`

	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *time.Time `sql:"index"`

	Role UserRole `json:"role"` // an int
}

OwnershipModelBase has a role

func (*OwnershipModelBase) BeforeCreate added in v0.3.0

func (o *OwnershipModelBase) BeforeCreate(scope *gorm.Scope) error

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

func (*OwnershipModelBase) GetID added in v0.3.0

func (o *OwnershipModelBase) GetID() *datatypes.UUID

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

func (*OwnershipModelBase) GetRole added in v0.1.3

func (o *OwnershipModelBase) GetRole() UserRole

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

func (*OwnershipModelBase) SetID added in v0.3.0

func (o *OwnershipModelBase) SetID(id *datatypes.UUID)

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

func (*OwnershipModelBase) SetRole added in v0.1.3

func (o *OwnershipModelBase) SetRole(r UserRole)

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

type OwnershipModelWithIDBase added in v0.3.0

type OwnershipModelWithIDBase struct {
	OwnershipModelBase

	UserID  *datatypes.UUID `json:"userID"` // I guess the user's table has to be named "User" then.
	ModelID *datatypes.UUID `json:"modelID"`
}

OwnershipModelWithIDBase is one with ID, if you don't need unique index for userID and modelID (if you don't expose the link table via LinkTableMapper) You can use this.

func (*OwnershipModelWithIDBase) GetModelID added in v0.3.0

func (o *OwnershipModelWithIDBase) GetModelID() *datatypes.UUID

GetModelID gets the id of the model, comforms to IOwnership

func (*OwnershipModelWithIDBase) GetUserID added in v0.3.0

func (o *OwnershipModelWithIDBase) GetUserID() *datatypes.UUID

GetUserID gets the user id of the model, comforms to IOwnership

func (*OwnershipModelWithIDBase) SetModelID added in v0.3.0

func (o *OwnershipModelWithIDBase) SetModelID(id *datatypes.UUID)

SetModelID sets the id of the model, comforms to IOwnership

func (*OwnershipModelWithIDBase) SetUserID added in v0.3.0

func (o *OwnershipModelWithIDBase) SetUserID(id *datatypes.UUID)

SetUserID sets the user id of the model, comforms to IOwnership

type Reg

type Reg struct {
	Typ            reflect.Type
	BatchEndpoints string     // Batch endpoints, "CRUD" for create, batch read, batch update, batch delete
	IDEndPoints    string     //  ID end points, "RUD" for read one, update one, and delete one
	Mapper         MapperType // Custmized mapper, default to datamapper.SharedOwnershipMapper

	AfterRead func(bhpData BatchHookPointData) error

	BeforeInsert func(bhpData BatchHookPointData) error
	AfterInsert  func(bhpData BatchHookPointData) error

	BeforeUpdate func(bhpData BatchHookPointData) error
	AfterUpdate  func(bhpData BatchHookPointData) error

	BeforePatch func(bhpData BatchHookPointData) error
	AfterPatch  func(bhpData BatchHookPointData) error

	BeforeDelete func(bhpData BatchHookPointData) error
	AfterDelete  func(bhpData BatchHookPointData) error
}

Reg is a registry item

type UserRole

type UserRole int

UserRole type with enum

const (
	// Negatives are not stored in DB
	// UserRoleAny not for value in db, but for permission where any is fine (link table)
	UserRoleAny UserRole = -2

	// Invalid for this resource
	Invalid UserRole = -1

	// Admin is admin UserRole
	Admin UserRole = 0

	// Guest is guest UserRole (screw go-lint man)
	Guest UserRole = 1

	// Public to all (global object)
	Public UserRole = 2
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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