fix default db bug

This commit is contained in:
Star 2024-10-26 23:32:08 +08:00
parent af36b16ece
commit a527592867
2 changed files with 8 additions and 6 deletions

8
db.go
View File

@ -16,6 +16,7 @@ var dbTS string
//go:embed README.md //go:embed README.md
var dbMD string var dbMD string
var defaultDB = "default"
func init() { func init() {
obj := map[string]any{ obj := map[string]any{
@ -26,7 +27,8 @@ func init() {
}, },
"setDefault": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { "setDefault": func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args := gojs.MakeArgs(&argsIn, vm).Check(1) args := gojs.MakeArgs(&argsIn, vm).Check(1)
conn := db.GetDB(args.Str(0), args.Logger) defaultDB = args.Str(0)
conn := db.GetDB(defaultDB, args.Logger)
args.This.ToObject(vm).Set("conn", conn) args.This.ToObject(vm).Set("conn", conn)
return nil return nil
}, },
@ -34,7 +36,7 @@ func init() {
gojs.Register("apigo.cc/gojs/db", gojs.Module{ gojs.Register("apigo.cc/gojs/db", gojs.Module{
ObjectMaker: func(vm *goja.Runtime) gojs.Map { ObjectMaker: func(vm *goja.Runtime) gojs.Map {
conn := db.GetDB("default", gojs.GetLogger(vm)) conn := db.GetDB(defaultDB, gojs.GetLogger(vm))
dbObj := makeDBObject(conn, nil) dbObj := makeDBObject(conn, nil)
for k, v := range obj { for k, v := range obj {
dbObj[k] = v dbObj[k] = v
@ -169,7 +171,7 @@ func makeDBObject(conn *db.DB, tx *db.Tx) map[string]any {
}, },
} }
if conn != nil { if tx == nil {
obj["make"] = func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value { obj["make"] = func(argsIn goja.FunctionCall, vm *goja.Runtime) goja.Value {
args, conn, _, logger := initDBArgs(argsIn, vm, 1) args, conn, _, logger := initDBArgs(argsIn, vm, 1)
arg0 := args.Str(0) arg0 := args.Str(0)

6
go.mod
View File

@ -3,9 +3,9 @@ module apigo.cc/gojs/db
go 1.18 go 1.18
require ( require (
apigo.cc/gojs v0.0.1 apigo.cc/gojs v0.0.4
github.com/ssgo/dao v0.1.5 github.com/ssgo/dao v0.1.5
github.com/ssgo/db v1.7.9 github.com/ssgo/db v1.7.11
github.com/ssgo/log v1.7.7 github.com/ssgo/log v1.7.7
github.com/ssgo/u v1.7.9 github.com/ssgo/u v1.7.9
modernc.org/sqlite v1.33.1 modernc.org/sqlite v1.33.1
@ -25,7 +25,7 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/ssgo/config v1.7.7 // indirect github.com/ssgo/config v1.7.8 // indirect
github.com/ssgo/standard v1.7.7 // indirect github.com/ssgo/standard v1.7.7 // indirect
github.com/ssgo/tool v0.4.27 // indirect github.com/ssgo/tool v0.4.27 // indirect
golang.org/x/sys v0.26.0 // indirect golang.org/x/sys v0.26.0 // indirect