TanStack / TanStack/db

[Feature Request] Split schema into selectSchema, insertSchema, updateSchema for granular control

Open
#1,115 8 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
3.9k
Forks
266
Avg merge
1d 4h
Merged PRs (30d)
55

Description

Problem

Currently, the schema property is used for both validation (on mutations) and type inference, but transformations are not applied to data coming from sync (e.g., ElectricSQL).

This was previously reported in #396 - the schema transforms work for insert/update but not for data received from the sync stream.

Example:

const schema = z.object({
  id: z.string(),
  createdAt: z.string().transform(val => new Date(val)), // Transform string → Date
});

// Expected: createdAt is Date
// Actual: createdAt is still string (from Electric sync)

Proposal

Replace the single schema property with granular schemas:

electricCollectionOptions({
  // Schema for transforming/validating data FROM sync (select/read)
  selectSchema: z.object({
    id: z.string(),
    message: z.string(),
    createdAt: z.string().transform(val => new Date(val)),
  }),
  
  // Schema for validating data on insert (with defaults)
  insertSchema: z.object({
    id: z.string().default(() => crypto.randomUUID()),
    message: z.string().min(1),
    createdAt: z.date().default(() => new Date()),
  }),
  
  // Schema for validating data on update (optional, falls back to insertSchema)
  updateSchema: z.object({
    message: z.string().min(1).optional(),
  }),
})

Benefits

  1. selectSchema would actually transform sync data (solving #396)
  2. insertSchema can have stricter validation (required fields, defaults)
  3. updateSchema can have partial/optional fields for updates
  4. Clear separation of concerns
  5. Similar pattern to PowerSync's deserializationSchema

Alternative

If splitting into 3 schemas is too complex, at minimum add deserializationSchema (like PowerSync has) to transform data from the sync stream.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating electricCollectionOptions and the current handling of the schema property, then trace how data from the sync stream is processed. Compare the proposed selectSchema, insertSchema, and updateSchema responsibilities, and consider the deserializationSchema alternative; done means sync data transformations, insert defaults, and update validation behave as specified.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.