@gettersethya/mira-tanstack-adapter 0.1.0-alpha.2 → 0.1.0-alpha.4
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 +136 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
# @gettersethya/mira-tanstack-adapter
|
|
2
|
+
|
|
3
|
+
> **Early pre-alpha.** Breaking changes may occur without notice.
|
|
4
|
+
|
|
5
|
+
TanStack Query adapters for [@gettersethya/mira-client](https://www.npmjs.com/package/@gettersethya/mira-client). Wraps collection client methods with `queryOptions` and `mutationOptions`, with structured query keys for fine-grained cache invalidation.
|
|
6
|
+
|
|
7
|
+
Supports React, Svelte, and Solid via separate entry points.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
# React
|
|
13
|
+
npm install @gettersethya/mira-tanstack-adapter @tanstack/react-query
|
|
14
|
+
|
|
15
|
+
# Svelte
|
|
16
|
+
npm install @gettersethya/mira-tanstack-adapter @tanstack/svelte-query
|
|
17
|
+
|
|
18
|
+
# Solid
|
|
19
|
+
npm install @gettersethya/mira-tanstack-adapter @tanstack/solid-query
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
`@gettersethya/mira-client` is a required peer dependency. Only install the TanStack package for the framework you use.
|
|
23
|
+
|
|
24
|
+
## Setup
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { createMiraClient } from "@gettersethya/mira-client"
|
|
28
|
+
import { collectionAdapter } from "@gettersethya/mira-tanstack-adapter/react"
|
|
29
|
+
// import { collectionAdapter } from "@gettersethya/mira-tanstack-adapter/svelte"
|
|
30
|
+
// import { collectionAdapter } from "@gettersethya/mira-tanstack-adapter/solid"
|
|
31
|
+
import { Posts } from "./collections.js"
|
|
32
|
+
|
|
33
|
+
const mira = createMiraClient("/api").withCollections({ posts: Posts })
|
|
34
|
+
const postsApi = collectionAdapter(mira, "posts")
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Queries
|
|
38
|
+
|
|
39
|
+
```tsx
|
|
40
|
+
import { useQuery } from "@tanstack/react-query"
|
|
41
|
+
import { postsApi } from "./setup.js"
|
|
42
|
+
|
|
43
|
+
function PostList() {
|
|
44
|
+
const { data, isLoading } = useQuery(
|
|
45
|
+
postsApi.getList({
|
|
46
|
+
filter: (f) => f.field("published").eq(true),
|
|
47
|
+
limit: 10,
|
|
48
|
+
}).queryOptions
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
if (isLoading) return <p>Loading...</p>
|
|
52
|
+
return <ul>{data?.items.map(p => <li key={p.id}>{p.title}</li>)}</ul>
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function PostDetail({ id }: { id: string }) {
|
|
56
|
+
const { data } = useQuery(postsApi.getOne(id).queryOptions)
|
|
57
|
+
return <h1>{data?.title}</h1>
|
|
58
|
+
}
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## Mutations
|
|
62
|
+
|
|
63
|
+
```tsx
|
|
64
|
+
import { useMutation, useQueryClient } from "@tanstack/react-query"
|
|
65
|
+
import { postsApi } from "./setup.js"
|
|
66
|
+
|
|
67
|
+
function PostActions() {
|
|
68
|
+
const queryClient = useQueryClient()
|
|
69
|
+
|
|
70
|
+
const create = useMutation({
|
|
71
|
+
...postsApi.create().mutationOptions,
|
|
72
|
+
onSuccess: () => postsApi.invalidateAll(queryClient),
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
const update = useMutation({
|
|
76
|
+
...postsApi.update().mutationOptions,
|
|
77
|
+
onSuccess: (post) => postsApi.invalidateOne(queryClient, post.id),
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
const remove = useMutation({
|
|
81
|
+
...postsApi.delete().mutationOptions,
|
|
82
|
+
onSuccess: () => postsApi.invalidateAll(queryClient),
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<button onClick={() => create.mutate({ title: "New Post", published: false })}>
|
|
87
|
+
Create
|
|
88
|
+
</button>
|
|
89
|
+
)
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Svelte and Solid
|
|
94
|
+
|
|
95
|
+
The API is identical across all three frameworks. Only the import path changes.
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
// Svelte
|
|
99
|
+
import { collectionAdapter } from "@gettersethya/mira-tanstack-adapter/svelte"
|
|
100
|
+
// use with createQuery / createMutation from @tanstack/svelte-query
|
|
101
|
+
|
|
102
|
+
// Solid
|
|
103
|
+
import { collectionAdapter } from "@gettersethya/mira-tanstack-adapter/solid"
|
|
104
|
+
// use with createQuery / createMutation from @tanstack/solid-query
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Query key structure
|
|
108
|
+
|
|
109
|
+
The adapter produces structured query keys for fine-grained cache invalidation:
|
|
110
|
+
|
|
111
|
+
```typescript
|
|
112
|
+
// ["posts", "getList", { limit, filter, sort, order, cursor, select, expand }]
|
|
113
|
+
// ["posts", "getOne", "post-id"]
|
|
114
|
+
// ["posts", "getFirstOrNone", { filter, sort, order, select, expand }]
|
|
115
|
+
// ["posts", "getFullList", { limit, filter, sort, order, select, expand }]
|
|
116
|
+
|
|
117
|
+
// Invalidate all queries for a collection
|
|
118
|
+
queryClient.invalidateQueries({ queryKey: ["posts"] })
|
|
119
|
+
|
|
120
|
+
// Invalidate only list queries
|
|
121
|
+
queryClient.invalidateQueries({ queryKey: ["posts", "getList"] })
|
|
122
|
+
|
|
123
|
+
// Invalidate a specific record
|
|
124
|
+
queryClient.invalidateQueries({ queryKey: ["posts", "getOne", "post-id"] })
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
The adapter also exposes two helpers:
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
postsApi.invalidateAll(queryClient) // invalidates all posts queries
|
|
131
|
+
postsApi.invalidateOne(queryClient, postId) // invalidates the getOne query for postId
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## More
|
|
135
|
+
|
|
136
|
+
See the [Mira root README](https://github.com/gettersethya/mira) for collection definitions, field types, rules, and client usage.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gettersethya/mira-tanstack-adapter",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "TanStack Query adapters for @gettersethya/mira client SDK",
|
|
6
6
|
"author": "Getter Sethya",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"@tanstack/react-query": "^5.0.0",
|
|
44
44
|
"typescript": "^6.0.3",
|
|
45
45
|
"vitest": "^4.1.6",
|
|
46
|
-
"@gettersethya/mira-client": "0.1.0-alpha.
|
|
46
|
+
"@gettersethya/mira-client": "0.1.0-alpha.4"
|
|
47
47
|
},
|
|
48
48
|
"publishConfig": {
|
|
49
49
|
"access": "public"
|