[lib] Generate converters from validators
Summary:
https://linear.app/comm/issue/ENG-3995/create-functions-for-migrating-the-client-stores
We need to migrate client db stores, and redux persist. We can't use validators themselves because they may be changed in the future. This diffs adds a script that takes a validator and generated a function (as a string containing js code) that converts the type described by this validator. This way the migration code will remain static even if the user updates the app. The generated function are kept in lib/_generated/migration-utils.js. The only manual change (other than pasting the generating functions) in this file is adding the required imports.
Example of the generated code for the linkStore:
export function convertInviteLinksStoreToNewIDSchema( input: InviteLinksStore, ): InviteLinksStore { return { ...input, links: Object.fromEntries( entries(input.links).map(([key, value]) => [ '256|' + key, { ...value, primaryLink: value.primaryLink !== null && value.primaryLink !== undefined ? { ...value.primaryLink, role: '256|' + value.primaryLink.role, communityID: '256|' + value.primaryLink.communityID, } : value.primaryLink, }, ]), ), }; }
Test Plan: Tested with the rest of the diffs, by running schema id migrations on clients using the generated functions.
Reviewers: kamil, tomek, ashoat
Reviewed By: tomek
Subscribers: bartek, atul, ashoat
Differential Revision: https://phab.comm.dev/D8355