0.2.1 • Published 5 months ago

cella.js v0.2.1

Weekly downloads
5
License
ISC
Repository
github
Last release
5 months ago

Cella.JS

Build Status Made with Typescript Documentation Status License: ISC Up to date dependencies Undergoing development Contributors wanted PRs Welcome Tests unspecified Logo Needed Thanks

Cella.JS is a JavaScript package that helps you manage data storage within the browser. It basically serves as a basic wrapper around the browser's default local storage APIs in order to provide extra functionality. Cella.JS eliminates the need to write extra code when working with the browser's local storage. It provides features such as:

  • In-built support for encryption.
  • Store abstract data types without needing to convert to a string first.
  • Ability to add custom storage engine.
  • Ability to add custom (data-transformational) functions to be executed before or after data is stored or retrieved.
  • #Working on in-built support for indexedDB.

Table of Contents

Table of contents generated with markdown-toc

Installation

NPM

$ npm install cella.js

Yarn

$ yarn add cella.js

Browser

https://unpkg.com/cella.js@0.0.0/dist/index.js

Developing locally

To clone and build this project locally, run the following commands:

$ git clone https://github.com/IggsGrey/cella.js.git
$ npm i
$ npm run dev

Usage

Importing the Library

ES6 Modules

import Cella from 'cella.js';

NodeJS Modules / Commonjs

const Cella = require('cella.js');

Browser

Import the library with a script tag and the Cella API shall be made available.

Cella

How to Use

Usage is unbelievably simple.

Examples

Basic Usage

import Cella from 'cella.js';

// create an instance of Cella!
const cella = new Cella();


// store an item
cella.store({
	key: 'sample-key',
	value: ['item1', 'item2', 'item3', 'item4']
}).then(() => {
    console.log('void');
});
// Promise<void>


// retrieve an item
const retrievedItem = cella.get({
	key: 'sample-key'
}).then((res) => {
    console.log('got',res);
});
// Promise<Array ['item1', 'item2', 'item3', 'item4']>


// delete an item
cella.eject({
	key: 'sample-key',
}).then(() => {
    console.log('void');
});
// Promise<void>

Applying Transforms And Encryption

import Cella from 'cella.js'

const mySetData = new Set(['pineapples', 'are', 'great']);

console.log(mySetData);

const cella = new Cella({
	encrypt: {
		secret: 'my-secret-key' // yup that's it!
	},
	transforms: [
		{
			inbound: function(yourSetData) {
				// convert set to array
				return [...yourSetData];
			},
			outbound: function(yourConvertedSetData) {
				// convert array back to set
				return new Set(yourConvertedSetData);
			}
		},
		// ...unlimitedTransforms
	]
});


cella.store({
	key: 'my-key',
	value: mySetData
});

cella.get({ key: 'my-key' }).then(res => console.log(res));

Specifying A Custom Storage Engine

import Cella from 'cella.js';

// create an instance of Cella!
const cella = new Cella({
    storage: {
        store: (key, value) => {
            // code to implement custom storing
        },
        get: (key) => {
            // code to implement custom retrieval
        },
        eject: (key) => {
            // code to implement custom deletion
        }
    }
});

API

Cella Class Properties (CellaInstance)

Properties you can pass to the new Cella class

Property nameDescriptionData typeRequiredDefault value
storageSpecify the StorageEngine to use and additional options. NOTE: If you specify a custom database storage engine object, it must implement the StorageEngine interface.SpecifiedStorageEngineORStorageEngineno{ type: 'localStorage' }
encryptAn object that specifies encryption details including the encryption secretRecord<'secret', string> Eg: { secret: 'some-string' }no-
transformsArray of objects defining function inbound and outbound functions. Useful for modifying data before storing and retrieving.TransformObject[]no-

Interfaces

StorageEngine

All instances of the in-built storage engines implement this interface, and all custom storage engines must implement this interface as well.

Property nameDescriptionData typeParameters
storeThe function that inserts data into the local database.FunctionStorageItem: {key: string (required)value: string (required)}
getThe function that retrieves data from the local database.FunctionRetrievedStorageItem: {key: string (required)}
ejectThe function that deletes data from the local database.FunctionDeletedStorageItem: {key: string (required)}

StorageItem

The object parameter properties for each time you call cella.store()

Property nameDescriptionData typeRequiredDefault value
keyThe key/label that identifies the item in the localstorage.stringtrue-
valueThe value of the item to be stored.anytrue-
encryptEncryption properties for the data to be stored.{ secret: string }falsenull

RetrievedStorageItem

The object parameter properties for each time you call cella.get()

Property nameDescriptionData typeRequiredDefault value
keyThe key/label that identifies the item in the localstorage.stringtrue-
decryptDecryption properties for the data to be stored.{ secret: string }falsenull

DeletedStorageItem

The object parameter properties for each time you call cella.eject()

Property nameDescriptionData typeRequiredDefault value
keyThe key/label that identifies the item in the localstorage.stringtrue-

SpecifiedStorageEngine

These properties are valid for the storage object value in the CellaInstance.

Property nameDescriptionData typeRequiredDefault value
typeThe preferred type of in-built storage.'localStorage' | 'indexedDB'true'localStorage'
nameThe preferred name of the indexedDB storage.stringrequired only when type == 'indexedDB'-

NOTE: IN-BUILT SUPPORT FOR INDEXEDDB IS NOT YET IMPLEMENTED

TransformObject

These properties are valid for an object value in the transforms array specified in a CellaInstance.

Property nameDescriptionData typeRequiredDefault value
inboundFunction that is executed ech time data is being stored. This function is executed before any other changes are made to the data.(args0: T) => anyyes-
outboundFunction that is executed ech time data is being retrieved. This function is executed after all other changes have been made to the data.(args0: T) => anyyes-

Contributing

Want to contribute? Great! I am currently seeking contributors for remaining features including adding in-built support for more DB engines. You can get started by reading the current issues or opening one yourself. Don't forget to read the ethics and conduct code for contributors in the CONTRIBUTING.md You may also contact @IggsGrey on:

  • jaygrey.jg@gmail.com
  • discord - scarrexx#5134
  • reddit - @scar_reX
  • twitter - @devx_gh

License

This project uses the ISC license and it can be found here: LICENSE

Upcoming Features!

  • In-built support for indexedDB.
  • Integration with web workers.
  • Data compression.
  • Smart fallbacks for browser support issues. Eg: fallback to localStorage in browsers that do not support indexedDB (optional ofcourse).

Todo

  • Write tests.
  • Implement features above (obviously Tom!).
  • Build docs site with demos
  • Work on browser support docs
  • Codesandbox examples

Browser Support

IE / EdgeIE / EdgeFirefoxFirefoxChromeChromeSafariSafariiOS SafariiOS SafariSamsungSamsungOperaOpera
123.5443.21.010.5

Developers for Firefox