@conceptho/adonis-state-machine

2.0.3 • Public • Published

Adonis State Machine

Codecov CircleCI branch npm (tag)

Adonis State Machine adds a functionality for handling status changes inside some model.

Instalation

  1. Add package:
$ npm i @conceptho/adonis-state-machine --save

or

$ yarn add @conceptho/adonis-state-machine
  1. Register Adonis State Machine provider inside the start/app.js file.
const providers = [
    ...
    '@conceptho/adonis-state-machine/provider',
    ...
]
  1. Registering the trait inside a Model(User for example).
class User extends Model {
    ...
    static boot () {
        super.boot()
        ...
        this.addTrait('@provider:Conceptho/StateMachine', {
            $attr: 'status', // Attribute name
            $namespace: 'App/Models/Status/User', // Namespace where the status objects are stored.
            $initial: 'active', // Initial status id
            $transitions: { // Transitions for the status change diagram
                active: ['unactive', 'deleted' ],
                unactive: ['deleted', 'active'],
                deleted: []
            }
        })
    }
    ...
}
  1. Changing a Status inside a Model
const user = new User()

user.changeTo(Deleted.ID) // This works
user.changeTo(Active.ID) // This does not work
  1. Example
const Model = use('Model')
const State = use('Conceptho/StateMachine/Status')

/** App/Models/User **/
class User extends Model {
    ...
    static boot () {
        super.boot()
        ...
        this.addTrait('@provider:Conceptho/StateMachine', {
            $attr: 'status', // Attribute name
            $namespace: 'App/Models/Status/User', // Namespace where the status objects are stored.
            $initial: 'active', // Initial status id
            $transitions: { // Transitions for the status change diagram
                active: ['unactive', 'deleted' ],
                unactive: ['deleted', 'active'],
                deleted: []
            }
        })
    }
    ...
}
...

/** App/Models/Status/User/Deleted **/
class Deleted extends State {
    static get ID () {
        return 'deleted'
    }
}
...

/** App/Models/Status/User/Unactive **/
class Unactive extends State {
    static get ID () {
        return 'unactive'
    }
}
...

/** App/Models/Status/User/Active **/
class Active extends State {
    static get ID () {
        return 'active'
    }
}
...

const user = new User()

const initialStatus = user.getStatus()
console.log(initialStatus.toString()) // active

await user.changeTo(Unactive.ID) // true
console.log(user.getStatus().toString()) // unactive
await user.changeTo(Deleted.ID) // true
console.log(user.getStatus().toString()) // deleted
await user.changeTo(Active.ID) // throws a error

Readme

Keywords

none

Package Sidebar

Install

npm i @conceptho/adonis-state-machine

Weekly Downloads

1

Version

2.0.3

License

MIT

Unpacked Size

19.7 kB

Total Files

15

Last publish

Collaborators

  • conceptho-owner
  • engylemure
  • metzen
  • savioserra