entx

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2024 License: Apache-2.0 Imports: 20 Imported by: 3

README

Build status Quality Gate Status

entx

A helper package for working with ent, which includes the following high-level utilities:

  • Mixin used for assigning a different ID type than the one generated by ent (we use ULIDs)
  • Mixin used for assigning some default columns (created_at, created_by, etc. - check out the mixin directory for more details)
  • A vanilla, drop-in, setup for using ent with our standard tool chains gqlgen, gqlgenc, and some other helpers
  • Soft-delete extension with cascade delete functionality added in
  • Multi-driver support for various databases
  • SQLite connection interface management

Usage

When using the generated graphql schemas, you must have the Upload scaler defined, which are used for the file upload resolvers:

scalar Upload

Documentation

Overview

Package entx is a package of tools for interacting with ent

Index

Constants

View Source
const (
	DefaultCacheTTL = 1 * time.Second
)

Variables

View Source
var CascadeAnnotationName = "DATUM_CASCADE"

CascadeAnnotationName is a name for our cascading delete annotation

View Source
var CascadeThroughAnnotationName = "DATUM_CASCADE_THROUGH"

CascadeThroughAnnotationName is a name for our cascading through edge delete annotation

View Source
var (
	// ErrUnsupportedDialect is returned when an unsupported dialect is used
	ErrUnsupportedDialect = errors.New("unsupported dialect")
)
View Source
var QueryGenAnnotationName = "DATUM_QUERYGEN"

QueryGenAnnotationName is a name for our graphql query generation annotation

View Source
var SchemaGenAnnotationName = "DATUM_SCHEMAGEN"

SchemaGenAnnotationName is a name for our graphql schema generation annotation

Functions

func CheckEntDialect added in v0.0.2

func CheckEntDialect(d string) (string, error)

CheckEntDialect checks if the dialect is supported and returns the ent dialect corresponding to the given dialect

func CheckIsSoftDelete

func CheckIsSoftDelete(ctx context.Context) bool

CheckIsSoftDelete checks if the softDeleteKey is set in the context

func CheckMultiwriteSupport added in v0.0.2

func CheckMultiwriteSupport(d string) bool

CheckMultiwriteSupport checks if the dialect supports multiwrite

func CheckSkipSoftDelete

func CheckSkipSoftDelete(ctx context.Context) bool

CheckSkipSoftDelete checks if the SoftDeleteSkipKey is set in the context

func Healthcheck added in v0.0.2

func Healthcheck(client *entsql.Driver) func(ctx context.Context) error

Healthcheck pings the DB to check if the connection is working

func IsSoftDelete

func IsSoftDelete(parent context.Context) context.Context

IsSoftDelete returns a new context that informs the delete is a soft-delete for interceptor/hooks.

func MarshalRawMessage

func MarshalRawMessage(t json.RawMessage) graphql.Marshaler

MarshalRawMessage provides a graphql.Marshaler for json.RawMessage

func SkipSoftDelete

func SkipSoftDelete(parent context.Context) context.Context

SkipSoftDelete returns a new context that skips the soft-delete interceptor/hooks.

func UnmarshalRawMessage

func UnmarshalRawMessage(v interface{}) (json.RawMessage, error)

UnmarshalRawMessage provides a graphql.Unmarshaler for json.RawMessage

Types

type CascadeAnnotation

type CascadeAnnotation struct {
	Field string
}

CascadeAnnotation is an annotation used to indicate that an edge should be cascaded

func CascadeAnnotationField

func CascadeAnnotationField(fieldname string) *CascadeAnnotation

CascadeAnnotationField sets the field name of the edge containing the ID of a record from the current schema

func (*CascadeAnnotation) Decode added in v0.3.1

func (a *CascadeAnnotation) Decode(annotation interface{}) error

Decode unmarshalls the CascadeAnnotation

func (CascadeAnnotation) Name

func (a CascadeAnnotation) Name() string

Name returns the name of the CascadeAnnotation

type CascadeThroughAnnotation

type CascadeThroughAnnotation struct {
	Schemas []ThroughCleanup
}

CascadeThroughAnnotation is an annotation used to indicate that an edge should be cascaded through

func CascadeThroughAnnotationField

func CascadeThroughAnnotationField(schemas []ThroughCleanup) *CascadeThroughAnnotation

CascadeThroughAnnotationField sets the field name of the edge containing the ID of a record from the current schema

func (*CascadeThroughAnnotation) Decode added in v0.3.1

func (a *CascadeThroughAnnotation) Decode(annotation interface{}) error

Decode unmarshalls the CascadeThroughAnnotation

func (CascadeThroughAnnotation) Name

Name returns the name of the CascadeThroughAnnotation

type Config added in v0.0.2

type Config struct {
	// Debug to print debug database logs
	Debug bool `json:"debug" koanf:"debug" jsonschema:"description=debug enables printing the debug database logs" default:"false"`
	// DatabaseName is the name of the database to use with otel tracing
	DatabaseName string `` /* 131-byte string literal not displayed */
	// DriverName name from dialect.Driver
	DriverName string `` /* 146-byte string literal not displayed */
	// MultiWrite enabled writing to two databases simultaneously
	MultiWrite bool `json:"multiWrite" koanf:"multiWrite" jsonschema:"description=enables writing to two databases simultaneously" default:"false"`
	// PrimaryDBSource is the primary database source for all read and write operations
	PrimaryDBSource string `` /* 132-byte string literal not displayed */
	// SecondaryDBSource for when multi write is enabled
	SecondaryDBSource string `` /* 156-byte string literal not displayed */
	// CacheTTL to have results cached for subsequent requests
	CacheTTL time.Duration `json:"cacheTTL" koanf:"cacheTTL" jsonschema:"description=cache results for subsequent requests, defaults to 1s" default:"1s"`
	// RunMigrations to run migrations on startup
	RunMigrations bool `json:"runMigrations" koanf:"runMigrations" jsonschema:"description=run migrations on startup" default:"true"`
	// MigrationProvider to use for running migrations
	MigrationProvider string `` /* 140-byte string literal not displayed */
	// EnableHistory to enable history data to be logged to the database
	EnableHistory bool `` /* 132-byte string literal not displayed */
}

Config Settings for the ent database client

type DBOption added in v0.0.2

type DBOption func(opts *EntClientConfig)

DBOption allows users to optionally supply configuration to the ent connection

func WithLogger added in v0.0.2

func WithLogger(l *zap.SugaredLogger) DBOption

WithLogger sets the logger for the ent client

func WithSecondaryDB added in v0.0.2

func WithSecondaryDB() DBOption

WithSecondaryDB sets the secondary db connection if the driver supports multiwrite

type EntClientConfig added in v0.0.2

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

EntClientConfig configures the entsql drivers

func NewDBConfig added in v0.0.2

func NewDBConfig(c Config, opts ...DBOption) (*EntClientConfig, error)

NewDBConfig returns a new ent database configuration

func (*EntClientConfig) GetPrimaryDB added in v0.0.2

func (c *EntClientConfig) GetPrimaryDB() *entsql.Driver

GetPrimaryDB returns the primary database configuration

func (*EntClientConfig) GetSecondaryDB added in v0.0.2

func (c *EntClientConfig) GetSecondaryDB() *entsql.Driver

GetSecondaryDB returns the secondary db connection

func (*EntClientConfig) NewEntDB added in v0.0.2

func (c *EntClientConfig) NewEntDB(dataSource string) (*entsql.Driver, error)

NewEntDB creates a new ent database connection

type Extension

type Extension struct {
	entc.DefaultExtension
	// contains filtered or unexported fields
}

Extension is an implementation of entc.Extension

func NewExtension

func NewExtension(opts ...ExtensionOption) (*Extension, error)

NewExtension returns an entc Extension that allows the entx package to generate the schema changes and templates needed to function

func (*Extension) GQLSchemaHooks

func (e *Extension) GQLSchemaHooks() []entgql.SchemaHook

GQLSchemaHooks of the extension to seamlessly edit the final gql interface

func (*Extension) Templates

func (e *Extension) Templates() []*gen.Template

Templates of the extension

type ExtensionOption

type ExtensionOption func(*Extension) error

ExtensionOption allow for control over the behavior of the generator

func WithJSONScalar

func WithJSONScalar() ExtensionOption

WithJSONScalar adds the JSON scalar definition

type MultiWriteDriver added in v0.0.2

type MultiWriteDriver struct {
	// Wp (write-primary), Ws (write-secondary) Drivers
	Wp, Ws dialect.Driver
}

MultiWriteDriver allows you to write to a primary and secondary database

func (*MultiWriteDriver) BeginTx added in v0.0.2

func (d *MultiWriteDriver) BeginTx(ctx context.Context, opts *sql.TxOptions) (dialect.Tx, error)

BeginTx adds an log-id for the transaction and calls the underlying driver BeginTx command if it is supported.

func (*MultiWriteDriver) Close added in v0.0.2

func (d *MultiWriteDriver) Close() error

Close the underlying connections

func (*MultiWriteDriver) Dialect added in v0.0.2

func (d *MultiWriteDriver) Dialect() string

Dialect returns the dialect name of the primary driver

func (*MultiWriteDriver) Exec added in v0.0.2

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

Exec logs its params and calls the underlying driver Exec method for both write drivers

func (*MultiWriteDriver) Query added in v0.0.2

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

Query will query the primary write database

func (*MultiWriteDriver) Tx added in v0.0.2

Tx wraps the Exec and Query operations in transaction.

type QueryGenAnnotation added in v0.3.1

type QueryGenAnnotation struct {
	Skip bool
}

QueryGenAnnotation is an annotation used to indicate that query generation should be skipped for this type

func QueryGenSkip added in v0.3.1

func QueryGenSkip(skip bool) *QueryGenAnnotation

QueryGenSkip sets whether the query generation should be skipped for this type

func (*QueryGenAnnotation) Decode added in v0.3.1

func (a *QueryGenAnnotation) Decode(annotation interface{}) error

Decode unmarshalls the QueryGenAnnotation

func (QueryGenAnnotation) Name added in v0.3.1

func (a QueryGenAnnotation) Name() string

Name returns the name of the QueryGenAnnotation

type SchemaGenAnnotation

type SchemaGenAnnotation struct {
	Skip bool
}

SchemaGenAnnotation is an annotation used to indicate that schema generation should be skipped for this type

func SchemaGenSkip

func SchemaGenSkip(skip bool) *SchemaGenAnnotation

SchemaGenSkip sets whether the schema generation should be skipped for this type

func (*SchemaGenAnnotation) Decode added in v0.3.1

func (a *SchemaGenAnnotation) Decode(annotation interface{}) error

Decode unmarshalls the SchemaGenAnnotation

func (SchemaGenAnnotation) Name

func (a SchemaGenAnnotation) Name() string

Name returns the name of the SchemaGenAnnotation

type SoftDeleteKey

type SoftDeleteKey struct{}

SoftDeleteKey is used to indicate a soft delete mutation is in progress

type SoftDeleteSkipKey

type SoftDeleteSkipKey struct{}

SoftDeleteSkipKey is used to indicate to allow soft deleted records to be returned in records and to skip soft delete on mutations and proceed with a regular delete

type ThroughCleanup

type ThroughCleanup struct {
	Field   string
	Through string
}

ThroughCleanup is a struct used to indicate the field and through edge to cascade through

Directories

Path Synopsis
Package genhooks provides a set of entgo hooks for generating files from the ent schema.
Package genhooks provides a set of entgo hooks for generating files from the ent schema.
Package mixin contains the mixin package
Package mixin contains the mixin package
vanilla
_example/ent/enums
Package enums has enums
Package enums has enums
_example/graphapi
Package graphapi does graphi stuff
Package graphapi does graphi stuff

Jump to

Keyboard shortcuts

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