tinyplex / tinybase

The reactive data store for local‑first apps.
https://tinybase.org
MIT License
3.39k stars 67 forks source link

Unable to run createQueries while using api generate getStoreApi #71

Closed fdfontes closed 11 months ago

fdfontes commented 12 months ago

Describe the bug

I used tinybase getStoreApi <schemaFile> <storeName> <outputDir> which allowed me to set & access data. However, I've been unable to use queries. I get the following error:

TypeError: s is not a function (it is undefined) (sometimes l is not a function)

I have tried both createQueries and useCreateQueries. I noticed that createQueries complains that store is not an instance of Store even though I used the command line above.

Your Example Website or App

https://github.com/fdfontes/tinybase-rn-broken-queries-repro

Steps to Reproduce the Bug or Issue

  1. Run tinybase getStoreApi <schemaFile> <storeName> <outputDir> schema in this case is dbSchema.json
  2. Generates 4 files testStore-ui-react.d.ts, testStore-ui-react.tsx, testStore.d.ts, testStore.ts
  3. Use createQueries in App.js

Expected behavior

I expected to be able to set up queries.

Screenshots or Videos

Simulator Screen Shot - iPhone 14 Plus - 2023-08-01 at 09 10 36

Platform

Additional context

I tried to include createQueries and useCreateQueries in my UI files. The documentation isn't super clear on how queries work when using the auto generated UI. Perhaps I'm not understanding some element of the implementation.

jamesgpearce commented 11 months ago

OK, this looks like it could be a bug! But I just want to check you are only seeing this in React Native?

The metro bundler does not always resolve modules as TinyBase needs (due to exports support in package.json), and my guess is that that might be part of the problem.

jamesgpearce commented 11 months ago

There's a PR waiting for you with the fixes :)

https://github.com/fdfontes/tinybase-rn-broken-queries-repro/pull/1

jamesgpearce commented 11 months ago

The TLDR is that you were trying to run queries on your typed Store API, but you need to run them against the underlying Store:

-const queries = createQueries(store);
+const queries = createQueries(store.getStore());

But the PR also includes some nice memoization and data access hooks to get you going.