Git Product home page Git Product logo

mongoose-state-machine's Introduction

@rohan89/mongoose-state-machine

A Mongoose plugin that implement a state machine into a Mongoose schema.

The plugin is based on javascript-state-machine.

Install

npm install @rohan89/mongoose-state-machine --save

Options

  • stateMachine Object The state machine declaration object (javascript-state-machine)
  • fieldName string The name of the schema field that stores the current state (optional, default status)

Usage

First you need to declare a schema and extend it using the plugin.

Important: The following schema paths cannot be used: state, is, can, cannot, transitions, allTransitions, allStates and transitions names. Because they conflict with javascript-state-machine

import mongoose from 'mongoose';
import stateMachinePlugin from '@rohan89/mongoose-state-machine';

// schema declaration
const matterSchema = new mongoose.Schema({ 
  matterState: String // field for storing state 
});

// state machine declaration
const stateMachine = {
  init: 'solid',
  transitions: [
    { name: 'melt', from: 'solid', to: 'liquid' },
    { name: 'freeze', from: 'liquid', to: 'solid' },
    { name: 'vaporize', from: 'liquid', to: 'gas' },
    { name: 'condense', from: 'gas', to: 'liquid' }
  ],
  methods: {
    onMelt() { console.log('I melted') },
    onFreeze() { console.log('I froze') },
    onVaporize() { console.log('I vaporized') },
    onCondense() { console.log('I condensed') }
  }
};

// extend schema with the plugin
matterSchema.plugin(stateMachinePlugin, { 
  fieldName: 'matterState', 
  stateMachine 
});
  
const Matter = mongoose.model('Matter', schema);

Now you can use javascript-state-machine API after created or retrieved a document from a database.

Important: The plugin does not manipulate data in a database. To save state in the database you need to use Mongoose API.

// create document
const matter = new Matter();
matter.matterState;   // solid (init state);
matter.is('solid');   // true
matter.is('liquid');  // false
matter.can('melt');   // true
matter.can('freeze'); // false

// transition
matter.melt();        // I melted!
matter.matterState;   // liquid 
await matter.save();  // save state

// retrieving from a database
const found = Matter.findById(matter.id);
found.matterState;  // liquid;
found.vaporize();   // I vaporized!
found.matterState;  // gas
found.melt();       // throw error

matter.matterState = 'solid'; // gas (no effect!)

Test

  • Copy the .env.example file to .env.test.local and fill in the values
  • Fill in the values in the .env.test.local file, for example:
    • DB_MONGO_URI=mongodb://localhost:27017/test
  • Run docker compose up to start the MongoDB container for the test environment
  • So, run npm test to run the tests

mongoose-state-machine's People

Contributors

safer-bwd avatar dependabot[bot] avatar amanfrinati avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.