Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
@rentspree/mongoose-state-machine
Advanced tools
A mongoose plugin fomr Javascript State Machine
A mongoose plugin fomr Javascript State Machine
The plugin base the module on Javascript State Machine which had done really well on creating a State-like environment for Javascript.
This Plugin will merge Mongoose to Javascript State Machine! The goal is to make the API simple and allow a mongoose model to have the State machine API provided by Javascript State Machine.
This plugin simply intercetp the Model Initilizer to include Javascript State Machine instance to the model.
$ npm install --save @rentspree/mongoose-state-machine
First lets initialize our mongoose model
import stateMachinePlugin from "@rentspree/mongoose-state-machine"
import mongoose from "mongoose"
// first create your model
const personSchema = new mongoose.Schema({
firstName: String,
lastName: String,
status: String // check this field out
})
Then, we can define the State Machine definition. This is the exact same object which should be passed upon creating a StateMachine
object here.
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: function() { console.log('I melted') },
onFreeze: function() { console.log('I froze') },
onVaporize: function() { console.log('I vaporized') },
onCondense: function() { console.log('I condensed') }
}
}
For more detail on this definitions, you can visit Javascript State Machine document. Every config in this part is passed to it.
Now, lets apply the plugin to our model
personSchema.plugin(stateMachinePlugin, { stateMachine: stateMachine } )
There are some options available, but we'll come back later.
const Person = mongoose.model("Person", personSchema)
Done!! The Person
Model now become a StateMachine model. The field status
declared earlier is the field responsible for the initial state of the State Machine. This is slightly different from the normal behaviour of the Javascript State Machine.
According to the example above, the first thing needs to clearify is that, the value in status
field of this Person
model would only have 3 states which are what declared in the State Machine; solid
, liquid
, and gas
.
Let's do the query, assuming a Person
in the database:
Person.create({
_id: "first-person-ever",
firstName: "John",
lastName: "Doe",
status: "liquid"
})
Now, on somewhere in the code, we can do this
const person = await Person.findOne({_id: "first-person-ever"})
console.log(person.status)
// this will long "liquid"
console.log(person.state)
// this is the Javascript State Machine api and it will log "liquid"
person.vaporize()
// this will log "I vaporized"
console.log(person.state)
// "gas"
console.log(person.status)
// "liquid" --IMPORTANT, explain below
await person.save()
console.log(person.status)
// "gas"
The Javascript State Machine API is available for the person
model here and it will behave like Applying State Machine Behavior to Existing Objects.
Important Note The important thing to notice here is that, this plugin will not manipulate the data at the database level. The goal is to make Mongoose model work together with Javascript State Machine. From the example above, the code must run person.save()
in order to update the latest status to the database. The purpose for this is to delegate database saving decision to the developer.
When the person.save()
happen, it will move the value in the state
of the Javascript State Machine into the field status
which relate to value in the database.
Things get a little different while creating new model. According to Mongoose, it is not recommended to use middleware instead of overriding the model method.
Normally, when creating new model in Mongoose
const person = new Person({firstName: "hero"})
// this still work
person.melt()
// error
Instead, use the method provided by the plugin
const person = Person.new({firstName: "hero"})
person.melt()
console.log(person.state)
// liquid
These are the option available when defining plugin
personSchema.plugin(stateMachinePlugin, options )
Option | description | default |
---|---|---|
stateMachine | The state machine definition object which will be passed to new StateMachine() of Javascript State Machine | null |
statusFieldName | The state field name in the database. This is the initial state for the state machine when getting Mongoose model from the database, also it's the field that the current state will be update upon save() | "status" |
MIT © Putt
0.1.1 (2019-06-24)
<a name="0.1.0"></a>
FAQs
A mongoose plugin fomr Javascript State Machine
The npm package @rentspree/mongoose-state-machine receives a total of 120 weekly downloads. As such, @rentspree/mongoose-state-machine popularity was classified as not popular.
We found that @rentspree/mongoose-state-machine demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
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.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.