Socket
Book a DemoInstallSign in
Socket

bookshelf-fsm

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bookshelf-fsm

Finite State Machine Plugin for Bookshelf.js ORM

0.0.3
latest
Source
npmnpm
Version published
Weekly downloads
1
-50%
Maintainers
1
Weekly downloads
 
Created
Source

bookshelf-fsm

A Finite State Machine Plugin for Bookshelf.js ORM. See fsm-as-promised for details regarding the underlying state machine implementation.

Getting Started

var BookshelfFSM = require('bookshelf-fsm');

/**
 * Begin by passing your `bookshelf` instance to `BookshelfFSM`.
 * This method will return a new model from which you can then 
 * extend. Any model that subsequently inherits from this model
 * will be able to take advantage of finite state machine
 * functionality.
 */
var BaseMachine = BookshelfFSM(bookshelf);

var Car = BaseMachine.extend({

	'tableName': 'cars',
	
	'defaults': {
		'state': 'off'
	},
	
	/**
	 * If you don't need to perform any special setup for your
	 * model(s), you can omit this method entirely. If you do
	 * override `initialize`, however, be sure you maintain the
	 * prototype chain as shown here.
	 */
    'initialize': function() {
        Base.prototype.initialize.apply(this, arguments);
    },
    
	'_getFSMCallbacks': function() {
	
		return {
			'onstart': function() {
				return new Promise(function(resolve, reject) {
					// ...
					return resolve();
				});
			},
			'onstop': function() {
				return new Promise(function(resolve, reject) {
					// ...
					return resolve();
				});
			}
		};

	},
	
	'_getFSMEvents': function() {
	
		return [
			{
				'name': 'start',
				'from': 'off',
				'to': 'on'
			},
			{
				'name': 'stop',
				'from': 'on',
				'to': 'off'
			}
		];

	}

});

var car = new Car();

car.on('transitioning', function(to, from, event) {
    console.log('car transitioning', {
        'to': to,
        'from': from,
        'event': event
    });
});

car.on('transitioned', function(to, from, event) {
    console.log('car transitioned', {
        'to': to,
        'from': from,
        'event': event
    });
});

car.start().then(function() {
	// The state transition succeeded. The model is automatically updated with the
	// appropriate value for `state` and saved before this link in the promise chain
	// is called.
}).catch(function(err) {
	// The state transition failed. The model's `state` attribute was not updated.
});

Acknowledgements

Bookshelf-fsm is basically just intelligent superglue that binds Bookshelf to a state machine made possible by the following libraries:

Keywords

bookshelf

FAQs

Package last updated on 20 Jan 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.