0.3.2 • Published 6 years ago

store-memory v0.3.2

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

store-memory

Simplified memory store.

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 (e instanceof memoryStore.NotFoundError) {
    // Do something.
  }
}

// Handle an invalid id.
try {
  productStore.find(null);
  productStore.remove(null);
  productStore.update(null);
} catch(e) {
  if (e instanceof ReferenceError) {
    // Do something.
  }
}

// Handle invalid data.
try {
  productStore.add(null);
  productStore.update(1, null);
} catch(e) {
  if (e instanceof ReferenceError) {
    // Do something.
  }
}

License

MIT

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago