Documentation
¶
Index ¶
- Variables
- func BindStruct(src any, target any) error
- func Create[T any](ctx context.Context, client Client, dataCreate any, schemas ...string) (T, error)
- func CreateFromJSON[T any](ctx context.Context, client Client, json string, schemas ...string) (T, error)
- func Delete[T any](ctx context.Context, client Client, predicates []*Predicate, schemas ...string) (int, error)
- func Exec(ctx context.Context, client Client, query string, args ...any) (sql.Result, error)
- func IsNotFound(err error) bool
- func RawQuery[T any](ctx context.Context, client Client, query string, args ...any) (ts []T, err error)
- func Update[T any](ctx context.Context, client Client, dataUpdate any, predicates []*Predicate, ...) ([]T, error)
- type Client
- type Config
- type CountOption
- type DBMutation
- func (m *DBMutation[T]) Create(ctx context.Context, dataCreate any) (t T, err error)
- func (m *DBMutation[T]) CreateFromJSON(ctx context.Context, json string) (t T, err error)
- func (m *DBMutation[T]) Delete(ctx context.Context) (affected int, err error)
- func (q *DBMutation[T]) Model() (Model, error)
- func (m *DBMutation[T]) Update(ctx context.Context, updateData any) (ts []T, err error)
- func (m *DBMutation[T]) Where(predicates ...*Predicate) *DBMutation[T]
- type DBQuery
- func (q *DBQuery[T]) Count(ctx context.Context, options *CountOption) (int, error)
- func (q *DBQuery[T]) First(ctx context.Context) (t T, err error)
- func (q *DBQuery[T]) Get(ctx context.Context) ([]T, error)
- func (q *DBQuery[T]) Limit(limit uint) *DBQuery[T]
- func (q *DBQuery[T]) Model() (Model, error)
- func (q *DBQuery[T]) Offset(offset uint) *DBQuery[T]
- func (q *DBQuery[T]) Only(ctx context.Context) (t T, err error)
- func (q *DBQuery[T]) Order(order ...string) *DBQuery[T]
- func (q *DBQuery[T]) Select(fields ...string) *DBQuery[T]
- func (q *DBQuery[T]) Where(predicates ...*Predicate) *DBQuery[T]
- type Hooks
- type Migration
- type Model
- type Mutator
- type NotFoundError
- type OperatorType
- type PostDBCreate
- type PostDBDelete
- type PostDBGet
- type PostDBUpdate
- type Predicate
- func And(predicates ...*Predicate) *Predicate
- func CreatePredicatesFromFilterObject(sb *schema.Builder, s *schema.Schema, filterObject string) ([]*Predicate, error)
- func EQ(field string, value any, relationFields ...string) *Predicate
- func GT(field string, value any, relationFields ...string) *Predicate
- func GTE(field string, value any, relationFields ...string) *Predicate
- func In(field string, values []any, relationFields ...string) *Predicate
- func IsFalse(field string, relationFields ...string) *Predicate
- func IsTrue(field string, relationFields ...string) *Predicate
- func LT(field string, value any, relationFields ...string) *Predicate
- func LTE(field string, value any, relationFields ...string) *Predicate
- func Like(field string, value string, relationFields ...string) *Predicate
- func NEQ(field string, value any, relationFields ...string) *Predicate
- func NotIn(field string, values []any, relationFields ...string) *Predicate
- func Null(field string, value bool, relationFields ...string) *Predicate
- func Or(predicates ...*Predicate) *Predicate
- type Querier
- type QueryOption
- type RenameItem
Constants ¶
This section is empty.
Variables ¶
var SupportDrivers = []string{"mysql", "pgx", "sqlite"}
SupportDrivers returns list of supported drivers.
Functions ¶
func BindStruct ¶ added in v0.1.0
func Create ¶ added in v0.1.0
func Create[T any]( ctx context.Context, client Client, dataCreate any, schemas ...string, ) (T, error)
Create is a shortcut to mutation.Create
func CreateFromJSON ¶ added in v0.1.0
func CreateFromJSON[T any]( ctx context.Context, client Client, json string, schemas ...string, ) (T, error)
CreateFromJSON is a shortcut to mutation.CreateFromJSON
func Delete ¶ added in v0.1.0
func Delete[T any]( ctx context.Context, client Client, predicates []*Predicate, schemas ...string, ) (int, error)
Delete is a shortcut to mutation.Delete
func IsNotFound ¶
Types ¶
type Client ¶
type Client interface {
Dialect() string
// Exec executes a query that does not return records. For example, in SQL, INSERT or UPDATE.
// It return a sql.Result and an error if any.
Exec(ctx context.Context, query string, args any) (sql.Result, error)
// Query executes a query that returns rows, typically a SELECT in SQL.
// It return a slice of *schema.Entity and an error if any.
Query(ctx context.Context, query string, args any) ([]*schema.Entity, error)
Rollback() error
Commit() error
CreateDBModel(s *schema.Schema, rs ...*schema.Relation) Model
Tx(ctx context.Context) (Client, error)
IsTx() bool
Model(name string, types ...any) (Model, error)
Close() error
SchemaBuilder() *schema.Builder
Reload(ctx context.Context, newSchemaBuilder *schema.Builder, migration *Migration) (Client, error)
DB() *sql.DB
Config() *Config
Hooks() *Hooks
}
type Config ¶ added in v0.1.0
type CountOption ¶
type DBMutation ¶ added in v0.1.0
type DBMutation[T any] struct { // contains filtered or unexported fields }
func Mutation ¶
func Mutation[T any](client Client, schemas ...string) *DBMutation[T]
Mutation is a helper function to create a new mutation for a given schema
The schema is inferred from the type of the entity T must be a pointer to a struct or a struct type
func (*DBMutation[T]) Create ¶ added in v0.1.0
func (m *DBMutation[T]) Create(ctx context.Context, dataCreate any) (t T, err error)
Create creates a new entity and return the newly created entity
func (*DBMutation[T]) CreateFromJSON ¶ added in v0.1.0
func (m *DBMutation[T]) CreateFromJSON(ctx context.Context, json string) (t T, err error)
CreateFromJSON creates a new entity from JSON
func (*DBMutation[T]) Delete ¶ added in v0.1.0
func (m *DBMutation[T]) Delete(ctx context.Context) (affected int, err error)
Delete deletes entities from the database
func (*DBMutation[T]) Model ¶ added in v0.1.0
func (q *DBMutation[T]) Model() (Model, error)
Model returns the actual model of the mutation.
func (*DBMutation[T]) Update ¶ added in v0.1.0
func (m *DBMutation[T]) Update(ctx context.Context, updateData any) (ts []T, err error)
Update updates the entity and returns the updated entities
func (*DBMutation[T]) Where ¶ added in v0.1.0
func (m *DBMutation[T]) Where(predicates ...*Predicate) *DBMutation[T]
Where adds a predicate to the mutation
type DBQuery ¶ added in v0.1.0
type DBQuery[T any] struct { // contains filtered or unexported fields }
func (*DBQuery[T]) Count ¶ added in v0.1.0
Count returns the number of entities that match the query.
func (*DBQuery[T]) Only ¶ added in v0.1.0
Only returns the matched entity or an error if there is more than one.
type Hooks ¶
type Hooks struct {
PostDBGet []PostDBGet
PostDBCreate []PostDBCreate
PostDBUpdate []PostDBUpdate
PostDBDelete []PostDBDelete
}
type Migration ¶
type Migration struct {
Dir string
RenameTables []*RenameItem
RenameFields []*RenameItem
}
type Mutator ¶ added in v0.1.0
type Mutator interface {
Where(predicates ...*Predicate) Mutator
GetRelationEntityIDs(fieldName string, fieldValue any) ([]driver.Value, error)
Create(ctx context.Context, e *schema.Entity) (id uint64, err error)
Update(ctx context.Context, e *schema.Entity) (affected int, err error)
Delete(ctx context.Context) (affected int, err error)
}
type NotFoundError ¶
type NotFoundError struct {
Message string
}
func (*NotFoundError) Error ¶
func (e *NotFoundError) Error() string
type OperatorType ¶
type OperatorType int
OperatorType is the type of the operator
const ( OpInvalid OperatorType = iota OpEQ OpNEQ OpGT OpGTE OpLT OpLTE OpLIKE OpIN OpNIN OpNULL )
func (OperatorType) MarshalJSON ¶
func (t OperatorType) MarshalJSON() ([]byte, error)
MarshalJSON marshal an enum value to the quoted json string value
func (OperatorType) String ¶
func (t OperatorType) String() string
String returns the string representation of a type.
func (*OperatorType) UnmarshalJSON ¶
func (t *OperatorType) UnmarshalJSON(b []byte) error
UnmarshalJSON unmashals a quoted json string to the enum value
func (OperatorType) Valid ¶
func (t OperatorType) Valid() bool
Valid reports if the given type if known type.
type PostDBCreate ¶ added in v0.3.0
type PostDBDelete ¶ added in v0.3.0
type PostDBUpdate ¶ added in v0.3.0
type Predicate ¶
type Predicate struct {
Field string
Operator OperatorType
Value any
RelationFieldNames []string
And []*Predicate
Or []*Predicate
}
func CreatePredicatesFromFilterObject ¶
func CreatePredicatesFromFilterObject( sb *schema.Builder, s *schema.Schema, filterObject string, ) ([]*Predicate, error)
CreatePredicateFromFilterObject creates a predicate from a filter object A filter object is a JSON object that contains the filter for the query E.g.
{
"approved": true,
"status": "online",
"name": {
"$like": "test%",
"$neq": "test2"
},
"age": {
"$gt": 1,
"$lt": 10
},
"$or": [
{
"age": 1
},
{
"bio": {
"$like": "test%",
"$neq": "test2"
}
},
{
"status": "offline"
},
{
"$and": [
{
"bio": {
"$neq": "test",
"$like": "%a"
}
},
{
"age": {
"$gt": 1
}
}
]
}
]
}
will be converted to "entgo.io/ent/dialect/sql" sql.And(
sql.EQ("approved", true),
sql.EQ("status", "online"),
sql.And(
sql.Like("name", "test%"),
sql.NEQ("name", "test2"),
),
sql.And(
sql.GT("age", 1),
sql.LT("age", 10),
),
sql.Or(
sql.EQ("age", 1),
sql.And(
sql.Like("bio", "test%"),
sql.NEQ("bio", "test2"),
),
sql.EQ("status", "offline"),
sql.And(
sql.NEQ("bio", "test"),
sql.Like("bio", "%a"),
sql.GT("age", 1),
),
),
)
type Querier ¶ added in v0.1.0
type Querier interface {
Where(predicates ...*Predicate) Querier
Limit(limit uint) Querier
Offset(offset uint) Querier
Select(columns ...string) Querier
Order(order ...string) Querier
Count(ctx context.Context, options *CountOption) (int, error)
Get(ctx context.Context) ([]*schema.Entity, error)
First(ctx context.Context) (*schema.Entity, error)
Only(ctx context.Context) (*schema.Entity, error)
Options() *QueryOption
}
type QueryOption ¶ added in v0.1.0
type RenameItem ¶
type RenameItem struct {
Type string `json:"type"` // "column" or "table"
From string `json:"from"`
To string `json:"to"`
IsJunctionTable bool `json:"is_junction_table,omitempty"` // use in rename table: If the table is a junction table
SchemaName string `json:"schema,omitempty"` // use in rename column: The schema name of the column
SchemaNamespace string `json:"schema_namespace,omitempty"` // use in rename column: The schema name of the column
}