Code Monkey home page Code Monkey logo

vault.js's Introduction

vault.js

Key-Value Storage of true data types for the browser and node

Vault is an API to store Key-Value pairs as true data types. It can utilize localStorage, sessionStorage, and cookies in the Browser. In node, it uses a file at the root of the project (redis support coming soon).

It handles JSON objects, arrays, booleans, numbers, and even returns undefined and null accurately.

npm install vault.js
bower install vault.js
<script src="//cdnjs.cloudflare.com/ajax/libs/vault.js/1.0.3/vault.min.js"></script>

What's new!

  • [1.0.0] Memory Storage. Automatic cleanup (expired items removed in the background). Moves data from one storage to another if the same var is set with different config.
  • [0.1.4] Support for use server-side added- writes data to .vault.json at the root level
  • [0.1.0] localStorage and sessionStorage support the path param

What's next?

  • add redis as a storage option
  • handle storage limit errors
  • add support to request more storage
  • handle storage events
  • add server-side storage for node- save data to a json file

Run/Develop locally

git clone [email protected]:jimmybyrum/vault.js.git
cd vault.js
npm install
npm run build

Usage

Local, Session, Cookie, File, Memory

Vault.set(String key, Mixed value, Object optional_config)
Vault.set(String key, Mixed value, Object optional_config);
rules for which storage to use
if node
  if config.expires exists and equals "session"
    use Memory
  else
    use File
else if config.expires exists and localStorage is supported, then
  use localStorage
else if sessionStorage is supported, then
  use sessionStorage
else
  use cookies

or, if you want to specify which storage to use:

// client side
Vault.Cookie.set(...);
Vault.Session.set(...);
Vault.Local.set(...);

// server side
Vault.File.set(...);
Vault.Memory.set(...);
optional_config
{
  // Cookie options.
  domain: ".jimmybyrum.com",
  secure: true,

  // Cookie and Local options
  path: "/",
  expires: "2014-09-25 8:24:32 pm", // or anything that can be parsed by new Date(...)
  expires: "+3 days", // works for all time increments from milliseconds to years.

  // File and Memory options
  expires

  // domain and local/session storage
  local/session is natively tied to the domain. Subdomain keys can only be set from that subdomain.
}

examples

Vault.set("year", 1999); // saves in localStorage (browser) or to a file (server)

Vault.set("year", 1999, { expires: 'session' }); // saves in sessionStorage (browser) or in Memory (server)

Vault.set("year", 1999, { expires: '1999-12-31 11:59:59 pm' }); // saves in localStorage until 11:59:59 on December 31, 1999

Vault.Session.set('foo', 'bar');
Vault.Session.set('foo', 'bar', {
  expires: '+6 months',
  path: '/examples'
});
Vault.Local.set('my_array', [1,2,3,4]);
Vault.Cookie.set('my_object', {
  foo: 'bar',
  an_array: [1,2,3],
  year: 2012
});
Vault.Local.set('age', 33);
Vault.get(String key)

Will check storage in this order: Memory, File (if node), Session, Local, Cookie

To specify which storage to get from, use:

Vault.Cookie.get(String key);
Vault.Local.get(String key);
Vault.Session.get(String key);
Vault.File.get(String key);
Vault.Memory.get(String key);

examples

Vault.get('foo'); returns from Memory, else File (if node), else Session, else Local, else Cookie, else undefined

Vault.Session.get('foo');
// returns 'bar'
Vault.Local.get('my_array');
// [1,2,3,4]
Vault.Cookie.get('my_object');
// {
//   foo: 'bar',
//   an_array: [1,2,3],
//   year: 2012
// }
Vault.Local.get('age');
// returns the number 33
remove

removes an item

Vault.remove('my_object'); // removes from all storage types
Vault.Session.remove('my_object');
Vault.Local.remove('my_array');
Vault.Cookie.remove('my_array');
Vault.File.remove('my_array');
Vault.Memory.remove('my_array');
clear

clears all items

Vault.clear(); // clears all storage types
Vault.Session.clear();
Vault.Local.clear();
Vault.Cookie.clear();
Vault.File.clear();
Vault.Memory.clear();
list

lists all items in Vault in the console

Vault.list(); // lists all storage types
Vault.Session.list();
Vault.Local.list();
Vault.Cookie.list();
Vault.File.list();
Vault.Memory.list();

Webpack

To use Vault.js with webpack you'll need to use json-loader.

npm i --save json-loader

Then in webpack config:

  module: {
    loaders: [
      {test: /\.json$/, loader: "json-loader" }
    ]
  },

You may also want to set up an alias so your require statements are a bit cleaner:

  resolve: {
    alias: {
      'vault.js': 'vault.js/browser.js'
    }
  }

vault.js's People

Contributors

jimmybyrum avatar dzoba avatar michaelbina avatar

Stargazers

Yilmaz Guleryuz avatar Premento avatar  avatar Paul pvhl avatar Muhammad Rafizeldi avatar Justin Beaudry avatar iacopo avatar  avatar Matthew Hadley avatar

Watchers

 avatar iacopo avatar

vault.js's Issues

Vault doesn't return an empty string if one is set

In my code I am doing the following:

Vault.set('hello', '')

I'd expect it to return an empty string, but I get this instead:

Vault.get('hello')
undefined

It seems to be affecting all of the stores.

Any help would be appreciated! Thanks

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.