Coder Social home page Coder Social logo

bedrock-web-store's Introduction

bedrock-web-store

A module for creating shared storage for Bedrock Web Apps. The design requires stored objects to have only one instance in memory. The objects may be backed by a persistent storage layer such as localStorage or IndexedDB. Whether or not persistent storage is used is determined by the underlying engine associated with a particular Store instance.

It is important to note that even if a persistent storage mechanism is used, any objects retrieved from storage via the Store API will have only one in-memory copy, ensuring that frontend components will share the same instance. That is to say that a naive implementation of a Store engine that simply maps the API onto, for example, localForage or localStorage would break this guarantee. There must also be a shared memory layer.

Usage

npm install bedrock-web-store

Simple default singleton storage

The most common setup for a Web App is to use a single instance of shared storage across the application. Namespacing can be achieved via the id parameter.

In setup file:

import {store, MemoryEngine} from 'bedrock-web-store';

store.setEngine({engine: new MemoryEngine()});

In file 1:

import {store} from 'bedrock-web-store';

const foo = {a: 1, b: 2};

store.create({id: '123', object: foo});

In file 2:

import {store} from 'bedrock-web-store';

const foo = store.get({id: '123'});

console.log(foo.a);
// yields "1"

Multiple stores

More complex Web Apps may have a reason for partitioning storage into different instances. This could be done for namespacing purposes where ids would otherwise clash or because different storage engines are needed for different purposes. In this case, the Store class can be imported and multiple instances (potentially with different engines) can be created and managed by the Web App.

In custom myStores.js:

import {Store, MemoryEngine} from 'bedrock-web-store';

export const stores = {};

stores.foos = new Store({engine: new MemoryEngine()});
stores.bars = new Store({engine: new MemoryEngine()});

In file 1:

import {stores} from './myStores.js';

const foo = {a: 1, b: 2};
stores.foos.create({id: '123', object: foo});

// ...

In file 2:

import {stores} from './myStores.js';

const foo = stores.foos.get({id: '123'});
// same `foo` as file 1

// ...

const bar = stores.bars.get({id: '123'});
// `bar` is undefined

bedrock-web-store's People

Contributors

dlongley avatar mattcollier avatar gannan08 avatar

Watchers

Manu Sporny avatar David I. Lehn avatar  avatar Andrew Jones avatar James Cloos avatar  avatar Dmitri Zagidulin avatar  avatar  avatar  avatar Tashi D. Gyeltshen avatar

bedrock-web-store's Issues

Simplify API and remove option to change the engine

This module was original conceived as being for storing data that could be persisted and reloaded and instead has become used exclusively as an in-memory store where users rely on objects being the same shared in-memory instance (not immutable copies of what is in the store).

No other use cases have come up in a number of years, so this module should either be deprecated and a simpler model of sharing in-memory references via import w/o any module should be adopted -- or -- if this module offers some other utility, the engine aspect should be removed because swapping the engine would break usage / expectations.

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.