diff --git a/pkg/module/store.go b/pkg/module/store.go index ad07753..bb30735 100644 --- a/pkg/module/store.go +++ b/pkg/module/store.go @@ -91,22 +91,24 @@ func (m *StoreModule) query(call goja.FunctionCall, rt *goja.Runtime) goja.Value queryOptionsFuncs := make([]storage.QueryOptionFunc, 0) - if queryOptions.Limit != nil { - queryOptionsFuncs = append(queryOptionsFuncs, storage.WithLimit(*queryOptions.Limit)) - } + if queryOptions != nil { + if queryOptions.Limit != nil { + queryOptionsFuncs = append(queryOptionsFuncs, storage.WithLimit(*queryOptions.Limit)) + } - if queryOptions.OrderBy != nil { - queryOptionsFuncs = append(queryOptionsFuncs, storage.WithOrderBy(*queryOptions.OrderBy)) - } + if queryOptions.OrderBy != nil { + queryOptionsFuncs = append(queryOptionsFuncs, storage.WithOrderBy(*queryOptions.OrderBy)) + } - if queryOptions.Offset != nil { - queryOptionsFuncs = append(queryOptionsFuncs, storage.WithOffset(*queryOptions.Limit)) - } + if queryOptions.Offset != nil { + queryOptionsFuncs = append(queryOptionsFuncs, storage.WithOffset(*queryOptions.Limit)) + } - if queryOptions.OrderDirection != nil { - queryOptionsFuncs = append(queryOptionsFuncs, storage.WithOrderDirection( - storage.OrderDirection(*queryOptions.OrderDirection), - )) + if queryOptions.OrderDirection != nil { + queryOptionsFuncs = append(queryOptionsFuncs, storage.WithOrderDirection( + storage.OrderDirection(*queryOptions.OrderDirection), + )) + } } documents, err := m.store.Query(ctx, collection, filter, queryOptionsFuncs...) @@ -144,6 +146,10 @@ func (m *StoreModule) assertCollection(value goja.Value, rt *goja.Runtime) strin } func (m *StoreModule) assertFilter(value goja.Value, rt *goja.Runtime) *filter.Filter { + if value.Export() == nil { + return nil + } + rawFilter, ok := value.Export().(map[string]interface{}) if !ok { panic(rt.NewTypeError(fmt.Sprintf("filter must be an object, got '%T'", value.Export()))) @@ -172,6 +178,10 @@ func (m *StoreModule) assertDocumentID(value goja.Value, rt *goja.Runtime) stora } func (m *StoreModule) assertQueryOptions(value goja.Value, rt *goja.Runtime) *queryOptions { + if value.Export() == nil { + return nil + } + rawQueryOptions, ok := value.Export().(map[string]interface{}) if !ok { panic(rt.NewTypeError(fmt.Sprintf("query options must be an object, got '%T'", value.Export()))) diff --git a/pkg/storage/sqlite/document_store.go b/pkg/storage/sqlite/document_store.go index f37afab..69f76c4 100644 --- a/pkg/storage/sqlite/document_store.go +++ b/pkg/storage/sqlite/document_store.go @@ -93,15 +93,22 @@ func (s *DocumentStore) Query(ctx context.Context, collection string, filter *fi var documents []storage.Document err := s.withTx(ctx, func(tx *sql.Tx) error { - criteria, args, err := filterSQL.ToSQL( - filter.Root(), - filterSQL.WithPreparedParameter("$", 2), - filterSQL.WithKeyTransform(func(key string) string { - return fmt.Sprintf("json_extract(data, '$.%s')", key) - }), - ) - if err != nil { - return errors.WithStack(err) + criteria := "1 = 1" + args := make([]any, 0) + + var err error + + if filter != nil { + criteria, args, err = filterSQL.ToSQL( + filter.Root(), + filterSQL.WithPreparedParameter("$", 2), + filterSQL.WithKeyTransform(func(key string) string { + return fmt.Sprintf("json_extract(data, '$.%s')", key) + }), + ) + if err != nil { + return errors.WithStack(err) + } } query := `