publish

package module
v0.0.0-...-5924cd4 Latest Latest
Warning

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

Go to latest
Published: May 7, 2019 License: MIT Imports: 15 Imported by: 1

README

Publish

The Publish Plugin decouples the timing of data updates in the QOR Admin interface from the display of data on the frontend of the website which QOR Admin is backing. Effectively, a GORM model can be withheld from frontend display until it is "published".

GoDoc

Usage

Embed publish.Status as an anonymous field in your model to apply the Publish feature.

type Product struct {
  Name        string
  Description string
  publish.Status
}
db, err = gorm.Open("sqlite3", "demo_db")
publish := publish.New(db)

// Run migration to add `publish_status` column from `publish.Status`
db.AutoMigrate(&Product{})
// Create `products_draft` table
publish.AutoMigrate(&Product{})

draftDB := publish.DraftDB() // Draft resources are saved here
productionDB := publish.ProductionDB() // Published resources saved here

With the draft db, all your changes will be saved in products_draft, with the productionDB, all your changes will be in products table and sync back to products_draft.

// Publish changes you made in `products_draft` to `products`
publish.Publish(&product)

// Overwrite this product's data in `products_draft` with its data in `products`
publish.Discard(&product)

Publish Event

To avoid a large amount (n) of draft events when applying the Publish feature to a set of (n) records, it is possible to batch the events into a single, high-level event which represents the set of (n) events. To do this, use the PublishEvent feature.

For example, when sorting products, say you have changed 100 products' positions but don't want to show 100 products as draft nor have to go through the horrible process of publishing 100 products, one at a time. Instead, you can show a single, high-level event such as Changed products' sorting in the draft page. After publishing this single event, all of the associated position changes will be published.

// Register Publish Event
publish.RegisterEvent("changed_product_sorting", changedSortingPublishEvent{})

// Publish Event definition
type changedSortingPublishEvent struct {
  Table       string
  PrimaryKeys []string
}

func (e changedSortingPublishEvent) Publish(db *gorm.DB, event publish.PublishEventInterface) (err error) {
  if event, ok := event.(*publish.PublishEvent); ok {
    if err = json.Unmarshal([]byte(event.Argument), &e); err == nil {
      // e.PrimaryKeys => get primary keys
      // sync your changes made for above primary keys to production
    }
  }
}

func (e changedSortingPublishEvent) Discard(db *gorm.DB, event publish.PublishEventInterface) (err error) {
  // discard your changes made in draft
}

func init() {
  // change an `changed_product_sorting` event, including primary key 1, 2, 3
  db.Create(&publish.PublishEvent{
    Name: "changed_product_sorting",
    Argument: "[1,2,3]",
  })
}

Store all changes in draft table by default

If you want all changes made to be stored in the draft table by default, initialize QOR Admin with the Publish value's draft DB. If you then want to manage those draft data, add the Publish value as resource to QOR Admin:

Admin := admin.New(&qor.Config{DB: Publish.DraftDB()})
Admin.AddResource(Publish)

Publish Demo: http://demo.getqor.com/admin/publish

License

Released under the MIT License.

Documentation

Index

Constants

View Source
const (
	// PUBLISHED publish status published
	PUBLISHED = false
	// DIRTY publish status dirty
	DIRTY = true
)
View Source
const (
	// PublishPermission publish permission
	PublishPermission roles.PermissionMode = "publish"
)

Variables

This section is empty.

Functions

func DraftTableName

func DraftTableName(table string) string

DraftTableName get draft table name of passed in string

func IsDraftMode

func IsDraftMode(db *gorm.DB) bool

IsDraftMode check if current db in draft mode

func IsPublishEvent

func IsPublishEvent(model interface{}) (ok bool)

IsPublishEvent check if current model is a publish event model

func IsPublishableModel

func IsPublishableModel(model interface{}) (ok bool)

IsPublishableModel check if current model is a publishable

func OriginalTableName

func OriginalTableName(table string) string

OriginalTableName get original table name of passed in string

func RegisterEvent

func RegisterEvent(name string, event EventInterface)

RegisterEvent register publish event

Types

type EventInterface

type EventInterface interface {
	Publish(db *gorm.DB, event PublishEventInterface) error
	Discard(db *gorm.DB, event PublishEventInterface) error
}

EventInterface defined methods needs for a publish event

type LoggerInterface

type LoggerInterface interface {
	Print(...interface{})
}

LoggerInterface logger interface used to print publish logs

var Logger LoggerInterface

Logger default logger used to print publish logs

type Publish

type Publish struct {
	DB              *gorm.DB
	SearchHandler   func(db *gorm.DB, context *qor.Context) *gorm.DB
	WorkerScheduler *worker.Worker
	// contains filtered or unexported fields
}

Publish defined a publish struct

func New

func New(db *gorm.DB) *Publish

New initialize a publish instance

func (*Publish) AutoMigrate

func (pb *Publish) AutoMigrate(values ...interface{})

AutoMigrate run auto migrate in draft tables

func (*Publish) ConfigureQorResource

func (publish *Publish) ConfigureQorResource(res resource.Resourcer)

ConfigureQorResource configure qor resource for qor admin

func (*Publish) ConfigureQorResourceBeforeInitialize

func (publish *Publish) ConfigureQorResourceBeforeInitialize(res resource.Resourcer)

ConfigureQorResourceBeforeInitialize configure qor resource when initialize qor admin

func (Publish) Discard

func (pb Publish) Discard(records ...interface{})

Discard discard records

func (Publish) DraftDB

func (pb Publish) DraftDB() *gorm.DB

DraftDB get db in draft mode

func (Publish) Logger

func (pb Publish) Logger(l LoggerInterface) *Publish

Logger set logger that used to print publish logs

func (Publish) ProductionDB

func (pb Publish) ProductionDB() *gorm.DB

ProductionDB get db in production mode

func (Publish) Publish

func (pb Publish) Publish(records ...interface{})

Publish publish records

func (*Publish) SetWorker

func (publish *Publish) SetWorker(w *worker.Worker)

SetWorker set publish's worker

type PublishEvent

type PublishEvent struct {
	gorm.Model
	Name          string
	Description   string
	Argument      string `sql:"size:65532"`
	PublishStatus bool
	PublishedBy   string
}

PublishEvent default publish event model

func (*PublishEvent) Discard

func (publishEvent *PublishEvent) Discard(db *gorm.DB) error

Discard discard data

func (*PublishEvent) Publish

func (publishEvent *PublishEvent) Publish(db *gorm.DB) error

Publish publish data

func (PublishEvent) VisiblePublishResource

func (PublishEvent) VisiblePublishResource(*qor.Context) bool

VisiblePublishResource force to display publish event in publish drafts even it is hidden in the menus

type PublishEventInterface

type PublishEventInterface interface {
	Publish(*gorm.DB) error
	Discard(*gorm.DB) error
}

PublishEventInterface defined publish event itself's interface

type QorWorkerArgument

type QorWorkerArgument struct {
	IDs []string
	worker.Schedule
}

QorWorkerArgument used for qor publish job's argument

type Status

type Status struct {
	PublishStatus bool
}

Status publish status, need to be embedded in your models to get the publish feature

func (Status) ConfigureQorResource

func (s Status) ConfigureQorResource(res resource.Resourcer)

ConfigureQorResource configure qor resource for qor admin

func (Status) GetPublishStatus

func (s Status) GetPublishStatus() bool

GetPublishStatus get publish status

func (*Status) SetPublishStatus

func (s *Status) SetPublishStatus(status bool)

SetPublishStatus set publish status

Jump to

Keyboard shortcuts

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