mixin

package
v1.37.4 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2026 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultIndex = "everything"
View Source
var ErrorExpiredToken = errors.New("This token is expired")

Error for expired jti's

View Source
var TokenNotFound = errors.New("Token not found")
View Source
var TokenNotFoundByName = errors.New("Token not found by name")

Functions

func DSKeyToOrm added in v1.35.0

func DSKeyToOrm(k iface.Key) orm.Key

DSKeyToOrm is the exported version for use in bridge code.

func OrmKeyToDS added in v1.35.0

func OrmKeyToDS(k orm.Key) iface.Key

OrmKeyToDS is the exported version for use in bridge code.

func SetSearchIndex

func SetSearchIndex(index SearchIndex)

SetSearchIndex sets the search index implementation to use

Types

type AccessTokens

type AccessTokens struct {
	// Entity is a struct with a Entity mixin
	Entity Entity `json:"-" datastore:"-"`

	// JWT secret (persisted for token verification)
	SecretKey []byte `json:"secretKey,omitempty"`

	// UseTokenId as JWT "jti" param, randomly generate upon generating a new key to expire all existing keys
	Tokens []accesstoken.AccessToken `json:"tokens,omitempty"`
}

AccessTokens is a mixin for securing objects with an AccessTokens

func (*AccessTokens) AddToken

func (at *AccessTokens) AddToken(name string, permissions bit.Mask) string

func (*AccessTokens) ClearTokens

func (at *AccessTokens) ClearTokens()

func (*AccessTokens) CompareToken

func (at *AccessTokens) CompareToken(tok1, tok2 *accesstoken.AccessToken) error

func (*AccessTokens) GetToken

func (at *AccessTokens) GetToken(accessToken string) (*accesstoken.AccessToken, error)

func (*AccessTokens) GetTokenByName

func (at *AccessTokens) GetTokenByName(name string) (*accesstoken.AccessToken, error)

func (*AccessTokens) GetWithAccessToken

func (at *AccessTokens) GetWithAccessToken(accessToken string) (*accesstoken.AccessToken, error)

func (*AccessTokens) Init

func (at *AccessTokens) Init(e Entity)

func (*AccessTokens) MustGetTokenByName

func (at *AccessTokens) MustGetTokenByName(name string) *accesstoken.AccessToken

func (*AccessTokens) RemoveToken

func (at *AccessTokens) RemoveToken(name string)

type AfterCreate

type AfterCreate interface {
	AfterCreate() error
}

type AfterDelete

type AfterDelete interface {
	AfterDelete() error
}

type AfterUpdate

type AfterUpdate interface {
	AfterUpdate(Entity) error
}

type BaseModel added in v1.35.0

type BaseModel struct {
	Db     *datastore.Datastore `json:"-" datastore:"-"`
	Entity Kind                 `json:"-" datastore:"-"`
	Parent datastore.Key        `json:"-" datastore:"-"`
	Mock   bool                 `json:"-" datastore:"-"`

	// Set by our mixin
	Id_       string    `json:"id,omitempty"`
	Loaded_   bool      `json:"-" datastore:"-"`
	CreatedAt time.Time `json:"createdAt,omitempty"`
	UpdatedAt time.Time `json:"updatedAt,omitempty"`
	Deleted   bool      `json:"deleted,omitempty"`

	// Flag used to specify that we're using a string key for this kind
	UseStringKey bool `json:"-" datastore:"-"`
	// contains filtered or unexported fields
}

Model is a mixin which adds Datastore/Validation/Serialization methods to any Kind that it has been embedded in.

func (*BaseModel) Clone added in v1.35.0

func (m *BaseModel) Clone() Entity

Create a clone of current entity

func (*BaseModel) CloneFromJSON added in v1.35.0

func (m *BaseModel) CloneFromJSON() Entity

Create a clone of currenty entity using only JSON-serializable data

func (*BaseModel) Context added in v1.35.0

func (m *BaseModel) Context() context.Context

Get context.Context

func (*BaseModel) Create added in v1.35.0

func (m *BaseModel) Create() error

Create new entity (should not exist yet)

func (*BaseModel) Created added in v1.35.0

func (m *BaseModel) Created() bool

Check if we have a previously created entity

func (*BaseModel) Datastore added in v1.35.0

func (m *BaseModel) Datastore() *datastore.Datastore

Return datastore

func (*BaseModel) Delete added in v1.35.0

func (m *BaseModel) Delete() error

Delete entity from Datastore

func (BaseModel) DeleteDocument added in v1.35.0

func (m BaseModel) DeleteDocument() error

func (*BaseModel) Exists added in v1.35.0

func (m *BaseModel) Exists() (bool, error)

Check if entity is in datastore.

func (*BaseModel) Get added in v1.35.0

func (m *BaseModel) Get(key datastore.Key) error

Get entity from datastore

func (*BaseModel) GetById added in v1.35.0

func (m *BaseModel) GetById(id string) error

Helper that will retrieve entity by id (which may be an encoded key/slug/sku)

func (*BaseModel) GetOrCreate added in v1.35.0

func (m *BaseModel) GetOrCreate(filterStr string, value interface{}) error

Get entity from datastore or create new one

func (*BaseModel) GetOrUpdate added in v1.35.0

func (m *BaseModel) GetOrUpdate(filterStr string, value interface{}) error

Get entity from datastore or create new one

func (*BaseModel) Id added in v1.35.0

func (m *BaseModel) Id() string

Returns ID for model

func (*BaseModel) IdExists added in v1.35.0

func (m *BaseModel) IdExists(id string) (datastore.Key, bool, error)

Check if entity is in datastore.

func (*BaseModel) Init added in v1.35.0

func (m *BaseModel) Init(db *datastore.Datastore, entity Kind)

Wire up model

func (*BaseModel) JSON added in v1.35.0

func (m *BaseModel) JSON() []byte

Serialize entity to JSON

func (*BaseModel) JSONString added in v1.35.0

func (m *BaseModel) JSONString() string

func (*BaseModel) Key added in v1.35.0

func (m *BaseModel) Key() (key datastore.Key)

Returns Key for this entity

func (*BaseModel) KeyExists added in v1.35.0

func (m *BaseModel) KeyExists(key datastore.Key) (bool, error)

Check if entity is in datastore.

func (BaseModel) Kind added in v1.35.0

func (m BaseModel) Kind() string

Return Kind

func (*BaseModel) Loaded added in v1.35.0

func (m *BaseModel) Loaded() bool

Helper to prevent duplicate deserialization

func (*BaseModel) MustCreate added in v1.35.0

func (m *BaseModel) MustCreate()

Create or panic

func (*BaseModel) MustDelete added in v1.35.0

func (m *BaseModel) MustDelete()

Delete or panic

func (*BaseModel) MustGet added in v1.35.0

func (m *BaseModel) MustGet(key datastore.Key)

Get or panic

func (*BaseModel) MustGetById added in v1.35.0

func (m *BaseModel) MustGetById(id string)

Get by id or panic

func (*BaseModel) MustPut added in v1.35.0

func (m *BaseModel) MustPut()

Put or panic

func (*BaseModel) MustSetKey added in v1.35.0

func (m *BaseModel) MustSetKey(key interface{})

Set key or panic

func (*BaseModel) MustUpdate added in v1.35.0

func (m *BaseModel) MustUpdate()

Update or panic

func (*BaseModel) Namespace added in v1.35.0

func (m *BaseModel) Namespace() string

Returns namespace for this model

func (*BaseModel) NewKey added in v1.35.0

func (m *BaseModel) NewKey() datastore.Key

Create a new key for this object

func (*BaseModel) Put added in v1.35.0

func (m *BaseModel) Put() error

Put entity in datastore

func (BaseModel) PutDocument added in v1.35.0

func (m BaseModel) PutDocument() error

func (*BaseModel) Query added in v1.35.0

func (m *BaseModel) Query() *ModelQuery

Return a query for this entity kind

func (*BaseModel) RunInTransaction added in v1.35.0

func (m *BaseModel) RunInTransaction(fn func() error, opts *datastore.TransactionOptions) error

Run in transaction using model's current context

func (*BaseModel) SetContext added in v1.35.0

func (m *BaseModel) SetContext(ctx context.Context)

Set context.Context

func (*BaseModel) SetEntity added in v1.35.0

func (m *BaseModel) SetEntity(entity interface{})

Set entity on mixin so it can be referenced later

func (*BaseModel) SetKey added in v1.35.0

func (m *BaseModel) SetKey(key interface{}) (err error)

Set's key for entity.

func (*BaseModel) SetNamespace added in v1.35.0

func (m *BaseModel) SetNamespace(namespace string)

Set context.Context namespace

func (*BaseModel) Slice added in v1.35.0

func (m *BaseModel) Slice() interface{}

Create a slice of entity type suitable for use with datastore.GetAll, etc.

func (*BaseModel) Update added in v1.35.0

func (m *BaseModel) Update() error

Update new entity (should already exist)

func (*BaseModel) Zero added in v1.35.0

func (m *BaseModel) Zero() Entity

Create a new zero'd entity of this type

type BeforeCreate

type BeforeCreate interface {
	BeforeCreate() error
}

type BeforeDelete

type BeforeDelete interface {
	BeforeDelete() error
}

type BeforeUpdate

type BeforeUpdate interface {
	BeforeUpdate(Entity) error
}

These last two interfaces are largely ignored -- we use helper below to have nicely typed update hooks in models.

type DatastoreAdapter added in v1.35.0

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

DatastoreAdapter wraps *datastore.Datastore to implement orm.DB.

func NewDatastoreAdapter added in v1.35.0

func NewDatastoreAdapter(ds *datastore.Datastore) *DatastoreAdapter

NewDatastoreAdapter creates a new adapter wrapping a commerce datastore.

func (*DatastoreAdapter) AllocateIDs added in v1.35.0

func (a *DatastoreAdapter) AllocateIDs(kind string, parent orm.Key, n int) ([]orm.Key, error)

func (*DatastoreAdapter) Close added in v1.35.0

func (a *DatastoreAdapter) Close() error

func (*DatastoreAdapter) Datastore added in v1.35.0

func (a *DatastoreAdapter) Datastore() *datastore.Datastore

Datastore returns the underlying commerce datastore.

func (*DatastoreAdapter) Delete added in v1.35.0

func (a *DatastoreAdapter) Delete(ctx context.Context, k orm.Key) error

func (*DatastoreAdapter) Get added in v1.35.0

func (a *DatastoreAdapter) Get(ctx context.Context, k orm.Key, dst interface{}) error

func (*DatastoreAdapter) NewIncompleteKey added in v1.35.0

func (a *DatastoreAdapter) NewIncompleteKey(kind string, parent orm.Key) orm.Key

func (*DatastoreAdapter) NewKey added in v1.35.0

func (a *DatastoreAdapter) NewKey(kind, stringID string, intID int64, parent orm.Key) orm.Key

func (*DatastoreAdapter) Put added in v1.35.0

func (a *DatastoreAdapter) Put(ctx context.Context, k orm.Key, src interface{}) (orm.Key, error)

func (*DatastoreAdapter) Query added in v1.35.0

func (a *DatastoreAdapter) Query(kind string) orm.Query

func (*DatastoreAdapter) RunInTransaction added in v1.35.0

func (a *DatastoreAdapter) RunInTransaction(ctx context.Context, fn func(tx orm.DB) error) error

func (*DatastoreAdapter) String added in v1.35.0

func (a *DatastoreAdapter) String() string

String representation for debugging.

type Defaults

type Defaults interface {
	Defaults()
}

type Document

type Document interface {
	Id() string
}

Document represents a searchable document

type DocumentSaveLoad

type DocumentSaveLoad struct {

	// Dummy field for gob, see: https://github.com/golang/go/issues/5819
	Dummy string `json:"-" datastore:"-"`
	// contains filtered or unexported fields
}

func (*DocumentSaveLoad) GetDocument

func (s *DocumentSaveLoad) GetDocument() reflect.Value

func (*DocumentSaveLoad) Load

func (s *DocumentSaveLoad) Load(fields []SearchField, meta *SearchDocumentMetadata) error

func (DocumentSaveLoad) Save

func (*DocumentSaveLoad) SetDocument

func (s *DocumentSaveLoad) SetDocument(doc interface{})

type Entity

type Entity interface {
	// TODO: Should not be embedded in Entity I don't think
	Kind

	// By convention where model is wired to entity
	Init(db *datastore.Datastore)

	// Get, Set context/namespace
	Context() context.Context
	SetContext(ctx context.Context)
	SetNamespace(namespace string)
	Namespace() string

	// Get, Set keys
	Key() (key datastore.Key)
	SetKey(key interface{}) error
	NewKey() datastore.Key
	Id() string

	// Various existential helpers
	Exists() (bool, error)
	IdExists(id string) (datastore.Key, bool, error)
	KeyExists(key datastore.Key) (bool, error)

	// Get, Put, Delete + Create, Update
	Get(key datastore.Key) error
	GetById(id string) error
	Put() error
	Create() error
	Update() error
	Delete() error

	// Must variants
	MustSetKey(key interface{})
	MustCreate()
	MustDelete()
	MustGet(key datastore.Key)
	MustGetById(id string)
	MustPut()
	MustUpdate()

	// Document
	PutDocument() error
	DeleteDocument() error

	// Get or Create, Update helpers
	GetOrCreate(filterStr string, value interface{}) error
	GetOrUpdate(filterStr string, value interface{}) error

	// Datastore
	Datastore() *datastore.Datastore
	RunInTransaction(fn func() error, opts *datastore.TransactionOptions) error

	// Query
	Query() *ModelQuery

	// Various helpers
	Zero() Entity
	Clone() Entity
	CloneFromJSON() Entity
	Slice() interface{}
	JSON() []byte
	JSONString() string
}

A specific datastore entity, with methods inherited from this mixin

type ErrFacetMismatch

type ErrFacetMismatch struct {
	StructType reflect.Type
	FacetName  string
	Reason     string
}

ErrFacetMismatch is returned when a facet is to be loaded into a different type than the one it was stored from, or when a field is missing or unexported in the destination struct. StructType is the type of the struct pointed to by the destination argument passed to Iterator.Next.

func (*ErrFacetMismatch) Error

func (e *ErrFacetMismatch) Error() string

type ErrFieldMismatch

type ErrFieldMismatch struct {
	FieldName string
	Reason    string
}

ErrFieldMismatch is returned when a field is to be loaded into a different than the one it was stored from, or when a field is missing or unexported in the destination struct.

func (*ErrFieldMismatch) Error

func (e *ErrFieldMismatch) Error() string

type ErrorMessage

type ErrorMessage struct {
	ErrorCode    string
	ErrorMessage string
}

type JsonResponse

type JsonResponse struct {
	Meta struct {
		RetrievedAt   string
		ExecutionTime int64
		CallsUsed     int64
	}

	Errors []ErrorMessage
}

type Kind

type Kind interface {
	Kind() string
}

A datastore kind that is compatible with the Model mixin

type Model

type Model[T any] struct {
	orm.Model[T]

	Parent       datastore.Key `json:"-" datastore:"-"`
	Mock         bool          `json:"-" datastore:"-"`
	UseStringKey bool          `json:"-" datastore:"-"`
	// contains filtered or unexported fields
}

Model[T] embeds orm.Model[T] and provides mixin.Entity compatibility. The orm.Model[T] must be the first field so self() pointer arithmetic works.

func (*Model[T]) Clone

func (b *Model[T]) Clone() Entity

func (*Model[T]) CloneFromJSON

func (b *Model[T]) CloneFromJSON() Entity

func (*Model[T]) Context

func (b *Model[T]) Context() context.Context

func (*Model[T]) Create

func (b *Model[T]) Create() error

func (*Model[T]) Created

func (b *Model[T]) Created() bool

func (*Model[T]) Datastore

func (b *Model[T]) Datastore() *datastore.Datastore

func (*Model[T]) Delete

func (b *Model[T]) Delete() error

func (*Model[T]) DeleteDocument

func (b *Model[T]) DeleteDocument() error

func (*Model[T]) Exists

func (b *Model[T]) Exists() (bool, error)

func (*Model[T]) Get

func (b *Model[T]) Get(key datastore.Key) error

Get overrides orm.Model[T].Get to accept datastore.Key.

func (*Model[T]) GetById

func (b *Model[T]) GetById(id string) error

GetById uses mixin.ModelQuery for hashid decoding, slug fallbacks, etc.

func (*Model[T]) GetCreatedAt added in v1.35.0

func (b *Model[T]) GetCreatedAt() time.Time

func (*Model[T]) GetOrCreate

func (b *Model[T]) GetOrCreate(filterStr string, value interface{}) error

func (*Model[T]) GetOrUpdate

func (b *Model[T]) GetOrUpdate(filterStr string, value interface{}) error

func (*Model[T]) GetUpdatedAt added in v1.35.0

func (b *Model[T]) GetUpdatedAt() time.Time

func (*Model[T]) Id

func (b *Model[T]) Id() string

Id returns the hashid-encoded entity ID. Overrides orm.Model[T].Id() to ensure hashid encoding even before Put().

func (*Model[T]) IdExists

func (b *Model[T]) IdExists(id string) (datastore.Key, bool, error)

func (*Model[T]) Init

func (b *Model[T]) Init(db *datastore.Datastore)

Init wires the bridge to a commerce datastore. Creates an ORM adapter, initializes the orm.Model, and applies defaults.

func (*Model[T]) JSON

func (b *Model[T]) JSON() []byte

func (*Model[T]) JSONString

func (b *Model[T]) JSONString() string

func (*Model[T]) Key

func (b *Model[T]) Key() datastore.Key

func (*Model[T]) KeyExists

func (b *Model[T]) KeyExists(key datastore.Key) (bool, error)

func (*Model[T]) Loaded

func (b *Model[T]) Loaded() bool

loaded_ prevents duplicate deserialization in Load() methods. Matches mixin.BaseModel.Loaded() semantics: returns true if already loaded, otherwise marks as loaded and returns false.

func (*Model[T]) MustCreate

func (b *Model[T]) MustCreate()

func (*Model[T]) MustDelete

func (b *Model[T]) MustDelete()

func (*Model[T]) MustGet

func (b *Model[T]) MustGet(key datastore.Key)

func (*Model[T]) MustGetById

func (b *Model[T]) MustGetById(id string)

func (*Model[T]) MustPut

func (b *Model[T]) MustPut()

func (*Model[T]) MustSetKey

func (b *Model[T]) MustSetKey(key interface{})

func (*Model[T]) MustUpdate

func (b *Model[T]) MustUpdate()

func (*Model[T]) Namespace

func (b *Model[T]) Namespace() string

func (*Model[T]) NewKey

func (b *Model[T]) NewKey() datastore.Key

func (*Model[T]) Put

func (b *Model[T]) Put() error

func (*Model[T]) PutDocument

func (b *Model[T]) PutDocument() error

func (*Model[T]) Query

func (b *Model[T]) Query() *ModelQuery

Query returns a mixin.ModelQuery for this entity.

func (*Model[T]) RunInTransaction

func (b *Model[T]) RunInTransaction(fn func() error, opts *datastore.TransactionOptions) error

func (*Model[T]) SetContext

func (b *Model[T]) SetContext(ctx context.Context)

func (*Model[T]) SetKey

func (b *Model[T]) SetKey(key interface{}) error

func (*Model[T]) SetNamespace

func (b *Model[T]) SetNamespace(namespace string)

func (*Model[T]) Slice

func (b *Model[T]) Slice() interface{}

func (*Model[T]) Update

func (b *Model[T]) Update() error

func (*Model[T]) Zero

func (b *Model[T]) Zero() Entity

type ModelQuery

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

This is a simple Query helper for individual models. Allows you to query for a single entity or key only as a convenience on an individual model.

func (*ModelQuery) All

func (q *ModelQuery) All() datastore.Query

func (*ModelQuery) Ancestor

func (q *ModelQuery) Ancestor(key datastore.Key) *ModelQuery

func (*ModelQuery) ById

func (q *ModelQuery) ById(id string) (bool, error)

Filter By Id

func (*ModelQuery) ByKey

func (q *ModelQuery) ByKey(key interface{}) (bool, error)

Filter by key

func (*ModelQuery) Filter

func (q *ModelQuery) Filter(filterStr string, value interface{}) *ModelQuery

func (*ModelQuery) Get

func (q *ModelQuery) Get() (bool, error)

Get entity

func (*ModelQuery) GetKey

func (q *ModelQuery) GetKey() (bool, error)

Get just key

func (*ModelQuery) IdExists

func (q *ModelQuery) IdExists(id string) (datastore.Key, bool, error)

Check if id exists

func (*ModelQuery) KeyExists

func (q *ModelQuery) KeyExists(key datastore.Key) (bool, error)

Check if key exists

func (*ModelQuery) Limit

func (q *ModelQuery) Limit(limit int) *ModelQuery

func (*ModelQuery) MustGet

func (q *ModelQuery) MustGet()

func (*ModelQuery) MustGetKey

func (q *ModelQuery) MustGetKey()

func (*ModelQuery) Offset

func (q *ModelQuery) Offset(offset int) *ModelQuery

func (*ModelQuery) Order

func (q *ModelQuery) Order(order string) *ModelQuery

type Salesforce

type Salesforce struct {
	PrimarySalesforceId_   string    `json:"-"`
	SecondarySalesforceId_ string    `json:"-"`
	ExternalId_            string    `json:"-"`
	LastSync_              time.Time `json:"-"`
}

func (*Salesforce) ExternalId

func (so *Salesforce) ExternalId() string

func (*Salesforce) LastSync

func (so *Salesforce) LastSync() time.Time

func (*Salesforce) SalesforceId

func (so *Salesforce) SalesforceId() string

func (*Salesforce) SalesforceId2

func (so *Salesforce) SalesforceId2() string

func (*Salesforce) SetExternalId

func (so *Salesforce) SetExternalId(id string)

func (*Salesforce) SetLastSync

func (so *Salesforce) SetLastSync()

func (*Salesforce) SetSalesforceId

func (so *Salesforce) SetSalesforceId(id string)

func (*Salesforce) SetSalesforceId2

func (so *Salesforce) SetSalesforceId2(id string)

type SearchDocumentMetadata

type SearchDocumentMetadata struct {
	Facets []SearchFacet
}

SearchDocumentMetadata holds metadata for a search document

type SearchFacet

type SearchFacet struct {
	Name  string
	Value interface{}
}

SearchFacet represents a facet in a search document

type SearchField

type SearchField struct {
	Name  string
	Value interface{}
}

SearchField represents a field in a search document

type SearchIndex

type SearchIndex interface {
	Put(ctx context.Context, id string, doc interface{}) error
	Delete(ctx context.Context, id string) error
}

SearchIndex provides an interface for search operations This allows pluggable search backends (Elasticsearch, Meilisearch, etc.)

func GetSearchIndex

func GetSearchIndex() SearchIndex

GetSearchIndex returns the current search index, or nil if not set

type Searchable

type Searchable interface {
	Document() Document
}

Searchable is implemented by models that can be indexed for search

Jump to

Keyboard shortcuts

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