@idb-orm/react-query 0.0.0 → 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,17 +9,22 @@ 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
- The package manages the internal query keys to ensure there is no overlap, and you can state an optional dependency array similar to traditiona react hooks like `useMemo` and `useEffect`.
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`.
23
28
 
24
29
  ## Installation
25
30
 
@@ -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 React.createElement(e.QueryClientProvider,{client:r},React.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
@@ -1,6 +1,6 @@
1
1
  import { core, Model } from '@idb-orm/core';
2
2
  import { QueryClient, DefinedUseQueryResult, UndefinedInitialDataOptions, QueryClientConfig } from '@tanstack/react-query';
3
- import { DependencyList, PropsWithChildren } from 'react';
3
+ import { default as React } from 'react';
4
4
  type QueryOptions<O> = Omit<UndefinedInitialDataOptions<O>, "queryFn" | "queryKey">;
5
5
  interface ModelQueryInterface<M extends Model<any, any, any>, Names extends string, Models extends core.CollectionObject<Names>> {
6
6
  /**
@@ -8,13 +8,13 @@ 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
- }, deps?: DependencyList) => DefinedUseQueryResult<O | undefined>;
14
+ }, deps?: React.DependencyList) => DefinedUseQueryResult<O | undefined>;
15
15
  useFindFirst: <I extends core.FindInput<Names, M, Models>, O = core.Simplify<core.FindOutput<Names, M, Models, I>>>(options: QueryOptions<O> & {
16
16
  query: I;
17
- }, deps?: DependencyList) => DefinedUseQueryResult<O | undefined>;
17
+ }, deps?: React.DependencyList) => DefinedUseQueryResult<O | undefined>;
18
18
  }
19
19
  interface ModelQueryClient {
20
20
  invalidate(): Promise<void>;
@@ -28,16 +28,16 @@ type IDBClientInterface<Names extends string, _Models extends core.CollectionObj
28
28
  } & ModelQueryClient>;
29
29
  export declare function createIDBQueryClient<Names extends string, Models extends core.CollectionObject<Names>>(client: core.DbClient<string, Names, Models>, config?: QueryClientConfig): {
30
30
  queryClient: IDBQueryClient<Names, Models>;
31
- context: import('react').Context<{ [K_1 in Names]: {
31
+ context: React.Context<{ [K_1 in Names]: {
32
32
  get: ModelQueryClient;
33
33
  findFirst: ModelQueryClient;
34
34
  find: ModelQueryClient;
35
35
  } & {
36
36
  invalidate: () => Promise<void>;
37
37
  }; } & ModelQueryClient extends infer T ? { [K in keyof T]: T[K]; } : never>;
38
- Provider: ({ client, children, }: PropsWithChildren<{
38
+ Provider: ({ client, children, }: React.PropsWithChildren<{
39
39
  client: IDBQueryClient<Names, Models>;
40
- }>) => import("react").JSX.Element;
40
+ }>) => React.JSX.Element;
41
41
  stores: IDBQueryInterface<Names, Models>;
42
42
  };
43
43
  declare class IDBQueryClient<Names extends string, Models extends core.CollectionObject<Names>> extends QueryClient {
package/dist/index.es.js CHANGED
@@ -1,2 +1,2 @@
1
- import{QueryClientProvider as e,QueryClient as t,useQuery as r}from"@tanstack/react-query";import{createContext as n,useMemo as i,useContext as s}from"react";function o(t,r){const s=new a(t,r),o=n({});return{queryClient:s,context:o,Provider:({client:t,children:r})=>{const n=i(()=>{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 React.createElement(e,{client:t},/* @__PURE__ */React.createElement(o.Provider,{value:n},r))},stores:s.createInterface(o)}}class a extends t{constructor(e,t){super(t),this.db=e}createInterface(e){const t={},n=this.db.getStoreNames(),i=this.db.stores;for(const s of n)t[s]={useFind:(e,t=[])=>r({...e,queryKey:[s,"find",...t],queryFn:()=>i[s].find(e.query)}),useFindFirst:(e,t=[])=>r({...e,queryKey:[s,"findFirst",...t],queryFn:()=>i[s].findFirst(e.query)}),useGet:e=>{r({...e,queryKey:[s,"get",e.key],queryFn:()=>i[s].get(e.key)})}};return{...t,useClient:()=>s(e)}}}export{o 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.0",
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": {