a-type / verdant

🌿 Storage, sync & realtime for local-first web apps. Build an app in minutes, deploy as a static page, and add sync with a few lines.
https://verdant.dev
GNU Affero General Public License v3.0
191 stars 4 forks source link

getOrCreate ? #372

Closed a-type closed 6 months ago

a-type commented 6 months ago

This pattern seems to come up frequently enough. Wanting to either retrieve or create a field.

The big question is, is it safe to do this? Seems as safe as any other approach, if not moreso.

The idea is to synchronously submit ops to create the value if it's not there, and always return it. Nullable fields are typed as non-null with this getter. A valid init must be supplied as second param, if the item can't be fully defaulted.