0.0.7 • Published 1 year ago

@typinghare/warehouse v0.0.7

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Warehouse

Get Started

Overview

Address

Just as the CPU fetches data from memory through an address, Warehouse fetches a piece of data via a specified address. The difference between the two is that a warehouse's address is a string and consists of two parts—namespace and label.

When we step into a warehouse, we see plenty of racks, and there may be some same type of goods on different racks. That is because some racks are for newly purchased items and some are not. When replenishment clerks are preparing for replenishment, they will fetch items from those old racks rather than new ones. However, if there are no goods on the old racks, they have to go for the new ones.

So namespaces are racks, and each namespace contains the same type of data; labels are identifiers of items. You can fetch an item from a namespace with a specified label, or fetch an item to a namespace.

The two-part of an address are separated with a colon (:). The followings are legal addresses:

  • me:username - Namespace is me and label is username.
  • home:env.debug - Namespace is home and label is env.debug.
  • database.type - In this case, the namespace is omitted, and will be replaced by the default namespace of the warehouse.

Warehouse

You can create a warehouse as follows:

const warehouse = new Warehouse();

A warehouse can contains multiple namespaces. When a warehouse is created, a default namespace instance will be created at the same time. If not specified, the default namespace is called convention. You can get a namespace by getNamespace().

const defaultNamespace = warehouse.getNamespace('convention');

You can also create a new namespace by createNamespace(), or delete a namespace by deleteNamespace(). Notice that you can never delete the default namespace.

const myNamespace = warehouse.createNamespace('mySpace');
warehouse.deleteNamespace('mySpace');

A more powerful function of warehouse is fetcher. Generate a fetcher by passing an array of namespaces. See fetcher for detailed specification of it.

const fetcher = warehouse.getFetcher(['convention', 'mySpace']);

Namespace

After you creating a namespace in a warehouse, you get a namespace instance. You can store an item by method store().

const myNamespace = warehouse.createNamespace('mySpace');
myNamespace.store('username', 'typinghare');

You can store multiple items by passing an object.

myNamespace.store({
	city: 'Quincy',
  state: 'MA',
  auto: 'Toyota'
});

Then, you can fetch items by their labels.

myNamespace.fetch('username');	// >> typinghare
myNamespace.fetch('city');	// >> Quincy

Notice that if you store two items with the same label, the latter will cover the former.

myNamespace.store('username', 'typinghare');
myNamespace.store('username', 'jameschan');
myNamespace.fetch('username');	// >> jameschan

Fetcher

First we create a warehouse and a customed namespace, and store some items.

const warehouse = new Warehouse();
const defaultNamespace = warehouse.getDefaultNamespace();
const myNamespace = warehouse.createNamespace('mySpace');
defaultNamespace.store({
  city: 'Chicago',
  state: 'IL',
  auto: 'Toyota'
});
myNamespace.store({
  city: 'Quincy',
  state: 'MA',
})

Then we create a fetcher, and try to get some items with it. You can see the values of city and state come from mySpace namespace, and the value of auto comes from the default namespace. This is how the fetcher work: it scans label from the last item of the given array backwards. If the label exists in one of the namespace, returns its value directly without continuing searching.

const fetcher = warehouse.getFetcher(['convention', 'mySpace']);
fetcher.fetch('city');	// >> Quincy
fetcher.fetch('state');	// >> MA
fetcher.fetch('auto');	// >> Toyota
0.0.1

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.0

2 years ago