reader

package
v0.2.7 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2022 License: Apache-2.0 Imports: 16 Imported by: 0

README

###Reader usage

You can create Reader instance using constructor method. If all of your Views that uses reader are initialized by calling View#Init method, or by using View loaded by NewResourceFromUrl method, you can pass emptry Resource:

emptyResource := data.EmptyResource()
service := reader.New(emptyResource)

Resource is needed only to try to initialize View unless View was initialized earlier.

In order to read data you need data.Session:

var fooView *data.View 

session := &data.Session{
	Dest: new(interface{}),
	View: fooView, 
	AllowUnmapped: true,
	
	Selectors:     Selectors{},
	Subject:     "",
	HttpRequest: *http.Request{},
	MatchedPath: "",
}
  • Dest is required. It has to be a pointer to interface{} or pointer to slice of T or *T
  • View is required. Due to optimization reasons it is important to create one instance, share it across the system and provide fully initialized View. For View creation see: create programmatically or load from file
  • Selectors are not used when datly is used only to communicate with database, but may be used when datly is configured also as request handler (see TODO)
  • AllowUnmapped is optional, by default is set to false. It is being used when some database table columns doesn't match View.Schema type.
  • Subject is optional. It is being used to build criteria and represents logged user.
  • HttpRequest is optional, needed only if any of Parameter location is path, query, cookie, header or if datly is used as request handler.
  • MatchedPath is optional, needed only if any of Parameter location is path or if datly is used as request handler. You can now read data from database using the reader instance.

You should let reader initialize the session. New session has to be created for each time you call reader.Read. Having reader and session you can fetch data from database:

err := service.Read(context.TODO(), session)
if err != nil {
	// ... handle error
}

toolbox.DumpIndent(session.Dest, false) //prints view fetched from database

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder added in v0.2.0

type Builder struct{}

Builder represent SQL Builder

func NewBuilder added in v0.2.0

func NewBuilder() *Builder

NewBuilder creates Builder instance

func (*Builder) Build added in v0.2.0

func (b *Builder) Build(aView *view.View, selector *view.Selector, batchData *view.BatchData, relation *view.Relation, parentOfAclView *view.View) (string, []interface{}, error)

Build builds SQL Select statement

type Event added in v0.2.0

type Event string
const (
	Pending Event = "Pending"
	Error   Event = "Error"
	Success Event = "Success"
)

type Metric added in v0.2.7

type Metric struct {
	View      string
	Elapsed   string
	ElapsedMs int
	Rows      int
}

Session groups view required to Read view

type OnFetcher added in v0.2.0

type OnFetcher interface {
	OnFetch(ctx context.Context) error
}

OnFetcher lifecycle interface that if entity implements, OnFetch(ctx context.Context) is call after record is fetched from db

type OnRelationer added in v0.2.0

type OnRelationer interface {
	OnRelation(ctx context.Context)
}

OnRelationer lifecycle interface that if entity implements, OnRelation(ctx context.Context) is call after relation is assembled

type Service

type Service struct {
	Resource *view.Resource
	// contains filtered or unexported fields
}

Service represents reader service

func New

func New() *Service

New creates Service instance

func (*Service) Read

func (s *Service) Read(ctx context.Context, session *Session) error

Read select view from database based on View and assign it to dest. ParentDest has to be pointer.

type Session added in v0.2.0

type Session struct {
	Dest      interface{} //slice
	View      *view.View
	Selectors *view.Selectors
	Parent    *view.View
	Metrics   []*Metric
	// contains filtered or unexported fields
}

Session groups view required to Read view

func NewSession added in v0.2.0

func NewSession(dest interface{}, aView *view.View) *Session

NewSession creates a session

func (*Session) AddCriteria added in v0.2.1

func (s *Session) AddCriteria(aView *view.View, criteria string, placeholders ...interface{})

AddCriteria adds the supplied view criteria

func (*Session) AddMetric added in v0.2.7

func (s *Session) AddMetric(m *Metric)

func (*Session) Init added in v0.2.0

func (s *Session) Init() error

Init initializes session

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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