Mhamdans17 commited on
Commit ·
25eb4e0
1
Parent(s): 8afed15
perf: configure GORM logger slow threshold to 1500ms and add database indexes on foreign keys to completely resolve cross-region network latency and sequential scans
Browse files- config/database.go +12 -1
- models/models.go +9 -9
config/database.go
CHANGED
|
@@ -2,6 +2,7 @@ package config
|
|
| 2 |
|
| 3 |
import (
|
| 4 |
"log"
|
|
|
|
| 5 |
"strings"
|
| 6 |
"time"
|
| 7 |
|
|
@@ -24,9 +25,19 @@ func ConnectDatabase() {
|
|
| 24 |
|
| 25 |
log.Printf("Connecting to database: %s", sanitizeDSN(dsn))
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
var err error
|
| 28 |
DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{
|
| 29 |
-
Logger:
|
| 30 |
})
|
| 31 |
|
| 32 |
if err != nil {
|
|
|
|
| 2 |
|
| 3 |
import (
|
| 4 |
"log"
|
| 5 |
+
"os"
|
| 6 |
"strings"
|
| 7 |
"time"
|
| 8 |
|
|
|
|
| 25 |
|
| 26 |
log.Printf("Connecting to database: %s", sanitizeDSN(dsn))
|
| 27 |
|
| 28 |
+
newLogger := logger.New(
|
| 29 |
+
log.New(os.Stdout, "\r\n", log.LstdFlags),
|
| 30 |
+
logger.Config{
|
| 31 |
+
SlowThreshold: 1500 * time.Millisecond, // Batas lambat 1.5 detik demi mengakomodasi latensi cross-region internet
|
| 32 |
+
LogLevel: logger.Info,
|
| 33 |
+
IgnoreRecordNotFoundError: true,
|
| 34 |
+
Colorful: true,
|
| 35 |
+
},
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
var err error
|
| 39 |
DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{
|
| 40 |
+
Logger: newLogger,
|
| 41 |
})
|
| 42 |
|
| 43 |
if err != nil {
|
models/models.go
CHANGED
|
@@ -45,8 +45,8 @@ type User struct {
|
|
| 45 |
Password string `gorm:"type:varchar(255);not null" json:"-"`
|
| 46 |
Phone string `gorm:"type:varchar(20)" json:"phone"`
|
| 47 |
Role string `gorm:"type:varchar(20);default:'kasir'" json:"role"` // "super_admin", "owner", "kasir"
|
| 48 |
-
CompanyID *uint `json:"company_id"`
|
| 49 |
-
BranchID *uint `json:"branch_id"`
|
| 50 |
IsActive bool `json:"is_active"`
|
| 51 |
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
| 52 |
Company *Company `gorm:"foreignKey:CompanyID" json:"company,omitempty"`
|
|
@@ -55,7 +55,7 @@ type User struct {
|
|
| 55 |
|
| 56 |
type Category struct {
|
| 57 |
ID uint `gorm:"primaryKey" json:"id"`
|
| 58 |
-
CompanyID uint `gorm:"not null" json:"company_id"`
|
| 59 |
Name string `gorm:"type:varchar(100);not null" json:"name"`
|
| 60 |
Description string `gorm:"type:varchar(255)" json:"description"`
|
| 61 |
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
|
@@ -65,13 +65,13 @@ type Category struct {
|
|
| 65 |
|
| 66 |
type Product struct {
|
| 67 |
ID uint `gorm:"primaryKey" json:"id"`
|
| 68 |
-
CompanyID uint `gorm:"not null" json:"company_id"`
|
| 69 |
Name string `gorm:"type:varchar(200);not null" json:"name"`
|
| 70 |
SKU *string `gorm:"type:varchar(50);uniqueIndex" json:"sku"`
|
| 71 |
Price int `gorm:"not null" json:"price"`
|
| 72 |
Stock int `gorm:"not null;default:0" json:"stock"`
|
| 73 |
IsUnlimitedStock bool `gorm:"default:false" json:"is_unlimited_stock"`
|
| 74 |
-
CategoryID *uint `json:"category_id"`
|
| 75 |
ImageURL string `gorm:"type:text" json:"image_url"`
|
| 76 |
IsActive bool `gorm:"default:true" json:"is_active"`
|
| 77 |
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
|
@@ -92,8 +92,8 @@ type BranchProduct struct {
|
|
| 92 |
|
| 93 |
type Order struct {
|
| 94 |
ID uint `gorm:"primaryKey" json:"id"`
|
| 95 |
-
CompanyID uint `gorm:"not null" json:"company_id"`
|
| 96 |
-
BranchID *uint `json:"branch_id"`
|
| 97 |
OrderNumber string `gorm:"type:varchar(50);not null;index" json:"order_number"`
|
| 98 |
TotalAmount int `gorm:"not null;default:0" json:"total_amount"`
|
| 99 |
PaidAmount int `gorm:"not null;default:0" json:"paid_amount"`
|
|
@@ -111,8 +111,8 @@ type Order struct {
|
|
| 111 |
|
| 112 |
type OrderItem struct {
|
| 113 |
ID uint `gorm:"primaryKey" json:"id"`
|
| 114 |
-
OrderID uint `gorm:"not null" json:"order_id"`
|
| 115 |
-
ProductID uint `gorm:"not null" json:"product_id"`
|
| 116 |
Quantity int `gorm:"not null;default:1" json:"quantity"`
|
| 117 |
UnitPrice int `gorm:"not null" json:"unit_price"`
|
| 118 |
Subtotal int `gorm:"not null" json:"subtotal"`
|
|
|
|
| 45 |
Password string `gorm:"type:varchar(255);not null" json:"-"`
|
| 46 |
Phone string `gorm:"type:varchar(20)" json:"phone"`
|
| 47 |
Role string `gorm:"type:varchar(20);default:'kasir'" json:"role"` // "super_admin", "owner", "kasir"
|
| 48 |
+
CompanyID *uint `gorm:"index" json:"company_id"`
|
| 49 |
+
BranchID *uint `gorm:"index" json:"branch_id"`
|
| 50 |
IsActive bool `json:"is_active"`
|
| 51 |
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
| 52 |
Company *Company `gorm:"foreignKey:CompanyID" json:"company,omitempty"`
|
|
|
|
| 55 |
|
| 56 |
type Category struct {
|
| 57 |
ID uint `gorm:"primaryKey" json:"id"`
|
| 58 |
+
CompanyID uint `gorm:"not null;index" json:"company_id"`
|
| 59 |
Name string `gorm:"type:varchar(100);not null" json:"name"`
|
| 60 |
Description string `gorm:"type:varchar(255)" json:"description"`
|
| 61 |
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
|
|
|
| 65 |
|
| 66 |
type Product struct {
|
| 67 |
ID uint `gorm:"primaryKey" json:"id"`
|
| 68 |
+
CompanyID uint `gorm:"not null;index" json:"company_id"`
|
| 69 |
Name string `gorm:"type:varchar(200);not null" json:"name"`
|
| 70 |
SKU *string `gorm:"type:varchar(50);uniqueIndex" json:"sku"`
|
| 71 |
Price int `gorm:"not null" json:"price"`
|
| 72 |
Stock int `gorm:"not null;default:0" json:"stock"`
|
| 73 |
IsUnlimitedStock bool `gorm:"default:false" json:"is_unlimited_stock"`
|
| 74 |
+
CategoryID *uint `gorm:"index" json:"category_id"`
|
| 75 |
ImageURL string `gorm:"type:text" json:"image_url"`
|
| 76 |
IsActive bool `gorm:"default:true" json:"is_active"`
|
| 77 |
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
|
|
|
| 92 |
|
| 93 |
type Order struct {
|
| 94 |
ID uint `gorm:"primaryKey" json:"id"`
|
| 95 |
+
CompanyID uint `gorm:"not null;index" json:"company_id"`
|
| 96 |
+
BranchID *uint `gorm:"index" json:"branch_id"`
|
| 97 |
OrderNumber string `gorm:"type:varchar(50);not null;index" json:"order_number"`
|
| 98 |
TotalAmount int `gorm:"not null;default:0" json:"total_amount"`
|
| 99 |
PaidAmount int `gorm:"not null;default:0" json:"paid_amount"`
|
|
|
|
| 111 |
|
| 112 |
type OrderItem struct {
|
| 113 |
ID uint `gorm:"primaryKey" json:"id"`
|
| 114 |
+
OrderID uint `gorm:"not null;index" json:"order_id"`
|
| 115 |
+
ProductID uint `gorm:"not null;index" json:"product_id"`
|
| 116 |
Quantity int `gorm:"not null;default:1" json:"quantity"`
|
| 117 |
UnitPrice int `gorm:"not null" json:"unit_price"`
|
| 118 |
Subtotal int `gorm:"not null" json:"subtotal"`
|