Skip to content

Commit b1c1b64

Browse files
committed
gorm: Setup initial project structure, schema, and db connection
1 parent 715de71 commit b1c1b64

File tree

5 files changed

+408
-0
lines changed

5 files changed

+408
-0
lines changed

go/gorm/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gorm

go/gorm/Makefile

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2016 The Cockroach Authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
# implied. See the License for the specific language governing
13+
# permissions and limitations under the License. See the AUTHORS file
14+
# for names of contributors.
15+
#
16+
# Author: Nathan VanBenschoten ([email protected])
17+
18+
.PHONY: start
19+
start:
20+
@go build
21+
@./gorm

go/gorm/main.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"net/http"
6+
7+
"github.com/jinzhu/gorm"
8+
_ "github.com/jinzhu/gorm/dialects/postgres"
9+
"github.com/julienschmidt/httprouter"
10+
)
11+
12+
func main() {
13+
db := setupDB()
14+
defer db.Close()
15+
16+
router := httprouter.New()
17+
18+
server := NewServer(db)
19+
server.RegisterRouter(router)
20+
21+
log.Fatal(http.ListenAndServe(":8080", router))
22+
}
23+
24+
func setupDB() *gorm.DB {
25+
db, err := gorm.Open("postgres", "postgresql://root@localhost:26257/company_gorm?sslmode=disable")
26+
if err != nil {
27+
panic("failed to connect database")
28+
}
29+
30+
// Migrate the schema
31+
migrateDB(db)
32+
33+
// Initialize the database if it's empty.
34+
var count int
35+
db.Model(&Product{}).Count(&count)
36+
if count == 0 {
37+
// Create Products.
38+
p1 := "P1"
39+
p2 := "P2"
40+
db.Create(&Product{Name: &p1, Price: 22.2})
41+
db.Create(&Product{Name: &p2, Price: 2.2})
42+
43+
// Create Customers.
44+
john := "John"
45+
fred := "Fred"
46+
db.Create(&Customer{Name: &john})
47+
db.Create(&Customer{Name: &fred})
48+
49+
// Create an Order.
50+
{
51+
tx := db.Begin()
52+
53+
var product Product
54+
tx.First(&product, "name = ?", "P2")
55+
56+
var customer Customer
57+
tx.First(&customer, "name = ?", "Fred")
58+
59+
tx.Create(&Order{
60+
Subtotal: product.Price,
61+
Customer: customer,
62+
Products: []Product{product},
63+
})
64+
tx.Commit()
65+
}
66+
}
67+
68+
return db
69+
}

go/gorm/models.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import "github.com/jinzhu/gorm"
4+
5+
// Customer is a model in the "customers" table.
6+
type Customer struct {
7+
ID int
8+
Name *string `gorm:"not null"`
9+
}
10+
11+
// Order is a model in the "orders" table.
12+
type Order struct {
13+
ID int
14+
Subtotal float64 `gorm:"type:decimal(18,2)"`
15+
16+
Customer Customer `gorm:"ForeignKey:CustomerID"`
17+
CustomerID int `json:"-"`
18+
19+
Products []Product `gorm:"many2many:order_products"`
20+
}
21+
22+
// Product is a model in the "products" table.
23+
type Product struct {
24+
ID int
25+
Name *string `gorm:"not null;unique"`
26+
Price float64 `gorm:"type:decimal(18,2)"`
27+
}
28+
29+
func migrateDB(db *gorm.DB) {
30+
db.AutoMigrate(&Customer{}, &Order{}, &Product{})
31+
}

0 commit comments

Comments
 (0)