Skip to content

Globally reusable value / reference storage that supports all data types; and 2 because all the first ones are inferior.

License

Notifications You must be signed in to change notification settings

servercharlie/keystore2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

KeyStore2 Build Status Node Security Status

Globally reusable value / reference storage that supports all data types; and 2 because all the first ones are inferior.

Simply put, instead of polluting your functions & promises w/ multiple arguments / single-arity functions, these keystore2 contexts gives your code more readability, and code reusability.

Best use cases / works best with:

  • Heavy asynchronous workloads (ie: working w/ multiple objects at the same time)
  • Chained promises
  • Pure functions that never sacrifices reusability.

Install

$ npm install --save keystore2

Usage

Basics

const KeyStore	= require('keystore2');

var _KeyStore	= new KeyStore();

_KeyStore.set('a', 123);
// true

_KeyStore.get('a');
// 123

_KeyStore.has('a');
// true

_KeyStore.has('b');
// false

Using contexts

const KeyStore	= require('keystore2');

var _KeyStore	= new KeyStore();

_KeyStore.pullContext('myContext').set('b', 456);
// true

_KeyStore.pullContext('myContext').get('b');
// 456

_KeyStore.clearContext('myContext');
// true

_KeyStore.pullContext('myContext').get('b');
// undefined

Nested contexts

const KeyStore	= require('keystore2');

var _KeyStore	= new KeyStore();

_KeyStore.pullContext('myContext').pullContext('mySubContext').set('c', 789);
// true

_KeyStore.pullContext('myContext').pullContext('mySubContext').get('c');
// 789

Random contexts (Added in 1.1.0)

const KeyStore	= require('keystore2');

var _KeyStore	= new KeyStore();

var randomContext = _KeyStore.pullRandomContext();

randomContext.id;
// uuidv4 random id, like '0b99b82f-62cf-4275-88b3-de039020f14e'

randomContext.set('a', 5);
// true

randomContext.get('a');
// 5

// Random contexts can also be nested!

randomContext.pullContext('nestedInRandom').set('c', 123);
// true

randomContext.pullContext('nestedInRandom').get('c');
// 123

Real-world example

const gulp = require('gulp');
const KeyStore	= require('keystore2');

var _KeyStore	= new KeyStore();

function FirstPromise(_context){

	var _KeyStoreContext = _KeyStore.pullContext(_context);
	
	return new Promise(function(resolve, reject){
	
		// promise chain ejecting if context-based error is present
		if(_KeyStoreContext.has('error') === true){
			reject(_context);
		}
		
		// some tasks..
		var _someKey = _KeyStoreContext.get('someKey');
		
		// on success
		_KeyStoreContext.set('newKey', 'sampleStringValue');
		resolve(_context);
		
		// OR
		
		// on failure:
		reject('some error message');
		
		// or this, if you wanna store it in the context.
		_KeyStoreContext.set('error', 'some error message.');
		reject(_context);
		
	});
	
}

// function SecondPromise...

// function ThirdPromise...

gulp.task(`test`, () => {
	return FirstPromise('myContext')
		.then((_context)=>SecondPromise(_context))
			.catch((err)=>{throw new Error(err);})
		.then((_context)=>ThirdPromise(_context))
			.catch((err)=>{throw new Error(err);});
});

Notes, References, Todo's, etc.

Testing w/ xo & ava

npm install && npm test

License

MIT © servercharlie

About

Globally reusable value / reference storage that supports all data types; and 2 because all the first ones are inferior.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published