Skip to main content
The Storage object is the single source of truth for all application data. It reads and writes to the browser’s localStorage using JSON serialization, and provides domain-specific methods for products, clients, and sales. All other modules go through Storage — nothing reads localStorage directly.

Constants

ConstantValueDescription
Storage.KEYS.PRODUCTS'pos_inventario'localStorage key for the products array.
Storage.KEYS.CLIENTS'pos_clientes'localStorage key for the clients array.
Storage.KEYS.SALES'pos_ventas'localStorage key for the sales array.
Storage.KEYS.VERSION'pos_version'localStorage key for the data version string.
Storage.VERSION'1.0.0'Current data schema version.
Storage.CURRENCY'S/.'Currency symbol used when rendering prices.

Methods

Storage.init()

Checks whether a version key exists in localStorage and writes it if not. Then calls getProducts() and, if the array is empty, seeds the store with sample product data via an internal initSampleProducts() call. Called once by App.init() before any other module is initialized.
Storage.init();

Storage.get(key)

Low-level helper. Reads a raw value from localStorage and parses it as JSON. Returns null if the key does not exist or if parsing fails.
key
string
required
The raw localStorage key to read.
return
any | null
The parsed value, or null on error or missing key.
const products = Storage.get('pos_inventario');

Storage.set(key, value)

Low-level helper. Serializes value as JSON and writes it to localStorage. Returns false if serialization or writing fails (e.g., storage quota exceeded).
key
string
required
The raw localStorage key to write.
value
any
required
The value to serialize and store.
return
boolean
true on success, false on error.
Storage.set('pos_inventario', []);

Product methods

Storage.getProducts()

Returns the full products array from localStorage. Returns an empty array if none are stored.
return
object[]
Array of product objects. Each product has id, codigo, nombre, and precio.
const products = Storage.getProducts();
console.log(products[0].nombre); // 'Coca Cola 1L'

Storage.saveProducts(products)

Overwrites the entire products array in localStorage.
products
object[]
required
The full array of product objects to persist.
return
boolean
true on success, false on write error.

Storage.addProduct(product)

Generates a unique id for the product (if one is not already set), appends it to the products array, and saves.
product
object
required
Product object with at minimum codigo, nombre, and precio.
return
object
The product object with its generated id.
const newProduct = Storage.addProduct({
  codigo: '1234567890',
  nombre: 'Agua Mineral 500ml',
  precio: 2.00
});
console.log(newProduct.id); // e.g. 'lrx2k9abc'

Storage.updateProduct(id, updates)

Finds the product with the given id and merges updates into it using spread ({ ...product, ...updates }). Saves the updated array.
id
string
required
The id of the product to update.
updates
object
required
Partial product object with the fields to update.
return
object | null
The updated product object, or null if no product with that id was found.
Storage.updateProduct('lrx2k9abc', { precio: 2.50 });

Storage.deleteProduct(id)

Removes the product with the given id from the array and saves.
id
string
required
The id of the product to remove.
return
boolean
true if a product was removed, false if no match was found.
const deleted = Storage.deleteProduct('lrx2k9abc');

Storage.findProductByCode(code)

Searches the products array for an exact match on the codigo field.
code
string
required
The barcode string to look up.
return
object | undefined
The matching product object, or undefined if not found.
const product = Storage.findProductByCode('7501234567890');
if (product) {
  Sales.addToCart(product);
}

Client methods

Storage.getClients()

Returns the full clients array from localStorage. Returns an empty array if none are stored.
return
object[]
Array of client objects. Each client has id, nombre, telefono, saldoDeuda, and notas.

Storage.saveClients(clients)

Overwrites the entire clients array in localStorage.
clients
object[]
required
The full array of client objects to persist.
return
boolean
true on success, false on write error.

Storage.addClient(client)

Generates a unique id for the client, initializes saldoDeuda to 0 and notas to [] if not already set, appends to the clients array, and saves.
client
object
required
Client object with at minimum nombre. telefono is optional.
return
object
The client object with its generated id, saldoDeuda, and notas.
const client = Storage.addClient({ nombre: 'Ana García', telefono: '987654321' });

Storage.updateClient(id, updates)

Finds the client with the given id and merges updates into it. Saves the updated array.
id
string
required
The id of the client to update.
updates
object
required
Partial client object with the fields to update.
return
object | null
The updated client object, or null if no client with that id was found.

Storage.deleteClient(id)

Removes the client with the given id from the array and saves.
id
string
required
The id of the client to remove.
return
boolean
true if a client was removed, false if no match was found.

Storage.addDebtNote(clientId, note)

Adds a debt note to a client’s notas array. Generates an id for the note, sets fecha to the current ISO timestamp if not already provided, and initializes pendiente to note.total. Recalculates client.saldoDeuda using calculateClientDebt().
clientId
string
required
The id of the client to add the note to.
note
object
required
Debt note object. Must include total (number), productos (array), and optionally pagado.
return
object | null
The note object with generated id, fecha, and pendiente. Returns null if the client was not found.
Storage.addDebtNote(client.id, {
  productos: cart,
  total: 15.50,
  pagado: 0
});

Storage.recordPayment(clientId, noteId, amount)

Adds amount to note.pagado, recalculates note.pendiente as note.total - note.pagado, and recalculates client.saldoDeuda.
clientId
string
required
The id of the client.
noteId
string
required
The id of the debt note to apply the payment to.
amount
number
required
The payment amount in the store currency.
return
boolean
true on success, false if the client or note was not found.
Storage.recordPayment(client.id, note.id, 10.00);

Storage.calculateClientDebt(client)

Sums the pendiente field across all of a client’s debt notes.
client
object
required
A client object with a notas array.
return
number
Total outstanding debt across all notes.
const total = Storage.calculateClientDebt(client);
console.log(total); // e.g. 45.50

Sales methods

Storage.getSales()

Returns the full sales array from localStorage. Returns an empty array if none are stored.
return
object[]
Array of sale objects. Each sale has id, fecha, productos, total, tipo, and clienteId.

Storage.saveSales(sales)

Overwrites the entire sales array in localStorage.
sales
object[]
required
The full array of sale objects to persist.
return
boolean
true on success, false on write error.

Storage.addSale(sale)

Generates a unique id for the sale and sets fecha to the current ISO timestamp if not already provided. Appends to the sales array and saves.
sale
object
required
Sale object with productos, total, tipo, and optionally clienteId.
return
object
The sale object with its generated id and fecha.

Utility methods

Storage.generateId()

Generates a short, unique string id using Date.now() and Math.random(), both converted to base-36.
return
string
A unique id string, e.g. 'lrx2k9abc3f'.
const id = Storage.generateId();

Storage.exportData()

Builds and returns a full data snapshot suitable for backup.
return
object
An object with version (string), timestamp (ISO string), products (array), clients (array), and sales (array).
const backup = Storage.exportData();
console.log(backup.version);   // '1.0.0'
console.log(backup.timestamp); // '2026-03-18T12:00:00.000Z'

Storage.importData(data)

Restores products, clients, and sales from a backup object produced by exportData(). Only writes the keys present in data.
data
object
required
Backup object. May contain products, clients, and/or sales arrays.
return
boolean
true on success, false if an error occurs during import.
const ok = Storage.importData(backup);

Storage.clearAll()

Removes all three data keys (pos_inventario, pos_clientes, pos_ventas) from localStorage, then calls Storage.init() to re-seed the store with sample products.
Storage.clearAll();
clearAll() permanently deletes all products, clients, and sales data. This action cannot be undone.

Build docs developers (and LLMs) love