feat: Isomorphic mutators - #6
Draft
Chriztiaan wants to merge 3 commits into
Draft
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Define each mutator once and run it on both the client and the server
Builds on the original Mutator PoC #2
Why
The original Mutator PoC introduced mutators: a named write operation with a Zod arg schema, invoked on the client and replayed on the server over POST
/api/mutators/invoke. It works, but every mutator has to be written twice:shared/mutators/index.tsand used in both the client and the server.This PR removes the duplication. A mutator is defined once, in
shared/, and both sides run the same function.How
The mutator body is written against Drizzle's query builder instead of raw SQL, with the transaction handle and the table definitions passed in:
See
shared/mutators/lists.tsfor more examples.Drizzle handles the placeholder syntax, boolean representation and timestamp formatting per dialect.
What doesn't change
The wire protocol and the upload path are untouched: mutator_calls is still an insert-only table,
DemoConnector.uploadDatastill turns those rows into envelopes,/api/mutators/invokekeeps the same request and response shape.Trade-offs:
pgTableandsqliteTableare still declared separately and kept aligned by hand. The mutator logic is written once, but you still maintain two schemas.RETURNING,onConflict, raw sql templates and dialect-specific column defaults break isomorphism. In practice that means values likecreated_atandcompleted_atare now set explicitly in the mutator rather than relying on Postgresdefault now().Limitations
This PoC only covered Postgres as a source database. Drizzle also supports MySQL and MSSQL so those would also be supported in this approach in theory, however Drizzle does not support MongoDB and having isomorphic mutators with the SQLite+MongoDB combination would need a different approach.
AI Disclosure
This PR was created with the help of Claude Code. Help constitutes assistance in research, planning, and rough outline of implementation. Beyond having a hand in the implementation, I have also manually tested this work.