This repository has been archived by the owner on Jul 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (119 loc) · 3.26 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// +build js,wasm
package main
import (
"context"
"errors"
"strconv"
"sync"
"syscall/js"
"github.com/genjidb/genji"
"github.com/genjidb/genji/document"
"github.com/genjidb/genji/engine/memoryengine"
)
func main() {
wrapper := GenjiWrapper{
dbs: make(map[int]*genji.DB),
}
js.Global().Set("openDB", js.FuncOf(runWithCallback(func(inputs []js.Value) (interface{}, error) {
return wrapper.OpenDB()
})))
js.Global().Set("dbExec", js.FuncOf(runWithCallback(func(inputs []js.Value) (interface{}, error) {
if len(inputs) < 3 {
return nil, errors.New("missing arguments")
}
return nil, wrapper.Exec(inputs[0].Int(), inputs[1].String(), jsArrayToSlice(inputs[2]))
})))
js.Global().Set("dbQuery", js.FuncOf(func(this js.Value, inputs []js.Value) interface{} {
if len(inputs) < 3 {
return errors.New("missing arguments")
}
callback := inputs[len(inputs)-1:][0]
inputs = inputs[:len(inputs)-1]
err := wrapper.Query(inputs[0].Int(), func(m map[string]interface{}) error {
callback.Invoke(nil, m)
return nil
}, inputs[1].String(), jsArrayToSlice(inputs[2]))
if err != nil {
callback.Invoke(err.Error(), nil)
return nil
}
callback.Invoke(nil, nil)
return nil
}))
select {}
}
// GenjiWrapper is a type that allows opening databases and running queries on them.
// It keeps a reference on all opened databases.
type GenjiWrapper struct {
dbs map[int]*genji.DB
lastid int
m sync.RWMutex
}
// OpenDB opens a database and returns an id that can be used by the Javascript code to select
// the right database when running a query.
func (w *GenjiWrapper) OpenDB() (int, error) {
db, err := genji.New(context.Background(), memoryengine.NewEngine())
if err != nil {
return 0, err
}
w.m.Lock()
defer w.m.Unlock()
w.lastid++
w.dbs[w.lastid] = db
return w.lastid, nil
}
// Exec calls Genji's db.Exec method on the database identified by the id passed as first argument.
func (w *GenjiWrapper) Exec(id int, query string, args []js.Value) error {
db, ok := w.dbs[id]
if !ok {
return errors.New("unknown database id " + strconv.Itoa(id))
}
params, err := jsValuesToParams(args)
if err != nil {
return err
}
return db.Exec(query, params...)
}
// Query calls Genji's db.Query method.
func (w *GenjiWrapper) Query(id int, cb func(m map[string]interface{}) error, query string, args []js.Value) error {
db, ok := w.dbs[id]
if !ok {
return errors.New("unknown database id " + strconv.Itoa(id))
}
params, err := jsValuesToParams(args)
if err != nil {
return err
}
res, err := db.Query(query, params...)
if err != nil {
return err
}
defer res.Close()
return res.Iterate(func(d document.Document) error {
m := make(map[string]interface{})
err := d.Iterate(func(f string, v document.Value) error {
vv, err := genjiValuesToJs(v)
if err != nil {
return err
}
m[f] = vv
return nil
})
if err != nil {
return err
}
return cb(m)
})
}
func runWithCallback(fn func(inputs []js.Value) (interface{}, error)) func(this js.Value, inputs []js.Value) interface{} {
return func(this js.Value, inputs []js.Value) interface{} {
callback := inputs[len(inputs)-1:][0]
ret, err := fn(inputs[:len(inputs)-1])
if err != nil {
callback.Invoke(err.Error(), nil)
} else {
callback.Invoke(nil, ret)
}
return nil
}
}