dao

package
v0.0.27 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2025 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrNotFound added in v0.0.18

type ErrNotFound struct {
	// ID is the identifier of the object that was not found.
	ID string
}

ErrNotFound is an error type that indicates that a requested object doesn't exist.

func (*ErrNotFound) Error added in v0.0.18

func (e *ErrNotFound) Error() string

Error returns the error message.

type Event

type Event struct {
	// Type is the type of the event.
	Type EventType

	// Table is the name of the table.
	Table string

	// Object is the object that was created, updated or deleted.
	Object proto.Message
}

Event represents a database event with a specific kind and associated object.

type EventCallback

type EventCallback func(ctx context.Context, event Event) error

EventCallback is a function that will be called to process an event.

type EventType

type EventType int

EventType represents the type of an event generated by a DAO.

const (
	// EventTypeCreated indicates that an object was created.
	EventTypeCreated EventType = iota

	// EventTypeUpdated indicates that an object was updated.
	EventTypeUpdated

	// EventTypeDeleted indicates that an object was deleted.
	EventTypeDeleted
)

func (EventType) String

func (k EventType) String() string

String generates a string representation of the event.

type FilterTranslator added in v0.0.3

type FilterTranslator[O proto.Message] struct {
	// contains filtered or unexported fields
}

FilterTranslator knows how to translate filter expressions into SQL where clauses.

func (*FilterTranslator[O]) Translate added in v0.0.3

func (t *FilterTranslator[O]) Translate(ctx context.Context, filter string) (sql string, err error)

Translate translate the given filter expression into a SQL where statement.

type FilterTranslatorBuilder added in v0.0.3

type FilterTranslatorBuilder[O proto.Message] struct {
	// contains filtered or unexported fields
}

FilterTranslatorBuilder contains the data and logic needed to create a filter translator. Don't create instances of this type directly, use the NewTranslationBuilder function instead.

func NewFilterTranslator added in v0.0.3

func NewFilterTranslator[O proto.Message]() *FilterTranslatorBuilder[O]

NewFilterTranslator creates a object that knows how to translate filter expressions into SQL where statements.

func (*FilterTranslatorBuilder[O]) Build added in v0.0.3

func (b *FilterTranslatorBuilder[O]) Build() (result *FilterTranslator[O], err error)

Build uses the data stored in the builder to create and configure a new filter translator.

func (*FilterTranslatorBuilder[O]) SetLogger added in v0.0.3

func (b *FilterTranslatorBuilder[O]) SetLogger(value *slog.Logger) *FilterTranslatorBuilder[O]

SetLogger sets the logger that will be used by the translator. This is mandatory.

type GenericDAO

type GenericDAO[O Object] struct {
	// contains filtered or unexported fields
}

GenericDAO provides generic data access operations for protocol buffers messages. It assumes that objects will be stored in tables with the following columns:

  • `id` - The unique identifier of the object.
  • `name` - The human friendly name of the object.
  • `creation_timestamp` - The time the object was created.
  • `deletion_timestamp` - The time the object was deleted.
  • `finalizers` - The list of finalizers for the object.
  • `creators` - The list of creators for the object.
  • `tenants` - The list of tenants for the object.
  • `data` - The serialized object, using the protocol buffers JSON serialization.

Objects must have field named `id` of string type.

func (*GenericDAO[O]) Create

func (d *GenericDAO[O]) Create(ctx context.Context, object O) (result O, err error)

Create adds a new row to the table with a generated identifier and serialized data.

func (*GenericDAO[O]) Delete

func (d *GenericDAO[O]) Delete(ctx context.Context, id string) (err error)

Delete removes a row from the table by its identifier.

func (*GenericDAO[O]) Exists

func (d *GenericDAO[O]) Exists(ctx context.Context, id string) (ok bool, err error)

Exists checks if a row with the given identifiers exists. Returns false and no error if there is no row with the given identifier.

func (*GenericDAO[O]) Get

func (d *GenericDAO[O]) Get(ctx context.Context, id string) (result O, err error)

Get retrieves a single row by its identifier and deserializes it into a message. Returns nil and no error if there is no row with the given identifier.

func (*GenericDAO[O]) List

func (d *GenericDAO[O]) List(ctx context.Context, request ListRequest) (response ListResponse[O], err error)

List retrieves all rows from the table and deserializes them into a slice of messages.

func (*GenericDAO[O]) Update

func (d *GenericDAO[O]) Update(ctx context.Context, object O) (result O, err error)

Update modifies an existing row in the table by its identifier with the result of serializing the provided object.

type GenericDAOBuilder

type GenericDAOBuilder[O Object] struct {
	// contains filtered or unexported fields
}

GenericDAOBuilder is a builder for creating generic data access objects.

func NewGenericDAO

func NewGenericDAO[O Object]() *GenericDAOBuilder[O]

NewGenericDAO creates a builder that can then be used to configure and create a generic DAO.

func (*GenericDAOBuilder[O]) AddEventCallback

func (b *GenericDAOBuilder[O]) AddEventCallback(value EventCallback) *GenericDAOBuilder[O]

AddEventCallback adds a function that will be called to process events when the DAO creates, updates or deletes an object.

The functions are called synchronously, in the same order they were added, and with the same context used by the DAO for its operations. If any of them returns an error the transaction will be rolled back.

func (*GenericDAOBuilder[O]) Build

func (b *GenericDAOBuilder[O]) Build() (result *GenericDAO[O], err error)

Build creates a new generic DAO using the configuration stored in the builder.

func (*GenericDAOBuilder[O]) SetAttributionLogic added in v0.0.12

func (b *GenericDAOBuilder[O]) SetAttributionLogic(value auth.AttributionLogic) *GenericDAOBuilder[O]

SetAttributionLogic sets the attribution logic that will be used to determine the creators for objects. The logic receives the context as a parameter and should return the names of the creators. If not provided, a default logic that returns no creators will be recorded.

func (*GenericDAOBuilder[O]) SetDefaultLimit

func (b *GenericDAOBuilder[O]) SetDefaultLimit(value int) *GenericDAOBuilder[O]

SetDefaultLimit sets the default number of items returned. It will be used when the value of the limit parameter of the list request is zero. This is optional, and the default is 100.

func (*GenericDAOBuilder[O]) SetLogger

func (b *GenericDAOBuilder[O]) SetLogger(value *slog.Logger) *GenericDAOBuilder[O]

SetLogger sets the logger. This is mandatory.

func (*GenericDAOBuilder[O]) SetMaxLimit

func (b *GenericDAOBuilder[O]) SetMaxLimit(value int) *GenericDAOBuilder[O]

SetMaxLimit sets the maximum number of items returned. This is optional and the default value is 1000.

func (*GenericDAOBuilder[O]) SetTable

func (b *GenericDAOBuilder[O]) SetTable(value string) *GenericDAOBuilder[O]

SetTable sets the table name. This is mandatory.

func (*GenericDAOBuilder[O]) SetTenancyLogic added in v0.0.12

func (b *GenericDAOBuilder[O]) SetTenancyLogic(value auth.TenancyLogic) *GenericDAOBuilder[O]

SetTenancyLogic sets the tenancy logic that will be used to determine the tenants value for objects. The logic receives the context as a parameter and should return the names of the tenants. If not provided, a default logic that returns no tenants will be used.

type ListRequest

type ListRequest struct {
	// Offset specifies the starting point.
	Offset int32

	// Limit specifies the maximum number of items.
	Limit int32

	// Filter is the CEL expression that defines which objects should be returned.
	Filter string
}

ListRequest represents the parameters for paginated queries.

type ListResponse

type ListResponse[I any] struct {
	// Size is the actual number of items returned.
	Size int32

	// Total is the total number of items available.
	Total int32

	// Items is the list of items.
	Items []I
}

ListResponse represents the result of a paginated query.

type Object

type Object interface {
	proto.Message
	GetId() string
	SetId(string)
}

Object is the interface that should be satisfied by objects to be managed by the generic DAO.

Jump to

Keyboard shortcuts

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