Init commit. DB, Connection, Create ProductTable
This commit is contained in:
commit
e01312c15e
Binary file not shown.
|
@ -0,0 +1,31 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
pg "github.com/go-pg/pg"
|
||||
)
|
||||
|
||||
func Connect() {
|
||||
opts := &pg.Options{
|
||||
User: "postgres",
|
||||
Password: "",
|
||||
Addr: "localhost:5432",
|
||||
Database: "myDB",
|
||||
}
|
||||
var db *pg.DB = pg.Connect(opts)
|
||||
if db == nil {
|
||||
log.Printf("Fail onnect")
|
||||
os.Exit(100)
|
||||
}
|
||||
log.Printf("Connected :)")
|
||||
CreateProductItemsTable(db)
|
||||
closeErr := db.Close()
|
||||
if closeErr != nil {
|
||||
log.Printf("Error with close connection", closeErr)
|
||||
os.Exit(100)
|
||||
}
|
||||
log.Printf("Connection closed")
|
||||
return
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package db
|
||||
|
||||
import(
|
||||
"log"
|
||||
"time"
|
||||
orm "github.com/go-pg/pg/orm"
|
||||
pg "github.com/go-pg/pg"
|
||||
)
|
||||
|
||||
type ProductItem struct {
|
||||
RefPointer int `sql:"-"`
|
||||
tableName struct{} `sql:"product_items_collection"`
|
||||
ID int `sql:"id,pk"`
|
||||
Name string `sql:"name,unique"`
|
||||
Desc string `sql:"desc"`
|
||||
Image string `sql:"image"`
|
||||
Price float64 `sql:"price,type:real"`
|
||||
Features struct {
|
||||
Name string
|
||||
Desc string
|
||||
Imp int
|
||||
} `sql:"features,type:jsonb"`
|
||||
CreateAt time.Time `sql:"created_at"`
|
||||
UpdateAt time.Time `sql:"update_at"`
|
||||
IsActive bool `sql:"is_active"`
|
||||
}
|
||||
|
||||
func CreateProductItemsTable(db *pg.DB) error {
|
||||
opts := &orm.CreateTableOptions{
|
||||
IfNotExists: true,
|
||||
}
|
||||
createErr := db.CreateTable(&ProductItem{}, opts)
|
||||
if createErr != nil {
|
||||
log.Printf("Blad z dodaniem productItem, Reason: %v\n", createErr)
|
||||
return createErr
|
||||
}
|
||||
log.Printf("Table ProductItems utworzone. \n")
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
hash: a6505100564a0f9d000c6777b4c353a643016a03db7fce6477b29faf64a8be0f
|
||||
updated: 2018-12-19T16:30:49.538935+01:00
|
||||
imports:
|
||||
- name: github.com/go-pg/pg
|
||||
version: ae5d5e7df4b2e598390e10b66b849c6af94f092b
|
||||
testImports: []
|
|
@ -0,0 +1,4 @@
|
|||
package: .
|
||||
import:
|
||||
- package: github.com/go-pg/pg
|
||||
version: v6.15.1
|
Loading…
Reference in New Issue