GORM - basic

Pooling

connection = [1]in use [2]not use & return to the pool

fail to release connections back to pool => running out of resources.

username:password@tcp(127.0.0.1:3001)/dbname

Don’t Open() and Close() databases frequently.

always prepare queries to be used multiple times = Prepared statements is better than concatenating strings (SQL injection)

GORM - psql

import (
    "github.com/jinzhu/gorm"
    _ "github.com/jinzhu/gorm/dialects/postgres"
)

func main() {
  db, err := gorm.Open(
    "postgres", 
    "host=myhost user=gorm dbname=gorm sslmode=disable password=mypassword"
  )
  defer db.Close()
}

GORM - sqlite

GORM - migrate

AutoMigrate: 只會創建table, row, index. 不會change type, delete.

GORM - CRUD

Last updated