entdbadapter

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

RelMaps map the relation type to the ent relation type

Functions

func CreateDBDSN

func CreateDBDSN(config *app.DBConfig) string

CreateDBDSN create a DSN string for the database connection

func CreateDebugFN

func CreateDebugFN(config *app.DBConfig) func(ctx context.Context, i ...any)

func GetEntDialect

func GetEntDialect(config *app.DBConfig) (string, error)

func NOW

func NOW(dialect string) any

func NewClient

func NewClient(config *app.DBConfig, schemaBuilder *schema.Builder) (_ app.DBClient, err error)

NewClient creates a new ent client

func NewDBAdapter

func NewDBAdapter(
	config *app.DBConfig,
	schemaBuilder *schema.Builder,
) (app.DBClient, error)

func NewEntClient

func NewEntClient(
	config *app.DBConfig,
	schemaBuilder *schema.Builder,
	useEntDrivers ...*dialectSql.Driver,
) (_ app.DBClient, err error)

NewEntClient creates a new ent client

func NewTestClient added in v0.0.5

func NewTestClient(
	migrationDir string,
	schemaBuilder *schema.Builder,
	hookFns ...func() *app.Hooks,
) (_ app.DBClient, err error)

NewTestClient creates a new ent client with sqlite memory

Types

type Adapter

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

Adapter is the ent adapter for app.Client

func (*Adapter) Close

func (d *Adapter) Close() error

Close closes the underlying driver.

func (*Adapter) Commit

func (d *Adapter) Commit() error

Commit commits the transaction.

func (*Adapter) Config

func (d *Adapter) Config() *app.DBConfig

func (*Adapter) CreateDBModel

func (d *Adapter) CreateDBModel(s *schema.Schema, relations ...*schema.Relation) app.Model

func (*Adapter) CreateModel

func (d *Adapter) CreateModel(s *schema.Schema, relations ...*schema.Relation) *Model

CreateModel create a new model from schema

func (*Adapter) DB

func (d *Adapter) DB() *sql.DB

func (*Adapter) Dialect

func (d *Adapter) Dialect() string

Dialect returns the dialect name.

func (*Adapter) Driver

func (d *Adapter) Driver() dialect.Driver

Driver returns the underlying driver.

func (*Adapter) Exec

func (d *Adapter) Exec(
	ctx context.Context,
	query string,
	args,
	bindValue any,
) error

Exec executes the query and bind the values to bindValue.

func (*Adapter) Hooks

func (d *Adapter) Hooks() *app.Hooks

func (*Adapter) IsTx

func (d *Adapter) IsTx() bool

IsTx returns true if the client is a transaction.

func (*Adapter) Migrate

func (d *Adapter) Migrate(
	migration *app.Migration,
	appendEntTables ...*entSchema.Table,
) (err error)

func (*Adapter) Model

func (d *Adapter) Model(name string) (app.Model, error)

Model return the model object for query and mutation.

func (*Adapter) NewEdgeSpec

func (d *Adapter) NewEdgeSpec(
	r *schema.Relation,
	nodeIDs []driver.Value,
) (*sqlgraph.EdgeSpec, error)

NewEdgeSpec create a new edge spec

func (*Adapter) NewEdgeStepOption

func (d *Adapter) NewEdgeStepOption(r *schema.Relation) (sqlgraph.StepOption, error)

NewEdgeStep create a new edge step

func (*Adapter) Reload

func (d *Adapter) Reload(
	newSchemaBuilder *schema.Builder,
	migration *app.Migration,
) (_ app.DBClient, err error)

func (*Adapter) Rollback

func (d *Adapter) Rollback() error

Rollback rollbacks the transaction.

func (*Adapter) SchemaBuilder

func (d *Adapter) SchemaBuilder() *schema.Builder

SchemaBuilder returns the schema builder.

func (*Adapter) SetDriver added in v0.0.5

func (d *Adapter) SetDriver(driver dialect.Driver)

func (*Adapter) SetSQLDB added in v0.0.5

func (d *Adapter) SetSQLDB(db *sql.DB)

func (*Adapter) Tx

func (d *Adapter) Tx(ctx context.Context) (app.DBClient, error)

Tx creates a new transaction.

type Column

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

Column is a wrapper around a schema field and an ent column

type EntAdapter added in v0.0.5

type EntAdapter interface {
	NewEdgeSpec(
		r *schema.Relation,
		nodeIDs []driver.Value,
	) (*sqlgraph.EdgeSpec, error)
	NewEdgeStepOption(r *schema.Relation) (sqlgraph.StepOption, error)
	Reload(
		newSchemaBuilder *schema.Builder,
		migration *app.Migration,
	) (_ app.DBClient, err error)
	Driver() dialect.Driver
	CreateDBModel(s *schema.Schema, relations ...*schema.Relation) app.Model
	Close() error
	Commit() error
	Rollback() error
	Config() *app.DBConfig
	DB() *sql.DB
	Dialect() string
	Exec(
		ctx context.Context,
		query string,
		args,
		bindValue any,
	) error
	Hooks() *app.Hooks
	IsTx() bool
	Model(name string) (app.Model, error)
	SchemaBuilder() *schema.Builder
	Tx(ctx context.Context) (app.DBClient, error)
	Migrate(
		migration *app.Migration,
		appendEntTables ...*entSchema.Table,
	) (err error)
	SetSQLDB(db *sql.DB)
	SetDriver(driver dialect.Driver)
}

type Model

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

Model holds the model data

func (*Model) Clone

func (m *Model) Clone() app.Model

func (*Model) Column

func (m *Model) Column(name string) (*Column, error)

Column returns a column by name

func (*Model) Create

func (m *Model) Create(e *schema.Entity) (_ uint64, err error)

Create creates a new entity

func (*Model) CreateFromJSON

func (m *Model) CreateFromJSON(json string) (_ uint64, err error)

CreateFromJSON creates a new entity from JSON

func (*Model) DBColumns

func (m *Model) DBColumns() []string

DBColumns returns the database columns

func (*Model) GetEntTable

func (m *Model) GetEntTable() *entSchema.Table

func (*Model) Mutation

func (m *Model) Mutation(skipTxs ...bool) app.Mutation

Mutation returns a new mutation builder for the model

func (*Model) Name

func (m *Model) Name() string

func (*Model) Query

func (m *Model) Query(predicates ...*app.Predicate) app.Query

Query returns a new query builder for the model

func (*Model) Schema

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

Schema returns the schema of the model

func (*Model) SetClient

func (m *Model) SetClient(client app.DBClient) app.Model

type Mutation

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

Mutation holds the entity mutation data

func (*Mutation) Create

func (m *Mutation) Create(e *schema.Entity) (_ uint64, err error)

Create creates a new entity in the database

func (*Mutation) Delete

func (m *Mutation) Delete() (affected int, err error)

Delete deletes entities from the database

func (*Mutation) GetRelationEntityIDs

func (m *Mutation) GetRelationEntityIDs(fieldName string, fieldValue any) ([]driver.Value, error)

GetRelationEntityIDs return the relation IDs from the given field value

func (*Mutation) ProcessFieldAdd

func (m *Mutation) ProcessFieldAdd(entAdapter EntAdapter, k string, v any) error

ProcessFieldAdd processes a field in the $add block

func (*Mutation) ProcessFieldClear

func (m *Mutation) ProcessFieldClear(entAdapter EntAdapter, k string, v any) error

ProcessFieldClear processes a field in the $clear block

func (*Mutation) ProcessFieldExpr

func (m *Mutation) ProcessFieldExpr(entAdapter EntAdapter, k string, v any) error

ProcessFieldExpr processes a field in the $expr block

func (*Mutation) ProcessUpdateBlockAdd

func (m *Mutation) ProcessUpdateBlockAdd(entAdapter EntAdapter, fieldValue any) error

ProcessUpdateBlockAdd processes the $add block

func (*Mutation) ProcessUpdateBlockClear

func (m *Mutation) ProcessUpdateBlockClear(entAdapter EntAdapter, fieldValue any) error

ProcessUpdateBlockClear processes the $clear block

func (*Mutation) ProcessUpdateBlockExpr

func (m *Mutation) ProcessUpdateBlockExpr(entAdapter EntAdapter, fieldValue any) error

ProcessUpdateBlockExpr processes the $expr block

func (*Mutation) ProcessUpdateBlockSet

func (m *Mutation) ProcessUpdateBlockSet(entAdapter EntAdapter, fieldValue any) error

ProcessUpdateBlockSet processes the $set block

func (*Mutation) ProcessUpdateFieldSet

func (m *Mutation) ProcessUpdateFieldSet(entAdapter EntAdapter, k string, v any) error

ProcessUpdateFieldSet processes a field in the $set block

func (*Mutation) Update

func (m *Mutation) Update(e *schema.Entity) (affected int, err error)

Update updates the entity and returns the updated entity

func (*Mutation) Where

func (m *Mutation) Where(predicates ...*app.Predicate) app.Mutation

Where adds a predicate to the mutation

type PredicateFN

type PredicateFN func(*sql.Selector) *sql.Predicate

func CreateFieldPredicate added in v0.0.5

func CreateFieldPredicate(predicate *app.Predicate) (PredicateFN, error)

CreateFieldPredicate convert a predicate to ent predicate

type Query

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

func (*Query) Count

func (q *Query) Count(options *app.CountOption, ctxs ...context.Context) (int, error)

Count returns the number of entities that match the query.

func (*Query) First

func (q *Query) First(ctxs ...context.Context) (*schema.Entity, error)

First returns the first entity that matches the query.

func (*Query) Get

func (q *Query) Get(ctxs ...context.Context) ([]*schema.Entity, error)

Get returns the list of entities that match the query.

func (*Query) Limit

func (q *Query) Limit(limit uint) app.Query

Limit sets the limit of the query.

func (*Query) Offset

func (q *Query) Offset(offset uint) app.Query

Offset sets the offset of the query.

func (*Query) Only

func (q *Query) Only(ctxs ...context.Context) (*schema.Entity, error)

Only returns the matched entity or an error if there is more than one.

func (*Query) Options

func (q *Query) Options() *app.QueryOption

func (*Query) Order

func (q *Query) Order(order ...string) app.Query

Order sets the order of the query.

func (*Query) Select

func (q *Query) Select(fields ...string) app.Query

Select sets the columns of the query.

func (*Query) Where

func (q *Query) Where(predicates ...*app.Predicate) app.Query

Where adds the given predicates to the query.

type Tx

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

Tx hold the transaction and the schema manager.

func NewTx

func NewTx(ctx context.Context, client app.DBClient) (*Tx, error)

NewTx creates a new transaction.

func (*Tx) Close

func (tx *Tx) Close() error

Close closes the transaction.

func (*Tx) Commit

func (tx *Tx) Commit() error

Commit commits the transaction.

func (*Tx) Config

func (tx *Tx) Config() *app.DBConfig

func (*Tx) CreateDBModel

func (tx *Tx) CreateDBModel(s *schema.Schema, relations ...*schema.Relation) app.Model

CreateDBModel creates a new model from the schema.

func (*Tx) DB

func (tx *Tx) DB() *sql.DB

func (*Tx) Dialect

func (tx *Tx) Dialect() string

Dialect returns the dialect name.

func (*Tx) Driver

func (tx *Tx) Driver() dialect.Driver

Driver returns the underlying driver.

func (*Tx) Exec

func (tx *Tx) Exec(ctx context.Context, query string, args any, bindValue any) error

Exec executes a query.

func (*Tx) Hooks

func (tx *Tx) Hooks() *app.Hooks

func (*Tx) IsTx

func (tx *Tx) IsTx() bool

IsTx returns true if the client is a transaction.

func (*Tx) Migrate added in v0.0.5

func (tx *Tx) Migrate(migration *app.Migration, appendEntTables ...*entSchema.Table) (err error)

func (*Tx) Model

func (tx *Tx) Model(name string) (app.Model, error)

Model returns the model by name.

func (*Tx) NewEdgeSpec

func (tx *Tx) NewEdgeSpec(r *schema.Relation, nodeIDs []driver.Value) (*sqlgraph.EdgeSpec, error)

func (*Tx) NewEdgeStepOption

func (tx *Tx) NewEdgeStepOption(r *schema.Relation) (sqlgraph.StepOption, error)

func (*Tx) Reload

func (tx *Tx) Reload(newSchemaBuilder *schema.Builder, migration *app.Migration) (app.DBClient, error)

Reload reloads the schema.

func (*Tx) Rollback

func (tx *Tx) Rollback() error

Rollback rollbacks the transaction.

func (*Tx) SchemaBuilder

func (tx *Tx) SchemaBuilder() *schema.Builder

SchemaBuilder returns the schema builder.

func (*Tx) SetDriver added in v0.0.5

func (tx *Tx) SetDriver(driver dialect.Driver)

func (*Tx) SetSQLDB added in v0.0.5

func (tx *Tx) SetSQLDB(db *sql.DB)

func (*Tx) Tx

func (tx *Tx) Tx(ctx context.Context) (t app.DBClient, err error)

Tx returns the transaction.

type TxDriver

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

TxDriver is the driver for transaction.

func (*TxDriver) Close

func (tx *TxDriver) Close() error

func (*TxDriver) Commit

func (tx *TxDriver) Commit() error

func (*TxDriver) Dialect

func (tx *TxDriver) Dialect() string

func (*TxDriver) Exec

func (tx *TxDriver) Exec(ctx context.Context, query string, args, v any) error

Exec calls tx.Exec.

func (*TxDriver) ID

func (tx *TxDriver) ID() string

ID returns the transaction id.

func (*TxDriver) Query

func (tx *TxDriver) Query(ctx context.Context, query string, args, v any) error

Query calls tx.Query.

func (*TxDriver) Rollback

func (tx *TxDriver) Rollback() error

func (*TxDriver) Tx

func (tx *TxDriver) Tx(context.Context) (dialect.Tx, error)

Jump to

Keyboard shortcuts

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