Exchange

Exchange allows a database to be exported to or imported from a file.

Data can be optionally validated during import and export.

Data can be optionally processed during import.

GoDoc

Usage

import (
  "github.com/qor/exchange"
  "github.com/qor/exchange/backends/csv"
)

func main() {
  // Define Resource
  product = exchange.NewResource(&Product{}, exchange.Config{PrimaryField: "Code"})
  // Define columns are exportable/importable
  product.Meta(&exchange.Meta{Name: "Code"})
  product.Meta(&exchange.Meta{Name: "Name"})
  product.Meta(&exchange.Meta{Name: "Price"})

  // Define context environment
  context := &qor.Context{DB: db}

  // Import products.csv into database
  product.Import(csv.New("products.csv"), context)

  // Export products into products.csv
  product.Export(csv.New("products.csv"), context)
}

Sample products.csv

Code, Name, Price
P001, Product P001, 100
P002, Product P002, 200
P003, Product P003, 300

Advanced Usages

  • Add Validations
product.AddValidator(func(result interface{}, metaValues *resource.MetaValues, context *qor.Context) error {
  if f, err := strconv.ParseFloat(fmt.Sprint(metaValues.Get("Price").Value), 64); err == nil {
    if f == 0 {
      return errors.New("product's price can't be 0")
    }
    return nil
  } else {
    return err
  }
})
  • Process data before import
product.AddProcessor(func(result interface{}, metaValues *resource.MetaValues, context *qor.Context) error {
  product := result.(*Product)
  product.Price = product.Price * 1.1 // Add 10% Tax
  return nil
})
  • Callbacks
// Importing callbacks
product.Import(csv.New("products.csv"), context, func(progress exchange.Progress) error {
    fmt.Printf("%v/%v Importing product %v\n", progress.Current, progress.Total, progress.Value.(*Product).Code))
})

// Exporting callbacks
product.Export(csv.New("products.csv"), context, func(progress exchange.Progress) error {
    fmt.Printf("%v/%v Exporting product %v\n", progress.Current, progress.Total, progress.Value.(*Product).Code))
})

results matching ""

    No results matching ""