Compare commits
No commits in common. "main" and "v1.5.4" have entirely different histories.
76
js_export.go
76
js_export.go
@ -12,69 +12,7 @@ import (
|
||||
|
||||
func init() {
|
||||
jsmod.Register("db", map[string]any{
|
||||
// 入口:支持别名获取,不传则默认 "default"
|
||||
"Get": func(ctx context.Context, name *string) (*jsDB, error) {
|
||||
target := "default"
|
||||
if name != nil {
|
||||
target = *name
|
||||
}
|
||||
return getJSDB(ctx, target)
|
||||
},
|
||||
|
||||
// 默认快捷调用 (面向 "default" 实例)
|
||||
"Query": func(ctx context.Context, query string, args ...any) (*QueryResult, error) {
|
||||
jd, err := getJSDB(ctx, "default")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return jd.Query(query, args...), nil
|
||||
},
|
||||
"Exec": func(ctx context.Context, query string, args ...any) (*ExecResult, error) {
|
||||
jd, err := getJSDB(ctx, "default")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return jd.Exec(query, args...), nil
|
||||
},
|
||||
"Insert": func(ctx context.Context, table string, data any) (*ExecResult, error) {
|
||||
jd, err := getJSDB(ctx, "default")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return jd.Insert(table, data), nil
|
||||
},
|
||||
"Update": func(ctx context.Context, table string, data any, conditions string, args ...any) (*ExecResult, error) {
|
||||
jd, err := getJSDB(ctx, "default")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return jd.Update(table, data, conditions, args...), nil
|
||||
},
|
||||
"Delete": func(ctx context.Context, table string, conditions string, args ...any) (*ExecResult, error) {
|
||||
jd, err := getJSDB(ctx, "default")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return jd.Delete(table, conditions, args...), nil
|
||||
},
|
||||
"Replace": func(ctx context.Context, table string, data any) (*ExecResult, error) {
|
||||
jd, err := getJSDB(ctx, "default")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return jd.Replace(table, data), nil
|
||||
},
|
||||
"Begin": func(ctx context.Context) (*jsTx, error) {
|
||||
jd, err := getJSDB(ctx, "default")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return jd.Begin()
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func getJSDB(ctx context.Context, name string) (*jsDB, error) {
|
||||
"get": func(ctx context.Context, name string) (*jsDB, error) {
|
||||
isReadOnly := false
|
||||
|
||||
// 安全模式下的动态判定
|
||||
@ -85,6 +23,7 @@ func getJSDB(ctx context.Context, name string) (*jsDB, error) {
|
||||
if idx := strings.Index(path, "?"); idx != -1 {
|
||||
path = path[:idx]
|
||||
}
|
||||
// 如果路径不在沙箱内,VerifyPathForSafeMode 会直接返回 error,阻止连接
|
||||
_, err := file.VerifyPathForSafeMode(ctx, path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -102,6 +41,7 @@ func getJSDB(ctx context.Context, name string) (*jsDB, error) {
|
||||
}
|
||||
}
|
||||
if !matched {
|
||||
// 未命中白名单,降级为只读模式
|
||||
isReadOnly = true
|
||||
}
|
||||
}
|
||||
@ -112,6 +52,8 @@ func getJSDB(ctx context.Context, name string) (*jsDB, error) {
|
||||
return nil, d.Error
|
||||
}
|
||||
return &jsDB{db: d, ctx: ctx, isReadOnly: isReadOnly}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
type jsDB struct {
|
||||
@ -179,10 +121,6 @@ func (jd *jsDB) Begin() (*jsTx, error) {
|
||||
return &jsTx{tx: tx, ctx: jd.ctx, isReadOnly: jd.isReadOnly}, nil
|
||||
}
|
||||
|
||||
// Metadata
|
||||
func (jd *jsDB) InKeys(numArgs int) string { return jd.db.InKeys(numArgs) }
|
||||
func (jd *jsDB) Quote(text string) string { return jd.db.Quote(text) }
|
||||
|
||||
// jsTx wraps *Tx for JS environment
|
||||
type jsTx struct {
|
||||
tx *Tx
|
||||
@ -244,3 +182,7 @@ func (jt *jsTx) Commit() error {
|
||||
}
|
||||
|
||||
func (jt *jsTx) Rollback() error { return jt.tx.Rollback() }
|
||||
|
||||
// Metadata
|
||||
func (jd *jsDB) InKeys(numArgs int) string { return jd.db.InKeys(numArgs) }
|
||||
func (jd *jsDB) Quote(text string) string { return jd.db.Quote(text) }
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user