1.2.1 • Published 2 years ago

lssv v1.2.1

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

LocalSessionStorageAPI

Contents

localSessionStorageAPI is an API that simplifies the CRUD operations with the localStorage and sessionStorage

The class can handle both sessionStorage and localStorage.

Introduction {#introduction}


Purpose

The primary purpose of LocalSessionStorageAPI (lss) is to lessen the requests to a . Frequent retrieval of data will be instead delegated to localStorage. Only the data that is scarcely used or will be handled by a server API.

lssv can also be used in native web applications. While being offline the user can store his data on the web storage.

Get Started

First import the module:

import lssv from "lssv";

Then create an object. You can specify the standard storage type the object will use throughout its life time. If you do not specify any storage type then localStorage is choosen as standard:

const l = new lssv();

Once we have created the object the storage type will look like this:

KeyValue
numberOfEntities0
entitiesEnum{}

Let's create an entity that symbolizes a person. For that we must pass arguments for entityName and entityProps in the the method addEntity().

entityProps is an object where the keys are the name of the properties such as 'name', 'prename', 'age' and 'location' and the values are strings inform us of the type such as 'string', 'number' or 'object'.

If a property is not necessary then we simply append a question mark at the end of its name. Here is an example how we add a new entity:

const entityProps = {
  name: "string",
  prename: "string",
  age: "number",
  "location?": {
    city: "string",
    county: "string",
    state: "string",
  },
};

l.addEntity("persons", entityProps);

After we added the entity the storage type will look like this:

KeyValue
numberOfEntities0
entitiesEnum{}
1.propertiesType{ name: "string", ..., "location?": ...}
1.necessaryProps{name: "string" ... }
1.optionalProps{ "location?": ... }
1.numberOfInstances0

Documentation


Terminology


Here are the explanation for terms that are used in the code. From here on the api is abbreviated as 'llsv' which stands for vanilla LocalSessionStorageAPI.

TermExplanation
EntityAn entity is an object that contains properties.
InstanceAn instance is
PropertyA property defines a part of an entity.
ObjectAn object is a simple JavaScript object.

Examples

 {title: "Pinocchio", author: "Carlo Collodi", pages: "200"}`
{"location?": {
"city?": "string",
"county?": "string",
"state?": "string"
}}
[key: string]: string | Object | string[] | number[]

If we speak of 'kind' we mean if the property is necessary id est whether it must be specified. We speak of 'type' for a property then we mean what is commonly understood for a type e.g. 'string', 'number' and so on.

Functioning


How does the API work?

properties is a listing of all the properties that the entity has. The first items are prefixed with a zero. They contain the settings for the api.

Entities

Entities such as persons, employees, products, books and so on begin with {1-9}.{nameOfEntity}. All the instances of the entities are then further prefixed with a point and a number starting with 1

To keep track of the entities we have 'entitiesEnum'. 'entitiesEnum' is an object where the key is the name of the entity and the value is another object with its properties ("props") and id ("id").

Best Practises


sessionStorage

sessionStorage should be used for data that is sensible because once the user finished interacting with the browser the data will not persist.

localStorage

localStorage on the other hand should be used for data that is not sensible because localStorage is persistent and someone else could access the browser and look up the data easily.

An example for a use case would be to store options for the settings of the user.

References


Here are resources to get sophisticated with web storage:

Q&A


How does lssv store data?

lssv saves each time we add an entity the properties of it. The properties for a person could be:

{name: "string", prename: "string"}

Each time we store an instance lssv does format the values as an array. The id

Improvements


Do you have improvements to the api? The project is still in development. If you have something that you

  • Create Exceptions for specific errors. That enhances debugging.

  • Write better documentation.

  • Instead of an object, use an array to hold the values while we map the keys to the values with an array that contains the properties

  • Only require storageChoice at the instanciation of of the class. To create an entity we must invoke the method addEntity()

  • Restrict the possible arguments of 'entityName' to the ones defined in the web storage.

  • Minify code in the compilation process.

  • Refactor the code that checkInstance() allows multiple types for a property

  • Refactor: migrate param autoIncrement to addEntity()

1.2.1

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago