Skip to content

Commit f4ca137

Browse files
新增 kv list
1 parent 562413b commit f4ca137

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

examples/kv/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,12 @@ if(request.path == "put"){
88
}
99
}
1010
response.write("当前存储的数值为 " + db.get('key'))
11+
12+
var test = kv.repo("test");
13+
for (let i = 0; i < 500; i++) {
14+
test.set("key" + i,"value" + i);
15+
}
16+
var list = test.list()
17+
console.log(list.keys.length)
18+
console.log(list.cursor)
19+
console.log(list.hasNext)

pkg/filters/goja/var_kv.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func kvResult(db kv.CursorPagedKV) func(ctx core.FilterContext, jsCtx *goja.Runt
2727
if group == "" {
2828
panic("kv: invalid group name")
2929
}
30-
db := db.Child(group)
30+
db := db.Child(group).(kv.CursorPagedKV)
3131
return jsCtx.ToValue(map[string]interface{}{
3232
"get": func(key string) goja.Value {
3333
get, err := db.Get(ctx, key)
@@ -66,6 +66,23 @@ func kvResult(db kv.CursorPagedKV) func(ctx core.FilterContext, jsCtx *goja.Runt
6666
}
6767
return swap
6868
},
69+
"list": func(limit int64, cursor string) map[string]any {
70+
if limit <= 0 {
71+
limit = 100
72+
}
73+
list, err := db.CursorList(ctx, &kv.ListOptions{
74+
Limit: limit,
75+
Cursor: cursor,
76+
})
77+
if err != nil {
78+
panic(err)
79+
}
80+
return map[string]any{
81+
"keys": list.Keys,
82+
"cursor": list.Cursor,
83+
"hasNext": list.HasMore,
84+
}
85+
},
6986
})
7087
}
7188
}

0 commit comments

Comments
 (0)