easyblockshq / easyblocks

The open-source visual builder framework.
https://easyblocks.io
GNU Affero General Public License v3.0
313 stars 44 forks source link

Feature: Document migrations #54

Open timoconnellaus opened 4 months ago

timoconnellaus commented 4 months ago

The issue: The schema in the definitions might change but there isn't a standard way to deal with keeping the document in line with updates to the schema.

The idea is to add a migrate function and version field to the NoCodeComponentDefinition object. The migrate function would be called (probably when reading a document from the backend) and would run migrations in order based on the current version of the definition and the version of the data that is stored in the document to bring the document up to the latest version.

This would allow for document schemas to change over time with the developer able to implement migration steps that automatically deal with the documents as they are read instead of having to build a bulk migration process that is run on deployment.

Some examples of when this would be utilised:

r00dY commented 4 months ago

That would be great. The simplest way I see it is like this:

export const linkDefinition: NoCodeComponentDefinition = {
  // ...
  version: 2,
  migrations: [
    // index 0 is migration from 0 -> 1
    (entry0: NoCodeComponentEntry) => {
      const entry1 : NoCodeComponentEntry = migrateEntryFrom0To1(entry0);
      return entry1
    },
    // index 1 is migration from 1 -> 2
    (entry1: NoCodeComponentEntry) => {
      const entry2 : NoCodeComponentEntry = migrateEntryFrom1To2(entry1);
      return entry2
    }
  ]
};

Do you think sth like this would work?