Replies: 1 comment 1 reply
-
I've found in the source of Collections.js things that seem promising, but there is absolutely no documentation on how these should be used. Obviously @radex has thought about this very common use case of needing to manipulate models that don't exist yet in the DB. /**
* Prepares a new record to be created, based on a raw object.
*
* Don't use this unless you know how RawRecords work in WatermelonDB. See docs for more details.
*
* This is useful as a performance optimization, when adding online-only features to an otherwise
* offline-first app, or if you're implementing your own sync mechanism.
*/
prepareCreateFromDirtyRaw(dirtyRaw: DirtyRaw): Record {
// $FlowFixMe
return this.modelClass._prepareCreateFromDirtyRaw(this, dirtyRaw)
}
/**
* Returns a disposable record, based on a raw object.
*
* A disposable record is a read-only record that **does not** exist in the actual database. It's
* not cached and cannot be saved in the database, updated, deleted, queried, or found by ID. It
* only exists for as long as you keep a reference to it.
*
* Don't use this unless you know how RawRecords work in WatermelonDB. See docs for more details.
*
* This is useful for adding online-only features to an otherwise offline-first app, or for
* temporary objects that are not meant to be persisted (as you can reuse existing Model helpers
* and compatible UI components to display a disposable record).
*/
disposableFromDirtyRaw(dirtyRaw: DirtyRaw): Record {
// $FlowFixMe
return this.modelClass._disposableFromDirtyRaw(this, dirtyRaw)
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Coming from sever side ORMs like laravel/bookshelf/objection etc... there's this concept of creating an instance of a model, doing modifications on it, then finally comitting or saving it.
Let's say I have a jouranling app. The user could create a new post, add some writing, add a title, then add some pictures. So in this example lets say theres the Post, Picture and Comment models. In this instance, I thought I would create a in memory instance of a Post model, allow the user to edit that. Create some in memory picture models. Then the user would commit these new instances into the db, or could back out and throw everything out.
I looked at the source a bit and it seems like there is no way to directly modify values on a model. What would be the correct way to enable this in an app? Would i just actually create everything, modify the sqlite db, then have those reactively update the views?
My worry with this approach is that wouldn't this mean every time lets say the post's content is changed by 1 letter (as the user types) this is writing to the sqlite db, and then the view is reactively updated? Seems like a lot of I/O. My other idea was to use the standard state, then only on save, copy everything from state and insert it as a new row in the db. This alternative leaves me annoyed that I can't make use of any custom methods of the model I may have.
Beta Was this translation helpful? Give feedback.
All reactions