directus

package module
v0.20.2 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 15 Imported by: 0

README

directus-go

Go Reference

Directus Go SDK.

Install

go get github.com/altipla-consulting/directus-go

Contributing

You can make pull requests or create issues in GitHub. Any code you send should be formatted using make gofmt.

License

MIT License

Documentation

Index

Constants

View Source
const (
	ErrorCodeRecordNotUnique   = "RECORD_NOT_UNIQUE"
	ErrorCodeInvalidForeignKey = "INVALID_FOREIGN_KEY"
)

Variables

View Source
var (
	// ErrItemNotFound is returned when the item is not found in the collection.
	ErrItemNotFound = errors.New("directus: item not found")

	// ErrIEmpty is returned when the item is returned empty.
	ErrEmpty = errors.New("directus: empty")
)

Functions

func FilterJSON

func FilterJSON(filter Filter) (string, error)

Types

type Accountability added in v0.16.0

type Accountability string
const (
	AccountabilityAll      Accountability = "all"
	AccountabilityActivity Accountability = "activity"
)

type Alterations added in v0.16.15

type Alterations[T any, PK string | int64] struct {
	Create []*T `json:"create,omitempty"`
	Update []*T `json:"update,omitempty"`
	Delete []PK `json:"delete,omitempty"`
}

type Client

type Client struct {
	Collections        *ResourceClient[Collection]
	CustomTranslations *ResourceClient[CustomTranslation]
	Folders            *ResourceClient[Folder]
	Relations          *ResourceClient[RelationDefinition]
	Roles              *ResourceClient[Role]
	Users              *ResourceClient[User]
	Presets            *ResourceClient[Preset]
	Operations         *ResourceClient[Operation]
	Flows              *ResourceClient[Flow]
	Files              *ResourceClient[File]
	Permissions        *ResourceClient[Permission]
	Dashboards         *ResourceClient[Dashboard]
	Panels             *ResourceClient[Panel]

	Fields *clientFields
	// contains filtered or unexported fields
}

Client keeps a connection to a Directus instance.

func NewClient

func NewClient(instance string, token string, opts ...ClientOption) *Client

NewClient creates a new connection to the Directus instance using the static token to authenticate.

type ClientOption

type ClientOption func(client *Client)

ClinetOption configures a client when creating it.

func WithBodyLogger

func WithBodyLogger() ClientOption

WithBodyLogger prints the request and response bodies to the logger.

func WithLogger

func WithLogger(logger *slog.Logger) ClientOption

WithLogger sets a custom logger for the sent requests and responses received from the server.

type Collection added in v0.16.0

type Collection struct {
	Collection string            `json:"collection"`
	Meta       CollectionMeta    `json:"meta"`
	Schema     *CollectionSchema `json:"schema,omitempty"`
	Fields     []*Field          `json:"fields,omitempty"`
}

type CollectionCollapse added in v0.16.0

type CollectionCollapse string
const (
	CollectionCollapseOpen   CollectionCollapse = "open"
	CollectionCollapseClosed CollectionCollapse = "closed"
	CollectionCollapseLocked CollectionCollapse = "locked"
)

type CollectionMeta added in v0.16.0

type CollectionMeta struct {
	Collection string `json:"collection,omitempty"`

	Color     string `json:"color,omitempty"`
	Icon      Icon   `json:"icon,omitempty"`
	Note      string `json:"note,omitempty"`
	Hidden    bool   `json:"hidden"`
	Singleton bool   `json:"singleton"`

	ArchiveField     string `json:"archive_field,omitempty"`
	ArchiveValue     string `json:"archive_value,omitempty"`
	UnarchiveValue   string `json:"unarchive_value,omitempty"`
	ArchiveAppFilter bool   `json:"archive_app_filter"`

	SortField string `json:"sort_field,omitempty"`

	Group    Nullable[string]   `json:"group,omitempty"`
	Sort     int64              `json:"sort,omitempty"`
	Collapse CollectionCollapse `json:"collapse,omitempty"`

	Versioning bool `json:"versioning,omitempty"`

	Accountability Nullable[Accountability] `json:"accountability"`

	System bool `json:"system,omitempty"`

	PreviewURL string `json:"preview_url,omitempty"`

	DisplayTemplate string `json:"display_template,omitempty"`

	Unknown map[string]any `json:"-"`
}

func (*CollectionMeta) MarshalJSON added in v0.16.0

func (meta *CollectionMeta) MarshalJSON() ([]byte, error)

func (*CollectionMeta) UnmarshalJSON added in v0.16.0

func (meta *CollectionMeta) UnmarshalJSON(data []byte) error

type CollectionSchema added in v0.16.0

type CollectionSchema struct {
	Name    string `json:"name"`
	Comment string `json:"comment,omitempty"`

	Collation string `json:"collation,omitempty"`
	Engine    string `json:"engine,omitempty"`
	Schema    string `json:"schema,omitempty"`

	Unknown map[string]any `json:"-"`
}

func (*CollectionSchema) MarshalJSON added in v0.16.0

func (schema *CollectionSchema) MarshalJSON() ([]byte, error)

func (*CollectionSchema) UnmarshalJSON added in v0.16.0

func (schema *CollectionSchema) UnmarshalJSON(data []byte) error

type CustomTranslation added in v0.16.0

type CustomTranslation struct {
	ID       string `json:"id,omitempty"`
	Key      string `json:"key,omitempty"`
	Language string `json:"language,omitempty"`
	Value    string `json:"value"`
}

type Dashboard added in v0.19.0

type Dashboard struct {
	ID    Nullable[string] `json:"id"`
	Name  string           `json:"name"`
	Icon  Icon             `json:"icon"`
	Color Nullable[string] `json:"color"`
	Note  Nullable[string] `json:"note"`
}

type Error added in v0.15.0

type Error struct {
	Message    string          `json:"message"`
	Extensions ErrorExtensions `json:"extensions"`
}

func (Error) Error added in v0.15.0

func (e Error) Error() string

type ErrorCode added in v0.15.0

type ErrorCode string

type ErrorExtensions added in v0.15.0

type ErrorExtensions struct {
	Code ErrorCode `json:"code"`
}

type Field added in v0.12.0

type Field struct {
	Collection string       `json:"collection"`
	Field      string       `json:"field"`
	Type       FieldType    `json:"type"`
	Meta       FieldMeta    `json:"meta"`
	Schema     *FieldSchema `json:"schema,omitempty"`
}

type FieldMeta added in v0.12.0

type FieldMeta struct {
	ID     int64      `json:"id,omitempty"`
	Hidden bool       `json:"hidden"`
	Width  FieldWidth `json:"width,omitempty"`

	ReadOnly bool `json:"read_only"`
	Required bool `json:"required"`

	Sort    int64          `json:"sort,omitempty"`
	System  bool           `json:"system,omitempty"`
	Special []FieldSpecial `json:"special,omitempty"`

	Unknown map[string]any `json:"-"`
}

func (*FieldMeta) HasSpecial added in v0.16.12

func (meta *FieldMeta) HasSpecial(special FieldSpecial) bool

func (*FieldMeta) UnmarshalJSON added in v0.16.0

func (meta *FieldMeta) UnmarshalJSON(data []byte) error

type FieldSchema added in v0.16.0

type FieldSchema struct {
	Name      string `json:"name,omitempty"`
	Table     string `json:"table,omitempty"`
	DataType  string `json:"data_type"`
	MaxLength int64  `json:"max_length,omitempty"`

	IsNullable       bool `json:"is_nullable"`
	IsUnique         bool `json:"is_unique,omitempty"`
	IsPrimaryKey     bool `json:"is_primary_key,omitempty"`
	HasAutoIncrement bool `json:"has_auto_increment,omitempty"`

	Unknown map[string]any `json:"-"`
}

func (*FieldSchema) UnmarshalJSON added in v0.16.0

func (schema *FieldSchema) UnmarshalJSON(data []byte) error

type FieldSpecial added in v0.16.0

type FieldSpecial string
const (
	FieldSpecialManyToOne   FieldSpecial = "m2o"
	FieldSpecialDateCreated FieldSpecial = "date-created"
	FieldSpecialDateUpdated FieldSpecial = "date-updated"
	FieldSpecialUUID        FieldSpecial = "uuid"
	FieldSpecialUserCreated FieldSpecial = "user-created"
	FieldSpecialUserUpdated FieldSpecial = "user-updated"
	FieldSpecialFile        FieldSpecial = "file"
	FieldSpecialAlias       FieldSpecial = "alias"
	FieldSpecialNoData      FieldSpecial = "no-data"
	FieldSpecialCastBoolean FieldSpecial = "cast-boolean"
	FieldSpecialGroup       FieldSpecial = "group"
)

func (*FieldSpecial) UnmarshalJSON added in v0.16.2

func (special *FieldSpecial) UnmarshalJSON(data []byte) error

type FieldType added in v0.12.0

type FieldType string
const (
	FieldTypeString    FieldType = "string"
	FieldTypeAlias     FieldType = "alias"
	FieldTypeDate      FieldType = "date"
	FieldTypeTimestamp FieldType = "timestamp"
)

type FieldWidth added in v0.12.0

type FieldWidth string
const (
	FieldWidthFull FieldWidth = "full"
	FieldWidthHalf FieldWidth = "half"
)

type File added in v0.19.0

type File struct {
	FileSize        Nullable[int64]  `json:"file_size"`
	ID              string           `json:"id,omitempty"`
	Folder          Nullable[string] `json:"folder"`
	Title           Nullable[string] `json:"title"`
	Type            Nullable[string] `json:"type"`
	Description     Nullable[string] `json:"description"`
	Storage         string           `json:"storage"`
	Charset         Nullable[string] `json:"charset"`
	FilenameDowload string           `json:"filename_download"`
	FocalPointX     Nullable[int32]  `json:"focal_point_x"`
	FocalPointY     Nullable[int32]  `json:"focal_point_y"`
	Width           Nullable[int32]  `json:"width"`
	Height          Nullable[int32]  `json:"height"`
	Duration        Nullable[int32]  `json:"duration"`
	Location        Nullable[string] `json:"location"`
	Tags            Nullable[string] `json:"tags"`
	Embed           Nullable[string] `json:"embed"`
	FilenameDisk    Nullable[string] `json:"filename_disk"`

	Unknown map[string]any `json:"-"`
}

func (*File) MarshalJSON added in v0.19.0

func (file *File) MarshalJSON() ([]byte, error)

func (*File) UnmarshalJSON added in v0.19.0

func (file *File) UnmarshalJSON(data []byte) error

type Filter

type Filter interface {
	String() string
	// contains filtered or unexported methods
}

func And

func And(filters ...Filter) Filter

func Between

func Between(field string, from, to any) Filter

func Empty

func Empty(field string) Filter

func Eq

func Eq(field string, value any) Filter

func Gt

func Gt(field string, value any) Filter

func Gte

func Gte(field string, value any) Filter

func In

func In(field string, values ...any) Filter

func Lt

func Lt(field string, value any) Filter

func Lte

func Lte(field string, value any) Filter

func Neq

func Neq(field string, value any) Filter

func Noop

func Noop() Filter

func NotEmpty

func NotEmpty(field string) Filter

func Or

func Or(filters ...Filter) Filter
func Related(field string, filter Filter) Filter

func StartsWith added in v0.17.0

func StartsWith(field string, value string) Filter

type Flow added in v0.19.0

type Flow struct {
	ID             string           `json:"id,omitempty"`
	Name           string           `json:"name"`
	Status         string           `json:"status"`
	Description    Nullable[string] `json:"description"`
	Accountability Accountability   `json:"accountability"`
	Color          string           `json:"color,omitempty"`
	Icon           Icon             `json:"icon,omitempty"`
	Operation      Nullable[string] `json:"operation"`

	Unknown map[string]any `json:"-"`
}

func (*Flow) MarshalJSON added in v0.19.0

func (flow *Flow) MarshalJSON() ([]byte, error)

func (*Flow) UnmarshalJSON added in v0.19.0

func (flow *Flow) UnmarshalJSON(data []byte) error

type Folder added in v0.16.0

type Folder struct {
	ID     string           `json:"id,omitempty"`
	Name   string           `json:"name"`
	Parent Nullable[string] `json:"parent"`
}

type Icon added in v0.16.0

type Icon string

type ItemsClient

type ItemsClient[T any] struct {
	// contains filtered or unexported fields
}

ItemsClient access the items API in a type-safe way.

func NewItemsClient

func NewItemsClient[T any](client *Client, collection string, opts ...ReadOption) *ItemsClient[T]

NewItemsClient creates a new client to access & write items in a type-safe way.

func (*ItemsClient[T]) Create

func (items *ItemsClient[T]) Create(ctx context.Context, item *T) (*T, error)

Create a new item in the collection.

func (*ItemsClient[T]) Delete

func (items *ItemsClient[T]) Delete(ctx context.Context, id string) error

Delete an item from the collection by its primary key.

func (*ItemsClient[T]) Filter

func (items *ItemsClient[T]) Filter(ctx context.Context, filter Filter, opts ...ReadOption) ([]*T, error)

Filter items of the collection.

func (*ItemsClient[T]) Get

func (items *ItemsClient[T]) Get(ctx context.Context, id string) (*T, error)

Get a single item by its primary key. If it cannot be found, it returns ErrItemNotFound.

func (*ItemsClient[T]) List

func (items *ItemsClient[T]) List(ctx context.Context, opts ...ReadOption) ([]*T, error)

List items of the collection.

func (*ItemsClient[T]) Update

func (items *ItemsClient[T]) Update(ctx context.Context, id string, item *T) (*T, error)

Update an item in the collection by its primary key.

type Nullable

type Nullable[T any] struct {
	Value T
	Valid bool
}

func (Nullable[T]) MarshalJSON

func (n Nullable[T]) MarshalJSON() ([]byte, error)

func (Nullable[T]) String

func (n Nullable[T]) String() string

func (*Nullable[T]) UnmarshalJSON

func (n *Nullable[T]) UnmarshalJSON(data []byte) error

type Operation added in v0.19.0

type Operation struct {
	ID        string           `json:"id,omitempty"`
	Flow      string           `json:"flow"`
	Key       string           `json:"key"`
	PositionX int32            `json:"position_x"`
	PositionY int32            `json:"position_y"`
	Type      string           `json:"type"`
	Name      Nullable[string] `json:"name"`
	Reject    Nullable[string] `json:"reject"`
	Resolve   Nullable[string] `json:"resolve"`

	Unknown map[string]any `json:"-"`
}

func (*Operation) MarshalJSON added in v0.19.0

func (operation *Operation) MarshalJSON() ([]byte, error)

func (*Operation) UnmarshalJSON added in v0.19.0

func (operation *Operation) UnmarshalJSON(data []byte) error

type Panel added in v0.19.0

type Panel struct {
	ID         Nullable[string] `json:"id"`
	Dashboard  string           `json:"dashboard"`
	Height     int32            `json:"height"`
	Width      int32            `json:"width"`
	PositionX  int32            `json:"position_x"`
	PositionY  int32            `json:"position_y"`
	ShowHeader bool             `json:"show_header"`
	Type       string           `json:"type"`
	Color      Nullable[string] `json:"color"`
	Icon       Icon             `json:"icon"`
	Name       Nullable[string] `json:"name"`
	Note       Nullable[string] `json:"note"`

	Unknown map[string]any `json:"-"`
}

func (*Panel) MarshalJSON added in v0.19.0

func (panel *Panel) MarshalJSON() ([]byte, error)

func (*Panel) UnmarshalJSON added in v0.19.0

func (panel *Panel) UnmarshalJSON(data []byte) error

type Permission added in v0.19.0

type Permission struct {
	ID         int64            `json:"id,omitempty"`
	Role       Nullable[string] `json:"role"`
	Collection string           `json:"collection"`
	Action     PermissionAction `json:"action"`
	Fields     Nullable[string] `json:"fields"`

	Unknown map[string]any `json:"-"`
}

func (*Permission) MarshalJSON added in v0.19.0

func (permission *Permission) MarshalJSON() ([]byte, error)

func (*Permission) UnmarshalJSON added in v0.19.0

func (permission *Permission) UnmarshalJSON(data []byte) error

type PermissionAction added in v0.19.0

type PermissionAction string
const (
	PermissionActionCreate PermissionAction = "create"
	PermissionActionRead   PermissionAction = "read"
	PermissionActionUpdate PermissionAction = "update"
	PermissionActionDelete PermissionAction = "delete"
)

type Preset added in v0.18.0

type Preset struct {
	ID         int64            `json:"id,omitempty"`
	Bookmark   Nullable[string] `json:"bookmark"`
	User       Nullable[string] `json:"user"`
	Role       Nullable[string] `json:"role"`
	Collection string           `json:"collection"`
	Layout     Nullable[string] `json:"layout"`
	Icon       Nullable[string] `json:"icon"`

	Unknown map[string]any `json:"-"`
}

func (*Preset) MarshalJSON added in v0.18.0

func (preset *Preset) MarshalJSON() ([]byte, error)

func (*Preset) UnmarshalJSON added in v0.18.0

func (preset *Preset) UnmarshalJSON(data []byte) error

type ProtoJSON

type ProtoJSON[T proto.Message] struct {
	Value T
}

func NewProtoJSON

func NewProtoJSON[T proto.Message](value T) ProtoJSON[T]

func (ProtoJSON[T]) MarshalJSON

func (n ProtoJSON[T]) MarshalJSON() ([]byte, error)

func (ProtoJSON[T]) String

func (n ProtoJSON[T]) String() string

func (*ProtoJSON[T]) UnmarshalJSON

func (n *ProtoJSON[T]) UnmarshalJSON(data []byte) error

type ReadOption

type ReadOption func(req *http.Request)

ReadOption configures the returned data from Directus when reading or returning items.

func WithDeepLimit added in v0.10.0

func WithDeepLimit(field string, limit int64) ReadOption

WithDeepLimit limits the number of returned deep relations of each item.

func WithDeepSort added in v0.10.0

func WithDeepSort(field string, sort ...string) ReadOption

WithDeepSort sorts the deep relations of each returned item by the given fields. Use a minus sign (-) to sort in descending order. It does not order the items themselves. To sort the items, use WithSort.

func WithFields

func WithFields(fields ...string) ReadOption

WithFields filters the fields of each returned item. It can add relations deep fields to the response to obtain them in the same request.

func WithLimit added in v0.10.0

func WithLimit(limit int64) ReadOption

WithLimit limits the number of returned items.

func WithNoLimit added in v0.10.0

func WithNoLimit() ReadOption

WithNoLimit returns all items of the collection.

func WithOffset added in v0.10.0

func WithOffset(offset int64) ReadOption

WithOffset skips the first n items.

func WithSort

func WithSort(sort ...string) ReadOption

WithSort sorts the returned items by the given fields. Use a minus sign (-) to sort in descending order. It does not order deep relations inside each of the items. To sort deep relations, use WithDeepSort.

type Relation

type Relation[T any] struct {
	// contains filtered or unexported fields
}

func NewRelation

func NewRelation[T any](data *T) Relation[T]

func (Relation[T]) Empty

func (r Relation[T]) Empty() bool

func (Relation[T]) MarshalJSON

func (r Relation[T]) MarshalJSON() ([]byte, error)

func (Relation[T]) NumericID

func (r Relation[T]) NumericID() int64

func (Relation[T]) String

func (r Relation[T]) String() string

func (Relation[T]) StringID

func (r Relation[T]) StringID() string

func (*Relation[T]) UnmarshalJSON

func (r *Relation[T]) UnmarshalJSON(data []byte) error

func (Relation[T]) Value

func (r Relation[T]) Value() *T

type RelationAction added in v0.16.0

type RelationAction string
const (
	RelationActionCascade  RelationAction = "CASCADE"
	RelationActionSetNull  RelationAction = "SET NULL"
	RelationActionNoAction RelationAction = "NO ACTION"
)

func (*RelationAction) UnmarshalJSON added in v0.16.11

func (action *RelationAction) UnmarshalJSON(data []byte) error

type RelationDefinition added in v0.16.0

type RelationDefinition struct {
	Collection        string `json:"collection"`
	Field             string `json:"field"`
	RelatedCollection string `json:"related_collection"`

	Schema RelationSchema `json:"schema"`
	Meta   RelationMeta   `json:"meta"`
}

type RelationMeta added in v0.16.0

type RelationMeta struct {
	ID int64 `json:"id,omitempty"`

	System bool `json:"system,omitempty"`

	Unknown map[string]any `json:"-"`
}

func (*RelationMeta) MarshalJSON added in v0.16.0

func (meta *RelationMeta) MarshalJSON() ([]byte, error)

func (*RelationMeta) UnmarshalJSON added in v0.16.0

func (meta *RelationMeta) UnmarshalJSON(data []byte) error

type RelationSchema added in v0.16.0

type RelationSchema struct {
	Table    string         `json:"table"`
	Column   string         `json:"column"`
	OnUpdate RelationAction `json:"on_update"`
	OnDelete RelationAction `json:"on_delete"`

	Unknown map[string]any `json:"-"`
}

func (*RelationSchema) MarshalJSON added in v0.16.0

func (schema *RelationSchema) MarshalJSON() ([]byte, error)

func (*RelationSchema) UnmarshalJSON added in v0.16.0

func (schema *RelationSchema) UnmarshalJSON(data []byte) error

type ResourceClient added in v0.16.0

type ResourceClient[T any] struct {
	// contains filtered or unexported fields
}

func NewResourceClient added in v0.16.0

func NewResourceClient[T any](client *Client, endpoint string) *ResourceClient[T]

func (*ResourceClient[T]) Create added in v0.16.0

func (rc *ResourceClient[T]) Create(ctx context.Context, item *T) (*T, error)

func (*ResourceClient[T]) Delete added in v0.16.0

func (rc *ResourceClient[T]) Delete(ctx context.Context, id string) error

func (*ResourceClient[T]) Get added in v0.16.0

func (rc *ResourceClient[T]) Get(ctx context.Context, id string) (*T, error)

func (*ResourceClient[T]) List added in v0.16.0

func (rc *ResourceClient[T]) List(ctx context.Context) ([]*T, error)

func (*ResourceClient[T]) Patch added in v0.16.0

func (rc *ResourceClient[T]) Patch(ctx context.Context, id string, item *T) (*T, error)

type Role

type Role struct {
	ID          string `json:"id,omitempty"`
	Icon        Icon   `json:"icon,omitempty"`
	Name        string `json:"name"`
	Description string `json:"description,omitempty"`

	AdminAccess bool `json:"admin_access"`
	AppAccess   bool `json:"app_access"`

	Users []string `json:"users,omitempty"`
}

type SingletonClient

type SingletonClient[T any] struct {
	// contains filtered or unexported fields
}

func NewSingletonClient

func NewSingletonClient[T any](client *Client, collection string, opts ...ReadOption) *SingletonClient[T]

func (*SingletonClient[T]) Get

func (s *SingletonClient[T]) Get(ctx context.Context) (*T, error)

func (*SingletonClient[T]) Update

func (s *SingletonClient[T]) Update(ctx context.Context, item *T) (*T, error)

type User added in v0.13.0

type User struct {
	ID        string `json:"id,omitempty"`
	FirstName string `json:"first_name,omitempty"`
	LastName  string `json:"last_name,omitempty"`
	Email     string `json:"email,omitempty"`
	Role      string `json:"role,omitempty"`

	Provider           string `json:"provider,omitempty"`
	ExternalIdentifier string `json:"external_identifier,omitempty"`
}

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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