feat(repo): 实现SQL存储库服务并替换内存存储库

- 在配置文件中添加SQL相关配置项,包括连接字符串和SQL命令路径
- 更新go.mod和go.sum文件,添加新的依赖项如sqlite、humanize等
- 注释掉内存存储库注册,启用SQL存储库服务注册
- 新增SqlRepositoryService实现,支持数据库初始化和种子数据加载
- 实现SQL存储库的单次加载和重置逻辑,确保服务启动时正确初始化数据库
This commit is contained in:
2025-11-08 18:22:18 +08:00
parent dd9b021513
commit 945d371dd2
5 changed files with 96 additions and 2 deletions

View File

@@ -12,5 +12,20 @@
"sessions": {
"key": "MY_SESSION_KEY",
"cycleKey": true
},
"sql": {
"connection_str": "store.db",
"always_reset": true,
"commands": {
"Init": "sql/init_db.sql",
"Seed": "sql/seed_db.sql",
"GetProduct": "sql/get_product.sql",
"GetProducts": "sql/get_products.sql",
"GetCategories": "sql/get_categories.sql",
"GetPage": "sql/get_product_page.sql",
"GetPageCount": "sql/get_page_count.sql",
"GetCategoryPage": "sql/get_category_product_page.sql",
"GetCategoryPageCount": "sql/get_category_product_page_count.sql"
}
}
}

View File

@@ -1,12 +1,25 @@
module sportstore
go 1.24
go 1.24.0
toolchain go1.24.9
require platform v1.0.0
require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/securecookie v1.1.2 // indirect
github.com/gorilla/sessions v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect
golang.org/x/sys v0.36.0 // indirect
modernc.org/libc v1.66.10 // indirect
modernc.org/mathutil v1.7.1 // indirect
modernc.org/memory v1.11.0 // indirect
modernc.org/sqlite v1.40.0 // indirect
)
replace platform v1.0.0 => ../platform

View File

@@ -1,4 +1,27 @@
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/securecookie v1.1.2 h1:YCIWL56dvtr73r6715mJs5ZvhtnY73hBvEF8kXD8ePA=
github.com/gorilla/securecookie v1.1.2/go.mod h1:NfCASbcHqRSY+3a8tlWJwsQap2VX5pwzwo4h3eOamfo=
github.com/gorilla/sessions v1.4.0 h1:kpIYOp/oi6MG/p5PgxApU8srsSw9tuFbt46Lt7auzqQ=
github.com/gorilla/sessions v1.4.0/go.mod h1:FLWm50oby91+hl7p/wRxDth9bWSuk0qVL2emc7lT5ik=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/yqS/lQJ6PmkyIV3YP+o=
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
modernc.org/libc v1.66.10 h1:yZkb3YeLx4oynyR+iUsXsybsX4Ubx7MQlSYEw4yj59A=
modernc.org/libc v1.66.10/go.mod h1:8vGSEwvoUoltr4dlywvHqjtAqHBaw0j1jI7iFBTAr2I=
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
modernc.org/sqlite v1.40.0 h1:bNWEDlYhNPAUdUdBzjAvn8icAs/2gaKlj4vM+tQ6KdQ=
modernc.org/sqlite v1.40.0/go.mod h1:9fjQZ0mB1LLP0GYrp39oOJXx/I2sxEnZtzCmEQIKvGE=

View File

@@ -15,7 +15,8 @@ import (
func registerServices() {
services.RegisterDefaultService()
repo.RegisterMemoryRepoService()
// repo.RegisterMemoryRepoService()
repo.RegisterSqlRepositoryService()
sessions.RegisterSessionService()
cart.RegisterCartService()
}

View File

@@ -0,0 +1,42 @@
package repo
import (
"context"
"database/sql"
"platform/config"
"platform/logging"
"platform/services"
"sportstore/models"
"sync"
)
func RegisterSqlRepositoryService() {
var db *sql.DB
var commands *SqlCommands
var needInit bool
loadOnce := sync.Once{}
resetOnce := sync.Once{}
err := services.AddScoped(func(ctx context.Context, config config.Configuration, logger logging.Logger) models.Repository {
loadOnce.Do(func() {
db, commands, needInit = openDb(config, logger)
})
repo := &SqlRepository{
Configuration: config,
Logger: logger,
Commands: *commands,
DB: db,
Context: services.NewServiceContext(ctx),
}
resetOnce.Do(func() {
if needInit || config.GetBoolDefault("sql:always_reset", true) {
repo.Init()
repo.Seed()
}
})
return repo
})
if err != nil {
panic(err)
return
}
}