---
tags: Golang
---
# Golang 筆記(2)-後端相關操作流程
### § create一個資料庫 (只有一開始建庫會用到)
* 創建
```
go run .\scripts\artisan.go DB create -e solomo
```
* 刪除
```
go run .\scripts\artisan.go DB drop -e solomo
```
* 一個資料夾是一個資料庫
D:\XinMedia-XinAPI\database\migrations\solomo
D:\XinMedia-XinAPI\database\migrations\member
### § create 該資料庫中的一個table
**1. 用指令產出up and down 檔案**
```
go run .\scripts\artisan.go migration [tableName] -e [dbName]
```
`dbName = member || solomo`
=> 執行後,自動會產出up and down檔案

**2. 進到up裡面,編輯完成table欄位 using SQL**
> SQL 建立table 指令參考
```
CREATE TABLE `product_classes` (
`id` int NOT NULL AUTO_INCREMENT,
`product_class_description` VARCHAR(15),
`updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY(`id`)
)
```
[SQL 基本語法補充資料](https://hackmd.io/@irishoho/H1yS2xrVI)
### § 執行migrate up (or down)
`artisan migrate up -e [dbName]`
>`go run .\scripts\artisan.go migrate up -e solomo`
`artisan migrate down -e [dbName]`
* 以dbName為單位,自動偵測尚未建立的up,跑一遍
* down 會回復一步
=> DB中會生成一個新的table
----
### § 建立models (go與資料庫溝通的規則)
**1. 執行 artisan model指令**
```
go run .\scripts\artisan.go model [modelName] -e [dbName]
```
**2. 產出成果放在 app/http/models**
> 只是空殼檔一個,需要自己定義


**3. 自己定義 struct**
| 名稱 |型別(go) | json檔顯示名稱 |db對應名稱|
| -------- | -------- | -------- | -------- |
|UserName | string | json:"user_name" | user_name|
```
type User struct {
ID uuid.UUID `json:"id" db:"id"`
UserName string `json:"user_name" db:"user_name"`
PassWord string `json:"pass_word" db:"pass_word"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}
```
### § 建立graphql (go與graphql溝通的規則)
### 1. 建立graphql 模板
在路徑 D:\XinMedia-XinAPI\schemas下建立檔案 order.graphql
#### 兩種模板: Query & Mutation
用意:
>* Query: get資料用的入口
>* Mutation: 新增/修改/刪除資料 用的入口
>* 一個專案通常只有這兩個大入口,其他都從這裡延伸
#### **Query (SELECT)**
> * 定義fn
* 傳入變數: ( id: Int!) !-必填
* 輸出變數 order
```
extend type Query{
getOrder(id: int!): order
//回傳一個結構
getAllOrder : [order!]!
}
```
>* 定義結構:
```
type order{
id: Int
price: Int
qty: String!
title
}
```
#### **Mutation (INSERT/UPDATE/DELETE)**
* 傳入變數: ( id: Int, price: Int, qty: String!)
* 輸出變數 order
```
extend type Mutation {
addOrder( id: Int, price: Int,
qty: String!): order
}
```
補充:
```
scalar Time
// qgl 擴充的時間戳記
```
### 2. 使用指令產生[name].resolvers.go檔案
* admin 的schemas用admin
`artican gql admin`
* api 的schemas用api
`artican gql api`
=> 執行完畢,產生檔案在:
`D:\XinMedia-XinAPI\app\graphql\admin\resolvers`

只是殼而已!!!
> 這段靠套件翻譯給GOLANG看的
> 每次有修改要重新run一次go
### 3. 依照需求,修改 [name].resolvers.go中的fn
```
func (r *mutationResolver) Hello(ctx context.Context, msg string) (string, error) {
newOrder := models.Order{Title:title, Qty: qty} 這個models 就是下面定義的
models_gen.order 這個models_gen是GQL的SCHEMAS
err = db.Create(&newOrder)
if err != nil{
return nil, err
}
return &newOrder, nil
```
* 和http中models檔案深度相關
引用
import (
"XinAPI/app/http/models" //import package
)
* m := new(models.Order)
找models的Order struct
```
type Order struct {
ID int `json:"id" db:"id"`
No string `json:"no" db:"no"`
Status int `j
}
```
* m.FindBySn(no) 取用 models的Order檔案裡的某個fn
### models裡面的各種fn
* 連接table `pop.Connect("member")`
pop是水牛設定好的fn
```
import (
"github.com/gobuffalo/pop"
)
```
----
### 長得很像的schema?
http modules
(後端取得資料用的)
```
type User struct {
ID uuid.UUID `json:"id" db:"id"`
UserName string `json:"user_name" db:"user_name"`
PassWord string `json:"pass_word" db:"pass_word"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
}
```
schema/admin
(graphQL 用的modles_gen.go )
```
extend type Query {
user(user_name: String): User
}
type User {
id: ID!
user_name: String!
}
```