Skip to content

Latest commit

 

History

History
68 lines (60 loc) · 2.09 KB

persistence.md

File metadata and controls

68 lines (60 loc) · 2.09 KB

How to add state persistence

Currently the only backend we have is defined in app/persistence/local.backend.ts

Example with App

export class AppProxy extends SingletonProxyMixin({
  // App is the Sequelize model
  model: App,
  // Maps 'app' state to an object that can be fed to sequelize for insert or update
  mapStateToObject: async state => ({
    version: state.get('version'),
    autoLaunchEnabled: state.get('autoLaunchEnabled'),
    downloadFolder: state.get('downloadFolder'),
  }),
  // Maps sequelize object to 'app' redux state
  // As our state is fully Immutable, we must be sure that this always return Immutable objects
  mapObjectToState: async obj => Immutable.Map({
    version: obj.version,
    autoLaunchEnabled: obj.autoLaunchEnabled,
    downloadFolder: obj.downloadFolder,
  }),
}) {
}
  • Simple maps should extend SingletonProxyMixin
  • Complex maps should extend KeyedProxyMixin
  • Set and List should extend ListProxyMixin

Then, at the end of the file, add your mapping like this:

export default {
  app: new SingletonStateProxy(AppProxy),
  ...
}

Unit tests

See test/persistence/test-app.ts for simple example, and test/persistence/applications-app.ts for more complex ones.

Edit or create Model

See app/database/model.ts

Migration script

Add your migration script in app/persistence/umzug-runs/ folder. Your migration script should be the latest executed script (alphabetically sorted).

Define new types

If we need to add a new mapping type (i.e. for Immutable.Record), here is what must be done:

  • Add a new type of proxy in app/persistence/backend.ts. The new class MUST implement the following methods
    • clear()
    • toState()
    • async get()
    • async set(state)
  • Add a new type of proxy in app/persistence/local.backend.ts. For simplicity, is can extend SingletonProxy. The new class MUST implement the following methods
    • static async mapStateToObject()
    • static mapObjectToState()
    • static async create()
    • get()
    • async update(state)
    • toJSON()
    • isEmpty()
    • toState()