@idb-orm/react-query 0.0.0
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/LICENSE +7 -0
- package/README.md +72 -0
- package/dist/index.cjs.js +1 -0
- package/dist/index.d.ts +53 -0
- package/dist/index.es.js +2 -0
- package/package.json +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
ISC License
|
|
2
|
+
|
|
3
|
+
Copyright 2025 jtb
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# @idb-orm/react-query
|
|
2
|
+
|
|
3
|
+
> ## THIS PACKAGE IS BRAND-NEW AND UNSTABLE. DO NOT USE
|
|
4
|
+
|
|
5
|
+
A React adapter for [Tanstack's Query library](https://tanstack.com/query/latest) for @idb-orm. It essentially offers easy to use shortcuts to manage querying idb-orm data and caching.
|
|
6
|
+
|
|
7
|
+
## Example
|
|
8
|
+
|
|
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
|
+
|
|
11
|
+
```tsx
|
|
12
|
+
// How to do it in Tanstack-query
|
|
13
|
+
useQuery({
|
|
14
|
+
queryKey: ["users", "find", userId],
|
|
15
|
+
queryFn: () => client.stores.users.find({ where: { id: userId } }),
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
// How to do it in this package
|
|
19
|
+
stores.users.useFind({ where: { id: userId } }, [userId]);
|
|
20
|
+
```
|
|
21
|
+
|
|
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`.
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
This package does not come bundled with Tanstack-query or react, they are both expected to already be installed. To install the adapter package, use:
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
npm i @idb-orm/react-query
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Getting Started
|
|
33
|
+
|
|
34
|
+
Define your @idb-orm schema in any file. Then, create the provider and client object like so:
|
|
35
|
+
|
|
36
|
+
```tsx
|
|
37
|
+
// idb-query.ts
|
|
38
|
+
|
|
39
|
+
import { createIDBClient } from "@idb-orm/react-query";
|
|
40
|
+
|
|
41
|
+
// File that creates the @idb-orm database client
|
|
42
|
+
import { client } from "./db-client";
|
|
43
|
+
|
|
44
|
+
const { queryClient, Provider, stores } = createIDBQueryClient(client);
|
|
45
|
+
export { queryClient, Provider, stores };
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Then, in your project's root, wrap your application in the provider:
|
|
49
|
+
|
|
50
|
+
```tsx
|
|
51
|
+
// App.tsx
|
|
52
|
+
|
|
53
|
+
import { Provider, queryClient } from "./idb-query";
|
|
54
|
+
|
|
55
|
+
function App(){
|
|
56
|
+
return (
|
|
57
|
+
<Provider client={queryClient}>
|
|
58
|
+
{/* Appliation */}
|
|
59
|
+
<Provider>
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Some things to note:
|
|
66
|
+
|
|
67
|
+
- The `stores` field should be exported for use throughout your app. That is what provides the shortcut functions for each store.
|
|
68
|
+
- The provider component already has the Tanstack `QueryClientProvider` included!
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
## Disclaimer
|
|
72
|
+
This project is still in the **very** early stages. Bugs will occur as I work on this package. Sorry in advance.
|
|
@@ -0,0 +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)}};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { core, Model } from '@idb-orm/core';
|
|
2
|
+
import { QueryClient, DefinedUseQueryResult, UndefinedInitialDataOptions, QueryClientConfig } from '@tanstack/react-query';
|
|
3
|
+
import { DependencyList, PropsWithChildren } from 'react';
|
|
4
|
+
type QueryOptions<O> = Omit<UndefinedInitialDataOptions<O>, "queryFn" | "queryKey">;
|
|
5
|
+
interface ModelQueryInterface<M extends Model<any, any, any>, Names extends string, Models extends core.CollectionObject<Names>> {
|
|
6
|
+
/**
|
|
7
|
+
* Query instance on a particular primary key. This query will acquire **only** the given document and no related documents.
|
|
8
|
+
*/
|
|
9
|
+
useGet: M extends Model<any, infer Fields, any> ? <O = core.Simplify<core.ModelStructure<Fields, Models>>>(primaryKey: {
|
|
10
|
+
key: core.PrimaryKeyType<M>;
|
|
11
|
+
} & QueryOptions<O>) => DefinedUseQueryResult<O | undefined> : never;
|
|
12
|
+
useFind: <I extends core.FindInput<Names, M, Models>, O = core.Simplify<NonNullable<core.FindOutput<Names, M, Models, I>>>[]>(options: QueryOptions<O> & {
|
|
13
|
+
query: I;
|
|
14
|
+
}, deps?: DependencyList) => DefinedUseQueryResult<O | undefined>;
|
|
15
|
+
useFindFirst: <I extends core.FindInput<Names, M, Models>, O = core.Simplify<core.FindOutput<Names, M, Models, I>>>(options: QueryOptions<O> & {
|
|
16
|
+
query: I;
|
|
17
|
+
}, deps?: DependencyList) => DefinedUseQueryResult<O | undefined>;
|
|
18
|
+
}
|
|
19
|
+
interface ModelQueryClient {
|
|
20
|
+
invalidate(): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
type IDBClientInterface<Names extends string, _Models extends core.CollectionObject<Names>> = core.Simplify<{
|
|
23
|
+
[K in Names]: {
|
|
24
|
+
get: ModelQueryClient;
|
|
25
|
+
findFirst: ModelQueryClient;
|
|
26
|
+
find: ModelQueryClient;
|
|
27
|
+
} & core.Simplify<ModelQueryClient>;
|
|
28
|
+
} & ModelQueryClient>;
|
|
29
|
+
export declare function createIDBQueryClient<Names extends string, Models extends core.CollectionObject<Names>>(client: core.DbClient<string, Names, Models>, config?: QueryClientConfig): {
|
|
30
|
+
queryClient: IDBQueryClient<Names, Models>;
|
|
31
|
+
context: import('react').Context<{ [K_1 in Names]: {
|
|
32
|
+
get: ModelQueryClient;
|
|
33
|
+
findFirst: ModelQueryClient;
|
|
34
|
+
find: ModelQueryClient;
|
|
35
|
+
} & {
|
|
36
|
+
invalidate: () => Promise<void>;
|
|
37
|
+
}; } & ModelQueryClient extends infer T ? { [K in keyof T]: T[K]; } : never>;
|
|
38
|
+
Provider: ({ client, children, }: PropsWithChildren<{
|
|
39
|
+
client: IDBQueryClient<Names, Models>;
|
|
40
|
+
}>) => import("react").JSX.Element;
|
|
41
|
+
stores: IDBQueryInterface<Names, Models>;
|
|
42
|
+
};
|
|
43
|
+
declare class IDBQueryClient<Names extends string, Models extends core.CollectionObject<Names>> extends QueryClient {
|
|
44
|
+
readonly db: core.DbClient<string, Names, Models>;
|
|
45
|
+
constructor(db: core.DbClient<string, Names, Models>, config?: QueryClientConfig);
|
|
46
|
+
createInterface(context: React.Context<IDBClientInterface<Names, Models>>): IDBQueryInterface<Names, Models>;
|
|
47
|
+
}
|
|
48
|
+
export type IDBQueryInterface<Names extends string, Models extends core.CollectionObject<Names>> = {
|
|
49
|
+
useClient(): IDBClientInterface<Names, Models>;
|
|
50
|
+
} & {
|
|
51
|
+
[K in Names]: ModelQueryInterface<Models[K], Names, Models>;
|
|
52
|
+
};
|
|
53
|
+
export {};
|
package/dist/index.es.js
ADDED
|
@@ -0,0 +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};
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@idb-orm/react-query",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "An adapter for the Tanstack React-Query library for @idb-orm",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"build": "vite build",
|
|
8
|
+
"build-test": "vite --config vite.config-test.ts build"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [
|
|
11
|
+
"typescript",
|
|
12
|
+
"react-query",
|
|
13
|
+
"tanstack-query",
|
|
14
|
+
"idb-orm"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.es.js",
|
|
20
|
+
"require": "./dist/index.cjs.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"author": "jtb",
|
|
24
|
+
"license": "ISC",
|
|
25
|
+
"peerDependencies": {
|
|
26
|
+
"@idb-orm/core": "^1.0.7",
|
|
27
|
+
"@tanstack/react-query": "^5.90.11",
|
|
28
|
+
"react": "^19.2.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@eslint/js": "^9.38.0",
|
|
32
|
+
"@rollup/plugin-alias": "^6.0.0",
|
|
33
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
34
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
35
|
+
"@rollup/plugin-typescript": "^12.1.4",
|
|
36
|
+
"@types/node": "^24.9.1",
|
|
37
|
+
"@types/react": "^19.2.7",
|
|
38
|
+
"eslint": "^9.38.0",
|
|
39
|
+
"rollup": "^4.52.4",
|
|
40
|
+
"serve": "^14.2.5",
|
|
41
|
+
"tslib": "^2.8.1",
|
|
42
|
+
"typescript": "^5.9.3",
|
|
43
|
+
"typescript-eslint": "^8.46.2",
|
|
44
|
+
"vite": "^7.2.4",
|
|
45
|
+
"vite-plugin-dts": "^4.5.4"
|
|
46
|
+
}
|
|
47
|
+
}
|