Nozbe / WatermelonDB

🍉 Reactive & asynchronous database for powerful React and React Native apps ⚡️
https://watermelondb.dev
MIT License
10.62k stars 600 forks source link

Overriding @field setter #312

Closed bogdancochioras closed 5 years ago

bogdancochioras commented 5 years ago

Hello, I have the following declaration for a field on a model: @field('x') x; Is it possible to override setter for this field on the model? How could I achieve that to not handle the value where I call the CRUD methods?

rkrajewski commented 5 years ago

@bogdancochioras You can try to to use @json field (https://github.com/Nozbe/WatermelonDB/blob/master/docs/Advanced/AdvancedFields.md#json) with its sanitizer function, but value will be always stored as a string (or null). You can also use another indirect field of model class, defined as getter/setter, to process value before get/set in @field.

bogdancochioras commented 5 years ago

I

@bogdancochioras You can try to to use @JSON field (https://github.com/Nozbe/WatermelonDB/blob/master/docs/Advanced/AdvancedFields.md#json) with its sanitizer function, but value will be always stored as a string (or null). You can also use another indirect field of model class, defined as getter/setter, to process value before get/set in @field.

I wanted to go with the second option you mentioned but thought there might be a more direct way to not change code implementation which already set data directly in the field

rkrajewski commented 5 years ago

to not change code implementation which already set data directly in the field

You can simply change the name of your @field('x') x to @field('x') _x, and create indirect getter/setter called x to process data before set it to '_x'. There is no other way to overwrite setter of @field.

radex commented 5 years ago

@bogdancochioras Closing, assuming this is a solved issue