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

How to implement storageDatabaseForClass. #135

Open
ddferrando opened this issue Jul 1, 2020 · 5 comments
Open

How to implement storageDatabaseForClass. #135

ddferrando opened this issue Jul 1, 2020 · 5 comments

Comments

@ddferrando
Copy link

I am trying to save a entity models in different databases depending on a user.
As per requirements in our app we need to maintain separate databases for each user with same entity models.
On user change I need to point to its user database.
I was making some tests with storageDatabaseForClass but I am not able to make it work.

In my SRKObject we implemented
+(NSString*)storageDatabaseForClass
{
return @"dataBase1"
}

I thought that system will create automatically a new dataBase, but I see it's not working properly.
Any suggestion?

@editfmah
Copy link
Owner

editfmah commented Jul 1, 2020

That was the intention, although I'm not sure if it asks for that information once, and not again. Therefore making it dynamic might not have the effect you were after.

I will have to create a sample app and see what actions are being taken.

Thanks for raising this.

@ddferrando
Copy link
Author

Wow!
Thanks for your superquick response.
In fact now I am facing with the issue that when implementing +(NSString*)storageDatabaseForClas sems that is not working.
If I implement this in one entity, pointing to a different dataBase this dataBase is not created in my documents folder.
Is there any step else that I need to implement.

@ddferrando
Copy link
Author

Hi.
I was making some tests regarding this issue and I am not able to create/open a new dataBase and create tables on it based on same objects/classes used in the old database.
The idea is to switch between databases depending on a user logged using same data models.
Is there anyone who can suggest out an approach in how to accomplish it?

Thanks in advance.

@editfmah
Copy link
Owner

editfmah commented Jul 3, 2020

@ddferrando, yeah that is what I've found too. So, there is the rather clunky method of close/re-open database, although i've often had weird issues with live objects from previous connections messing things up.

I've not made changes to shark in a while now, but I feel like the best possible implementation may be permanent filters. So if you were to add a userId column to the objects. Then we could maybe modify the query constructor to always include the filter.

So something like:

SharkORM.setFilter(Person.self, .query("userId", .equals, 1234))
SharkORM.setFilter(Department.self, .query("userId", .equals, 1234))

This, would be one of the easiest adaptations. I'm not sure if it feels "right", or if actually creating materialised views would be better, and then query the views instead of the tables if set.

How would you feel about those kinds of adaptations?

@ddferrando
Copy link
Author

Hi @editfmah
The permanent filters solution seems an interesting solution, although in my case, one user usually have other child users associated to it.
For me the cleanest solution would be the way to open/close database and store same models in different databases based on main user.

The problem I am facing is than I am not able to create new tables on the new Database with same SharkObjects as used in other database.

The steps I am doing are the following:
Close the previous DataBase, Opening a new one and try to setupt tables from classes in the new database, but for some reason I am not able to set new tables on based on my models. Only the default ones (_entityRevision and _schemeRevision)

Any suggestions to solve this issue?

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

No branches or pull requests

2 participants