store-memory

0.3.2 • Public • Published

store-memory

Simplified memory store.

Travis Codecov Travis

Install

$ npm install store-memory

Usage

// Create a new store.
const store = []
const memoryStore = require('store-memory')
const productStore = memoryStore(store);
 
// Add a new product to the store.
const product = {name: "mug"}
const id = productStore.add(product);
 
// Find all products in the store.
const products = productStore.findAll();
 
// Find a product by id.
const product = productStore.find(id);
 
// Remove all products.
productStore.removeAll();
 
// Remove a product by id.
productStore.remove(id);
 
// Update a product.
productStore.update(id, {name: "tshirt"});
 
// Count the products.
productStore.count();
 
// Handle a product that does not exist.
try {
  productStore.find(42);
  productStore.remove(42);
  productStore.update(42, {});
} catch(e) {
  if (instanceof memoryStore.NotFoundError) {
    // Do something.
  }
}
 
// Handle an invalid id.
try {
  productStore.find(null);
  productStore.remove(null);
  productStore.update(null);
} catch(e) {
  if (instanceof ReferenceError) {
    // Do something.
  }
}
 
// Handle invalid data.
try {
  productStore.add(null);
  productStore.update(1, null);
} catch(e) {
  if (instanceof ReferenceError) {
    // Do something.
  }
}

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i store-memory

Weekly Downloads

4

Version

0.3.2

License

MIT

Unpacked Size

215 kB

Total Files

10

Last publish

Collaborators

  • simonrenoult