Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Changelog

## Unreleased

### Added

- Add `GetAll`, `GetAllKey`, and `GetAllRange` methods on `ObjectStore` and `Index` to retrieve multiple values without cursor iteration.

### Fixed

- Avoid a nil-pointer panic in `ObjectStore.GetAllKeysRange` and `Index.GetAllKeysRange` when `query` is `nil`. Passing `query == nil` now applies no key filter (while still honoring `maxCount` when `> 0`), matching the IndexedDB specification.
45 changes: 34 additions & 11 deletions idb/base_object_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,51 @@ func (b *baseObjectStore) CountRange(keyRange *KeyRange) (*UintRequest, error) {
return newUintRequest(req), nil
}

// GetAll returns an ArrayRequest that retrieves all objects in the object store or index.
func (b *baseObjectStore) GetAll() (*ArrayRequest, error) {
return b.callArrayRequest("getAll")
}

// GetAllKey returns an ArrayRequest that retrieves all objects in the object store or index matching the specified key.
func (b *baseObjectStore) GetAllKey(key safejs.Value) (*ArrayRequest, error) {
return b.callArrayRequest("getAll", key)
}

// GetAllRange returns an ArrayRequest that retrieves objects in the object store or index matching the specified query, up to maxCount. If query is nil, no key filter is applied (up to maxCount objects are returned). If maxCount is 0, all matching objects are retrieved.
func (b *baseObjectStore) GetAllRange(query *KeyRange, maxCount uint) (*ArrayRequest, error) {
return b.callArrayRequest("getAll", rangeQueryArgs(query, maxCount)...)
}

// GetAllKeys returns an ArrayRequest that retrieves record keys for all objects in the object store or index.
func (b *baseObjectStore) GetAllKeys() (*ArrayRequest, error) {
reqValue, err := b.jsObjectStore.Call("getAllKeys")
return b.callArrayRequest("getAllKeys")
}

// GetAllKeysRange returns an ArrayRequest that retrieves record keys for objects in the object store or index matching the specified query, up to maxCount. If query is nil, no key filter is applied (up to maxCount keys are returned). If maxCount is 0, all matching keys are retrieved.
func (b *baseObjectStore) GetAllKeysRange(query *KeyRange, maxCount uint) (*ArrayRequest, error) {
return b.callArrayRequest("getAllKeys", rangeQueryArgs(query, maxCount)...)
}

func (b *baseObjectStore) callArrayRequest(method string, args ...interface{}) (*ArrayRequest, error) {
reqValue, err := b.jsObjectStore.Call(method, args...)
if err != nil {
return nil, tryAsDOMException(err)
}
req := wrapRequest(b.txn, reqValue)
return newArrayRequest(req), nil
}

// GetAllKeysRange returns an ArrayRequest that retrieves record keys for all objects in the object store or index matching the specified query. If maxCount is 0, retrieves all objects matching the query.
func (b *baseObjectStore) GetAllKeysRange(query *KeyRange, maxCount uint) (*ArrayRequest, error) {
args := []interface{}{query.jsKeyRange}
if maxCount > 0 {
args = append(args, maxCount)
func rangeQueryArgs(query *KeyRange, maxCount uint) []interface{} {
if query != nil {
if maxCount > 0 {
return []interface{}{query.jsKeyRange, maxCount}
}
return []interface{}{query.jsKeyRange}
}
reqValue, err := b.jsObjectStore.Call("getAllKeys", args...)
if err != nil {
return nil, tryAsDOMException(err)
if maxCount > 0 {
return []interface{}{safejs.Null(), maxCount}
}
req := wrapRequest(b.txn, reqValue)
return newArrayRequest(req), nil
return nil
}

// Get returns a Request, and, in a separate thread, returns the objects selected by the specified key. This is for retrieving specific records from an object store or index.
Expand Down
17 changes: 16 additions & 1 deletion idb/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,27 @@ func (i *Index) CountRange(keyRange *KeyRange) (*UintRequest, error) {
return i.base.CountRange(keyRange)
}

// GetAll returns an ArrayRequest that retrieves all objects in the index.
func (i *Index) GetAll() (*ArrayRequest, error) {
return i.base.GetAll()
}

// GetAllKey returns an ArrayRequest that retrieves all objects in the index matching the specified key.
func (i *Index) GetAllKey(key js.Value) (*ArrayRequest, error) {
return i.base.GetAllKey(safejs.Safe(key))
}

// GetAllRange returns an ArrayRequest that retrieves objects in the index matching the specified query, up to maxCount. If query is nil, no key filter is applied (up to maxCount objects are returned). If maxCount is 0, all matching objects are retrieved.
func (i *Index) GetAllRange(query *KeyRange, maxCount uint) (*ArrayRequest, error) {
return i.base.GetAllRange(query, maxCount)
}

// GetAllKeys returns an ArrayRequest that retrieves record keys for all objects in the index.
func (i *Index) GetAllKeys() (*ArrayRequest, error) {
return i.base.GetAllKeys()
}

// GetAllKeysRange returns an ArrayRequest that retrieves record keys for all objects in the index matching the specified query. If maxCount is 0, retrieves all objects matching the query.
// GetAllKeysRange returns an ArrayRequest that retrieves record keys for objects in the index matching the specified query, up to maxCount. If query is nil, no key filter is applied (up to maxCount keys are returned). If maxCount is 0, all matching keys are retrieved.
func (i *Index) GetAllKeysRange(query *KeyRange, maxCount uint) (*ArrayRequest, error) {
return i.base.GetAllKeysRange(query, maxCount)
}
Expand Down
212 changes: 212 additions & 0 deletions idb/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package idb

import (
"context"
"syscall/js"
"testing"

Expand Down Expand Up @@ -113,3 +114,214 @@ func TestIndexUnique(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, true, unique)
}

func TestIndexGetAllKeys(t *testing.T) {
t.Parallel()

for _, tc := range []struct {
name string
records []map[string]interface{}
getFn func(*Index) (*ArrayRequest, error)
expectResult []js.Value
}{
{
name: "get all keys",
records: []map[string]interface{}{
{"id": "1", "tag": "b"},
{"id": "2", "tag": "a"},
{"id": "3", "tag": "b"},
},
getFn: func(index *Index) (*ArrayRequest, error) {
return index.GetAllKeys()
},
expectResult: []js.Value{js.ValueOf("2"), js.ValueOf("1"), js.ValueOf("3")},
},
{
name: "get all keys query",
records: []map[string]interface{}{
{"id": "1", "tag": "b"},
{"id": "2", "tag": "a"},
{"id": "3", "tag": "b"},
},
getFn: func(index *Index) (*ArrayRequest, error) {
keyRange, err := NewKeyRangeOnly(js.ValueOf("b"))
assert.NoError(t, err)
return index.GetAllKeysRange(keyRange, 1)
},
expectResult: []js.Value{js.ValueOf("1")},
},
{
name: "get all keys nil range with maxCount",
records: []map[string]interface{}{
{"id": "1", "tag": "b"},
{"id": "2", "tag": "a"},
{"id": "3", "tag": "c"},
},
getFn: func(index *Index) (*ArrayRequest, error) {
return index.GetAllKeysRange(nil, 2)
},
expectResult: []js.Value{js.ValueOf("2"), js.ValueOf("1")},
},
{
name: "get all keys nil range unbounded",
records: []map[string]interface{}{
{"id": "1", "tag": "b"},
{"id": "2", "tag": "a"},
},
getFn: func(index *Index) (*ArrayRequest, error) {
return index.GetAllKeysRange(nil, 0)
},
expectResult: []js.Value{js.ValueOf("2"), js.ValueOf("1")},
},
{
name: "get all keys empty index",
getFn: func(index *Index) (*ArrayRequest, error) {
return index.GetAllKeys()
},
expectResult: []js.Value(nil),
},
} {
tc := tc // keep loop-local copy of test case for parallel runs
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
db := testDB(t, func(db *Database) {
store, err := db.CreateObjectStore("mystore", ObjectStoreOptions{
KeyPath: js.ValueOf("id"),
})
assert.NoError(t, err)
_, err = store.CreateIndex("byTag", js.ValueOf("tag"), IndexOptions{})
assert.NoError(t, err)
})
txn, err := db.Transaction(TransactionReadWrite, "mystore")
assert.NoError(t, err)
store, err := txn.ObjectStore("mystore")
assert.NoError(t, err)
for _, rec := range tc.records {
_, err = store.Add(js.ValueOf(rec))
assert.NoError(t, err)
}
index, err := store.Index("byTag")
assert.NoError(t, err)

req, err := tc.getFn(index)
assert.NoError(t, err)
result, err := req.Await(context.Background())
assert.NoError(t, err)
assert.Equal(t, tc.expectResult, result)
})
}
}

func TestIndexGetAll(t *testing.T) {
t.Parallel()

for _, tc := range []struct {
name string
records []map[string]interface{}
getFn func(*Index) (*ArrayRequest, error)
expectIDs []string
}{
{
name: "get all",
records: []map[string]interface{}{
{"id": "1", "tag": "b"},
{"id": "2", "tag": "a"},
{"id": "3", "tag": "b"},
},
getFn: func(index *Index) (*ArrayRequest, error) {
return index.GetAll()
},
expectIDs: []string{"2", "1", "3"},
},
{
name: "get all key matching multiple",
records: []map[string]interface{}{
{"id": "1", "tag": "b"},
{"id": "2", "tag": "a"},
{"id": "3", "tag": "b"},
},
getFn: func(index *Index) (*ArrayRequest, error) {
return index.GetAllKey(js.ValueOf("b"))
},
expectIDs: []string{"1", "3"},
},
{
name: "get all range",
records: []map[string]interface{}{
{"id": "1", "tag": "b"},
{"id": "2", "tag": "a"},
{"id": "3", "tag": "b"},
},
getFn: func(index *Index) (*ArrayRequest, error) {
keyRange, err := NewKeyRangeOnly(js.ValueOf("b"))
assert.NoError(t, err)
return index.GetAllRange(keyRange, 1)
},
expectIDs: []string{"1"},
},
{
name: "get all nil range with maxCount",
records: []map[string]interface{}{
{"id": "1", "tag": "b"},
{"id": "2", "tag": "a"},
{"id": "3", "tag": "c"},
},
getFn: func(index *Index) (*ArrayRequest, error) {
return index.GetAllRange(nil, 2)
},
expectIDs: []string{"2", "1"},
},
{
name: "get all nil range unbounded",
records: []map[string]interface{}{
{"id": "1", "tag": "b"},
{"id": "2", "tag": "a"},
},
getFn: func(index *Index) (*ArrayRequest, error) {
return index.GetAllRange(nil, 0)
},
expectIDs: []string{"2", "1"},
},
{
name: "get all empty index",
getFn: func(index *Index) (*ArrayRequest, error) {
return index.GetAll()
},
expectIDs: nil,
},
} {
tc := tc // keep loop-local copy of test case for parallel runs
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
db := testDB(t, func(db *Database) {
store, err := db.CreateObjectStore("mystore", ObjectStoreOptions{
KeyPath: js.ValueOf("id"),
})
assert.NoError(t, err)
_, err = store.CreateIndex("byTag", js.ValueOf("tag"), IndexOptions{})
assert.NoError(t, err)
})
txn, err := db.Transaction(TransactionReadWrite, "mystore")
assert.NoError(t, err)
store, err := txn.ObjectStore("mystore")
assert.NoError(t, err)
for _, rec := range tc.records {
_, err = store.Add(js.ValueOf(rec))
assert.NoError(t, err)
}
index, err := store.Index("byTag")
assert.NoError(t, err)

req, err := tc.getFn(index)
assert.NoError(t, err)
result, err := req.Await(context.Background())
assert.NoError(t, err)

var gotIDs []string
for _, val := range result {
gotIDs = append(gotIDs, val.Get("id").String())
}
assert.Equal(t, tc.expectIDs, gotIDs)
})
}
}
17 changes: 16 additions & 1 deletion idb/object_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,27 @@ func (o *ObjectStore) DeleteIndex(name string) error {
return tryAsDOMException(err)
}

// GetAll returns an ArrayRequest that retrieves all objects in the object store.
func (o *ObjectStore) GetAll() (*ArrayRequest, error) {
return o.base.GetAll()
}

// GetAllKey returns an ArrayRequest that retrieves all objects in the object store matching the specified key.
func (o *ObjectStore) GetAllKey(key js.Value) (*ArrayRequest, error) {
return o.base.GetAllKey(safejs.Safe(key))
}

// GetAllRange returns an ArrayRequest that retrieves objects in the object store matching the specified query, up to maxCount. If query is nil, no key filter is applied (up to maxCount objects are returned). If maxCount is 0, all matching objects are retrieved.
func (o *ObjectStore) GetAllRange(query *KeyRange, maxCount uint) (*ArrayRequest, error) {
return o.base.GetAllRange(query, maxCount)
}

// GetAllKeys returns an ArrayRequest that retrieves record keys for all objects in the object store.
func (o *ObjectStore) GetAllKeys() (*ArrayRequest, error) {
return o.base.GetAllKeys()
}

// GetAllKeysRange returns an ArrayRequest that retrieves record keys for all objects in the object store matching the specified query. If maxCount is 0, retrieves all objects matching the query.
// GetAllKeysRange returns an ArrayRequest that retrieves record keys for objects in the object store matching the specified query, up to maxCount. If query is nil, no key filter is applied (up to maxCount keys are returned). If maxCount is 0, all matching keys are retrieved.
func (o *ObjectStore) GetAllKeysRange(query *KeyRange, maxCount uint) (*ArrayRequest, error) {
return o.base.GetAllKeysRange(query, maxCount)
}
Expand Down
Loading