model

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

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

Go to latest
Published: Nov 27, 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 *Store) 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 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) (*Store, bool)

func (*Module) GetSchema

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

func (*Module) MustGetModel

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

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) Stores

func (m *Module) Stores() *Stores

func (*Module) String

func (m *Module) String() string

type Options

type Options struct {
	// SetStorageer 手动设置数据库
	SetStorageer func() (Storageer, error)
	// SetAlternateModels 动态设置关联表
	SetAlternateModels func() ([]*Store, error)
	// SchemaMiddleware 自定义 schema 中间件
	SchemaMiddleware func() []znet.Handler
	// Prefix 模型前缀
	Prefix string
	// SchemaDir 模型定义目录
	SchemaDir string
	// SchemaApi schema api 路径
	SchemaApi string
	// Schemas 定义模型
	Schemas schema.Schemas
	// SchemaOptions 模型选项
	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) GetOptions

func (s *SQL) GetOptions() ztype.Map

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) Transaction

func (s *SQL) Transaction(run func(s *SQL) error) (err 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 {
	// contains filtered or unexported fields
}

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() *Store

func (*Schema) ParseLables

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

type SchemaOptions

type SchemaOptions struct {
	// DisabledMigrator 禁用自动迁移
	DisabledMigrator bool `z:"disabled_migrator,omitempty"`
	// SoftDeletes 开启软删除
	SoftDeletes bool `z:"soft_deletes,omitempty"`
	// Timestamps 注入创建/更新时间
	Timestamps bool `z:"timestamps,omitempty"`
	// CryptID 加密 ID
	CryptID bool `z:"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() *Stores

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
	GetOptions() ztype.Map
	Transaction(run func(s *SQL) error) (err error)
	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, tablePrefix string, o ...func(*SQLOptions)) Storageer

type Store

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

func (*Store) Count

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

Count 统计数量

func (*Store) Delete

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

Delete 删除数据

func (*Store) DeleteByID

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

DeleteByID 通过ID删除数据

func (*Store) DeleteMany

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

DeleteMany 删除多条数据

func (*Store) Exists

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

Exists 数据是否存在

func (*Store) Find

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

Find 查询数据

func (*Store) FindCols

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

FindCols 查询指定字段

func (*Store) FindOne

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

FindOne 查询一条数据

func (*Store) FindOneByID

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

FindOneByID 通过ID查询

func (*Store) Insert

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

Insert 插入数据

func (*Store) InsertMany

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

InsertMany 批量插入数据

func (*Store) Pages

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

Pages 分页查询

func (*Store) Schema

func (o *Store) Schema(Storage ...Storageer) *Schema

Schema 获取模型

func (*Store) Update

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

Update 更新数据

func (*Store) UpdateByID

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

UpdateByID 通过ID更新

func (*Store) UpdateMany

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

UpdateMany 更新多条数据

type Stores

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

Stores 快捷操作

func (*Stores) All

func (m *Stores) All() (models []*Store)

All 全部模型

func (*Stores) Get

func (m *Stores) Get(name string) (*Store, bool)

Get 获取操作对象

func (*Stores) MustGet

func (m *Stores) MustGet(name string) *Store

MustGet 获取操作对象

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