Skip to content

Instantly share code, notes, and snippets.

@renowncoder
Forked from jquiterio/go_pg_json.go
Created January 3, 2024 22:06
Show Gist options
  • Save renowncoder/2899f4dc50e09a8a63b091c6e008ecc5 to your computer and use it in GitHub Desktop.
Save renowncoder/2899f4dc50e09a8a63b091c6e008ecc5 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"gorm.io/datatypes"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
type Planet struct {
ID uint `gorm:"primaryKey;autoIncrement"`
Name string `gorm:"unique;type:string;size:100"`
NumSatellites uint32
Specs JSONMap // from https://gist.github.com/jquiterio/ef31f8640ebe2960a8fd3a0bcccd9689
var db *gorm.DB
func init() {
var err error
dsn := "host=localhost user=medium dbname=medium port=5432 sslmode=disable"
if db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{}); err != nil {
panic(err)
}
// automigrate table planets
db.AutoMigrate(&Planet{})
}
func CreatePlanet() {
// declaring planet specs
earthSpecs := map[string]interface{}{
"alt_name": "gaia",
"main_composition": []string{"nitrogen", "oxygen"},
"others": map[string]interface{}{
"is_nice": true,
"orbital_period_days": 365,
},
}
marsSpecs := map[string]interface{}{
"main_composition": []string{
"carbon dioxide",
"argon",
"nitrogen",
"oxygen",
},
"is_red": true,
"others": map[string]interface{}{
"is_nice": "hum...",
"orbital_period_days": 686,
},
}
jupiterSpecs := map[string]interface{}{
"main_composition": []string{"hydrogen", "helium"},
}
// creating Array of Planets
earth := Planet{Name: "Earth", NumSatellites: 1, Specs: JSONMap(earthSpecs)}
mars := Planet{Name: "Mars", NumSatellites: 2, Specs: JSONMap(marsSpecs)}
jupiter := Planet{Name: "Jupiter", NumSatellites: 79, Specs: JSONMap(jupiterSpecs)}
planets := []Planet{earth, mars, jupiter}
db.Debug().Create(&planets)
}
func GetPlanetsWithAltName() {
planets := []Planet{}
db.Debug().Find(&planets, datatypes.JSONQuery("specs").HasKey("alt_name"))
fmt.Printf("%+v", planets)
}
func GetPlanetByAltName(alt_name string) {
planets := []Planet{}
db.First(&planets, datatypes.JSONQuery("specs").Equals(alt_name, "alt_name"))
fmt.Printf("%+v", planets)
}
func GetPlanetsByComposition(composition string) {
planets := []Planet{}
db.Debug().Model(&Planet{}).Find(&planets, datatypes.JSONQuery("specs").HasKey("main_composition", composition))
fmt.Printf("%+v", planets)
}
func GetAllPlanets() {
planets := []Planet{}
db.Find(&planets)
fmt.Printf("%+v", planets)
}
func UpdatePlanetByItsComposition(composition string) {
planets := []Planet{}
updatedPlanets := []Planet{}
db.Debug().Model(&Planet{}).Find(&planets, datatypes.JSONQuery("specs").HasKey("main_composition", composition))
for _, planet := range planets {
specs := planet.Specs
specs["type"] = "cool!"
planet.Specs = JSONMap(specs)
db.Save(&planet)
}
db.Debug().Model(&Planet{}).Find(&updatedPlanets, datatypes.JSONQuery("specs").HasKey("type", "cool!"))
fmt.Printf("%+v", updatedPlanets)
}
func DeletePlanetWithoutAComposition(composition string) {
planets := []Planet{}
db.Not(datatypes.JSONQuery("specs").HasKey("main_composition", composition)).Find(&planets)
for _, planet := range planets {
db.Delete(&planet)
}
//double check
db.Find(&planets)
fmt.Printf("%+v", planets)
}
func main() {
CreatePlanet()
GetAllPlanets()
GetPlanetsWithAltName()
GetPlanetByAltName("gaia")
GetPlanetsByComposition("oxygen")
UpdatePlanetByItsComposition("oxygen")
DeletePlanetWithoutAComposition("oxygen")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment