ujjwalguptaofficial / JsStore

Simplifying IndexedDB with SQL like syntax and promises
http://jsstore.net/
MIT License
858 stars 110 forks source link

Issues and questions about versioning #162

Closed kwlayman closed 4 years ago

kwlayman commented 4 years ago

Trying your library, so far is awesome, nice work!

I do have an issues with versioning though. I'm looking at your example of how to change a table by doing a select all, then changing schema, then importing all. When I try this I have a couple of issues/questions:

  1. What to do if insert fails because old data is not 100% compatible with new schema? It seems in my tests that unless something like a temp table is manually created and used the data will be lost.

  2. What about the Autoincremented Primary Key that is used in other tables as a FK? Is there a way to handle this?

  3. I don't seem to be able to add a table to an existing schema w/o recreating entire schema with new table?

ujjwalguptaofficial commented 4 years ago

Thanks @kwlayman

Answer to your issues is -

  1. yes data will be lost, so better is to leave old table , create another table with new schema and then import data from old table to new table.

  2. No, IndexedDb is nosql database, so there is no relationship between tables. You will have to take care this manually. Basically insert each data manually & verify with second table & if there is any issue remove inserted data , you can use transaction for this.

  3. You can't update a table schema without recreating it. This is IndexedDb limitation.

kwlayman commented 4 years ago

Thanks for the quick response.

Regarding #2 my question relates to how to keep from recreating primary keys so as to keep from losing the existing relationships with the foreign keys in related tables. So specifically Is there a way to temporarily disable autoincrement on a Primary Key?

kwlayman commented 4 years ago

I should have said disable autoincrement for the purpose of importing existing records with Primary Keys, and then re-enabling autoincrement for normal insertions?

ujjwalguptaofficial commented 4 years ago

you can use skipDataCheck in insert query.

kwlayman commented 4 years ago

Regarding #1, how can I create a new table in the same schema w/o losing the data in the existing tables? I'm obviously missing something here as I understand you to say I can't add a table to an existing schema w/o recreating entire schema, so how can I then copy the data from the old table when it has been replaced with the new schema?

kwlayman commented 4 years ago

Regarding #1 you can disregard my last comment, I think I understand how to do it, thx!

kwlayman commented 4 years ago

Still seems fraught with danger though as the data only exists in memory during the process?

kwlayman commented 4 years ago

Maybe better to create new database entirely? JsStore can maintain open connections to multiple databases?

ujjwalguptaofficial commented 4 years ago

no it wont be in memory - when you create another table your old table data persists. So its just you fetch data from one table & insert to another.

e.g - let's say you have a table customer & you are upgrading your schema so you created anoher table customerV2, now when db is created you move data from customer to customerV2. Now customerV2 is your active table.

ujjwalguptaofficial commented 4 years ago

Yes its simple & clean to create another database but you will have to make sure you are not having issues with indexedDb quota.

Yes jsstore can maintain multiple database connection but only when you are using web worker. Without webworker there is a context mixup issue.

kwlayman commented 4 years ago

When I create a new table the old tables disappear in JsStore even though they are still present in IndexedDB according to Google developer tools. They do not show up using getDbSchema, only the new table does.

kwlayman commented 4 years ago

Ok I figured out what I was doing wrong, I'll update this with my mistakes.

ujjwalguptaofficial commented 4 years ago

@kwlayman here is an example of changing db schema - https://github.com/ujjwalguptaofficial/jsstore-examples/tree/master/change-db-schema