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
| Constant | Value | Description |
|---|---|---|
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.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.
The raw localStorage key to read.
The parsed value, or
null on error or missing key.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).
The raw localStorage key to write.
The value to serialize and store.
true on success, false on error.Product methods
Storage.getProducts()
Returns the full products array from localStorage. Returns an empty array if none are stored.
Array of product objects. Each product has
id, codigo, nombre, and precio.Storage.saveProducts(products)
Overwrites the entire products array in localStorage.
The full array of product objects to persist.
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 with at minimum
codigo, nombre, and precio.The product object with its generated
id.Storage.updateProduct(id, updates)
Finds the product with the given id and merges updates into it using spread ({ ...product, ...updates }). Saves the updated array.
The id of the product to update.
Partial product object with the fields to update.
The updated product object, or
null if no product with that id was found.Storage.deleteProduct(id)
Removes the product with the given id from the array and saves.
The id of the product to remove.
true if a product was removed, false if no match was found.Storage.findProductByCode(code)
Searches the products array for an exact match on the codigo field.
The barcode string to look up.
The matching product object, or
undefined if not found.Client methods
Storage.getClients()
Returns the full clients array from localStorage. Returns an empty array if none are stored.
Array of client objects. Each client has
id, nombre, telefono, saldoDeuda, and notas.Storage.saveClients(clients)
Overwrites the entire clients array in localStorage.
The full array of client objects to persist.
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 with at minimum
nombre. telefono is optional.The client object with its generated
id, saldoDeuda, and notas.Storage.updateClient(id, updates)
Finds the client with the given id and merges updates into it. Saves the updated array.
The id of the client to update.
Partial client object with the fields to update.
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.
The id of the client to remove.
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().
The id of the client to add the note to.
Debt note object. Must include
total (number), productos (array), and optionally pagado.The note object with generated
id, fecha, and pendiente. Returns null if the client was not found.Storage.recordPayment(clientId, noteId, amount)
Adds amount to note.pagado, recalculates note.pendiente as note.total - note.pagado, and recalculates client.saldoDeuda.
The id of the client.
The id of the debt note to apply the payment to.
The payment amount in the store currency.
true on success, false if the client or note was not found.Storage.calculateClientDebt(client)
Sums the pendiente field across all of a client’s debt notes.
A client object with a
notas array.Total outstanding debt across all notes.
Sales methods
Storage.getSales()
Returns the full sales array from localStorage. Returns an empty array if none are stored.
Array of sale objects. Each sale has
id, fecha, productos, total, tipo, and clienteId.Storage.saveSales(sales)
Overwrites the entire sales array in localStorage.
The full array of sale objects to persist.
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 with
productos, total, tipo, and optionally clienteId.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.
A unique id string, e.g.
'lrx2k9abc3f'.Storage.exportData()
Builds and returns a full data snapshot suitable for backup.
An object with
version (string), timestamp (ISO string), products (array), clients (array), and sales (array).Storage.importData(data)
Restores products, clients, and sales from a backup object produced by exportData(). Only writes the keys present in data.
Backup object. May contain
products, clients, and/or sales arrays.true on success, false if an error occurs during import.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.