Skip to content

billyadelphia/sequelize-state-machine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sequelize State Machine (Work in progress)

If you're familiar with my AASM, then this is a similar take – just implemented in Sequelize v5 Model.

Installation

npm install sequelize-state-machine
or
yarn add sequelize-state-machine

Usage Example

//test.js model
import stateMachine from "sequelize-state-machine";
class Test extends Model {

    static states() {
        return [{"pending": {"initial": true}}, "active", {"inactive": {"final": true}}];
    }

    static stateTransitionIsStrict() {
        return true;
    }

    static statesTransition() {
        return {
            "activate": {
                "from": "pending",
                "to": "active"
            },
            "deactivate": {
                "from": ["pending", "active"],
                "to": "inactive"
            }
        };
    }

    async validateActive() {
        return true;
    }

    async beforeActive() {
        //some function here
    }

    async afterActive() {
        //some function here
    }
    
}

Test.init(
    {
        state: DataTypes.STRING
    }, {
        sequelize,
        modelName: 'Test',
        tableName: 'test',
        hooks: {
            async beforeCreate(model, options) {
               
            },
            async afterCreate(model, options) {

            },
            async beforeUpdate(model, options) {

            },
            async afterUpdate(model, options) {

            }
        }
        // options
    });


//place state machine after model init

stateMachine.addStates(Test.states())
    .addTransition(Test.statesTransition())
    .isStrict(Test.stateTransitionIsStrict())
    .stateField("state")
    .init(Test);

export default Test;

About

sequelize state machine

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published