GithubHelp home page GithubHelp logo

key-guard's Introduction

key-guard

What is this about?

Orignally this module was created to keep track of keys in redis. But you can use it for any other application of course. Redis is an awesome key/value store, but you can easily lose track of all keys used in production, because there is no schema that every developer of your team has to follow.

Install

npm install key-guard --save

How to use?

const coreKey = require("key-guard")(options);

optionsis an object with following attributes:

  • namespace - string - A string that is prefixed to every generated key.
  • minLength- number - Min length of generated key (default: 1).
  • maxLength- number - Max length of generated key (default: 100).
  • fragments- object - A fragment is a string that is part of the key and separated by fragments.delimiter.
  • fragments.delimiter- string - Delimiter between every key fragement (default: :).
  • fragments.regexp- regexp - Regular expression that every key fragment is checked against (default: /^[a-zA-Z0-9]+$/).
  • fragments.count.min- number - Min number of fragments (default: 2).
  • fragments.count.max- number - Max number of fragments (default: 5).
  • fragments.len.min- number - Min length of fragment string (default: 2).
  • fragments.len.max- number - Max length of fragment string (default: 20).
{
  namespace: 'myApp',
  minLength: 1,
  maxLength: 100,
  fragments: {
    delimiter: ':',
    count: {
      min: 2,
      max: 5
    },
    len: {
      min: 2,
      max: 20
    },
    regexp: /^[a-zA-Z0-9]+$/
  }
}

Example

const keyGuard = require("key-guard");
let coreKey = keyGuard({
  namespace: "myApp"
});
let key = coreKey().update("users").update("283").get();
console.log(key);
// => "myApp:users:283"

You can also overwrite the namespace or entire prefixes:

let key = coreKey("newNamespace:records").update("2384").get();
console.log(key);
// => "newNamespace:records:2384"

Pro tip #1

Create one file that creates the core key and the is included in every other page that needs generated keys:

coreKey.js

const keyGuard = require("key-guard");
module.exports = keyGuard({
  namespace: "myApp"
});

index.js

const coreKey = require("./coreKey.js");
let key = coreKey().update("users").update("283").get();
console.log(key);
// => "myApp:users:283"

Pro tip #2

Usually scripts use keys for different fragments/sections multiple times. Declare a base key on top of each script or within coreKey.js.

coreKey.js

const keyGuard = require("key-guard");
module.exports = keyGuard({
  namespace: "myApp"
});

users.js

const coreKey = require("./coreKey.js");
const usersKey = coreKey().update("users");

console.log(usersKey.update("283").get());
console.log(usersKey.update("284").get());
console.log(usersKey.update("285").get());
// => "myApp:users:283"
// => "myApp:users:284"
// => "myApp:users:285"

Pro tip #3

If you do not want to have any literals within you scripts, load variables from config files or environment variables.

coreKey.js

const keyGuard = require("key-guard");
module.exports = keyGuard({
  namespace: process.env.REDIS_NAMESPACE_KEY
});

> REDIS_NAMESPACE_KEY=myApp node index.js

key-guard's People

Contributors

amberlamps avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

bobo6891

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.