store-memory
v0.3.2
Published
Simplified memory store
Downloads
26
Readme
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