Meet npm Pro: unlimited public & private packages + package-based permissions.Learn more »

@byu-oit/node-global-data-manager

1.4.2 • Public • Published

node-global-data-manager

Server-side caching made easy

Introduction

This module originally served to store server configurations in a single location and accessing/changing them throughout the server files as necessary. I've since come across dozens of similar scenarios since that have led to the creation of this module.

With the global-data-manager, you can create, modify, or delete project-global variables in any file without the hassle of exporting and importing variables from multiple files.

This pattern makes local caching easy. Be aware on systems that use load balancing, caches between server instances will be unequal.

Installation

npm i @byu-oit/node-global-data-manager

Accessing variables

fetch - Retrieves the key-value pair all - Retrieves all the store properties and values

put - Stores the key-value pair, over-writes the key if it already exists putMany - Stores the key-value pairs, over-writes a key if it already exists

create - Creates the key-value pair, will not over-write the key if it already exists createMany - Creates the key-value pairs, will not over-write a key if it already exists

erase - Removes the property from the global store eraseMany - Removes the properties from the global store

copy - Copy the value from one property to another

rename - Rename a property without changing the value

reset - Resets the global data store for the instance

expire - Removes the property from the store after a ttl in milliseconds expireMany - Removes the properties from the store after a ttl in milliseconds

getId - Retrieves the Manager instance identifier

Usage Examples (with Chai)

(These and other examples may be found in the test directory)

Using the Manager instance, fetch, and all
const Manager = require('@byu-oit/node-global-data-manager')
const { expect } = require('chai')
 
const store = Manager()
const store1 = Manager('a')
const store2 = Manager('b')
const store2copy = Manager('b')
 
store1.put('id', 1)
store2.put('id', 2)
 
expect(store.getId()).to.equal(0);
expect(store1.getId()).to.equal('a')
expect(store2.getId()).to.equal('b')
 
expect(store.fetch('id')).to.deep.equal(undefined)
expect(store1.all()).to.deep.equal({ id: 1 })
expect(store2.all()).to.deep.equal({ id: 2 })
expect(store2copy.fetch('id')).to.deep.equal(2)
Using put and putMany
const store = require('@byu-oit/node-global-data-manager').Manager()
const { expect } = require('chai')
 
const toStore = {
  id: 1,
  name: 'Jose',
  birth: new Date(),
  attributes: {
    hairColor: 'brn',
    ethnicity: 'latin-american'
  }
}
 
const local = store.putMany(toStore)
expect(local).to.deep.equal(toStore)
expect(store.all()).to.deep.equal(toStore)
 
const item = store.put('id', 2)
expect(item).to.deep.equal({id: 2})
expect(store.fetch('id')).to.equal(2)
Using create and createMany
const store = require('@byu-oit/node-global-data-manager').Manager()
const { expect } = require('chai')
 
const toStore = {
  id: 1,
  name: 'Jose',
  birth: new Date(),
  attributes: {
    hairColor: 'brn',
    ethnicity: 'latin-american'
  }
}
 
const result1 = store.createMany(toStore)
const result2 = store.createMany(toStore)
 
expect(result1).to.deep.equal(toStore)
expect(store.all()).to.deep.equal(toStore)
 
const keys = Object.keys(result2)
expect(keys.length).to.equal(4)
keys.map(key => {
  expect(result2[key]).to.equal(null)
})
Using erase and eraseMany
const store = require('@byu-oit/node-global-data-manager').Manager()
const { expect } = require('chai')
 
const toStore = {
  id: 1,
  name: 'Jose',
  birth: new Date(),
  attributes: {
    hairColor: 'brn',
    ethnicity: 'latin-american'
  }
}
 
expect(store.getId()).to.equal(3)
 
store.createMany(toStore)
store.erase('id')
expect(store.fetch('id')).to.equal(undefined)
store.eraseMany('birth', 'attributes');
expect(store.all()).to.deep.equal({name: 'Jose'})
Using copy
const store = require('@byu-oit/node-global-data-manager').Manager()
const { expect } = require('chai')
 
store.put('id', 1)
const res = store.copy('id', 'count')
 
expect(res).to.deep.equal({count: 1})
expect(store.fetch('count')).to.equal(1)
expect(store.fetch('id')).to.equal(1)
Using rename
const store = require('@byu-oit/node-global-data-manager').Manager()
const { expect } = require('chai')
 
store.put('id', 1)
const res = store.rename('id', 'count')
 
expect(res).to.deep.equal({count: 1})
expect(store.fetch('count')).to.equal(1)
expect(store.fetch('id')).to.equal(undefined)
Using reset
const store = require('@byu-oit/node-global-data-manager').Manager()
const { expect } = require('chai')
 
store.put('id', 1)
store.reset()
expect(store.all()).to.deep.equal({})
Using expire and expireMany

You can either set a ttl while using the put, putMany, create, or createMany methods

const store = require('@byu-oit/node-global-data-manager').Manager()
const { expect } = require('chai')
 
const duration = 3000
const offset = 1
const key = 'color'
const value = 'green'
 
store.put(key, value, duration)
clock.tick(duration - offset)
expect(store.fetch(key)).to.equal(value)
clock.tick(offset)
expect(store.fetch(key)).to.equal(undefined)

or set a ttl using the expire, or expireMany methods

const store = require('@byu-oit/node-global-data-manager').Manager()
const { expect } = require('chai')
 
const duration = 3000
const offset = 1
const key = 'color'
const value = 'green'
     
store.put(key, value)
store.expire(duration, key)
clock.tick(duration - offset)
expect(store.fetch(key)).to.equal(value)
clock.tick(offset)
expect(store.fetch(key)).to.equal(undefined)
Using getId
const Manager = require('@byu-oit/node-global-data-manager')
const { expect } = require('chai')
 
const id = 'The Greatest Storage Ever'
const myStorage = Manager(id)
expect(myStorage.getId()).to.equal(id)

Install

npm i @byu-oit/node-global-data-manager

DownloadsWeekly Downloads

135

Version

1.4.2

License

Apache-2.0

Unpacked Size

61.8 kB

Total Files

24

Last publish

Collaborators

  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar
  • avatar