@kivacore/tanstack-db-generator 0.0.0 → 0.10.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/README.md CHANGED
@@ -1,3 +1,140 @@
1
1
  # @kivacore/tanstack-db-generator
2
2
 
3
- Placeholder. Install the latest version.
3
+ Generate TanStack Query collections and TypeScript types from OpenAPI 3.1.0 specifications.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @kivacore/tanstack-db-generator
9
+ # or
10
+ pnpm add @kivacore/tanstack-db-generator
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ### CLI
16
+
17
+ ```bash
18
+ # Generate all collections and types from OpenAPI spec
19
+ tanstack-db-generator path/to/openapi-spec.json
20
+
21
+ # Specify output directory
22
+ tanstack-db-generator path/to/openapi-spec.json -o ./src/generated
23
+
24
+ # Generate data source collections for selected apps only
25
+ tanstack-db-generator path/to/openapi-spec.json --apps crm support
26
+
27
+ # Generate only specific data source collections (appSlug/dataSourceSlug format)
28
+ tanstack-db-generator path/to/openapi-spec.json --dataSources crm/contacts crm/accounts
29
+
30
+ # Combine both filters (intersection — must match both)
31
+ tanstack-db-generator path/to/openapi-spec.json --apps crm --dataSources crm/contacts
32
+
33
+ # Comma-separated values are also accepted
34
+ tanstack-db-generator path/to/openapi-spec.json --apps crm,support
35
+ tanstack-db-generator path/to/openapi-spec.json --dataSources crm/contacts,crm/accounts
36
+ ```
37
+
38
+ ### Programmatic API
39
+
40
+ ```typescript
41
+ import { generateFromOpenAPI } from '@kivacore/tanstack-db-generator';
42
+
43
+ const spec = JSON.parse(fs.readFileSync('openapi-spec.json', 'utf-8'));
44
+
45
+ // Generate all collections
46
+ await generateFromOpenAPI(spec, './src');
47
+
48
+ // Generate with filters
49
+ await generateFromOpenAPI(spec, './src', {
50
+ apps: ['crm', 'support'],
51
+ dataSources: ['crm/contacts', 'crm/accounts']
52
+ });
53
+ ```
54
+
55
+ ### Filtering Data Sources
56
+
57
+ The `--apps` and `--dataSources` options let you selectively generate collections for a subset of the spec:
58
+
59
+ | Option | Format | Example |
60
+ |--------|--------|---------|
61
+ | `--apps` | `<appSlug> [appSlug...]` | `--apps crm support` |
62
+ | `--dataSources` | `<appSlug/dataSourceSlug> [...]` | `--dataSources crm/contacts crm/accounts` |
63
+
64
+ **Behavior:**
65
+
66
+ - When **neither** filter is provided, all data source collections and non-data-source endpoint groups are generated.
67
+ - When **either** filter is provided, only matching data source collections are generated. Non-data-source endpoint groups are still always (re)generated so they stay in sync with the spec on every run.
68
+ - When **both** filters are provided, they are combined as an **intersection** — a data source must match both the app filter and the data source filter.
69
+ - The generator **validates** filter values against the spec and throws an error if an unknown app or data source is requested.
70
+ - All existing `*.collection.ts` files in the output collections folder are removed before generation, so filtered runs do not leave stale files behind.
71
+
72
+ ## Features
73
+
74
+ - Automatically generates TanStack Query collections for CRUD operations
75
+ - Creates TypeScript type definitions from OpenAPI schemas
76
+ - Supports dataSource endpoints with the following pattern:
77
+ - `GET /v1/apps/:appName/data-sources/:dataSourceName/items` - List items
78
+ - `GET /v1/apps/:appName/data-sources/:dataSourceName/items/:itemId` - Get single item
79
+ - `POST /v1/apps/:appName/data-sources/:dataSourceName/items` - Create item
80
+ - `PATCH /v1/apps/:appName/data-sources/:dataSourceName/items/:itemId` - Update item
81
+ - `DELETE /v1/apps/:appName/data-sources/:dataSourceName/items/:itemId` - Delete item
82
+ - `DELETE /v1/apps/:appName/data-sources/:dataSourceName/items` - Delete many items
83
+
84
+ ## Generated Files Structure
85
+
86
+ ```
87
+ src/db/
88
+ ├── types/
89
+ │ └── index.ts # TypeScript interfaces and Zod schemas
90
+ ├── collections/
91
+ │ ├── app-datasource.collection.ts # Individual collection files
92
+ │ └── index.ts # Exports all collections
93
+ └── index.ts # Main entry point
94
+ ```
95
+
96
+ ## Required client setup
97
+
98
+ The generated CRUD methods (`list`, `get`, `create`, `update`, `delete`, `deleteMany`) call `client.get/post/patch/delete` and return the result directly — they do **not** read `.data` from a Docyrus `{ success, data }` envelope themselves. Register `@kivacore/app-utils`' `envelopeUnwrapInterceptor` on your `RestApiClient` before any collection method runs, otherwise `list()` returns the envelope object instead of the typed array and TanStack DB breaks.
99
+
100
+ ```ts
101
+ import { RestApiClient } from '@kivacore/api-client';
102
+ import { envelopeUnwrapInterceptor } from '@kivacore/app-utils';
103
+
104
+ const client = new RestApiClient({ baseURL: 'https://api.example.com' });
105
+ client.addInterceptor(envelopeUnwrapInterceptor);
106
+ ```
107
+
108
+ If your client is built elsewhere (e.g. by `@kivacore/signin`'s `DocyrusAuthProvider`), register the interceptor on the resulting instance the same way — registration is idempotent.
109
+
110
+ The interceptor is shape-strict and lossless: domain payloads that carry their own `data` field (e.g. `AppConfig`, `UserAppConfig`, records with a `data` column) are left alone, and envelopes carrying `meta` (paginated lists), `error`, or `message` are preserved so callers can read them. See [`@kivacore/app-utils` → Envelope handling](https://github.com/Docyrus/docyrus-devkit/tree/main/packages/app-utils#envelope-handling) for the full table.
111
+
112
+ ## Example Usage
113
+
114
+ After generation, you can use the collections in your React components:
115
+
116
+ ```tsx
117
+ import { useDrizzleQuery } from '@tanstack/react-db';
118
+ import { baseContactCollection } from './collections';
119
+
120
+ function ContactList() {
121
+ const { data, isLoading, error } = useDrizzleQuery(
122
+ baseContactCollection.list()
123
+ );
124
+
125
+ if (isLoading) return <div>Loading...</div>;
126
+ if (error) return <div>Error: {error.message}</div>;
127
+
128
+ return (
129
+ <ul>
130
+ {data?.data.map(contact => (
131
+ <li key={contact.id}>{contact.name}</li>
132
+ ))}
133
+ </ul>
134
+ );
135
+ }
136
+ ```
137
+
138
+ ## License
139
+
140
+ MIT