This repository has been archived by the owner on Nov 10, 2023. It is now read-only.
/
idb.go
226 lines (191 loc) · 5.83 KB
/
idb.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// +build js,wasm
// https://www.w3.org/TR/IndexedDB
package wasm
type (
// https://www.w3.org/TR/IndexedDB/#idbrequest
IDBRequest interface {
EventTarget
Result() interface{}
Error() DOMException
Source() IDBRequestSource
Transaction() IDBTransaction
ReadyState() IDBRequestReadyState
OnSuccess(func(Event)) EventHandler
OnError(func(Event)) EventHandler
}
// (IDBObjectStore or IDBIndex or IDBCursor)?
IDBRequestSource interface{}
// https://www.w3.org/TR/IndexedDB/#idbopendbrequest
IDBOpenDBRequest interface {
IDBRequest
OnBlocked(func(Event)) EventHandler
OnUpgradeNeeded(func(IDBVersionChangeEvent)) EventHandler
}
// https://www.w3.org/TR/IndexedDB/#idbversionchangeevent
IDBVersionChangeEvent interface {
Event
OldVersion() uint
NewVersion() uint
}
// https://www.w3.org/TR/IndexedDB/#idbfactory
IDBFactory interface {
Open(string, ...uint) IDBOpenDBRequest
DeleteDatabase(string) IDBOpenDBRequest
Cmp(interface{}, interface{}) int16
}
// https://www.w3.org/TR/IndexedDB/#idbdatabase
IDBDatabase interface {
EventTarget
Name() string
Version() uint
ObjectStoreNames() []string
Transaction([]string, ...IDBTransactionMode) IDBTransaction
Close()
CreateObjectStore(string, ...IDBObjectStoreParameters) IDBObjectStore
DeleteObjectStore(string)
OnAbort(func(Event)) EventHandler
OnClose(func(Event)) EventHandler
OnError(func(Event)) EventHandler
OnVersionchange(func(Event)) EventHandler
}
// https://www.w3.org/TR/IndexedDB/#idbobjectstore
IDBObjectStore interface {
Name() string
SetName(string)
KeyPath() string // any
IndexNames() []string
Transaction() IDBTransaction
AutoIncrement() bool
Put(interface{}, ...interface{}) IDBRequest
Add(interface{}, ...interface{}) IDBRequest
Delete(interface{}) IDBRequest
Clear() IDBRequest
Get(interface{}) IDBRequest
Key(interface{}) IDBRequest
All(...interface{}) IDBRequest
AllKeys(...interface{}) IDBRequest
Count(...interface{}) IDBRequest
OpenCursor(...interface{}) IDBRequest
OpenKeyCursor(...interface{}) IDBRequest
Index(string) IDBIndex
CreateIndex(string, string, ...IDBIndexParameters) IDBIndex
DeleteIndex(string)
}
// https://www.w3.org/TR/IndexedDB/#idbindex
IDBIndex interface {
Name() string
SetName(string)
ObjectStore() IDBObjectStore
KeyPath() string
MultiEntry() bool
Unique() bool
Get(interface{}) IDBRequest
Key(interface{}) IDBRequest
All(...interface{}) IDBRequest
AllKeys(...interface{}) IDBRequest
Count(...interface{}) IDBRequest
OpenCursor(...interface{}) IDBRequest
OpenKeyCursor(...interface{}) IDBRequest
}
// https://www.w3.org/TR/IndexedDB/#idbkeyrange
IDBKeyRange interface {
Lower() interface{}
Upper() interface{}
LowerOpen() bool
UpperOpen() bool
Only(interface{}) IDBKeyRange //static
LowerBound(interface{}, ...bool) IDBKeyRange //static
UpperBound(interface{}, ...bool) IDBKeyRange //static
Bound(interface{}, interface{}, ...bool) IDBKeyRange //static
Includes(interface{}) bool
}
// https://www.w3.org/TR/IndexedDB/#idbcursor
IDBCursor interface {
Source() IDBCursorSource
Direction() IDBCursorDirection
Key() interface{}
PrimaryKey() interface{}
Advance(uint)
Continue(...interface{})
ContinuePrimaryKey(interface{}, interface{})
Update(interface{}) IDBRequest
Delete() IDBRequest
}
// (IDBObjectStore or IDBIndex)
IDBCursorSource interface{}
// https://www.w3.org/TR/IndexedDB/#idbcursorwithvalue
IDBCursorWithValue interface {
IDBCursor
Value() interface{}
}
// https://www.w3.org/TR/IndexedDB/#idbtransaction
IDBTransaction interface {
EventTarget
ObjectStoreNames() []string // DOMStringList
Mode() IDBTransactionMode
DB() IDBDatabase
Error() DOMException
ObjectStore(string) IDBObjectStore
Abort()
OnAbort(func(Event)) EventHandler
OnComplete(func(Event)) EventHandler
OnError(func(Event)) EventHandler
}
)
// https://www.w3.org/TR/IndexedDB/#enumdef-idbrequestreadystate
type IDBRequestReadyState string
const (
IDBRequestReadyStatePending IDBRequestReadyState = "pending"
IDBRequestReadyStateDone IDBRequestReadyState = "done"
)
// https://www.w3.org/TR/IndexedDB/#enumdef-idbcursordirection
type IDBCursorDirection string
const (
IDBCursorDirectionNext IDBCursorDirection = "next"
IDBCursorDirectionNextUnique IDBCursorDirection = "nextunique"
IDBCursorDirectionPrev IDBCursorDirection = "prev"
IDBCursorDirectionPrevUnique IDBCursorDirection = "prevunique"
)
// https://www.w3.org/TR/IndexedDB/#enumdef-idbtransactionmode
type IDBTransactionMode string
const (
IDBTransactionModeReadOnly IDBTransactionMode = "readonly"
IDBTransactionModeReadWrite IDBTransactionMode = "readwrite"
IDBTransactionModeVersionChange IDBTransactionMode = "versionchange"
)
// -------------8<---------------------------------------
// https://www.w3.org/TR/IndexedDB/#dictdef-idbversionchangeeventinit
type IDBVersionChangeEventInit struct {
OldVersion uint
NewVersion uint // default null
}
func (p IDBVersionChangeEventInit) JSValue() jsValue {
o := jsObject.New()
o.Set("oldVersion", p.OldVersion)
o.Set("newVersion", p.NewVersion)
return o
}
// -------------8<---------------------------------------
// https://www.w3.org/TR/IndexedDB/#dictdef-idbobjectstoreparameters
type IDBObjectStoreParameters struct {
KeyPath []string
AutoIncrement bool
}
func (p IDBObjectStoreParameters) JSValue() jsValue {
o := jsObject.New()
o.Set("keyPath", ToJSArray(p.KeyPath))
o.Set("autoIncrement", p.AutoIncrement)
return o
}
// -------------8<---------------------------------------
// https://www.w3.org/TR/IndexedDB/#dictdef-idbindexparameters
type IDBIndexParameters struct {
Unique bool
MultiEntry bool
}
func (p IDBIndexParameters) JSValue() jsValue {
o := jsObject.New()
o.Set("unique", p.Unique)
o.Set("multiEntry", p.MultiEntry)
return o
}