bunderstack-query 0.1.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/src/types.ts ADDED
@@ -0,0 +1,103 @@
1
+ import type { QueryClient, UseMutationOptions } from '@tanstack/react-query'
2
+ import type { TableAccessInput } from 'bunderstack/access'
3
+
4
+ import type {
5
+ BucketClient,
6
+ BucketMutationOptions,
7
+ } from './bucket-client'
8
+ import type { TableMutationOptions } from './mutation-options'
9
+ import type { TableClient } from './table-client'
10
+
11
+ export type Paginated<T> = {
12
+ items: T[]
13
+ limit: number
14
+ offset?: number
15
+ cursor?: string
16
+ nextCursor?: string
17
+ hasMore: boolean
18
+ total?: number
19
+ q?: string
20
+ sort?: string
21
+ order?: 'asc' | 'desc'
22
+ }
23
+
24
+ export type ListParams = {
25
+ limit?: number
26
+ offset?: number
27
+ sort?: string
28
+ order?: 'asc' | 'desc'
29
+ q?: string
30
+ cursor?: string
31
+ count?: boolean
32
+ /** Keyset pagination — first page omits offset. Used by listInfiniteQuery. */
33
+ cursorMode?: boolean
34
+ } & Record<
35
+ string,
36
+ // Array values are comma-joined into `?column=a,b,c`, matched server-side
37
+ // as `column IN (...)` — handy for fetching exactly the rows referenced by
38
+ // a page of results instead of an unbounded `list`.
39
+ string | number | boolean | null | undefined | readonly (string | number)[]
40
+ >
41
+
42
+ export type InferSelect<T> = T extends { $inferSelect: infer R } ? R : never
43
+
44
+ export type InferInsert<T> = T extends { $inferInsert: infer R } ? R : never
45
+
46
+ export type AuthTableName = 'user' | 'session' | 'account' | 'verification'
47
+
48
+ export type CrudTableKey<TSchema extends Record<string, unknown>> = {
49
+ [K in keyof TSchema & string]: K extends AuthTableName ? never : K
50
+ }[keyof TSchema & string]
51
+
52
+ export type CreateClientOptions<
53
+ TSchema extends Record<string, unknown>,
54
+ TAccess extends Record<string, TableAccessInput> | undefined = undefined,
55
+ > = {
56
+ /** Drizzle schema object — used to resolve CRUD-exposed tables at runtime. */
57
+ schema?: TSchema
58
+ access?: TAccess
59
+ /** Explicit table keys when schema cannot be imported on the client (type-only import + tables). */
60
+ tables?: readonly (keyof TSchema & string)[]
61
+ baseUrl?: string
62
+ fetch?: typeof fetch
63
+ /** TanStack Query client — enables cache invalidation in mutation options. */
64
+ queryClient?: QueryClient
65
+ }
66
+
67
+ export type TableQueryOptions<
68
+ TRow,
69
+ TCreate = Partial<TRow>,
70
+ TUpdate = Partial<TRow>,
71
+ > = TableClient<TRow, TCreate, TUpdate> &
72
+ TableMutationOptions<TRow, TCreate, TUpdate>
73
+
74
+ export type TableQueryOptionsForKey<
75
+ TSchema extends Record<string, unknown>,
76
+ K extends keyof TSchema,
77
+ > = TableQueryOptions<
78
+ InferSelect<TSchema[K]>,
79
+ InferInsert<TSchema[K]>,
80
+ Partial<InferSelect<TSchema[K]>>
81
+ >
82
+
83
+ export type BunderstackQueryClient<
84
+ TSchema extends Record<string, unknown>,
85
+ TExposed extends keyof TSchema & string = CrudTableKey<TSchema>,
86
+ > = {
87
+ [K in TExposed]: TableQueryOptionsForKey<TSchema, K>
88
+ }
89
+
90
+ export type FilesQueryClient<TBuckets extends string> = {
91
+ files: {
92
+ [K in TBuckets]: BucketClient & BucketMutationOptions
93
+ }
94
+ }
95
+
96
+ export type ExposedTableKeys<
97
+ TSchema extends Record<string, unknown>,
98
+ TTables extends readonly (keyof TSchema & string)[] | undefined,
99
+ > = TTables extends readonly (keyof TSchema & string)[]
100
+ ? TTables[number]
101
+ : CrudTableKey<TSchema>
102
+
103
+ export type { UseMutationOptions }