Skip to content

Commit a8244ef

Browse files
authored
Merge pull request #4 from cockroachdb/nvanbenschoten/gorm
gorm: Setup initial project structure, schema, and db connection
2 parents 715de71 + b89e37a commit a8244ef

File tree

9 files changed

+843
-0
lines changed

9 files changed

+843
-0
lines changed

Makefile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
GO ?= go
19+
20+
.PHONY: all
21+
all: test
22+
23+
.PHONY: test
24+
test:
25+
$(GO) test -v -i ./testing
26+
$(GO) test -v ./testing
27+
28+
.PHONY: deps
29+
deps:
30+
$(GO) get -d -t ./...

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: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
ifneq ($(ADDR),)
19+
ADDRFLAG = -addr=$(ADDR)
20+
endif
21+
22+
.PHONY: start
23+
start:
24+
@go build
25+
@./gorm $(ADDRFLAG)

go/gorm/main.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"log"
6+
"net/http"
7+
8+
"github.com/cockroachdb/examples-orms/go/gorm/model"
9+
"github.com/jinzhu/gorm"
10+
_ "github.com/jinzhu/gorm/dialects/postgres"
11+
"github.com/julienschmidt/httprouter"
12+
)
13+
14+
var (
15+
addr = flag.String("addr", "postgresql://root@localhost:26257/company_gorm?sslmode=disable", "the address of the database")
16+
)
17+
18+
func main() {
19+
flag.Parse()
20+
21+
db := setupDB(*addr)
22+
defer db.Close()
23+
24+
router := httprouter.New()
25+
26+
server := NewServer(db)
27+
server.RegisterRouter(router)
28+
29+
log.Fatal(http.ListenAndServe(":6543", router))
30+
}
31+
32+
func setupDB(addr string) *gorm.DB {
33+
db, err := gorm.Open("postgres", addr)
34+
if err != nil {
35+
panic("failed to connect database")
36+
}
37+
38+
// Migrate the schema
39+
db.AutoMigrate(&model.Customer{}, &model.Order{}, &model.Product{})
40+
41+
return db
42+
}

go/gorm/model/models.go

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

0 commit comments

Comments
 (0)