fix(db_context): lazy load on uow.Begin()
This commit is contained in:
parent
b3c87e9621
commit
81c177bca2
6 changed files with 7 additions and 15 deletions
|
|
@ -13,7 +13,7 @@ func main() {
|
||||||
server := server.NewServer()
|
server := server.NewServer()
|
||||||
|
|
||||||
server.RegisterHandler("initialize", handlers.NewInitializeHandler())
|
server.RegisterHandler("initialize", handlers.NewInitializeHandler())
|
||||||
server.RegisterHandler("createNote", handlers.NewCreateNoteHandler())
|
server.RegisterHandler("create_note", handlers.NewCreateNoteHandler())
|
||||||
|
|
||||||
if err := server.Run(os.Stdin, os.Stdout); err != nil {
|
if err := server.Run(os.Stdin, os.Stdout); err != nil {
|
||||||
slog.Error("server error", "error", err)
|
slog.Error("server error", "error", err)
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,6 @@ type createNoteCommand struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
TemplatePath string `json:"templatePath"`
|
TemplatePath string `json:"templatePath"`
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
Vars map[string]string `json:"vars"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type CreateNoteHandler struct{
|
type CreateNoteHandler struct{
|
||||||
|
|
|
||||||
BIN
dendrite
BIN
dendrite
Binary file not shown.
|
|
@ -21,11 +21,11 @@ func InitializeDBContext(vaultPath string) (error) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDBContext() (*DBContext, error) {
|
func GetDBContext() (*DBContext) {
|
||||||
if dbContext == nil {
|
if dbContext == nil {
|
||||||
panic("DBContext is not initialized. Call InitializeDbContext first.")
|
panic("DBContext is not initialized. Call InitializeDbContext first.")
|
||||||
}
|
}
|
||||||
return dbContext, nil
|
return dbContext
|
||||||
}
|
}
|
||||||
|
|
||||||
func CloseDBContext() error {
|
func CloseDBContext() error {
|
||||||
|
|
|
||||||
|
|
@ -8,21 +8,17 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type UnitOfWork struct {
|
type UnitOfWork struct {
|
||||||
dbContext *sql.DB
|
|
||||||
Transaction *sql.Tx
|
Transaction *sql.Tx
|
||||||
FileStore *store.FileStore
|
FileStore *store.FileStore
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewUnitOfWork() *UnitOfWork {
|
func NewUnitOfWork() *UnitOfWork {
|
||||||
db, err := persistence.GetDBContext()
|
return &UnitOfWork{FileStore: store.NewFileStore()}
|
||||||
if err != nil {
|
|
||||||
panic("failed to get DB context: " + err.Error())
|
|
||||||
}
|
|
||||||
return &UnitOfWork{dbContext: db.DB, FileStore: store.NewFileStore()}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *UnitOfWork) Begin() (tx *sql.Tx, err error) {
|
func (u *UnitOfWork) Begin() (tx *sql.Tx, err error) {
|
||||||
tx, err = u.dbContext.Begin()
|
dbContext := persistence.GetDBContext()
|
||||||
|
tx, err = dbContext.DB.Begin()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -29,10 +29,7 @@ func NewDBFixture() *DBFixture {
|
||||||
|
|
||||||
dbPath := filepath.Join(os.TempDir(), ".index", "index.db")
|
dbPath := filepath.Join(os.TempDir(), ".index", "index.db")
|
||||||
|
|
||||||
dbContext, err := persistence.GetDBContext()
|
dbContext := persistence.GetDBContext()
|
||||||
if err != nil {
|
|
||||||
panic("failed to get DB context: " + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
return &DBFixture{
|
return &DBFixture{
|
||||||
DB: dbContext.DB,
|
DB: dbContext.DB,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue