npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tamed-state-machine-backend

v1.0.5

Published

The backend library for the tamed-state-machine

Downloads

2

Readme

WHY?

This is the backend part of the tamed-state-machine library. For full setup please refer to https://github.com/MehmetKaplan/tamed-state-machine.

This, tamed-state-machine-backend library, is a set of functions that read the state machine configurations within the database. And associate your application's objects with those state machines.

As a general rule this association is defined by following 3 parameters, which you can see frequently in the function parameters:

  1. externalName - Connection to your application. This information is valuable for you, it defines which application you are connecting. The value is free text and tamed-state-machine keeps it for your association. (For example if you are implementing a document approval process, this is the name of the application that you are implementing the process for).
  2. externalId - Connection to your application, here the value is usually the primary key of the connected document. (For example if you are implementing a document approval process, this is the internal id of the document in your application).
  3. smName - Name of the state machine that is configured within the database. In order to see how the state machines can be configured, please refer to the state machine configuration manual.

Note: Whenever the database is modified with new state machines, states, transitions, etc, the backend server will be able to read and use the definitions since we do not cache the state machine configurations. The rationale behind this is that we do not want to restart the backend server each time a new state machine is defined.

Once these functions are exposed as a backend server, they can be consumed by the tamed-state-machine-frontend functions.

IMPORTANT: This library does not focus on the authorization. It should be handled separately.

SETUP

  1. Add the request handlers as a dependency of your project.
yarn add tamed-state-machine-backend
  1. Initialize parameters (modify below object according to your environment)

Name below example configuration as server-parameters.js and place it in the root directory of your express server. This file is to be required by your express server in the next step. You should modify the credentials, according to your environment.

module.exports = {
	pgKeys: {
		user: 'tsmapp',
		password: 'tsmapp.', // coming from database-setup/step00001.sql
		database: 'tsmdb',
		host: 'localhost',
		port: 5432,
	},
	httpsKeys: {
		keyPath: undefined, // modify this if https is to be used
		certPath: undefined, // modify this if https is to be used
	},
	port: process.env.TSM_PORT || 3000
}
  1. Call the init function of the library to initialize the db connection pool.
const serialEntrepreneurBackendHandlers = require('tamed-state-machine-backend');
const serverParameters = require('./server-parameters.js');
...
const startServer = async () => {
	await tsmb.init(
		{
			pgKeys: serverParameters.pgKeys,
			applicationName: 'YOUR APPLICATION NAME',
		}
	);
	// ...
	// Rest of your application server code
}
  1. Finally start your server. Now the state machine backend is ready to be consumed by the frontend.

  2. Each time you need a state machine model configure it in the database as in the state machine configuration manual

API

init

| Parameter | Type | Description | | --- | --- | --- | | p_params | Object | Parameters for the backend server. |

p_params

| Key | Type | Value | | --- | --- | --- | | pgKeys | Object | PostgreSQL connection parameters. | | applicationName | String | Application name. Not used, reserved for future. |

Returns: If successful, resolves to true. Otherwise, rejects with an error message.

initiateInstance

Initializes a state machine instance. This instance is association between your application and a configured state machine.

| Parameter | Type | Description | | --- | --- | --- | | externalName| String | Connection to an application, here the value is a free-text. | | externalId | String | Connection to an application, here the value is usually the primary key of the connected document. (For example if you are implementing a document approval process, this is the internal id of the document). | | smName| String | Name of the state machine that is configured within the database. | | generatedBy| String | The user that initiated the state machine. |

Returns

If successful, resolves to an object in the following form. payload describes the next possible transitions for the state machine.

{
			result: 'OK',
			payload: ...
}

If not successful, rejects with an error message.

getInstance

Gets the instance of the state machine. This instance is association between your application and a configured state machine.

| Parameter | Type | Description | | --- | --- | --- | | externalName| String | Connection to an application, here the value is a free-text. | | externalId | String | Connection to an application, here the value is usually the primary key of the connected document. (For example if you are implementing a document approval process, this is the internal id of the document). | | smName| String | Name of the state machine that is being queried. |

Returns

If successful, resolves to an object in the following form. payload describes the instance of the state machine.

{
			result: 'OK',
			payload: ...
}

If not successful, rejects with an error message.

getPossibleTransitions

Finds the state machine instance and returns the possible transitions for the current state.

| Parameter | Type | Description | | --- | --- | --- | | externalName| String | Connection to an application, here the value is a free-text. | | externalId | String | Connection to an application, here the value is usually the primary key of the connected document. (For example if you are implementing a document approval process, this is the internal id of the document). | | smName| String | Name of the state machine that the current state transitions are being queried. |

Returns

If successful, resolves to an object in the following form. payload describes the next possible transitions for the existing state of the state machine instance.

{
			result: 'OK',
			payload: ...
}

If not successful, rejects with an error message.

transitionInstance

Finds the state machine instance and performs the desired transition.

| Parameter | Type | Description | | --- | --- | --- | | externalName| String | Connection to an application, here the value is a free-text. | | externalId | String | Connection to an application, here the value is usually the primary key of the connected document. (For example if you are implementing a document approval process, this is the internal id of the document). | | smName| String | Name of the state machine that the state is requested to be transitioned. | | transitionName| String | Name of the transition that is being requested. | | transitionMadeBy| String | The user that requested the transition. | | comment| String | Comment for the transition for instance history. |

Returns

If successful, resolves to an object in the following form. payload describes the next possible transitions for the existing (new) state of the state machine instance.

{
			result: 'OK',
			payload: ...
}

If not successful, rejects with an error message.

getInstanceHistory

Finds the state machine instance and returns the history of the transitions.

| Parameter | Type | Description | | --- | --- | --- | | externalName| String | Connection to an application, here the value is a free-text. | | externalId | String | Connection to an application, here the value is usually the primary key of the connected document. (For example if you are implementing a document approval process, this is the internal id of the document). | | smName| String | Name of the state machine that the history is being queried. |

Returns

If successful, resolves to an object in the following form. payload describes the transition history of the state machine instance.

{
			result: 'OK',
			payload: ...
}

If not successful, rejects with an error message.

getAllPossibleTransitions

Gives the state machine transition configurations.

| Parameter | Type | Description | | --- | --- | --- | | smName| String | Name of the state machine that the transitions are being queried. |

Returns

If successful, resolves to an object in the following form. payload describes all possible transitions of the state machine. This is simply the transition configuration for the state machine.

{
			result: 'OK',
			payload: ...
}

If not successful, rejects with an error message.

deleteInstance

Deletes the state machine instance.

| Parameter | Type | Description | | --- | --- | --- | | externalName| String | Connection to an application, here the value is a free-text. | | externalId | String | Connection to an application, here the value is usually the primary key of the connected document. (For example if you are implementing a document approval process, this is the internal id of the document). | | smName| String | Name of the state machine that the instance is being deleted. |

Returns

If successful, resolves to an object in the following form.

{
			result: 'OK',
			payload: undefined
}

If not successful, rejects with an error message.

Example

For a better understanding of how to use this library, please refer to this example.

License

The license is MIT and full text here.

Used Modules

  • tick-log license here
  • tamed-pg license here
  • fetch-lean license here