[Feature Request] Split schema into selectSchema, insertSchema, updateSchema for granular control
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
- selectSchema would actually transform sync data (solving #396)
- insertSchema can have stricter validation (required fields, defaults)
- updateSchema can have partial/optional fields for updates
- Clear separation of concerns
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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