You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Add detailed response explaining TanStack DB schema types to PowerSync team
This document clarifies the TInput/TOutput architecture and explains how
PowerSync can support arbitrary type transformations (like Date objects)
by handling serialization in their integration layer rather than
constraining TOutput to match SQLite types.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* Add comprehensive schema documentation proposal
This proposal addresses the lack of documentation around TInput/TOutput
schema types and transformations. It includes:
- Complete content outline for new schemas.md guide
- Data flow diagrams and examples
- Guidance for both app developers and integration authors
- Common patterns for Date handling, defaults, and type conversions
- Updates to existing docs (overview, mutations, collection-options-creator)
The proposal directly addresses confusion like what the PowerSync team
experienced regarding how to handle type transformations and
serialization in integrations.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* Add refined schema documentation plan based on deep investigation
After investigating all existing docs (overview, mutations, error-handling,
live-queries, collection-options-creator) and examples, created a refined
plan that addresses:
KEY FINDING: Two distinct type conversion mechanisms
1. Integration-level parsing (storage format ↔ in-memory format)
2. Schema validation/transformation (TInput → TOutput for mutations)
The plan includes:
- Analysis of what's currently documented (and gaps)
- Comprehensive schemas.md guide structure (11 sections)
- Specific updates to 5 existing docs with exact content
- Separate guidance for app developers vs integration authors
- Clear distinction between integration parsing and schema validation
- Complete working examples and best practices
- Implementation order and success criteria
This directly addresses the PowerSync confusion about TInput/TOutput and
provides clear guidance for both audiences.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* Add comprehensive schemas guide for TanStack DB
New guide (docs/guides/schemas.md) covering:
- Introduction with validation-first example
- Core concepts: TInput vs TOutput with data flow diagram
- Validation patterns (types, strings, numbers, enums, arrays, custom)
- Transformation patterns (Date conversion, JSON, computed fields)
- Default values (literals, functions, complex)
- Handling updates with union types pattern
- Error handling with SchemaValidationError
- Best practices (with performance callout)
- Two complete working examples (Todo app, E-commerce)
- Brief integration authors section linking to collection-options-creator
- Related topics links
This addresses the documentation gap identified in the PowerSync question
about TInput/TOutput and provides clear guidance for both app developers
and integration authors on schema validation and type transformations.
~850 lines, 12 sections, 30+ code examples
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* refactor: revise schemas.md based on feedback
Address review feedback to make the guide more focused and practical:
- Add clarification that schemas only validate client changes, not server data
- Remove "Handling Sync Validation Errors" section
- Fix QueryFn example to show manual parsing is required for API responses
- Rename "Handling Updates" to "Handling Timestamps" with better focus on common patterns
- Remove "Safe Parsing (Zod)" section
- Remove "When to Use Schemas" from Best Practices
- Remove "Schema Organization" from Best Practices
- Replace lengthy "For Integration Authors" section with brief link to collection-options-creator.md
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* docs: clarify schema validation and improve queryFn example
- Explicitly mention schemas catch invalid data from optimistic mutations
- Show reusing schema with .parse() in queryFn to transform API responses
- Remove The Data Flow diagram section (had errors and wasn't useful)
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* docs: update all docs with schema information
Updates across documentation to explain schemas and type transformations:
**overview.md:**
- Expand collection schemas section with comprehensive example
- Add list of what schemas do (validation, transformations, defaults, type safety)
- Link to new schemas guide
**mutations.md:**
- Add "Schema Validation in Mutation Handlers" section
- Explain that handlers receive TOutput (transformed data)
- Show serialization pattern for backends
**error-handling.md:**
- Add "When schema validation occurs" section
- Clarify schemas only validate client mutations, not sync data
- Link to schemas guide
**collection-options-creator.md:**
- Add "Schemas and Type Transformations" section
- Explain three approaches: parse/serialize helpers, user handles, automatic serialization
- Show examples from TrailBase and Query Collection
- Document design principles for integration authors
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
* Remove docs
* docs: clarify TInput must be superset of TOutput requirement
Addresses PR review feedback from samwillis about the critical design principle that TInput must be a superset of TOutput when using transformations.
Key improvements:
- Add prominent "Critical Design Principle" section explaining why TInput must accept all TOutput values
- Clarify that union types are REQUIRED (not optional) for transformations
- Add clear ❌/✅ examples showing what breaks and why
- Explain the draft parameter typing issue in collection.update()
- Strengthen language in Best Practices from "should" to "must"
This makes it clear that when schemas transform type A to type B, you must use z.union([A, B]) to ensure updates work correctly.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <[email protected]>
---------
Co-authored-by: Claude <[email protected]>
Copy file name to clipboardExpand all lines: docs/guides/collection-options-creator.md
+118-1Lines changed: 118 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -219,7 +219,124 @@ parse: {
219
219
}
220
220
```
221
221
222
-
### 5. Mutation Handler Patterns
222
+
### 5. Schemas and Type Transformations
223
+
224
+
When building a custom collection, you need to decide how to handle the relationship between your backend's storage format and the client-side types users work with in their collections.
225
+
226
+
#### Two Separate Concerns
227
+
228
+
**Backend Format** - The types your storage layer uses (SQLite, Postgres, Firebase, etc.)
229
+
- Examples: Unix timestamps, ISO strings, JSON strings, PostGIS geometries
230
+
231
+
**Client Format** - The types users work with in their TanStack DB collections
232
+
- Examples: Date objects, parsed JSON, GeoJSON
233
+
234
+
Schemas in TanStack DB define the **client format** (TInput/TOutput for mutations). How you bridge between backend and client format depends on your integration design.
// If successful, stores: { created_at: Date } // TOutput: Date
70
+
```
71
+
72
+
For more details on schema validation and type transformations, see the [Schemas guide](./schemas.md).
73
+
48
74
## Query Collection Error Tracking
49
75
50
76
Query collections provide enhanced error tracking utilities through the `utils` object. These methods expose error state information and provide recovery mechanisms for failed queries:
When a schema is configured for a collection, TanStack DB automatically validates and transforms data during mutations. The mutation handlers receive the **transformed data** (TOutput), not the raw input.
492
+
493
+
```typescript
494
+
const todoSchema =z.object({
495
+
id: z.string(),
496
+
text: z.string(),
497
+
created_at: z.string().transform(val=>newDate(val)) // TInput: string, TOutput: Date
498
+
})
499
+
500
+
const collection =createCollection({
501
+
schema: todoSchema,
502
+
onInsert: async ({ transaction }) => {
503
+
const item =transaction.mutations[0].modified
504
+
505
+
// item.created_at is already a Date object (TOutput)
0 commit comments