GithubHelp home page GithubHelp logo

lugovsky / localstorage-schema Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 3.0 160 KB

A javascript library that provides api to work with browser localStorage like with a tree-based structure.

JavaScript 100.00%

localstorage-schema's Introduction

localstorage-schema

A javascript library that provides api to work with browser localStorage like with a tree-based structure. So you can treat it like javascript database on top of browser's localStorage!

Creating a connection

Of course, there is no physical connection itself, but to access the schema structure, you should create so-called schema object. CUrrently library exposes localStorageSchema function object to create it.

var schema = localStorageSchema();

It also takes single parameter in which you can provide options for the connection:

  • schemaPrefix: the default prefix for all database records. Defaults to _lsscm.
  • storage: currently either localStorage or sessionStorage objects.
  • schemaDelimiter: the delimiter between keys of nodes and leafs in the tree. Defaults to :.
  • schemaAccessor: interface, used to access the storage itself. You can use JSONStorageAccessor or RawStorageAccessor (if you are using raw types only). Or you can write you own accessor just for your needs. Defaults to JSONStorageAccessor.

Calling the constructor with options looks like:

var schema = localStorageSchema({
    schemaPrefix: '_lsscm',
    storage: window.localStorage,
    schemaDelimiter: ':',
    schemaAccessor: 'JSONStorageAccessor'
});

Getting data from the schema

Every piece of data in the schema is hidden behind its own DAO. Of cource you can access objects this way schema.get('books:1'). But the correct way is to use DAOs .There are two common types of DAO: ObjectDao and CollectionDAO. ObjectDao is used to access singletone objects in the schema, and the second one used to access collection-like structures.

You can create new collection and insert object to it like this.

var books = schema.collection('books'),
    id = books.insert({ author: 'Stephen King', name: 'Shining' });

To get existing element from collection you can use this code:

books.get(id);
// This code is equal to
books.object(id).read();

Disclaimer

Library is currently in development. So if you have some suggestions, please feel free to contact me.

localstorage-schema's People

Contributors

lugovsky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

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.