get-convex / convex-helpers

A collection of useful code to complement the official packages.
MIT License
66 stars 9 forks source link

pretend validators #82

Closed ianmacartney closed 3 months ago

ianmacartney commented 3 months ago

Adds convenience validators for when you don't mind some targeted incorrectness, esp. while you do migrations

Can be part of a post on pragmatic migration dev tips. Saves you from turning on & off schemaValidation.

Flow to add a field:

  1. Add myField: pretendRequired(v.string()), and code that sets & uses myField as if it exists[ and a migration to set it].
  2. Deploy & change the data in the dashboard [or run the migration]
  3. Some time later, take off the pretendRequired wrapper and deploy.

Improves over the following in that you don't have to sprinkle if statements everywhere:

  1. Add myField: v.optional(v.string()) and code that handles it not being there[ and a migration to set it].
  2. Same as above
  3. Update the code to assume it's there (take out all the if(!doc.newField) throw ... lines) and deploy.

Flow to change a field's type:

  1. Add myField: pretend(v.number()) and code that assumes number, and a migration to change string -> number.
  2. Same as above
  3. Remove the pretend and deploy.

This saves you from changing all of your code to handle the union, just in order to deploy something that allows you to edit data in the dashboard.

xixixao commented 3 months ago

But isn't it better to turn off schema validation? This doesn't really work for online migrations, because your reads will be broken (the schema pretends the data is there, but it is not).

ianmacartney commented 3 months ago

Turning off schema validation stops checking all the other fields, and can take a long time to turn back on to validate literal unions etc. And yes this makes it your problem to read bad data- similar to a typical ORM. This is a more targeted way to turn off schema validation that is hopefully short-term

On Thu, Mar 14, 2024 at 2:15 AM Michal Srb @.***> wrote:

But isn't it better to turn off schema validation? This doesn't really work for online migrations, because your reads will be broken (the schema pretends the data is there, but it is not).

— Reply to this email directly, view it on GitHub https://github.com/get-convex/convex-helpers/pull/82#issuecomment-1996982038, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACZQW75ZPYCBYIQQAQ6E6LYYFTBNAVCNFSM6AAAAABEVBHXICVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSOJWHE4DEMBTHA . You are receiving this because you authored the thread.Message ID: @.***>

ianmacartney commented 3 months ago

other naming thoughts: typesOnly instead of pretend optionalButTypeAsRequired instead of pretendRequired but I want to keep the latter as easy to type as possible, since it'll (hopefully) be used in dev mode mostly.