model

package
v0.0.0-...-6a037c9 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2024 License: MIT Imports: 31 Imported by: 0

README

model

use

model.New(func(o *model.Options) {
    o.SchemaApi = "/api/v1/schema"

    o.SchemaMiddleware = func() []znet.Handler {
    	return []znet.Handler{}
    }

    o.Schemas.Append(define.Schema{})
})

Documentation

Index

Constants

View Source
const (
	CreatedAtKey = "created_at"
	// CreatedByKey = "created_by"
	UpdatedAtKey = "updated_at"
	DeletedAtKey = "deleted_at"
)

Variables

View Source
var Common = &common{}
View Source
var InsideOption = &inside{}

Functions

func Count

func Count[T Filter](m *Schema, filter T, fn ...func(*CondOptions)) (uint64, error)

func Delete

func Delete[T filter](m *Schema, filter T, fn ...func(*CondOptions)) (int64, error)

func DeleteMany

func DeleteMany[T filter](m *Schema, filter T, fn ...func(*CondOptions)) (int64, error)

func Find

func Find[T filter](m *Schema, filter T, fn ...func(*CondOptions)) (ztype.Maps, error)

func FindCol

func FindCol[T filter](
	m *Schema,
	field string,
	filter T,
	fn ...func(*CondOptions),
) (ztype.Type, bool, error)

func FindCols

func FindCols[T filter](
	m *Schema,
	field string,
	filter T,
	fn ...func(*CondOptions),
) (ztype.SliceType, error)

func FindOne

func FindOne[T filter](m *Schema, filter T, fn ...func(*CondOptions)) (ztype.Map, error)

func GetEngine

func GetEngine[T *zdb.DB](m *Model) T

func IDKey

func IDKey() string

func Insert

func Insert(m *Schema, data ztype.Map, fn ...func(*InsertOptions)) (lastId interface{}, err error)

func InsertMany

func InsertMany(m *Schema, datas ztype.Maps, fn ...func(*InsertOptions)) (lastIds []interface{}, err error)

func Update

func Update[T filter](
	m *Schema,
	filter T,
	data ztype.Map,
	fn ...func(*CondOptions),
) (total int64, err error)

func UpdateMany

func UpdateMany[T filter](
	m *Schema,
	filter T,
	data ztype.Map,
	fn ...func(*CondOptions),
) (total int64, err error)

func VerifiData

func VerifiData(data ztype.Map, columns mSchema.Fields, active activeType) (ztype.Map, error)

VerifiData 验证数据

Types

type ColumnEnum

type ColumnEnum struct {
	Value string `json:"value"`
	Label string `json:"label"`
}

type CondOptions

type CondOptions struct {
	Fields  []string
	GroupBy []string
	OrderBy map[string]string
	Join    []StorageJoin
	Limit   int
}

type CryptProcess

type CryptProcess func(string) (string, error)

type DataTime

type DataTime struct {
	time.Time
}

func (DataTime) MarshalJSON

func (t DataTime) MarshalJSON() ([]byte, error)

func (*DataTime) Scan

func (t *DataTime) Scan(v interface{}) error

func (*DataTime) String

func (t *DataTime) String() string

func (*DataTime) UnmarshalJSON

func (t *DataTime) UnmarshalJSON(data []byte) error

func (DataTime) Value

func (t DataTime) Value() (driver.Value, error)

type DealOldColumn

type DealOldColumn uint8

type Filter

type Filter ztype.Map

func NewFilter

func NewFilter() Filter

func (Filter) Cond

func (f Filter) Cond(fn func(*builder.BuildCond) string) Filter

func (Filter) Get

func (f Filter) Get(field string) ztype.Type

func (Filter) Set

func (f Filter) Set(field string, cond any) Filter

type InsertOptions

type InsertOptions struct {
	Options string
}

type Migration

type Migration struct {
	Model *Schema
	DB    *zdb.DB
}

func (*Migration) Auto

func (m *Migration) Auto(oldColumn ...DealOldColumn) (err error)

func (*Migration) CreateTable

func (m *Migration) CreateTable(db *zdb.DB) error

func (*Migration) GetFields

func (m *Migration) GetFields() (ztype.Map, error)

func (*Migration) HasTable

func (m *Migration) HasTable() bool

func (*Migration) Indexs

func (m *Migration) Indexs(db *zdb.DB) error

func (*Migration) InitValue

func (m *Migration) InitValue(first bool) error

func (*Migration) UpdateTable

func (m *Migration) UpdateTable(db *zdb.DB, oldColumn ...DealOldColumn) error

type Migrationer

type Migrationer interface {
	Auto(deleteColumn ...DealOldColumn) (err error)
	HasTable() bool
	GetFields() (ztype.Map, error)
}

type Model

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

func (*Model) Count

func (o *Model) Count(filter Filter, fn ...func(*CondOptions)) (uint64, error)

Count 统计数量

func (*Model) Delete

func (o *Model) Delete(filter Filter, fn ...func(*CondOptions)) (total int64, err error)

Delete 删除数据

func (*Model) DeleteByID

func (o *Model) DeleteByID(id any, fn ...func(*CondOptions)) (total int64, err error)

DeleteByID 通过ID删除数据

func (*Model) DeleteMany

func (o *Model) DeleteMany(filter Filter, fn ...func(*CondOptions)) (total int64, err error)

DeleteMany 删除多条数据

func (*Model) Exists

func (o *Model) Exists(filter Filter, fn ...func(*CondOptions)) (bool, error)

Exists 数据是否存在

func (*Model) Find

func (o *Model) Find(filter Filter, fn ...func(*CondOptions)) (ztype.Maps, error)

Find 查询数据

func (*Model) FindCols

func (o *Model) FindCols(field string, filter Filter) (ztype.SliceType, error)

FindCols 查询指定字段

func (*Model) FindOne

func (o *Model) FindOne(filter Filter, fn ...func(*CondOptions)) (ztype.Map, error)

FindOne 查询一条数据

func (*Model) FindOneByID

func (o *Model) FindOneByID(id any, fn ...func(*CondOptions)) (ztype.Map, error)

FindOneByID 通过ID查询

func (*Model) Insert

func (o *Model) Insert(data ztype.Map, fn ...func(*InsertOptions)) (lastId interface{}, err error)

Insert 插入数据

func (*Model) InsertMany

func (o *Model) InsertMany(data ztype.Maps, fn ...func(*InsertOptions)) (lastId interface{}, err error)

InsertMany 批量插入数据

func (*Model) Pages

func (o *Model) Pages(page, pagesize int, filter Filter, fn ...func(*CondOptions)) (*PageData, error)

Pages 分页查询

func (*Model) Schema

func (o *Model) Schema() *Schema

Schema 获取模型

func (*Model) Update

func (o *Model) Update(filter Filter, data ztype.Map, fn ...func(*CondOptions)) (total int64, err error)

Update 更新数据

func (*Model) UpdateByID

func (o *Model) UpdateByID(id any, data ztype.Map, fn ...func(*CondOptions)) (total int64, err error)

UpdateByID 通过ID更新

func (*Model) UpdateMany

func (o *Model) UpdateMany(filter Filter, data ztype.Map, fn ...func(*CondOptions)) (total int64, err error)

UpdateMany 更新多条数据

type Models

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

Models 快捷操作

func (*Models) All

func (m *Models) All() (models []*Model)

All 全部模型

func (*Models) Get

func (m *Models) Get(name string) (*Model, bool)

Get 获取操作对象

func (*Models) MustGet

func (m *Models) MustGet(name string) *Model

MustGet 获取操作对象

type Module

type Module struct {
	service.ModuleLifeCycle
	Options Options
	// contains filtered or unexported fields
}

func New

func New(o ...func(*Options)) (m *Module)

func (*Module) GetModel

func (m *Module) GetModel(name string) (*Model, bool)

func (*Module) GetSchema

func (m *Module) GetSchema(name string) (*Schema, bool)

func (*Module) Models

func (m *Module) Models() *Models

func (*Module) MustGetModel

func (m *Module) MustGetModel(name string) *Model

func (*Module) MustGetSchema

func (m *Module) MustGetSchema(name string) *Schema

func (*Module) Name

func (m *Module) Name() string

func (*Module) Schemas

func (m *Module) Schemas() *Schemas

func (*Module) String

func (m *Module) String() string

type Options

type Options struct {
	SetDB              func() (*zdb.DB, error)
	SetAlternateModels func() ([]*Model, error)
	SchemaMiddleware   func() []znet.Handler
	Prefix             string
	SchemaDir          string
	SchemaApi          string
	Schemas            schema.Schemas
	SchemaOptions
}

type PageData

type PageData struct {
	Items ztype.Maps `json:"items"`
	Page  PageInfo   `json:"page"`
	// contains filtered or unexported fields
}

func Pages

func Pages[T filter](
	m *Schema,
	page, pagesize int,
	filter T,
	fn ...func(*CondOptions),
) (*PageData, error)

func (*PageData) Map

func (p *PageData) Map(fn func(index int, item ztype.Map) ztype.Map, parallel ...uint) *PageData

type PageInfo

type PageInfo struct {
	zdb.Pages
}

type SQL

type SQL struct {
	Options SQLOptions
	// contains filtered or unexported fields
}

func (*SQL) Delete

func (s *SQL) Delete(table string, fields []string, filter ztype.Map, fn ...func(*CondOptions)) (int64, error)

func (*SQL) Find

func (s *SQL) Find(table string, fields []string, filter ztype.Map, fn ...func(*CondOptions)) (ztype.Maps, error)

func (*SQL) First

func (s *SQL) First(table string, fields []string, filter ztype.Map, fn ...func(*CondOptions)) (ztype.Map, error)

func (*SQL) GetDB

func (s *SQL) GetDB() *zdb.DB

func (*SQL) GetStorageType

func (s *SQL) GetStorageType() StorageType

func (*SQL) Insert

func (s *SQL) Insert(table string, fields []string, data ztype.Map, fn ...func(*InsertOptions)) (lastId interface{}, err error)

func (*SQL) InsertMany

func (s *SQL) InsertMany(table string, fields []string, data ztype.Maps, fn ...func(*InsertOptions)) (lastIds []interface{}, err error)

func (*SQL) Migration

func (s *SQL) Migration(model *Schema) Migrationer

func (*SQL) Pages

func (s *SQL) Pages(table string, fields []string, page, pagesize int, filter ztype.Map, fn ...func(*CondOptions)) (ztype.Maps, PageInfo, error)

func (*SQL) Update

func (s *SQL) Update(table string, fields []string, data ztype.Map, filter ztype.Map, fn ...func(*CondOptions)) (int64, error)

type SQLOptions

type SQLOptions struct {
	Prefix string
}

type Schema

type Schema struct {
	Storage Storageer

	Hashid *hashid.HashID `json:"-"`

	JSONPath string

	JSON []byte

	StorageType StorageType
	// contains filtered or unexported fields
}

func (*Schema) DI

func (m *Schema) DI() zdi.Injector

func (*Schema) DeCrypt

func (m *Schema) DeCrypt(row ztype.Map) (success bool)

DeCrypt 解密 ID

func (*Schema) DeCryptID

func (m *Schema) DeCryptID(nid string) (id string, err error)

DeCryptID 解密 ID

func (*Schema) EnCrypt

func (m *Schema) EnCrypt(row *ztype.Map) (err error)

EnCrypt 加密 ID

func (*Schema) EnCryptID

func (m *Schema) EnCryptID(id string) (nid string, err error)

EnCryptID 加密 ID

func (*Schema) GetAfterProcess

func (m *Schema) GetAfterProcess(p []string) (fn []afterProcess, err error)

func (*Schema) GetAlias

func (m *Schema) GetAlias() string

func (*Schema) GetBeforeProcess

func (m *Schema) GetBeforeProcess(p []string) (fn []beforeProcess, err error)

func (*Schema) GetComment

func (m *Schema) GetComment() string

func (*Schema) GetCryptProcess

func (m *Schema) GetCryptProcess(cryptName string) (fn CryptProcess, err error)

func (*Schema) GetDefine

func (m *Schema) GetDefine() schema.Schema

func (*Schema) GetExtend

func (m *Schema) GetExtend() ztype.Map

func (*Schema) GetField

func (m *Schema) GetField(name string) (mSchema.Field, bool)

func (*Schema) GetFields

func (m *Schema) GetFields(exclude ...string) []string

func (*Schema) GetModelFields

func (m *Schema) GetModelFields() mSchema.Fields

func (*Schema) GetName

func (m *Schema) GetName() string

func (*Schema) GetTableName

func (m *Schema) GetTableName() string

func (*Schema) GetViewFields

func (m *Schema) GetViewFields(view string) []string

func (*Schema) GetViews

func (m *Schema) GetViews() ztype.Map

func (*Schema) MarshalJSON

func (m *Schema) MarshalJSON() ([]byte, error)

func (*Schema) Migration

func (m *Schema) Migration() Migrationer

func (*Schema) Model

func (m *Schema) Model() *Model

func (*Schema) ParseLables

func (m *Schema) ParseLables(items ztype.Maps) ztype.Maps

type SchemaOptions

type SchemaOptions struct {
	DisabledMigrator bool `json:"disabled_migrator,omitempty"`
	SoftDeletes      bool `json:"soft_deletes,omitempty"`
	Timestamps       bool `json:"timestamps,omitempty"`
	CryptID          bool `json:"crypt_id,omitempty"`
}

type Schemas

type Schemas struct {
	SchemaOption SchemaOptions
	// contains filtered or unexported fields
}

func NewSchemas

func NewSchemas(di zdi.Injector, s Storageer, o SchemaOptions) *Schemas

func (*Schemas) BatchReg

func (ss *Schemas) BatchReg(models map[string]schema.Schema, force bool) error

func (*Schemas) ForEach

func (ss *Schemas) ForEach(fn func(key string, m *Schema) bool)

func (*Schemas) Get

func (ss *Schemas) Get(alias string) (*Schema, bool)

func (*Schemas) Models

func (ss *Schemas) Models() *Models

func (*Schemas) MustGet

func (ss *Schemas) MustGet(alias string) *Schema

func (*Schemas) Reg

func (ss *Schemas) Reg(name string, data schema.Schema, force bool) (*Schema, error)

func (*Schemas) Storage

func (ss *Schemas) Storage() Storageer

func (*Schemas) String

func (ss *Schemas) String() string

type StorageJoin

type StorageJoin struct {
	Table        string
	As           string
	Expr         string
	ModelOptions builder.JoinOption
}

type StorageModelOptions

type StorageModelOptions struct{}

type StorageType

type StorageType uint8
const (
	SQLStorage StorageType = iota + 1
	NoSQLStorage
)

func (StorageType) String

func (s StorageType) String() string

type Storageer

type Storageer interface {
	GetStorageType() StorageType
	Find(table string, fields []string, filter ztype.Map, fn ...func(*CondOptions)) (ztype.Maps, error)
	First(table string, fields []string, filter ztype.Map, fn ...func(*CondOptions)) (ztype.Map, error)
	Pages(table string, fields []string, page, pagesize int, filter ztype.Map, fn ...func(*CondOptions)) (ztype.Maps, PageInfo, error)
	Migration(model *Schema) Migrationer
	Insert(table string, fields []string, data ztype.Map, fn ...func(*InsertOptions)) (lastId interface{}, err error)
	InsertMany(table string, fields []string, data ztype.Maps, fn ...func(*InsertOptions)) (lastIds []interface{}, err error)
	Delete(table string, fields []string, filter ztype.Map, fn ...func(*CondOptions)) (int64, error)
	Update(table string, fields []string, data ztype.Map, filter ztype.Map, fn ...func(*CondOptions)) (int64, error)
}

func NewSQL

func NewSQL(db *zdb.DB, o ...func(*SQLOptions)) Storageer

type ValidTriggerType

type ValidTriggerType uint
const (
	ValidTriggerAll ValidTriggerType = iota
	ValidTriggerCreate
	ValidTriggerUpdate
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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