Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need a way to reset Realm db Models #6009

Open
somasekharkakarla opened this issue Jul 26, 2023 · 9 comments
Open

Need a way to reset Realm db Models #6009

somasekharkakarla opened this issue Jul 26, 2023 · 9 comments

Comments

@somasekharkakarla
Copy link

somasekharkakarla commented Jul 26, 2023

Problem

Currently we have deleteAll() method in realm which help use to delete all models from realm. But there are N number of use case where we need a way to reset the db instead deleting the models. By now, we can only do it a way getting all objects and delete it.
But we need a way to reset the model without we do all this.

For example: if a user change from one region to another. We need to remove all pref and data from the region and add new data. If we use deleteAll() it also losing the it reference which causing errors.

Solution

Implementation of resetAll() and reset(['Model1', 'Model2']) which helps to reset data instead of calling a objects of all models and deleting it.

Alternatives

No response

How important is this improvement for you?

Would be a major improvement

Feature would mainly be used with

Local Database only

@somasekharkakarla somasekharkakarla changed the title Need a way to reset Realm db Need a way to reset Realm db Models Jul 26, 2023
@takameyer
Copy link
Contributor

@somasekharkakarla Can you clarify what you mean by "reset"? Currently you can use realm.delete to remove any data as you see fit without blowing away the entire database. I'm unsure how a "reset" would differ from a "delete".

@somasekharkakarla
Copy link
Author

Hi @takameyer,

For an example:

we have 5 models
Model 1, Model 2, Model 3, Model 4, Model 5

if we want to clear the object in these Models

const model1Objects = realm.objects('Model 1')
realm.delete(model1Objects)
const model2Objects = realm.objects('Model 2')
realm.delete(model2Objects)
const model3Objects = realm.objects('Model 3')
realm.delete(model3Objects)
const model4Objects = realm.objects('Model 4')
realm.delete(model4Objects)
const model5Objects = realm.objects('Model 5')
realm.delete(model5Objects)

Now all Objects are deleted in the model which i call a reset of model
what if we have 20 models
we will have 40 lines

if we can introduce reset(['Model1', 'Model2']) and resetAll() function. we can achieve it very fast instead of writing long code

now code will be
if i want to reset only two models
realm.reset([Model 1, Model 2])

if i want to reset all
just
realm.resetAll()

@takameyer
Copy link
Contributor

takameyer commented Jul 27, 2023

@somasekharkakarla it's an interesting idea, but very specialized. You could write these as helper functions in your own application:

function resetRealmModels(models, realm){
  for( const model of models ){
    realm.delete(realm.objects(model));
  }
}

I don't see how realm.resetAll() is any different than realm.deleteAll()

@somasekharkakarla
Copy link
Author

Delete all will delete including Model. No model will be there in db right. Yes i can write a helper function. Its not that important but just if we have one it will great

@somasekharkakarla
Copy link
Author

And also @takameyer can you please look into it
#5864

@marysiapietraszewska marysiapietraszewska added the Needs-Attention Reporter has responded. Review comment. label Jul 28, 2023
@sync-by-unito sync-by-unito bot added gathering-interest and removed Needs-Attention Reporter has responded. Review comment. labels Jul 29, 2023
@kneth
Copy link
Contributor

kneth commented Aug 29, 2023

We can create small aux. methods for reset and resetAll e.g.,

resetAll() =>  this.schema.forEach((s) => this.delete(this.objects(s.name)); 

@somasekharkakarla
Copy link
Author

Great

@somasekharkakarla
Copy link
Author

Hi @kneth
resetAll() => this.schema.forEach((s) => this.delete(this.objects(s.name));
i am using as you defined. it working but not every time.
if there is large data this.delete(this.objects(s.name) is taking time . And there is no way i can know that objects are deleted. Due this, reseting DB is causing the issue. Please add patch to delete to return a promise on delete

@somasekharkakarla
Copy link
Author

Hi @kneth , any update on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants