@idb-orm/react-query 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -9,14 +9,19 @@ A React adapter for [Tanstack's Query library](https://tanstack.com/query/latest
9
9
  Here's an example comparison on how to do a `find` query on a `users` document collection in traditional Tanstack-query and with this package.
10
10
 
11
11
  ```tsx
12
- // How to do it in Tanstack-query
12
+ // How to do it in vanilla Tanstack-query
13
13
  useQuery({
14
14
  queryKey: ["users", "find", userId],
15
15
  queryFn: () => client.stores.users.find({ where: { id: userId } }),
16
16
  });
17
17
 
18
18
  // How to do it in this package
19
- stores.users.useFind({ where: { id: userId } }, [userId]);
19
+ stores.users.useFind({ where: { id: userId } });
20
+
21
+ // Or
22
+ stores.users.useFind({ where: { id: userId } }, [
23
+ /* Any additional dependences */
24
+ ]);
20
25
  ```
21
26
 
22
27
  The package manages the internal query keys to ensure there is no overlap, and you can state an optional dependency array similar to traditional react hooks like `useMemo` and `useEffect`.
@@ -59,14 +64,44 @@ function App(){
59
64
  <Provider>
60
65
  )
61
66
  }
67
+ ```
68
+
69
+ ## Usage
70
+
71
+ Anywhere inside a component, you can call one of three hooks for each store:
72
+
73
+ ```ts
74
+ // Will find all documents that matches the filter/selector
75
+ stores.users.useFind({
76
+ query: {
77
+ /* Selector Object */
78
+ },
79
+ /* Additional options */
80
+ });
62
81
 
82
+ // Will find the first document that matches the filter/selector
83
+ stores.users.useFindFirst({
84
+ query: {
85
+ /* Selector Object */
86
+ },
87
+ /* Additional options */
88
+ });
89
+
90
+ // Will find the first document that matches the filter/selector
91
+ stores.users.useGet({
92
+ key: /* Primary Key */
93
+ /* Additional options */
94
+ });
63
95
  ```
64
96
 
97
+ These hooks return the same type as the result of the Tanstack [useQuery](https://tanstack.com/query/latest/docs/framework/react/reference/useQuery) hook. Check out their documentation for more information as well as additional options.
98
+
65
99
  Some things to note:
66
100
 
101
+ - Fields passed into the `query` key of a hook input will be used to determine query invalidation. These query objects are serialized into JSON by Tanstack, so any TS/JS types that are not serializable into JSON (functions, files, undefined) will be omitted from the serialized representation. So if any of these types of values change, the query will **NOT** be automatically invalidated.
67
102
  - The `stores` field should be exported for use throughout your app. That is what provides the shortcut functions for each store.
68
103
  - The provider component already has the Tanstack `QueryClientProvider` included!
69
104
 
70
-
71
105
  ## Disclaimer
72
- This project is still in the **very** early stages. Bugs will occur as I work on this package. Sorry in advance.
106
+
107
+ This project is still in the **very** early stages. Bugs will occur as I work on this package. Sorry in advance.
package/dist/index.cjs.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("@tanstack/react-query"),t=require("react");class r extends e.QueryClient{constructor(e,t){super(t),this.db=e}createInterface(r){const n={},i=this.db.getStoreNames(),s=this.db.stores;for(const t of i)n[t]={useFind:(r,n=[])=>e.useQuery({...r,queryKey:[t,"find",...n],queryFn:()=>s[t].find(r.query)}),useFindFirst:(r,n=[])=>e.useQuery({...r,queryKey:[t,"findFirst",...n],queryFn:()=>s[t].findFirst(r.query)}),useGet:r=>{e.useQuery({...r,queryKey:[t,"get",r.key],queryFn:()=>s[t].get(r.key)})}};return{...n,useClient:()=>t.useContext(r)}}}exports.createIDBQueryClient=function(n,i){const s=new r(n,i),u=t.createContext({});return{queryClient:s,context:u,Provider:({client:r,children:n})=>{const i=t.useMemo(()=>{function e(e){return{invalidate:()=>r.invalidateQueries({queryKey:e})}}const t=e([]),n=r.db.getStoreNames();for(const r of n)"invalidate"===r&&console.warn("Model name 'invalidate' causes the invalidate() function of useClient() to not work. Please fix by renaming the model."),t[r]={...e([r]),get:e([r,"get"]),find:e([r,"find"]),findFirst:e([r,"findFirst"])};return t},[r]);return t.createElement(e.QueryClientProvider,{client:r},t.createElement(u.Provider,{value:i},n))},stores:s.createInterface(u)}};
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("@tanstack/react-query"),t=require("react");class r extends e.QueryClient{constructor(e,t){super(t),this.db=e}createInterface(r){const n={},i=this.db.getStoreNames(),u=this.db.stores;for(const t of i)n[t]={useFind:(r,n=[])=>e.useQuery({...r,queryKey:[t,"find",r.query,...n],queryFn:()=>u[t].find(r.query)}),useFindFirst:(r,n=[])=>e.useQuery({...r,queryKey:[t,"findFirst",r.query,...n],queryFn:()=>u[t].findFirst(r.query)}),useGet:(r,n=[])=>{e.useQuery({...r,queryKey:[t,"get",r.key,...n],queryFn:()=>u[t].get(r.key)})}};return{...n,useClient:()=>t.useContext(r)}}}exports.createIDBQueryClient=function(n,i){const u=new r(n,i),s=t.createContext({});return{queryClient:u,context:s,Provider:({client:r,children:n})=>{const i=t.useMemo(()=>{function e(e){return{invalidate:()=>r.invalidateQueries({queryKey:e})}}const t=e([]),n=r.db.getStoreNames();for(const r of n)"invalidate"===r&&console.warn("Model name 'invalidate' causes the invalidate() function of useClient() to not work. Please fix by renaming the model."),t[r]={...e([r]),get:e([r,"get"]),find:e([r,"find"]),findFirst:e([r,"findFirst"])};return t},[r]);return t.createElement(e.QueryClientProvider,{client:r},t.createElement(s.Provider,{value:i},n))},stores:u.createInterface(s)}};
package/dist/index.d.ts CHANGED
@@ -8,7 +8,7 @@ interface ModelQueryInterface<M extends Model<any, any, any>, Names extends stri
8
8
  */
9
9
  useGet: M extends Model<any, infer Fields, any> ? <O = core.Simplify<core.ModelStructure<Fields, Models>>>(primaryKey: {
10
10
  key: core.PrimaryKeyType<M>;
11
- } & QueryOptions<O>) => DefinedUseQueryResult<O | undefined> : never;
11
+ } & QueryOptions<O>, deps?: React.DependencyList) => DefinedUseQueryResult<O | undefined> : never;
12
12
  useFind: <I extends core.FindInput<Names, M, Models>, O = core.Simplify<NonNullable<core.FindOutput<Names, M, Models, I>>>[]>(options: QueryOptions<O> & {
13
13
  query: I;
14
14
  }, deps?: React.DependencyList) => DefinedUseQueryResult<O | undefined>;
package/dist/index.es.js CHANGED
@@ -1,2 +1,2 @@
1
- import{QueryClientProvider as e,QueryClient as t,useQuery as n}from"@tanstack/react-query";import r from"react";function i(t,n){const i=new o(t,n),s=r.createContext({});return{queryClient:i,context:s,Provider:({client:t,children:n})=>{const i=r.useMemo(()=>{function e(e){return{invalidate:()=>t.invalidateQueries({queryKey:e})}}const n=e([]),r=t.db.getStoreNames();for(const t of r)"invalidate"===t&&console.warn("Model name 'invalidate' causes the invalidate() function of useClient() to not work. Please fix by renaming the model."),n[t]={...e([t]),get:e([t,"get"]),find:e([t,"find"]),findFirst:e([t,"findFirst"])};return n},[t]);/* @__PURE__ */
2
- return r.createElement(e,{client:t},/* @__PURE__ */r.createElement(s.Provider,{value:i},n))},stores:i.createInterface(s)}}class o extends t{constructor(e,t){super(t),this.db=e}createInterface(e){const t={},i=this.db.getStoreNames(),o=this.db.stores;for(const r of i)t[r]={useFind:(e,t=[])=>n({...e,queryKey:[r,"find",...t],queryFn:()=>o[r].find(e.query)}),useFindFirst:(e,t=[])=>n({...e,queryKey:[r,"findFirst",...t],queryFn:()=>o[r].findFirst(e.query)}),useGet:e=>{n({...e,queryKey:[r,"get",e.key],queryFn:()=>o[r].get(e.key)})}};return{...t,useClient:()=>r.useContext(e)}}}export{i as createIDBQueryClient};
1
+ import{QueryClientProvider as e,QueryClient as t,useQuery as r}from"@tanstack/react-query";import n from"react";function i(t,r){const i=new o(t,r),s=n.createContext({});return{queryClient:i,context:s,Provider:({client:t,children:r})=>{const i=n.useMemo(()=>{function e(e){return{invalidate:()=>t.invalidateQueries({queryKey:e})}}const r=e([]),n=t.db.getStoreNames();for(const t of n)"invalidate"===t&&console.warn("Model name 'invalidate' causes the invalidate() function of useClient() to not work. Please fix by renaming the model."),r[t]={...e([t]),get:e([t,"get"]),find:e([t,"find"]),findFirst:e([t,"findFirst"])};return r},[t]);/* @__PURE__ */
2
+ return n.createElement(e,{client:t},/* @__PURE__ */n.createElement(s.Provider,{value:i},r))},stores:i.createInterface(s)}}class o extends t{constructor(e,t){super(t),this.db=e}createInterface(e){const t={},i=this.db.getStoreNames(),o=this.db.stores;for(const n of i)t[n]={useFind:(e,t=[])=>r({...e,queryKey:[n,"find",e.query,...t],queryFn:()=>o[n].find(e.query)}),useFindFirst:(e,t=[])=>r({...e,queryKey:[n,"findFirst",e.query,...t],queryFn:()=>o[n].findFirst(e.query)}),useGet:(e,t=[])=>{r({...e,queryKey:[n,"get",e.key,...t],queryFn:()=>o[n].get(e.key)})}};return{...t,useClient:()=>n.useContext(e)}}}export{i as createIDBQueryClient};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@idb-orm/react-query",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "An adapter for the Tanstack React-Query library for @idb-orm",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {