You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A subtle issue you can find in most ORMs today is that you mutate model instances as you go and sync them with the remote database from time to time. It seems like an easy and convenient approach, but falls short if a model instance is used concurrently (esp. dangerous in JS with async code everywhere!).
Consider this:
constuser=awaitUser.findByName('Karl')awaitPromise.all([renameUser(user,'John'),changeBirthday(user,'1990-08-01')])asyncfunctionrenameUser(user,newName){awaitsomething()user.name=newName}asyncfunctionchangeBirthday(user,birthday){awaitsomethingElse()user.birthday=birthdayconsole.log(`User ${user.name}'s birthday is the ${birthday}.`)// Race Condition! Maybe `user.name` is still 'Karl', maybe it is already 'John'!}
You can easily spend an hour of debugging to recognize the issue. And fixing it may be even harder.
So I'd like to propose a different approach:
constuser=awaitUser.findByName('Karl')awaitrenameUser(user,'John')asyncfunctionrenameUser(user,newName){awaitsomething()// Returns an updated user instance, the old instance remains untoucheduser=user.setName(newName)}
The setter methods could probably be easily derived from the field name. You could also track the change operations (setName, setBirthday, ...) since the last database sync and have a change history. Might be nice for debugging and to only update fields in the database that have actually changed.
The text was updated successfully, but these errors were encountered:
A subtle issue you can find in most ORMs today is that you mutate model instances as you go and sync them with the remote database from time to time. It seems like an easy and convenient approach, but falls short if a model instance is used concurrently (esp. dangerous in JS with async code everywhere!).
Consider this:
You can easily spend an hour of debugging to recognize the issue. And fixing it may be even harder.
So I'd like to propose a different approach:
The setter methods could probably be easily derived from the field name. You could also track the change operations (
setName
,setBirthday
, ...) since the last database sync and have a change history. Might be nice for debugging and to only update fields in the database that have actually changed.The text was updated successfully, but these errors were encountered: