Documentation
¶
Index ¶
- type Connection
- type ConnectionManager
- type ListenNotifySubscriptionManager
- func (sm *ListenNotifySubscriptionManager) Close() error
- func (sm *ListenNotifySubscriptionManager) Init() (rErr error)
- func (sm *ListenNotifySubscriptionManager) ListenerEventCallback(_ pq.ListenerEventType, err error)
- func (sm *ListenNotifySubscriptionManager) Notify(ctx context.Context, c Connection, event interface{}) error
- func (sm *ListenNotifySubscriptionManager) PublishCreateEvent(ctx context.Context, e frameless.CreateEvent) error
- func (sm *ListenNotifySubscriptionManager) PublishDeleteAllEvent(ctx context.Context, e frameless.DeleteAllEvent) error
- func (sm *ListenNotifySubscriptionManager) PublishDeleteByIDEvent(ctx context.Context, e frameless.DeleteByIDEvent) error
- func (sm *ListenNotifySubscriptionManager) PublishUpdateEvent(ctx context.Context, e frameless.UpdateEvent) error
- func (sm *ListenNotifySubscriptionManager) SubscribeToCreatorEvents(ctx context.Context, s frameless.CreatorSubscriber) (frameless.Subscription, error)
- func (sm *ListenNotifySubscriptionManager) SubscribeToDeleterEvents(ctx context.Context, s frameless.DeleterSubscriber) (frameless.Subscription, error)
- func (sm *ListenNotifySubscriptionManager) SubscribeToUpdaterEvents(ctx context.Context, s frameless.UpdaterSubscriber) (frameless.Subscription, error)
- type Mapper
- type Mapping
- type MetaAccessor
- type Storage
- func (pg *Storage) BeginTx(ctx context.Context) (context.Context, error)
- func (pg *Storage) Close() error
- func (pg *Storage) CommitTx(ctx context.Context) error
- func (pg *Storage) Create(ctx context.Context, ptr interface{}) (rErr error)
- func (pg *Storage) DeleteAll(ctx context.Context) (rErr error)
- func (pg *Storage) DeleteByID(ctx context.Context, id interface{}) (rErr error)
- func (pg *Storage) FindAll(ctx context.Context) frameless.Iterator
- func (pg *Storage) FindByID(ctx context.Context, ptr, id interface{}) (bool, error)
- func (pg *Storage) RollbackTx(ctx context.Context) error
- func (pg *Storage) SubscribeToCreatorEvents(ctx context.Context, s frameless.CreatorSubscriber) (frameless.Subscription, error)
- func (pg *Storage) SubscribeToDeleterEvents(ctx context.Context, s frameless.DeleterSubscriber) (frameless.Subscription, error)
- func (pg *Storage) SubscribeToUpdaterEvents(ctx context.Context, s frameless.UpdaterSubscriber) (frameless.Subscription, error)
- func (pg *Storage) Update(ctx context.Context, ptr interface{}) (rErr error)
- type SubscriptionManager
- type T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Connection ¶
type ConnectionManager ¶
type ConnectionManager interface {
io.Closer
// Connection returns the current context's connection.
// This can be a *sql.DB or if we are within a transaction, then an *sql.Tx
Connection(ctx context.Context) (Connection, error)
BeginTx(ctx context.Context) (context.Context, error)
CommitTx(ctx context.Context) error
RollbackTx(ctx context.Context) error
}
Example ¶
package main
import (
"context"
"github.com/adamluzsi/frameless/postgresql"
)
func main() {
connectionManager := postgresql.NewConnectionManager(`dsn`)
defer connectionManager.Close()
ctx := context.Background()
c, err := connectionManager.Connection(ctx)
if err != nil {
panic(err)
}
_, err = c.ExecContext(ctx, `SELECT VERSION()`)
if err != nil {
panic(err)
}
}
func NewConnectionManager ¶
func NewConnectionManager(dsn string) ConnectionManager
type ListenNotifySubscriptionManager ¶
type ListenNotifySubscriptionManager struct {
T interface{}
Mapping Mapping
MetaAccessor MetaAccessor
ConnectionManager ConnectionManager
Listener *pq.Listener
DSN string
ReconnectMinInterval time.Duration
ReconnectMaxInterval time.Duration
// contains filtered or unexported fields
}
func NewListenNotifySubscriptionManager ¶
func NewListenNotifySubscriptionManager(T T, m Mapping, dsn string, cm ConnectionManager) *ListenNotifySubscriptionManager
func (*ListenNotifySubscriptionManager) Close ¶
func (sm *ListenNotifySubscriptionManager) Close() error
func (*ListenNotifySubscriptionManager) Init ¶
func (sm *ListenNotifySubscriptionManager) Init() (rErr error)
Init will initialize the ListenNotifySubscriptionManager The ctx argument must represent a process lifetime level context.Context. Otherwise, context.Background() is expected for it.
func (*ListenNotifySubscriptionManager) ListenerEventCallback ¶
func (sm *ListenNotifySubscriptionManager) ListenerEventCallback(_ pq.ListenerEventType, err error)
func (*ListenNotifySubscriptionManager) Notify ¶
func (sm *ListenNotifySubscriptionManager) Notify(ctx context.Context, c Connection, event interface{}) error
func (*ListenNotifySubscriptionManager) PublishCreateEvent ¶
func (sm *ListenNotifySubscriptionManager) PublishCreateEvent(ctx context.Context, e frameless.CreateEvent) error
func (*ListenNotifySubscriptionManager) PublishDeleteAllEvent ¶
func (sm *ListenNotifySubscriptionManager) PublishDeleteAllEvent(ctx context.Context, e frameless.DeleteAllEvent) error
func (*ListenNotifySubscriptionManager) PublishDeleteByIDEvent ¶
func (sm *ListenNotifySubscriptionManager) PublishDeleteByIDEvent(ctx context.Context, e frameless.DeleteByIDEvent) error
func (*ListenNotifySubscriptionManager) PublishUpdateEvent ¶
func (sm *ListenNotifySubscriptionManager) PublishUpdateEvent(ctx context.Context, e frameless.UpdateEvent) error
func (*ListenNotifySubscriptionManager) SubscribeToCreatorEvents ¶
func (sm *ListenNotifySubscriptionManager) SubscribeToCreatorEvents(ctx context.Context, s frameless.CreatorSubscriber) (frameless.Subscription, error)
func (*ListenNotifySubscriptionManager) SubscribeToDeleterEvents ¶
func (sm *ListenNotifySubscriptionManager) SubscribeToDeleterEvents(ctx context.Context, s frameless.DeleterSubscriber) (frameless.Subscription, error)
func (*ListenNotifySubscriptionManager) SubscribeToUpdaterEvents ¶
func (sm *ListenNotifySubscriptionManager) SubscribeToUpdaterEvents(ctx context.Context, s frameless.UpdaterSubscriber) (frameless.Subscription, error)
type Mapper ¶
type Mapper struct {
// Table is the entity's table name
Table string
// ID is the entity's id column name
ID string
// Columns hold the entity's column names
Columns []string
NewIDFn func(ctx context.Context) (interface{}, error)
ToArgsFn func(ptr interface{}) ([]interface{}, error)
MapFn iterators.SQLRowMapperFunc
}
Example ¶
package main
import (
"context"
"time"
"github.com/adamluzsi/frameless/iterators"
"github.com/adamluzsi/frameless/postgresql"
)
func main() {
type ExampleEntity struct {
ID int64
Col1 int
Col2 string
Col3 bool
}
_ = postgresql.Mapper /* [ExampleEntity] */ {
Table: `"public"."entities"`,
ID: "entity_id",
Columns: []string{"entity_id", "col1", "col2", "col3"},
NewIDFn: func(ctx context.Context) (interface{}, error) {
// a really bad way to make id,
// but this is only an example
return time.Now().UnixNano(), nil
},
ToArgsFn: func(ptr interface{}) ([]interface{}, error) {
ent := ptr.(*ExampleEntity) // Go1.18 will solve this with generics
return []interface{}{ent.ID, ent.Col1, ent.Col2, ent.Col3}, nil
},
MapFn: func(scanner iterators.SQLRowScanner, ptr interface{}) error {
ent := ptr.(*ExampleEntity)
return scanner.Scan(&ent.ID, &ent.Col1, &ent.Col2, &ent.Col3)
},
}
}
func (Mapper) ColumnRefs ¶
type Mapping ¶
type Mapping interface {
// TableRef is the entity's postgresql table name.
// eg.:
// - "public"."table_name"
// - "table_name"
// - table_name
//
TableRef() string
// IDRef is the entity's id column name, which can be used to access an individual record for update purpose.
IDRef() string
// ColumnRefs are the table's column names.
// The order of the column names related to Row mapping and query argument passing.
ColumnRefs() []string
// NewID creates a stateless entity id that can be used by CREATE operation.
// Serial and similar id solutions not supported without serialize transactions.
NewID(context.Context) (interface{}, error)
// ToArgs convert an entity ptr to a list of query argument that can be used for CREATE or UPDATE purpose.
ToArgs(ptr interface{}) ([]interface{}, error)
iterators.SQLRowMapper
}
type Storage ¶
type Storage struct {
T interface{}
Mapping Mapping
ConnectionManager ConnectionManager
SubscriptionManager SubscriptionManager
MetaAccessor
}
Storage is a frameless external resource supplier to store a certain entity type. The Storage supplier itself is a stateless entity.
SRP: DBA
Example ¶
package main
import (
"context"
"math/rand"
"os"
"github.com/adamluzsi/frameless/iterators"
"github.com/adamluzsi/frameless/postgresql"
)
func main() {
type Entity struct {
ID int `ext:"ID"`
Value string
}
mapping := postgresql.Mapper{
Table: "entities",
ID: "id",
Columns: []string{`id`, `value`},
NewIDFn: func(ctx context.Context) (interface{}, error) {
// only example, don't do this in production code.
return rand.Int(), nil
},
ToArgsFn: func(ptr interface{}) ([]interface{}, error) {
ent := ptr.(*Entity)
return []interface{}{ent.ID, ent.Value}, nil
},
MapFn: func(s iterators.SQLRowScanner, ptr interface{}) error {
ent := ptr.(*Entity)
return s.Scan(&ent.ID, &ent.Value)
},
}
stg := postgresql.NewStorageByDSN(Entity{}, mapping, os.Getenv("DATABASE_URL"))
defer stg.Close()
}
func NewStorageByDSN ¶
func (*Storage) DeleteByID ¶
func (*Storage) SubscribeToCreatorEvents ¶
func (pg *Storage) SubscribeToCreatorEvents(ctx context.Context, s frameless.CreatorSubscriber) (frameless.Subscription, error)
func (*Storage) SubscribeToDeleterEvents ¶
func (pg *Storage) SubscribeToDeleterEvents(ctx context.Context, s frameless.DeleterSubscriber) (frameless.Subscription, error)
func (*Storage) SubscribeToUpdaterEvents ¶
func (pg *Storage) SubscribeToUpdaterEvents(ctx context.Context, s frameless.UpdaterSubscriber) (frameless.Subscription, error)
type SubscriptionManager ¶
type SubscriptionManager interface {
io.Closer
PublishCreateEvent(ctx context.Context, e frameless.CreateEvent) error
PublishUpdateEvent(ctx context.Context, e frameless.UpdateEvent) error
PublishDeleteByIDEvent(ctx context.Context, e frameless.DeleteByIDEvent) error
PublishDeleteAllEvent(ctx context.Context, e frameless.DeleteAllEvent) error
SubscribeToCreatorEvents(ctx context.Context, s frameless.CreatorSubscriber) (frameless.Subscription, error)
SubscribeToUpdaterEvents(ctx context.Context, s frameless.UpdaterSubscriber) (frameless.Subscription, error)
SubscribeToDeleterEvents(ctx context.Context, s frameless.DeleterSubscriber) (frameless.Subscription, error)
}
Example ¶
package main
import (
"os"
"github.com/adamluzsi/frameless/postgresql"
)
func main() {
type ExampleEntity struct {
ID string `ext:"id"`
}
connectionManager := postgresql.NewConnectionManager(os.Getenv(`DATABASE_URL`))
mapping := postgresql.Mapper{ /* real mapping data here */ }
subscriptionManager := postgresql.NewListenNotifySubscriptionManager(ExampleEntity{}, mapping, os.Getenv("DATABASE_URL"), connectionManager)
defer subscriptionManager.Close()
}
Source Files
¶
Click to show internal directories.
Click to hide internal directories.