apiko-rest-api / apiko

The Apiko itself!
https://apiko-rest-api.github.io/
MIT License
16 stars 6 forks source link

Database structure synchronization on setup reload #1

Open kasp1 opened 7 years ago

kasp1 commented 7 years ago

When the server setup is changed (whenever a change to endpoints or collections in the UI is made and the setup is "saved" to the server), the server triggers a reload and it is necessary to synchronize the database structure according to the new setup of collections.

What needs to be synchronized:

The problem is that Sequelize.js, an awesome lib in many ways that we use for ORM in Apiko, does not fully support this kind of "programmatic migrations" yet! See this Sequelize issue.

This feature is indeed very critical for Apiko to be any useful.

What Sequlize does whenever a database structure changes in the model is that it drops and recreates the table, which dumps all data.

We need this to get to work in the sync() function of data.js. The Sequlize models can be accessed from this function as this.store, it's an object where each model is named according to the database table name, so e.g. the users model could be accessed with this.store.users. When this sync() function is called during a (re)load, the models are already updated to the new setup, so can be used as a reference of "what should the new database structure look like".

meyer9 commented 7 years ago

https://github.com/sequelize/sequelize/issues/7230

kasp1 commented 7 years ago

Awesome job @meyer9 thank you!

kasp1 commented 7 years ago

@meyer9 's PR was finally merged to master, but this feature doesn't seem to have been released yet according to Sequelize changelogs: http://docs.sequelizejs.com/en/v3/changelog/

There are no dates. Let's keep checking.